diff --git a/runtime/src/main/java/com/tns/Runtime.java b/runtime/src/main/java/com/tns/Runtime.java index 79a69aa70..053e4d187 100644 --- a/runtime/src/main/java/com/tns/Runtime.java +++ b/runtime/src/main/java/com/tns/Runtime.java @@ -233,6 +233,7 @@ public void handleMessage(Message msg) { WorkerGlobalOnMessageCallback(currentRuntime.runtimeId, msg.obj.toString()); } else if (msg.arg1 == MessageType.TerminateThread) { currentRuntime.isTerminating = true; + currentRuntime.gcListener.unsubscribe(currentRuntime); runtimeCache.remove(currentRuntime.runtimeId); @@ -251,6 +252,7 @@ public void handleMessage(Message msg) { currentRuntime.mainThreadHandler.sendMessage(msgToMain); currentRuntime.isTerminating = true; + currentRuntime.gcListener.unsubscribe(currentRuntime); runtimeCache.remove(currentRuntime.runtimeId); @@ -291,8 +293,17 @@ public void run() { WorkThreadScheduler workThreadScheduler = new WorkThreadScheduler(new WorkerThreadHandler()); DynamicConfiguration dynamicConfiguration = new DynamicConfiguration(workerId, workThreadScheduler, mainThreadScheduler, callingJsDir); + + if(staticConfiguration.logger.isEnabled()) { + staticConfiguration.logger.write("Worker (id=" + workerId + ")'s Runtime is initializing!"); + } + Runtime runtime = initRuntime(dynamicConfiguration); + if(staticConfiguration.logger.isEnabled()) { + staticConfiguration.logger.write("Worker (id=" + workerId + ")'s Runtime initialized!"); + } + /* Send a message to the Main Thread to `shake hands`, Main Thread will cache the Worker Handler for later use diff --git a/runtime/src/main/jni/ArgConverter.cpp b/runtime/src/main/jni/ArgConverter.cpp index 1756ae464..669de995d 100644 --- a/runtime/src/main/jni/ArgConverter.cpp +++ b/runtime/src/main/jni/ArgConverter.cpp @@ -184,18 +184,10 @@ std::string ArgConverter::jstringToString(jstring value) return string(); } - jsize utfLength; - bool readInBuffer = ReadJStringInBuffer(value, utfLength); - if (readInBuffer) - { - string s(charBuffer, utfLength); - return s; - } - JEnv env; - jboolean f = false; - const char* chars = env.GetStringUTFChars(value, &f); + jboolean f = JNI_FALSE; + auto chars = env.GetStringUTFChars(value, &f); string s(chars); env.ReleaseStringUTFChars(value, chars); @@ -218,27 +210,6 @@ Local ArgConverter::jstringToV8String(Isolate *isolate, jstring value) return v8String; } -bool ArgConverter::ReadJStringInBuffer(jstring value, jsize& utfLength) -{ - if (value == nullptr) - { - return false; - } - - JEnv env; - utfLength = env.GetStringUTFLength(value); - if (utfLength > BUFFER_SIZE) - { - return false; - } - - jsize strLength = env.GetStringLength(value); - // use existing buffer to prevent extensive memory allocation - env.GetStringUTFRegion(value, (jsize) 0, strLength, charBuffer); - - return true; -} - Local ArgConverter::jcharToV8String(Isolate *isolate, jchar value) { auto v8String = ConvertToV8String(isolate, &value, 1); @@ -340,5 +311,4 @@ Local ArgConverter::ConvertToV8String(Isolate *isolate, const char *data } -std::map ArgConverter::s_type_long_operations_cache; -char* ArgConverter::charBuffer = new char[ArgConverter::BUFFER_SIZE]; +std::map ArgConverter::s_type_long_operations_cache; \ No newline at end of file diff --git a/runtime/src/main/jni/ArgConverter.h b/runtime/src/main/jni/ArgConverter.h index 478493098..098ac8abf 100644 --- a/runtime/src/main/jni/ArgConverter.h +++ b/runtime/src/main/jni/ArgConverter.h @@ -55,8 +55,6 @@ namespace tns static TypeLongOperationsCache *GetTypeLongCache(v8::Isolate *isolate); - static bool ReadJStringInBuffer(jstring value, jsize& utfLength); - static jstring ObjectToString(jobject object); static v8::Local jcharToV8String(v8::Isolate *isolate, jchar value); @@ -67,9 +65,6 @@ namespace tns static void NativeScriptLongToStringFunctionCallback(const v8::FunctionCallbackInfo& args); - static char *charBuffer; - static const int BUFFER_SIZE = 1024 * 64; // 64KB size. TODO: Do we need a larger/smaller buffer? - /* * "s_type_long_operations_cache" used to keep function * dealing with operations concerning java long -> javascript number. diff --git a/runtime/src/main/jni/CallbackHandlers.cpp b/runtime/src/main/jni/CallbackHandlers.cpp index d1ea01801..9d012cc8a 100644 --- a/runtime/src/main/jni/CallbackHandlers.cpp +++ b/runtime/src/main/jni/CallbackHandlers.cpp @@ -919,8 +919,6 @@ void CallbackHandlers::NewThreadCallback(const v8::FunctionCallbackInfoSetWeak(); - DEBUG_WRITE("Called Worker constructor id=%d", workerId); JEnv env; diff --git a/runtime/src/main/jni/MetadataNode.cpp b/runtime/src/main/jni/MetadataNode.cpp index 7878f01ec..c9ba26a28 100644 --- a/runtime/src/main/jni/MetadataNode.cpp +++ b/runtime/src/main/jni/MetadataNode.cpp @@ -829,6 +829,8 @@ Local MetadataNode::GetConstructorFunctionTemplate(Isolate *is node->SetInstanceMembers(isolate, ctorFuncTemplate, prototypeTemplate, instanceMethodsCallbackData, baseInstanceMethodsCallbackData, treeNode); + ctorFuncTemplate->SetClassName(ConvertToV8String(node->m_treeNode->name)); + auto ctorFunc = ctorFuncTemplate->GetFunction(); auto origin = Constants::APP_ROOT_FOLDER_PATH + node->m_name; diff --git a/runtime/src/main/jni/Runtime.cpp b/runtime/src/main/jni/Runtime.cpp index f17450328..2a7ed30f7 100644 --- a/runtime/src/main/jni/Runtime.cpp +++ b/runtime/src/main/jni/Runtime.cpp @@ -417,6 +417,8 @@ static void InitializeV8() { V8::Initialize(); } +bool x = false; + Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName, jstring callingDir, jobject jsDebugger, jstring profilerOutputDir) { Isolate::CreateParams create_params; @@ -605,7 +607,10 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName, m_profiler.Init(isolate, global, pckName, outputDir); JsDebugger::Init(isolate, pckName, jsDebugger); - MetadataNode::BuildMetadata(filesPath); + // Do not build metadata (which should be static for the process) for non-main threads + if (!s_mainThreadInitialized) { + MetadataNode::BuildMetadata(filesPath); + } auto enableProfiler = !outputDir.empty(); MetadataNode::EnableProfiler(enableProfiler); diff --git a/test-app/app/src/main/assets/app/mainpage.js b/test-app/app/src/main/assets/app/mainpage.js index 93893d954..766b2dbe3 100644 --- a/test-app/app/src/main/assets/app/mainpage.js +++ b/test-app/app/src/main/assets/app/mainpage.js @@ -19,7 +19,7 @@ var shared = require("./shared"); shared.runRequireTests(); shared.runWeakRefTests(); shared.runRuntimeTests(); -//shared.runWorkerTests(); +shared.runWorkerTests(); require("./tests/testMetadata"); require("./tests/testAsserts"); diff --git a/test-app/app/src/main/assets/app/shared b/test-app/app/src/main/assets/app/shared index 00e3ef0d3..d69e4d3a2 160000 --- a/test-app/app/src/main/assets/app/shared +++ b/test-app/app/src/main/assets/app/shared @@ -1 +1 @@ -Subproject commit 00e3ef0d3d29e1d849531bf9a956744e726ce2af +Subproject commit d69e4d3a2c93eba303669331dc3310acbd455496 diff --git a/test-app/app/src/main/assets/app/tests/testArrays.js b/test-app/app/src/main/assets/app/tests/testArrays.js index e44540389..96120909f 100644 --- a/test-app/app/src/main/assets/app/tests/testArrays.js +++ b/test-app/app/src/main/assets/app/tests/testArrays.js @@ -8,7 +8,7 @@ describe("Tests array operations", function () { jasmine.addCustomEqualityTester(myCustomEquality); }); - it("TestWorkingWithJavaArrayDoesNotMakeMemoryLeak", function () { + xit("TestWorkingWithJavaArrayDoesNotMakeMemoryLeak", function () { __log("TEST: TestWorkingWithJavaArrayDoesNotMakeMemoryLeak"); var size = 10 * 1024 * 1024; diff --git a/test-app/app/src/main/assets/app/tests/tests.js b/test-app/app/src/main/assets/app/tests/tests.js index 8a49c6158..85bbc9390 100644 --- a/test-app/app/src/main/assets/app/tests/tests.js +++ b/test-app/app/src/main/assets/app/tests/tests.js @@ -622,27 +622,6 @@ describe("Tests ", function () { expect(file).toBe(file2); }); - it("TestWorkingWithJavaArrayDoesNotMakeMemoryLeak", function () { - __log("TEST: TestWorkingWithJavaArrayDoesNotMakeMemoryLeak"); - - var size = 10 * 1024 * 1024; - - for (var i = 0; i < 100; i++) { - - var arr = java.lang.reflect.Array.newInstance(java.lang.Byte.class.getField("TYPE").get(null), size); - - var length = arr.length; - - expect(length).toEqual(size); - - arr[0] = 123; - - var el = arr[0]; - - expect(el).toEqual(123); - } - }); - it("TestConstructorOverride", function () {