-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathETInstallerModule.cpp
More file actions
80 lines (64 loc) · 2.73 KB
/
ETInstallerModule.cpp
File metadata and controls
80 lines (64 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "ETInstallerModule.h"
#include <rnexecutorch/RnExecutorchInstaller.h>
#include "EmulatorDetection.h"
#include <jni.h>
#include <jsi/jsi.h>
namespace rnexecutorch {
JavaVM *java_machine;
using namespace facebook::jni;
ETInstallerModule::ETInstallerModule(
jni::alias_ref<ETInstallerModule::jhybridobject> &jThis,
jsi::Runtime *jsiRuntime,
const std::shared_ptr<facebook::react::CallInvoker> &jsCallInvoker)
: javaPart_(make_global(jThis)), jsiRuntime_(jsiRuntime),
jsCallInvoker_(jsCallInvoker) {}
jni::local_ref<ETInstallerModule::jhybriddata> ETInstallerModule::initHybrid(
jni::alias_ref<jhybridobject> jThis, jlong jsContext,
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject>
jsCallInvokerHolder) {
auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();
auto rnRuntime = reinterpret_cast<jsi::Runtime *>(jsContext);
return makeCxxInstance(jThis, rnRuntime, jsCallInvoker);
}
void ETInstallerModule::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", ETInstallerModule::initHybrid),
makeNativeMethod("injectJSIBindings",
ETInstallerModule::injectJSIBindings),
});
}
void ETInstallerModule::injectJSIBindings() {
// Grab a function for fetching images via URL from Java
auto fetchDataByUrl = [](std::string url) {
// Attaching Current Thread to JVM
JNIEnv *env = nullptr;
int getEnvStat = java_machine->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) {
if (java_machine->AttachCurrentThread(&env, nullptr) != 0) {
throw std::runtime_error("Failed to attach thread to JVM");
}
}
static jclass cls = javaClassStatic().get();
static jmethodID method = env->GetStaticMethodID(
cls, "fetchByteDataFromUrl", "(Ljava/lang/String;)[B");
jstring jUrl = env->NewStringUTF(url.c_str());
jbyteArray byteData =
(jbyteArray)env->CallStaticObjectMethod(cls, method, jUrl);
if (env->IsSameObject(byteData, NULL)) {
throw std::runtime_error("Error fetching data from a url");
}
int size = env->GetArrayLength(byteData);
jbyte *bytes = env->GetByteArrayElements(byteData, JNI_FALSE);
std::byte *dataBytePtr = reinterpret_cast<std::byte *>(bytes);
return std::vector<std::byte>(dataBytePtr, dataBytePtr + size);
};
auto _isEmulator = isEmulator();
RnExecutorchInstaller::injectJSIBindings(jsiRuntime_, jsCallInvoker_,
fetchDataByUrl, _isEmulator);
}
} // namespace rnexecutorch
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
rnexecutorch::java_machine = vm;
return facebook::jni::initialize(
vm, [] { rnexecutorch::ETInstallerModule::registerNatives(); });
}