1 // Copyright © 2021-present 650 Industries, Inc. (aka Expo)
2 
3 #pragma once
4 
5 #include "JavaScriptObject.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 /**
19  * Represents any JavaScript value. Its purpose is to expose the `jsi::Value` API back to Kotlin.
20  */
21 class JavaScriptValue : public jni::HybridClass<JavaScriptValue> {
22 public:
23   static auto constexpr
24     kJavaDescriptor = "Lexpo/modules/kotlin/jni/JavaScriptValue;";
25   static auto constexpr TAG = "JavaScriptValue";
26 
27   static void registerNatives();
28 
29   JavaScriptValue(
30     std::weak_ptr<JavaScriptRuntime> runtime,
31     std::shared_ptr<jsi::Value> jsValue
32   );
33 
34   std::string kind();
35 
36   bool isNull();
37   bool isUndefined();
38   bool isBool();
39   bool isNumber();
40   bool isString();
41   bool isSymbol();
42   bool isFunction();
43   bool isObject();
44 
45   bool getBool();
46   double getDouble();
47   std::string getString();
48   jni::local_ref<JavaScriptObject::javaobject> getObject();
49 
50 private:
51   friend HybridBase;
52   std::weak_ptr<JavaScriptRuntime> runtimeHolder;
53   std::shared_ptr<jsi::Value> jsValue;
54 
55   jni::local_ref<jstring> jniKind();
56   jni::local_ref<jstring> jniGetString();
57 };
58 } // namespace expo
59