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