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