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