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