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