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