From 5e32ce4e53d38616f6ca12bdd4456168f09fb520 Mon Sep 17 00:00:00 2001 From: farfromrefuge <> Date: Sat, 16 Dec 2023 21:46:00 +0100 Subject: [PATCH 1/4] fix: correctly load ts_helpers.js in workers --- .../src/main/java/com/tns/RuntimeHelper.java | 4 ++-- .../runtime/src/main/java/com/tns/Runtime.java | 18 +++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/test-app/app/src/main/java/com/tns/RuntimeHelper.java b/test-app/app/src/main/java/com/tns/RuntimeHelper.java index 423cc44b1..9847f3642 100644 --- a/test-app/app/src/main/java/com/tns/RuntimeHelper.java +++ b/test-app/app/src/main/java/com/tns/RuntimeHelper.java @@ -148,7 +148,7 @@ public static Runtime initRuntime(Context context) { StaticConfiguration config = new StaticConfiguration(logger, appName, nativeLibDir, rootDir, appDir, classLoader, dexDir, dexThumb, appConfig, isDebuggable); - runtime = Runtime.initializeRuntimeWithConfiguration(config); + runtime = Runtime.initializeRuntimeWithConfiguration(config, appDir.toString()); if (isDebuggable) { try { v8Inspector = new AndroidJsV8Inspector(context.getFilesDir().getAbsolutePath(), context.getPackageName()); @@ -190,7 +190,7 @@ public static Runtime initRuntime(Context context) { waitForLiveSync(context); } - runtime.runScript(new File(appDir, "internal/ts_helpers.js")); +// runtime.runScript(new File(appDir, "internal/ts_helpers.js")); File javaClassesModule = new File(appDir, "app/tns-java-classes.js"); if (javaClassesModule.exists()) { diff --git a/test-app/runtime/src/main/java/com/tns/Runtime.java b/test-app/runtime/src/main/java/com/tns/Runtime.java index ad8caed91..4c436b926 100644 --- a/test-app/runtime/src/main/java/com/tns/Runtime.java +++ b/test-app/runtime/src/main/java/com/tns/Runtime.java @@ -445,13 +445,15 @@ private static class WorkerThread extends HandlerThread { private ThreadScheduler mainThreadScheduler; private String filePath; private String callingJsDir; + private String appDir; - public WorkerThread(String name, Integer workerId, ThreadScheduler mainThreadScheduler, String callingJsDir) { + public WorkerThread(String name, Integer workerId, ThreadScheduler mainThreadScheduler, String callingJsDir, String appDir) { super("W" + workerId + ": " + name); this.filePath = name; this.workerId = workerId; this.mainThreadScheduler = mainThreadScheduler; this.callingJsDir = callingJsDir; + this.appDir = appDir; } public void startRuntime() { @@ -470,7 +472,7 @@ public void run() { staticConfiguration.logger.write("Worker (id=" + workerId + ")'s Runtime is initializing!"); } - Runtime runtime = initRuntime(dynamicConfiguration); + Runtime runtime = initRuntime(dynamicConfiguration, appDir); if (staticConfiguration.logger.isEnabled()) { staticConfiguration.logger.write("Worker (id=" + workerId + ")'s Runtime initialized!"); @@ -575,11 +577,11 @@ else if (msg.arg1 == MessageType.BubbleUpException) { This method initializes the runtime and should always be called first and through the main thread in order to set static configuration that all following workers can use */ - public static Runtime initializeRuntimeWithConfiguration(StaticConfiguration config) { + public static Runtime initializeRuntimeWithConfiguration(StaticConfiguration config, String appDir) { staticConfiguration = config; WorkThreadScheduler mainThreadScheduler = new WorkThreadScheduler(new MainThreadHandler(Looper.myLooper())); DynamicConfiguration dynamicConfiguration = new DynamicConfiguration(0, mainThreadScheduler, null); - Runtime runtime = initRuntime(dynamicConfiguration); + Runtime runtime = initRuntime(dynamicConfiguration, appDir); return runtime; } @@ -591,9 +593,10 @@ public static Runtime initializeRuntimeWithConfiguration(StaticConfiguration con public static void initWorker(String jsFileName, String callingJsDir, int id) { // This method will always be called from the Main thread Runtime runtime = Runtime.getCurrentRuntime(); - ThreadScheduler mainThreadScheduler = runtime.getDynamicConfig().myThreadScheduler; - WorkerThread worker = new WorkerThread(jsFileName, id, mainThreadScheduler, callingJsDir); + ThreadScheduler mainThreadScheduler = runtime.getDynamicConfig().myThreadScheduler; + String appDir = runtime.config.appDir.toString(); + WorkerThread worker = new WorkerThread(jsFileName, id, mainThreadScheduler, callingJsDir, appDir); worker.start(); worker.startRuntime(); } @@ -602,9 +605,10 @@ public static void initWorker(String jsFileName, String callingJsDir, int id) { This method deals with initializing the runtime with given configuration Does it for both workers and for the main thread */ - private static Runtime initRuntime(DynamicConfiguration dynamicConfiguration) { + private static Runtime initRuntime(DynamicConfiguration dynamicConfiguration, String appDir) { Runtime runtime = new Runtime(staticConfiguration, dynamicConfiguration); runtime.init(); + runtime.runScript(new File(appDir, "internal/ts_helpers.js")); return runtime; } From 3563478f5358924bb2dee5c1a16889840c20b5e7 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 26 Feb 2024 13:08:05 -0300 Subject: [PATCH 2/4] chore: remove redundant arguments --- test-app/app/src/main/java/com/tns/RuntimeHelper.java | 2 +- test-app/runtime/src/main/java/com/tns/Runtime.java | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test-app/app/src/main/java/com/tns/RuntimeHelper.java b/test-app/app/src/main/java/com/tns/RuntimeHelper.java index 9847f3642..90a64f16e 100644 --- a/test-app/app/src/main/java/com/tns/RuntimeHelper.java +++ b/test-app/app/src/main/java/com/tns/RuntimeHelper.java @@ -148,7 +148,7 @@ public static Runtime initRuntime(Context context) { StaticConfiguration config = new StaticConfiguration(logger, appName, nativeLibDir, rootDir, appDir, classLoader, dexDir, dexThumb, appConfig, isDebuggable); - runtime = Runtime.initializeRuntimeWithConfiguration(config, appDir.toString()); + runtime = Runtime.initializeRuntimeWithConfiguration(config); if (isDebuggable) { try { v8Inspector = new AndroidJsV8Inspector(context.getFilesDir().getAbsolutePath(), context.getPackageName()); diff --git a/test-app/runtime/src/main/java/com/tns/Runtime.java b/test-app/runtime/src/main/java/com/tns/Runtime.java index 4c436b926..fe604a4c6 100644 --- a/test-app/runtime/src/main/java/com/tns/Runtime.java +++ b/test-app/runtime/src/main/java/com/tns/Runtime.java @@ -472,7 +472,7 @@ public void run() { staticConfiguration.logger.write("Worker (id=" + workerId + ")'s Runtime is initializing!"); } - Runtime runtime = initRuntime(dynamicConfiguration, appDir); + Runtime runtime = initRuntime(dynamicConfiguration); if (staticConfiguration.logger.isEnabled()) { staticConfiguration.logger.write("Worker (id=" + workerId + ")'s Runtime initialized!"); @@ -577,11 +577,11 @@ else if (msg.arg1 == MessageType.BubbleUpException) { This method initializes the runtime and should always be called first and through the main thread in order to set static configuration that all following workers can use */ - public static Runtime initializeRuntimeWithConfiguration(StaticConfiguration config, String appDir) { + public static Runtime initializeRuntimeWithConfiguration(StaticConfiguration config) { staticConfiguration = config; WorkThreadScheduler mainThreadScheduler = new WorkThreadScheduler(new MainThreadHandler(Looper.myLooper())); DynamicConfiguration dynamicConfiguration = new DynamicConfiguration(0, mainThreadScheduler, null); - Runtime runtime = initRuntime(dynamicConfiguration, appDir); + Runtime runtime = initRuntime(dynamicConfiguration); return runtime; } @@ -605,10 +605,10 @@ public static void initWorker(String jsFileName, String callingJsDir, int id) { This method deals with initializing the runtime with given configuration Does it for both workers and for the main thread */ - private static Runtime initRuntime(DynamicConfiguration dynamicConfiguration, String appDir) { + private static Runtime initRuntime(DynamicConfiguration dynamicConfiguration) { Runtime runtime = new Runtime(staticConfiguration, dynamicConfiguration); runtime.init(); - runtime.runScript(new File(appDir, "internal/ts_helpers.js")); + runtime.runScript(new File(staticConfiguration.appDir, "internal/ts_helpers.js")); return runtime; } From 5828ca6c11d6cb894423bacde11d75061ca534bf Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 26 Feb 2024 14:53:03 -0300 Subject: [PATCH 3/4] feat: don't polyfill JavaProxy on worker --- test-app/app/src/main/assets/internal/ts_helpers.js | 4 +++- test-app/runtime/src/main/cpp/Runtime.cpp | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/test-app/app/src/main/assets/internal/ts_helpers.js b/test-app/app/src/main/assets/internal/ts_helpers.js index 1a0625f45..d1860dc47 100644 --- a/test-app/app/src/main/assets/internal/ts_helpers.js +++ b/test-app/app/src/main/assets/internal/ts_helpers.js @@ -170,6 +170,8 @@ Object.defineProperty(global, "__extends", { value: __extends }); Object.defineProperty(global, "__decorate", { value: __decorate }); - global.JavaProxy = JavaProxy; + if (!global.__ns__worker) { + global.JavaProxy = JavaProxy; + } global.Interfaces = Interfaces; })() \ No newline at end of file diff --git a/test-app/runtime/src/main/cpp/Runtime.cpp b/test-app/runtime/src/main/cpp/Runtime.cpp index 7e542170b..c7264148d 100644 --- a/test-app/runtime/src/main/cpp/Runtime.cpp +++ b/test-app/runtime/src/main/cpp/Runtime.cpp @@ -566,6 +566,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native else { m_isMainThread = false; auto postMessageFuncTemplate = FunctionTemplate::New(isolate, CallbackHandlers::WorkerGlobalPostMessageCallback); + globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__ns__worker"), Boolean::New(isolate, true)); globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "postMessage"), postMessageFuncTemplate); auto closeFuncTemplate = FunctionTemplate::New(isolate, CallbackHandlers::WorkerGlobalCloseCallback); globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "close"), closeFuncTemplate); From 4f9cc6376c48f9172b0d7b67251ab09cc296c18f Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 26 Feb 2024 15:01:42 -0300 Subject: [PATCH 4/4] chore: remove unneeded appDir --- test-app/runtime/src/main/java/com/tns/Runtime.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test-app/runtime/src/main/java/com/tns/Runtime.java b/test-app/runtime/src/main/java/com/tns/Runtime.java index fe604a4c6..17670e3dc 100644 --- a/test-app/runtime/src/main/java/com/tns/Runtime.java +++ b/test-app/runtime/src/main/java/com/tns/Runtime.java @@ -445,15 +445,13 @@ private static class WorkerThread extends HandlerThread { private ThreadScheduler mainThreadScheduler; private String filePath; private String callingJsDir; - private String appDir; - public WorkerThread(String name, Integer workerId, ThreadScheduler mainThreadScheduler, String callingJsDir, String appDir) { + public WorkerThread(String name, Integer workerId, ThreadScheduler mainThreadScheduler, String callingJsDir) { super("W" + workerId + ": " + name); this.filePath = name; this.workerId = workerId; this.mainThreadScheduler = mainThreadScheduler; this.callingJsDir = callingJsDir; - this.appDir = appDir; } public void startRuntime() { @@ -593,10 +591,9 @@ public static Runtime initializeRuntimeWithConfiguration(StaticConfiguration con public static void initWorker(String jsFileName, String callingJsDir, int id) { // This method will always be called from the Main thread Runtime runtime = Runtime.getCurrentRuntime(); - ThreadScheduler mainThreadScheduler = runtime.getDynamicConfig().myThreadScheduler; - String appDir = runtime.config.appDir.toString(); - WorkerThread worker = new WorkerThread(jsFileName, id, mainThreadScheduler, callingJsDir, appDir); + + WorkerThread worker = new WorkerThread(jsFileName, id, mainThreadScheduler, callingJsDir); worker.start(); worker.startRuntime(); }