@@ -31,14 +31,22 @@ struct MultithreadingModule : jni::JavaClass<MultithreadingModule> {
3131
3232private:
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);
0 commit comments