1 // Copyright © 2021-present 650 Industries, Inc. (aka Expo)
2 
3 #include "JNIFunctionBody.h"
4 #include "JavaReferencesCache.h"
5 
6 namespace jni = facebook::jni;
7 namespace react = facebook::react;
8 
9 namespace expo {
10 jni::local_ref<react::ReadableNativeArray::javaobject>
11 JNIFunctionBody::invoke(jobjectArray args) {
12   // Do NOT use getClass here!
13   // Method obtained from `getClass` will point to the overridden version of the method.
14   // Because of that, it can't be cached - we will try to invoke the nonexistent method
15   // if we receive an object of a different class than the one used to obtain the method id.
16   // The only cacheable method id can be obtain from the base class.
17   static const auto method = jni::findClassLocal("expo/modules/kotlin/jni/JNIFunctionBody")
18     ->getMethod<jni::local_ref<react::ReadableNativeArray::javaobject>(jobjectArray)>(
19       "invoke",
20       "([Ljava/lang/Object;)Lcom/facebook/react/bridge/ReadableNativeArray;"
21     );
22 
23   return method(this->self(), args);
24 }
25 
26 void JNIAsyncFunctionBody::invoke(
27   jobjectArray args,
28   jobject promise
29 ) {
30   // Do NOT use getClass here!
31   // Method obtained from `getClass` will point to the overridden version of the method.
32   // Because of that, it can't be cached - we will try to invoke the nonexistent method
33   // if we receive an object of a different class than the one used to obtain the method id.
34   // The only cacheable method id can be obtain from the base class.
35   static const auto method = jni::findClassLocal("expo/modules/kotlin/jni/JNIAsyncFunctionBody")
36     ->getMethod<
37       void(jobjectArray , jobject)
38     >(
39       "invoke",
40       "([Ljava/lang/Object;Ljava/lang/Object;)V"
41     );
42 
43   method(this->self(), args, promise);
44 }
45 } // namespace expo
46