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   static jsi::Object preparePropertyDescriptor(jsi::Runtime &jsRuntime, int options);
63 
64 protected:
65   WeakRuntimeHolder runtimeHolder;
66   std::shared_ptr<jsi::Object> jsObject;
67 
68 private:
69   friend HybridBase;
70 
71   bool jniHasProperty(jni::alias_ref<jstring> name);
72 
73   jni::local_ref<jni::HybridClass<JavaScriptValue>::javaobject> jniGetProperty(
74     jni::alias_ref<jstring> name
75   );
76 
77   jni::local_ref<jni::JArrayClass<jstring>> jniGetPropertyNames();
78 
79   /**
80    * Unsets property with the given name.
81    */
82   void unsetProperty(jni::alias_ref<jstring> name);
83 
84   /**
85    * A template to generate different versions of the `setProperty` method based on the `jsi_type_converter` trait.
86    * Those generated methods will be exported and visible in the Kotlin codebase.
87    * On the other hand, we could just make one function that would take a generic Java Object,
88    * but then we would have to decide what to do with it and how to convert it to jsi::Value
89    * in cpp. That would be expensive. So it's easier to ensure that
90    * we call the correct version of `setProperty` in the Kotlin code.
91    *
92    * This template will work only if the jsi_type_converter exists for a given type.
93    */
94   template<
95     class T,
96     typename = std::enable_if_t<is_jsi_type_converter_defined<T>>
97   >
98   void setProperty(jni::alias_ref<jstring> name, T value) {
99     jsi::Runtime &jsRuntime = runtimeHolder.getJSRuntime();
100 
101     auto cName = name->toStdString();
102     jsObject->setProperty(
103       jsRuntime,
104       cName.c_str(),
105       jsi_type_converter<T>::convert(jsRuntime, value)
106     );
107   }
108 
109   template<
110     class T,
111     typename = std::enable_if_t<is_jsi_type_converter_defined<T>>
112   >
113   void defineProperty(jni::alias_ref<jstring> name, T value, int options) {
114     jsi::Runtime &jsRuntime = runtimeHolder.getJSRuntime();
115 
116     auto cName = name->toStdString();
117     jsi::Object global = jsRuntime.global();
118     jsi::Object objectClass = global.getPropertyAsObject(jsRuntime, "Object");
119     jsi::Function definePropertyFunction = objectClass.getPropertyAsFunction(
120       jsRuntime,
121       "defineProperty"
122     );
123     jsi::Object descriptor = preparePropertyDescriptor(jsRuntime, options);
124 
125     descriptor.setProperty(jsRuntime, "value", jsi_type_converter<T>::convert(jsRuntime, value));
126 
127     definePropertyFunction.callWithThis(jsRuntime, objectClass, {
128       jsi::Value(jsRuntime, *jsObject),
129       jsi::String::createFromUtf8(jsRuntime, cName),
130       std::move(descriptor)
131     });
132   }
133 };
134 } // namespace expo
135