Skip to content

Commit 312e8da

Browse files
committed
Use ThreadScope::WithClassLoader to use app's JNI class_loader
1 parent b95f47f commit 312e8da

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

android/src/main/cpp/cpp-adapter.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@ struct MultithreadingModule : jni::JavaClass<MultithreadingModule> {
3131

3232
private:
3333
static std::shared_ptr<react::JSExecutorFactory> makeJSExecutorFactory() {
34-
ThreadScope scope; // JNI needs to attach this thread because this function is being called from a different Thread
35-
36-
__android_log_write(ANDROID_LOG_INFO, TAG, "Calling Java method MultithreadingModule.makeJSExecutor()...");
37-
static const auto cls = javaClassStatic();
38-
static const auto method = cls->getStaticMethod<react::JavaScriptExecutorHolder()>("makeJSExecutor");
39-
auto result = method(cls);
40-
__android_log_write(ANDROID_LOG_INFO, TAG, "JavaScriptExecutor created! Getting factory...");
41-
return result->cthis()->getExecutorFactory();
34+
std::shared_ptr<react::JSExecutorFactory> factory;
35+
36+
ThreadScope::WithClassLoader([&factory]() {
37+
__android_log_write(ANDROID_LOG_INFO, TAG, "Calling Java method MultithreadingModule.makeJSExecutor()...");
38+
static const auto cls = javaClassStatic();
39+
static const auto method = cls->getStaticMethod<react::JavaScriptExecutorHolder()>("makeJSExecutor");
40+
auto result = method(cls);
41+
__android_log_write(ANDROID_LOG_INFO, TAG, "JavaScriptExecutor created! Getting factory...");
42+
auto cxxInstance = result->cthis()->getExecutorFactory();
43+
factory = cxxInstance;
44+
});
45+
if (factory == nullptr) {
46+
throw std::runtime_error("Failed to create the JSExecutorFactory! shared_ptr was null.");
47+
}
48+
49+
return factory;
4250
}
4351

4452
static void installNative(jni::alias_ref<JClass>,
@@ -61,6 +69,9 @@ struct MultithreadingModule : jni::JavaClass<MultithreadingModule> {
6169
auto makeJsExecutor = []() -> std::unique_ptr<jsi::Runtime> {
6270
__android_log_write(ANDROID_LOG_DEBUG, TAG, "Creating JSExecutorFactory..");
6371
try {
72+
// JNI needs to attach this thread because this function is being called from a different Thread
73+
ThreadScope scope;
74+
6475
std::shared_ptr<react::ExecutorDelegate> delegate = std::shared_ptr<react::ExecutorDelegate>();
6576
std::shared_ptr<react::MessageQueueThread> jsQueue = std::shared_ptr<react::MessageQueueThread>();
6677
auto jsExecutorFactory = makeJSExecutorFactory();
@@ -69,11 +80,16 @@ struct MultithreadingModule : jni::JavaClass<MultithreadingModule> {
6980
jsQueue);
7081
auto runtimePointer = static_cast<jsi::Runtime *>(executor->getJavaScriptContext());
7182
__android_log_write(ANDROID_LOG_DEBUG, TAG, "JSExecutor created!");
83+
84+
// the returned value is now responsible for releasing the runtime.
85+
auto _ = executor.release();
7286
return std::unique_ptr<jsi::Runtime>(runtimePointer);
87+
7388
} catch (std::exception& exc) {
89+
// Fatal error - the runtime can't be created at all.
7490
__android_log_write(ANDROID_LOG_ERROR, TAG, "Failed to create JSExecutor!");
7591
__android_log_write(ANDROID_LOG_ERROR, TAG, exc.what());
76-
return std::unique_ptr<jsi::Runtime>(); // TODO: Remove this and let the caller handle the exception.
92+
abort();
7793
}
7894
};
7995
mrousavy::multithreading::install(*runtime, makeJsExecutor, makeScheduler, makeErrorHandler);

cpp/RNMultithreadingInstaller.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,12 @@ namespace mrousavy {
7373

7474
pool->enqueue([resolver, rejecter, worklet]() {
7575
try {
76-
auto m = manager.get();
77-
if (!m) throw std::runtime_error("RuntimeManager was not initialized!");
78-
auto r = m->runtime.get();
79-
if (!r) throw std::runtime_error("Runtime was not initialized!");
80-
auto& runtime = *r;
81-
throw std::runtime_error("1");
76+
auto& runtime = *manager->runtime;
8277

8378
auto function = worklet->getValue(runtime).asObject(runtime).asFunction(runtime);
84-
throw std::runtime_error("2");
8579
auto result = function.getFunction(runtime).callWithThis(runtime, function);
86-
throw std::runtime_error("3");
8780

8881
auto shareableResult = reanimated::ShareableValue::adapt(runtime, result, manager.get());
89-
throw std::runtime_error("4");
9082
resolver(shareableResult);
9183
} catch (std::exception& exc) {
9284
rejecter(exc.what());

0 commit comments

Comments
 (0)