1 #include "RemoteObject.h"
2 #include <jsi/jsi.h>
3 #include "RuntimeDecorator.h"
4 #include "SharedParent.h"
5 
6 using namespace facebook;
7 
8 namespace reanimated {
9 
maybeInitializeOnWorkletRuntime(jsi::Runtime & rt)10 void RemoteObject::maybeInitializeOnWorkletRuntime(jsi::Runtime &rt) {
11   if (initializer.get() != nullptr) {
12     backing = getWeakRef(rt);
13     *backing.lock() = initializer->shallowClone(rt);
14     initializer = nullptr;
15   }
16 }
17 
get(jsi::Runtime & rt,const jsi::PropNameID & name)18 jsi::Value RemoteObject::get(jsi::Runtime &rt, const jsi::PropNameID &name) {
19   if (RuntimeDecorator::isWorkletRuntime(rt)) {
20     return backing.lock()->getObject(rt).getProperty(rt, name);
21   }
22   return jsi::Value::undefined();
23 }
24 
set(jsi::Runtime & rt,const jsi::PropNameID & name,const jsi::Value & value)25 void RemoteObject::set(
26     jsi::Runtime &rt,
27     const jsi::PropNameID &name,
28     const jsi::Value &value) {
29   if (RuntimeDecorator::isWorkletRuntime(rt)) {
30     backing.lock()->getObject(rt).setProperty(rt, name, value);
31   }
32   // TODO: we should throw if trying to update remote from host runtime
33 }
34 
getPropertyNames(jsi::Runtime & rt)35 std::vector<jsi::PropNameID> RemoteObject::getPropertyNames(jsi::Runtime &rt) {
36   std::vector<jsi::PropNameID> res;
37   auto propertyNames = backing.lock()->getObject(rt).getPropertyNames(rt);
38   for (size_t i = 0, size = propertyNames.size(rt); i < size; i++) {
39     res.push_back(jsi::PropNameID::forString(
40         rt, propertyNames.getValueAtIndex(rt, i).asString(rt)));
41   }
42   return res;
43 }
44 
45 } // namespace reanimated
46