1 // Copyright © 2021-present 650 Industries, Inc. (aka Expo)
2 
3 #pragma once
4 
5 #include "types/CppType.h"
6 #include "types/ExpectedType.h"
7 #include "types/AnyType.h"
8 
9 #include <jsi/jsi.h>
10 #include <fbjni/fbjni.h>
11 #include <ReactCommon/TurboModuleUtils.h>
12 #include <react/jni/ReadableNativeArray.h>
13 #include <memory>
14 #include <vector>
15 #include <folly/dynamic.h>
16 #include <jsi/JSIDynamic.h>
17 
18 namespace jni = facebook::jni;
19 namespace jsi = facebook::jsi;
20 namespace react = facebook::react;
21 
22 namespace expo {
23 class JSIInteropModuleRegistry;
24 
25 /**
26  * A class that holds information about the exported function.
27  */
28 class MethodMetadata {
29 public:
30   /**
31    * Function name
32    */
33   std::string name;
34   /**
35    * Number of arguments
36    */
37   int args;
38   /*
39    * Whether this function is async
40    */
41   bool isAsync;
42   /**
43    * Representation of expected argument types.
44    */
45   std::vector<std::unique_ptr<AnyType>> argTypes;
46 
47   MethodMetadata(
48     std::string name,
49     int args,
50     bool isAsync,
51     jni::local_ref<jni::JArrayClass<ExpectedType>> expectedArgTypes,
52     jni::global_ref<jobject> &&jBodyReference
53   );
54 
55   MethodMetadata(
56     std::string name,
57     int args,
58     bool isAsync,
59     std::vector<std::unique_ptr<AnyType>> &&expectedArgTypes,
60     jni::global_ref<jobject> &&jBodyReference
61   );
62 
63   // We deleted the copy contractor to not deal with transforming the ownership of the `jBodyReference`.
64   MethodMetadata(const MethodMetadata &) = delete;
65 
66   MethodMetadata(MethodMetadata &&other) = default;
67 
68   /**
69    * MethodMetadata owns the only reference to the Kotlin function.
70    * We have to clean that, cause it's a `global_ref`.
71    */
72   ~MethodMetadata() {
73     if (jBodyReference != nullptr) {
74       jBodyReference.release();
75     }
76   }
77 
78   /**
79    * Transforms metadata to a jsi::Function.
80    *
81    * @param runtime
82    * @param moduleRegistry
83    * @return shared ptr to the jsi::Function that wrapped the underlying Kotlin's function.
84    */
85   std::shared_ptr<jsi::Function> toJSFunction(
86     jsi::Runtime &runtime,
87     JSIInteropModuleRegistry *moduleRegistry
88   );
89 
90   /**
91    * Calls the underlying Kotlin function.
92    */
93   jsi::Value callSync(
94     jsi::Runtime &rt,
95     JSIInteropModuleRegistry *moduleRegistry,
96     const jsi::Value *args,
97     size_t count
98   );
99 
100 private:
101   /**
102    * Reference to one of two java objects - `JNIFunctionBody` or `JNIAsyncFunctionBody`.
103    *
104    * In case when `isAsync` is `true`, this variable will point to `JNIAsyncFunctionBody`.
105    * Otherwise to `JNIFunctionBody`
106    */
107   jni::global_ref<jobject> jBodyReference;
108 
109   /**
110    * To not create a jsi::Function always when we need it, we cached that value.
111    */
112   std::shared_ptr<jsi::Function> body = nullptr;
113 
114   jsi::Function toSyncFunction(jsi::Runtime &runtime, JSIInteropModuleRegistry *moduleRegistry);
115 
116   jsi::Function toAsyncFunction(jsi::Runtime &runtime, JSIInteropModuleRegistry *moduleRegistry);
117 
118   jsi::Function createPromiseBody(
119     jsi::Runtime &runtime,
120     JSIInteropModuleRegistry *moduleRegistry,
121     jobjectArray globalArgs
122   );
123 
124   jobjectArray convertJSIArgsToJNI(
125     JSIInteropModuleRegistry *moduleRegistry,
126     JNIEnv *env,
127     jsi::Runtime &rt,
128     const jsi::Value *args,
129     size_t count
130   );
131 };
132 } // namespace expo
133