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