1 // Copyright © 2021-present 650 Industries, Inc. (aka Expo)
2 
3 #pragma once
4 
5 #include "JSIObjectWrapper.h"
6 #include "WeakRuntimeHolder.h"
7 #include "JavaScriptTypedArray.h"
8 #include "JNIDeallocator.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 JavaScriptRuntime;
20 
21 class JavaScriptObject;
22 
23 class JavaScriptTypedArray;
24 
25 class JavaScriptFunction;
26 
27 /**
28  * Represents any JavaScript value. Its purpose is to expose the `jsi::Value` API back to Kotlin.
29  */
30 class JavaScriptValue : public jni::HybridClass<JavaScriptValue, Destructible>, JSIValueWrapper {
31 public:
32   static auto constexpr
33     kJavaDescriptor = "Lexpo/modules/kotlin/jni/JavaScriptValue;";
34   static auto constexpr TAG = "JavaScriptValue";
35 
36   static void registerNatives();
37 
38   static jni::local_ref<JavaScriptValue::javaobject> newInstance(
39     JSIInteropModuleRegistry *jsiInteropModuleRegistry,
40     std::weak_ptr<JavaScriptRuntime> runtime,
41     std::shared_ptr<jsi::Value> jsValue
42   );
43 
44   JavaScriptValue(
45     std::weak_ptr<JavaScriptRuntime> runtime,
46     std::shared_ptr<jsi::Value> jsValue
47   );
48 
49   JavaScriptValue(
50     WeakRuntimeHolder runtime,
51     std::shared_ptr<jsi::Value> jsValue
52   );
53 
54   std::shared_ptr<jsi::Value> get() override;
55 
56   std::string kind();
57 
58   bool isNull();
59 
60   bool isUndefined();
61 
62   bool isBool();
63 
64   bool isNumber();
65 
66   bool isString();
67 
68   bool isSymbol();
69 
70   bool isFunction();
71 
72   bool isArray();
73 
74   bool isObject();
75 
76   bool isTypedArray();
77 
78   bool getBool();
79 
80   double getDouble();
81 
82   std::string getString();
83 
84   jni::local_ref<jni::HybridClass<JavaScriptObject, Destructible>::javaobject> getObject();
85 
86   jni::local_ref<jni::JArrayClass<JavaScriptValue::javaobject>> getArray();
87 
88   jni::local_ref<JavaScriptTypedArray::javaobject> getTypedArray();
89 
90   jni::local_ref<jni::HybridClass<JavaScriptFunction, Destructible>::javaobject> jniGetFunction();
91 
92 private:
93   friend HybridBase;
94 
95   WeakRuntimeHolder runtimeHolder;
96   std::shared_ptr<jsi::Value> jsValue;
97 
98   jni::local_ref<jstring> jniKind();
99 
100   jni::local_ref<jstring> jniGetString();
101 };
102 } // namespace expo
103