1 // Copyright 2022-present 650 Industries. All rights reserved.
2 
3 #include "JSIUtils.h"
4 #include "LazyObject.h"
5 
6 namespace expo {
7 
8 LazyObject::LazyObject(const LazyObjectInitializer initializer) : initializer(initializer) {}
9 
10 LazyObject::~LazyObject() {
11   backedObject = nullptr;
12 }
13 
14 jsi::Value LazyObject::get(jsi::Runtime &runtime, const jsi::PropNameID &name) {
15   if (!backedObject) {
16     if (name.utf8(runtime) == "$$typeof") {
17       // React Native asks for this property for some reason, we can just ignore it.
18       return jsi::Value::undefined();
19     }
20     backedObject = initializer(runtime);
21   }
22   return backedObject ? backedObject->getProperty(runtime, name) : jsi::Value::undefined();
23 }
24 
25 void LazyObject::set(jsi::Runtime &runtime, const jsi::PropNameID &name, const jsi::Value &value) {
26   if (!backedObject) {
27     backedObject = initializer(runtime);
28   }
29   if (backedObject) {
30     backedObject->setProperty(runtime, name, value);
31   }
32 }
33 
34 std::vector<jsi::PropNameID> LazyObject::getPropertyNames(jsi::Runtime &runtime) {
35   if (!backedObject) {
36     backedObject = initializer(runtime);
37   }
38   if (backedObject) {
39     jsi::Array propertyNames = backedObject->getPropertyNames(runtime);
40     return jsiArrayToPropNameIdsVector(runtime, propertyNames);
41   }
42   return {};
43 }
44 
45 } // namespace expo
46