1 // Copyright 2022-present 650 Industries. All rights reserved.
2 
3 #include "JSIUtils.h"
4 
5 namespace expo::common {
6 
jsiArrayToPropNameIdsVector(jsi::Runtime & runtime,const jsi::Array & array)7 std::vector<jsi::PropNameID> jsiArrayToPropNameIdsVector(jsi::Runtime &runtime, const jsi::Array &array) {
8   size_t size = array.size(runtime);
9   std::vector<jsi::PropNameID> vector;
10 
11   vector.reserve(size);
12 
13   for (size_t i = 0; i < size; i++) {
14     jsi::String name = array.getValueAtIndex(runtime, i).getString(runtime);
15     vector.push_back(jsi::PropNameID::forString(runtime, name));
16   }
17   return vector;
18 }
19 
definePropertyOnJSIObject(jsi::Runtime & runtime,jsi::Object * jsthis,const char * name,jsi::Object descriptor)20 void definePropertyOnJSIObject(
21   jsi::Runtime &runtime,
22   jsi::Object *jsthis,
23   const char *name,
24   jsi::Object descriptor
25 ) {
26   jsi::Object global = runtime.global();
27   jsi::Object objectClass = global.getPropertyAsObject(runtime, "Object");
28   jsi::Function definePropertyFunction = objectClass.getPropertyAsFunction(
29     runtime,
30     "defineProperty"
31   );
32 
33   // This call is basically the same as `Object.defineProperty(object, name, descriptor)` in JS
34   definePropertyFunction.callWithThis(runtime, objectClass, {
35     jsi::Value(runtime, *jsthis),
36     jsi::String::createFromUtf8(runtime, name),
37     std::move(descriptor),
38   });
39 }
40 
41 } // namespace expo::common
42