1 // Copyright © 2021-present 650 Industries, Inc. (aka Expo)
2 
3 #pragma once
4 
5 #include "JSIObjectWrapper.h"
6 #include "JSITypeConverter.h"
7 #include "JavaScriptRuntime.h"
8 #include "WeakRuntimeHolder.h"
9 
10 #include <fbjni/fbjni.h>
11 #include <jsi/jsi.h>
12 
13 #include <memory>
14 
15 namespace jni = facebook::jni;
16 namespace jsi = facebook::jsi;
17 
18 namespace expo {
19 class JavaScriptValue;
20 
21 /**
22  * Represents any JavaScript object. Its purpose is to exposes `jsi::Object` API back to Kotlin.
23  */
24 class JavaScriptObject : public jni::HybridClass<JavaScriptObject>, JSIObjectWrapper {
25 public:
26   static auto constexpr
27     kJavaDescriptor = "Lexpo/modules/kotlin/jni/JavaScriptObject;";
28   static auto constexpr TAG = "JavaScriptObject";
29 
30   static void registerNatives();
31 
32   JavaScriptObject(
33     std::weak_ptr<JavaScriptRuntime> runtime,
34     std::shared_ptr<jsi::Object> jsObject
35   );
36 
37   JavaScriptObject(
38     WeakRuntimeHolder runtime,
39     std::shared_ptr<jsi::Object> jsObject
40   );
41 
42   std::shared_ptr<jsi::Object> get() override;
43 
44   /**
45    * @return a bool whether the object has a property with the given name
46    */
47   bool hasProperty(const std::string &name);
48 
49   /**
50    * @return the property of the object with the given name.
51    * If the name isn't a property on the object, returns the `jsi::Value::undefined` value.
52    */
53   jsi::Value getProperty(const std::string &name);
54 
55   /**
56    * @return a vector consisting of all enumerable property names in the object and its prototype chain.
57    */
58   std::vector<std::string> getPropertyNames();
59 
60   void setProperty(const std::string &name, jsi::Value value);
61 
62 private:
63   friend HybridBase;
64   WeakRuntimeHolder runtimeHolder;
65   std::shared_ptr<jsi::Object> jsObject;
66 
67   bool jniHasProperty(jni::alias_ref<jstring> name);
68 
69   jni::local_ref<jni::HybridClass<JavaScriptValue>::javaobject> jniGetProperty(
70     jni::alias_ref<jstring> name
71   );
72 
73   jni::local_ref<jni::JArrayClass<jstring>> jniGetPropertyNames();
74 
75   /**
76    * Unsets property with the given name.
77    */
78   void unsetProperty(jni::alias_ref<jstring> name);
79 
80   /**
81    * A template to generate different versions of the `setProperty` method based on the `jsi_type_converter` trait.
82    * Those generated methods will be exported and visible in the Kotlin codebase.
83    * On the other hand, we could just make one function that would take a generic Java Object,
84    * but then we would have to decide what to do with it and how to convert it to jsi::Value
85    * in cpp. That would be expensive. So it's easier to ensure that
86    * we call the correct version of `setProperty` in the Kotlin code.
87    *
88    * This template will work only if the jsi_type_converter exists for a given type.
89    */
90   template<
91     class T,
92     typename = std::enable_if_t<is_jsi_type_converter_defined<T>>
93   >
94   void setProperty(jni::alias_ref<jstring> name, T value) {
95     jsi::Runtime &jsRuntime = runtimeHolder.getJSRuntime();
96 
97     auto cName = name->toStdString();
98     jsObject->setProperty(
99       jsRuntime,
100       cName.c_str(),
101       jsi_type_converter<T>::convert(jsRuntime, value)
102     );
103   }
104 
105   template<
106     class T,
107     typename = std::enable_if_t<is_jsi_type_converter_defined<T>>
108   >
109   void defineProperty(jni::alias_ref<jstring> name, T value, int options) {
110     jsi::Runtime &jsRuntime = runtimeHolder.getJSRuntime();
111 
112     auto cName = name->toStdString();
113     jsi::Object global = jsRuntime.global();
114     jsi::Object objectClass = global.getPropertyAsObject(jsRuntime, "Object");
115     jsi::Function definePropertyFunction = objectClass.getPropertyAsFunction(
116       jsRuntime,
117       "defineProperty"
118     );
119     jsi::Object descriptor = preparePropertyDescriptor(jsRuntime, options);
120 
121     descriptor.setProperty(jsRuntime, "value", jsi_type_converter<T>::convert(jsRuntime, value));
122 
123     definePropertyFunction.callWithThis(jsRuntime, objectClass, {
124       jsi::Value(jsRuntime, *jsObject),
125       jsi::String::createFromUtf8(jsRuntime, cName),
126       std::move(descriptor)
127     });
128   }
129 
130   static jsi::Object preparePropertyDescriptor(jsi::Runtime &jsRuntime, int options);
131 };
132 } // namespace expo
133