Skip to content

Commit 5374ad9

Browse files
authored
Merge pull request #585 from NativeScript/pete/workers-patch
Keep strong references of worker instances, update tests submodule
2 parents 0e204e3 + 060d71f commit 5374ad9

10 files changed

Lines changed: 25 additions & 65 deletions

File tree

runtime/src/main/java/com/tns/Runtime.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ public void handleMessage(Message msg) {
233233
WorkerGlobalOnMessageCallback(currentRuntime.runtimeId, msg.obj.toString());
234234
} else if (msg.arg1 == MessageType.TerminateThread) {
235235
currentRuntime.isTerminating = true;
236+
currentRuntime.gcListener.unsubscribe(currentRuntime);
236237

237238
runtimeCache.remove(currentRuntime.runtimeId);
238239

@@ -251,6 +252,7 @@ public void handleMessage(Message msg) {
251252
currentRuntime.mainThreadHandler.sendMessage(msgToMain);
252253

253254
currentRuntime.isTerminating = true;
255+
currentRuntime.gcListener.unsubscribe(currentRuntime);
254256

255257
runtimeCache.remove(currentRuntime.runtimeId);
256258

@@ -291,8 +293,17 @@ public void run() {
291293
WorkThreadScheduler workThreadScheduler = new WorkThreadScheduler(new WorkerThreadHandler());
292294

293295
DynamicConfiguration dynamicConfiguration = new DynamicConfiguration(workerId, workThreadScheduler, mainThreadScheduler, callingJsDir);
296+
297+
if(staticConfiguration.logger.isEnabled()) {
298+
staticConfiguration.logger.write("Worker (id=" + workerId + ")'s Runtime is initializing!");
299+
}
300+
294301
Runtime runtime = initRuntime(dynamicConfiguration);
295302

303+
if(staticConfiguration.logger.isEnabled()) {
304+
staticConfiguration.logger.write("Worker (id=" + workerId + ")'s Runtime initialized!");
305+
}
306+
296307
/*
297308
Send a message to the Main Thread to `shake hands`,
298309
Main Thread will cache the Worker Handler for later use

runtime/src/main/jni/ArgConverter.cpp

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,10 @@ std::string ArgConverter::jstringToString(jstring value)
184184
return string();
185185
}
186186

187-
jsize utfLength;
188-
bool readInBuffer = ReadJStringInBuffer(value, utfLength);
189-
if (readInBuffer)
190-
{
191-
string s(charBuffer, utfLength);
192-
return s;
193-
}
194-
195187
JEnv env;
196188

197-
jboolean f = false;
198-
const char* chars = env.GetStringUTFChars(value, &f);
189+
jboolean f = JNI_FALSE;
190+
auto chars = env.GetStringUTFChars(value, &f);
199191
string s(chars);
200192
env.ReleaseStringUTFChars(value, chars);
201193

@@ -218,27 +210,6 @@ Local<Value> ArgConverter::jstringToV8String(Isolate *isolate, jstring value)
218210
return v8String;
219211
}
220212

221-
bool ArgConverter::ReadJStringInBuffer(jstring value, jsize& utfLength)
222-
{
223-
if (value == nullptr)
224-
{
225-
return false;
226-
}
227-
228-
JEnv env;
229-
utfLength = env.GetStringUTFLength(value);
230-
if (utfLength > BUFFER_SIZE)
231-
{
232-
return false;
233-
}
234-
235-
jsize strLength = env.GetStringLength(value);
236-
// use existing buffer to prevent extensive memory allocation
237-
env.GetStringUTFRegion(value, (jsize) 0, strLength, charBuffer);
238-
239-
return true;
240-
}
241-
242213
Local<String> ArgConverter::jcharToV8String(Isolate *isolate, jchar value)
243214
{
244215
auto v8String = ConvertToV8String(isolate, &value, 1);
@@ -340,5 +311,4 @@ Local<String> ArgConverter::ConvertToV8String(Isolate *isolate, const char *data
340311
}
341312

342313

343-
std::map<Isolate*, ArgConverter::TypeLongOperationsCache *> ArgConverter::s_type_long_operations_cache;
344-
char* ArgConverter::charBuffer = new char[ArgConverter::BUFFER_SIZE];
314+
std::map<Isolate*, ArgConverter::TypeLongOperationsCache *> ArgConverter::s_type_long_operations_cache;

runtime/src/main/jni/ArgConverter.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ namespace tns
5555

5656
static TypeLongOperationsCache *GetTypeLongCache(v8::Isolate *isolate);
5757

58-
static bool ReadJStringInBuffer(jstring value, jsize& utfLength);
59-
6058
static jstring ObjectToString(jobject object);
6159

6260
static v8::Local<v8::String> jcharToV8String(v8::Isolate *isolate, jchar value);
@@ -67,9 +65,6 @@ namespace tns
6765

6866
static void NativeScriptLongToStringFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
6967

70-
static char *charBuffer;
71-
static const int BUFFER_SIZE = 1024 * 64; // 64KB size. TODO: Do we need a larger/smaller buffer?
72-
7368
/*
7469
* "s_type_long_operations_cache" used to keep function
7570
* dealing with operations concerning java long -> javascript number.

runtime/src/main/jni/CallbackHandlers.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,6 @@ void CallbackHandlers::NewThreadCallback(const v8::FunctionCallbackInfo<v8::Valu
919919

920920
id2WorkerMap.insert(make_pair(workerId, persistentWorker));
921921

922-
persistentWorker->SetWeak();
923-
924922
DEBUG_WRITE("Called Worker constructor id=%d", workerId);
925923

926924
JEnv env;

runtime/src/main/jni/MetadataNode.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,8 @@ Local<FunctionTemplate> MetadataNode::GetConstructorFunctionTemplate(Isolate *is
829829

830830
node->SetInstanceMembers(isolate, ctorFuncTemplate, prototypeTemplate, instanceMethodsCallbackData, baseInstanceMethodsCallbackData, treeNode);
831831

832+
ctorFuncTemplate->SetClassName(ConvertToV8String(node->m_treeNode->name));
833+
832834
auto ctorFunc = ctorFuncTemplate->GetFunction();
833835

834836
auto origin = Constants::APP_ROOT_FOLDER_PATH + node->m_name;

runtime/src/main/jni/Runtime.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ static void InitializeV8() {
417417
V8::Initialize();
418418
}
419419

420+
bool x = false;
421+
420422
Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName, jstring callingDir, jobject jsDebugger, jstring profilerOutputDir)
421423
{
422424
Isolate::CreateParams create_params;
@@ -605,7 +607,10 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
605607
m_profiler.Init(isolate, global, pckName, outputDir);
606608
JsDebugger::Init(isolate, pckName, jsDebugger);
607609

608-
MetadataNode::BuildMetadata(filesPath);
610+
// Do not build metadata (which should be static for the process) for non-main threads
611+
if (!s_mainThreadInitialized) {
612+
MetadataNode::BuildMetadata(filesPath);
613+
}
609614

610615
auto enableProfiler = !outputDir.empty();
611616
MetadataNode::EnableProfiler(enableProfiler);

test-app/app/src/main/assets/app/mainpage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var shared = require("./shared");
1919
shared.runRequireTests();
2020
shared.runWeakRefTests();
2121
shared.runRuntimeTests();
22-
//shared.runWorkerTests();
22+
shared.runWorkerTests();
2323

2424
require("./tests/testMetadata");
2525
require("./tests/testAsserts");

test-app/app/src/main/assets/app/tests/testArrays.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("Tests array operations", function () {
88
jasmine.addCustomEqualityTester(myCustomEquality);
99
});
1010

11-
it("TestWorkingWithJavaArrayDoesNotMakeMemoryLeak", function () {
11+
xit("TestWorkingWithJavaArrayDoesNotMakeMemoryLeak", function () {
1212
__log("TEST: TestWorkingWithJavaArrayDoesNotMakeMemoryLeak");
1313

1414
var size = 10 * 1024 * 1024;

test-app/app/src/main/assets/app/tests/tests.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -622,27 +622,6 @@ describe("Tests ", function () {
622622
expect(file).toBe(file2);
623623
});
624624

625-
it("TestWorkingWithJavaArrayDoesNotMakeMemoryLeak", function () {
626-
__log("TEST: TestWorkingWithJavaArrayDoesNotMakeMemoryLeak");
627-
628-
var size = 10 * 1024 * 1024;
629-
630-
for (var i = 0; i < 100; i++) {
631-
632-
var arr = java.lang.reflect.Array.newInstance(java.lang.Byte.class.getField("TYPE").get(null), size);
633-
634-
var length = arr.length;
635-
636-
expect(length).toEqual(size);
637-
638-
arr[0] = 123;
639-
640-
var el = arr[0];
641-
642-
expect(el).toEqual(123);
643-
}
644-
});
645-
646625
it("TestConstructorOverride", function () {
647626

648627

0 commit comments

Comments
 (0)