From 97f9f26f4bebc8db98608df6e7bcf45322ee189b Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 22 Oct 2012 16:26:30 -0700 Subject: [PATCH] handle no-data worker api calls, and more tests --- src/library_browser.js | 22 +++++++++++++--------- src/postamble.js | 20 +++++++++++++------- tests/runner.py | 2 +- tests/worker_api_2_main.cpp | 29 +++++++++++++++++++++++++++-- tests/worker_api_2_worker.cpp | 17 +++++++++++++++++ 5 files changed, 71 insertions(+), 19 deletions(-) diff --git a/src/library_browser.js b/src/library_browser.js index b671e6f91..7c1d43201 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -585,14 +585,18 @@ mergeInto(LibraryManager.library, { if (!callbackInfo) return; // no callback or callback removed meanwhile info.callbacks[callbackId] = null; // TODO: reuse callbackIds, compress this var data = msg.data['data']; - if (!data.byteLength) data = new Uint8Array(data); - if (!info.buffer || info.bufferSize < data.length) { - if (info.buffer) _free(info.buffer); - info.bufferSize = data.length; - info.buffer = _malloc(data.length); + if (data) { + if (!data.byteLength) data = new Uint8Array(data); + if (!info.buffer || info.bufferSize < data.length) { + if (info.buffer) _free(info.buffer); + info.bufferSize = data.length; + info.buffer = _malloc(data.length); + } + HEAPU8.set(data, info.buffer); + callbackInfo.func(info.buffer, data.length, callbackInfo.arg); + } else { + callbackInfo.func(0, 0, callbackInfo.arg); } - HEAPU8.set(data, info.buffer); - callbackInfo.func(info.buffer, data.length, callbackInfo.arg); }; Browser.workers.push(info); return id; @@ -619,7 +623,7 @@ mergeInto(LibraryManager.library, { info.worker.postMessage({ 'funcName': funcName, 'callbackId': callbackId, - 'data': {{{ makeHEAPView('U8', 'data', 'data + size') }}} + 'data': data ? {{{ makeHEAPView('U8', 'data', 'data + size') }}} : 0 }); }, @@ -629,7 +633,7 @@ mergeInto(LibraryManager.library, { workerResponded = true; postMessage({ 'callbackId': workerCallbackId, - 'data': {{{ makeHEAPView('U8', 'data', 'data + size') }}} + 'data': data ? {{{ makeHEAPView('U8', 'data', 'data + size') }}} : 0 }); } }); diff --git a/src/postamble.js b/src/postamble.js index 365f77d0b..a4c430182 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -122,18 +122,24 @@ onmessage = function(msg) { var func = Module['_' + msg.data['funcName']]; if (!func) throw 'invalid worker function to call: ' + msg.data['funcName']; var data = msg.data['data']; - if (!data.byteLength) data = new Uint8Array(data); - if (!buffer || bufferSize < data.length) { - if (buffer) _free(buffer); - bufferSize = data.length; - buffer = _malloc(data.length); + if (data) { + if (!data.byteLength) data = new Uint8Array(data); + if (!buffer || bufferSize < data.length) { + if (buffer) _free(buffer); + bufferSize = data.length; + buffer = _malloc(data.length); + } + HEAPU8.set(data, buffer); } - HEAPU8.set(data, buffer); inWorkerCall = true; workerResponded = false; workerCallbackId = msg.data['callbackId']; - func(buffer, data.length); + if (data) { + func(buffer, data.length); + } else { + func(0, 0); + } inWorkerCall = false; } diff --git a/tests/runner.py b/tests/runner.py index e5b8d13a1..b0f4f507b 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -9119,7 +9119,7 @@ elif 'browser' in str(sys.argv): self.btest('worker_api_main.cpp', expected='566') def test_worker_api_2(self): - Popen(['python', EMCC, path_from_root('tests', 'worker_api_2_worker.cpp'), '-o', 'worker.js', '-s', 'BUILD_AS_WORKER=1', '-O2', '--minify', '0', '-s', 'EXPORTED_FUNCTIONS=["_one", "_two"]']).communicate() + Popen(['python', EMCC, path_from_root('tests', 'worker_api_2_worker.cpp'), '-o', 'worker.js', '-s', 'BUILD_AS_WORKER=1', '-O2', '--minify', '0', '-s', 'EXPORTED_FUNCTIONS=["_one", "_two", "_three", "_four"]']).communicate() self.btest('worker_api_2_main.cpp', args=['-O2', '--minify', '0'], expected='11') pids_to_clean = [] diff --git a/tests/worker_api_2_main.cpp b/tests/worker_api_2_main.cpp index 4824cf745..da24a43bd 100644 --- a/tests/worker_api_2_main.cpp +++ b/tests/worker_api_2_main.cpp @@ -17,6 +17,28 @@ Info x[3] = { { 22, 3.159, 97, 2.1828 }, int stage = 1; +int c3_7 = 0, c3_8 = 0; + +void c3(char *data, int size, void *arg) { // tests calls different in different workers. + int calls = *((int*)data); + printf("%d: %d\n", (int)arg, calls); + if ((int)arg == 7) { + assert(c3_7 == 0); + c3_7++; + assert(calls == 5); + } else { + assert((int)arg == 8); + assert(c3_8 == 0); + c3_8++; + assert(calls == 1); + } + if (c3_7 && c3_7) { // note: racey, responses from 2 workers here + emscripten_destroy_worker(w1); + int result = 11; + REPORT_RESULT(); + } +} + void c2(char *data, int size, void *arg) { // tests queuing up several messages, each with different data assert((int)arg == stage); Info *x2 = (Info*)data; @@ -31,8 +53,11 @@ void c2(char *data, int size, void *arg) { // tests queuing up several messages, assert(x2[0].d == x[i].d-1); if (stage == 5) { - int result = 11; - REPORT_RESULT(); + int w2 = emscripten_create_worker("worker.js"); + emscripten_call_worker(w2, "three", NULL, 0, NULL, (void*)6); // bump calls in new worker, once + + emscripten_call_worker(w1, "four", NULL, 0, c3, (void*)7); + emscripten_call_worker(w2, "four", NULL, 0, c3, (void*)8); } stage++; } diff --git a/tests/worker_api_2_worker.cpp b/tests/worker_api_2_worker.cpp index 6db704a5e..d7033bc41 100644 --- a/tests/worker_api_2_worker.cpp +++ b/tests/worker_api_2_worker.cpp @@ -8,13 +8,17 @@ struct Info { double d; }; +int calls = 0; // global state that is not in all workers + extern "C" { void one(char *data, int size) { + calls++; emscripten_worker_respond(data, size); } void two(char *data, int size) { + calls++; Info *x = (Info*)data; x[0].i++; x[0].f--; @@ -23,5 +27,18 @@ void two(char *data, int size) { emscripten_worker_respond(data, size); } +void three(char *data, int size) { + assert(data == 0); + assert(size == 0); + calls++; + // no response +} + +void four(char *data, int size) { + assert(data == 0); + assert(size == 0); + emscripten_worker_respond((char*)&calls, sizeof(calls)); +} + }