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 
9 #include <fbjni/fbjni.h>
10 #include <jsi/jsi.h>
11 
12 #include <memory>
13 
14 namespace jni = facebook::jni;
15 namespace jsi = facebook::jsi;
16 
17 namespace expo {
18 class JavaScriptRuntime;
19 
20 class JavaScriptObject;
21 
22 class JavaScriptTypedArray;
23 
24 class JavaScriptFunction;
25 
26 /**
27  * Represents any JavaScript value. Its purpose is to expose the `jsi::Value` API back to Kotlin.
28  */
29 class JavaScriptValue : public jni::HybridClass<JavaScriptValue>, JSIValueWrapper {
30 public:
31   static auto constexpr
32     kJavaDescriptor = "Lexpo/modules/kotlin/jni/JavaScriptValue;";
33   static auto constexpr TAG = "JavaScriptValue";
34 
35   static void registerNatives();
36 
37   JavaScriptValue(
38     std::weak_ptr<JavaScriptRuntime> runtime,
39     std::shared_ptr<jsi::Value> jsValue
40   );
41 
42   JavaScriptValue(
43     WeakRuntimeHolder runtime,
44     std::shared_ptr<jsi::Value> jsValue
45   );
46 
47   std::shared_ptr<jsi::Value> get() override;
48 
49   std::string kind();
50 
51   bool isNull();
52 
53   bool isUndefined();
54 
55   bool isBool();
56 
57   bool isNumber();
58 
59   bool isString();
60 
61   bool isSymbol();
62 
63   bool isFunction();
64 
65   bool isArray();
66 
67   bool isObject();
68 
69   bool isTypedArray();
70 
71   bool getBool();
72 
73   double getDouble();
74 
75   std::string getString();
76 
77   jni::local_ref<jni::HybridClass<JavaScriptObject>::javaobject> getObject();
78 
79   jni::local_ref<jni::JArrayClass<JavaScriptValue::javaobject>> getArray();
80 
81   jni::local_ref<JavaScriptTypedArray::javaobject> getTypedArray();
82 
83   jni::local_ref<jni::HybridClass<JavaScriptFunction>::javaobject> jniGetFunction();
84 
85 private:
86   friend HybridBase;
87 
88   WeakRuntimeHolder runtimeHolder;
89   std::shared_ptr<jsi::Value> jsValue;
90 
91   jni::local_ref<jstring> jniKind();
92 
93   jni::local_ref<jstring> jniGetString();
94 };
95 } // namespace expo
96