diff options
-rw-r--r-- | src/library_browser.js | 14 | ||||
-rw-r--r-- | src/postamble.js | 8 | ||||
-rwxr-xr-x | tests/runner.py | 8 | ||||
-rw-r--r-- | tests/worker_api_2_main.cpp | 70 | ||||
-rw-r--r-- | tests/worker_api_2_worker.cpp | 27 |
5 files changed, 114 insertions, 13 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 7e2866b8..b671e6f9 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -580,11 +580,11 @@ mergeInto(LibraryManager.library, { info.worker.onmessage = function(msg) { var info = Browser.workers[id]; if (!info) return; // worker was destroyed meanwhile - var callbackId = msg.data.callbackId; + var callbackId = msg.data['callbackId']; var callbackInfo = info.callbacks[callbackId]; if (!callbackInfo) return; // no callback or callback removed meanwhile info.callbacks[callbackId] = null; // TODO: reuse callbackIds, compress this - var data = msg.data.data; + 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); @@ -617,9 +617,9 @@ mergeInto(LibraryManager.library, { }); } info.worker.postMessage({ - funcName: funcName, - callbackId: callbackId, - data: {{{ makeHEAPView('U8', 'data', 'data + size') }}} + 'funcName': funcName, + 'callbackId': callbackId, + 'data': {{{ makeHEAPView('U8', 'data', 'data + size') }}} }); }, @@ -628,8 +628,8 @@ mergeInto(LibraryManager.library, { if (workerResponded) throw 'already responded!'; workerResponded = true; postMessage({ - callbackId: workerCallbackId, - data: {{{ makeHEAPView('U8', 'data', 'data + size') }}} + 'callbackId': workerCallbackId, + 'data': {{{ makeHEAPView('U8', 'data', 'data + size') }}} }); } }); diff --git a/src/postamble.js b/src/postamble.js index 86c990e8..365f77d0 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -119,9 +119,9 @@ var buffer = 0, bufferSize = 0; var inWorkerCall = false, workerResponded = false, workerCallbackId = -1; 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; + 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); @@ -132,7 +132,7 @@ onmessage = function(msg) { inWorkerCall = true; workerResponded = false; - workerCallbackId = msg.data.callbackId; + workerCallbackId = msg.data['callbackId']; func(buffer, data.length); inWorkerCall = false; } diff --git a/tests/runner.py b/tests/runner.py index 12febeee..e5b8d13a 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -9115,8 +9115,12 @@ elif 'browser' in str(sys.argv): self.btest('pre_run_deps.cpp', expected='10', args=['--pre-js', 'pre.js']) def test_worker_api(self): - Popen(['python', EMCC, path_from_root('tests', 'worker_api_worker.cpp'), '-o', 'worker.js', '-s', 'BUILD_AS_WORKER=1', '-O0', '--closure', '0', '-s', 'EXPORTED_FUNCTIONS=["_one", "_two"]']).communicate() - self.btest('worker_api_main.cpp', args=['-O0', '--closure', '0'], expected='566') + Popen(['python', EMCC, path_from_root('tests', 'worker_api_worker.cpp'), '-o', 'worker.js', '-s', 'BUILD_AS_WORKER=1', '-s', 'EXPORTED_FUNCTIONS=["_one"]']).communicate() + 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() + self.btest('worker_api_2_main.cpp', args=['-O2', '--minify', '0'], expected='11') pids_to_clean = [] def clean_pids(self): diff --git a/tests/worker_api_2_main.cpp b/tests/worker_api_2_main.cpp new file mode 100644 index 00000000..4824cf74 --- /dev/null +++ b/tests/worker_api_2_main.cpp @@ -0,0 +1,70 @@ +#include <stdio.h> +#include <assert.h> +#include <emscripten.h> + +struct Info { + int i; + float f; + char c; + double d; +}; + +int w1; + +Info x[3] = { { 22, 3.159, 97, 2.1828 }, + { 55123, 987612.563, 190, 0.0009 }, + { -102, -12.532, -21, -51252 } }; + +int stage = 1; + +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; + + int i = stage - 3; + printf("c2-%d\n", i); + printf("%d, %.2f, %d, %.2f\n", x2[0].i, x2[0].f, x2[0].c, x2[0].d); + printf("%d, %.2f, %d, %.2f\n", x[i].i, x[i].f, x[i].c, x[i].d); + assert(x2[0].i == x[i].i+1); + assert(x2[0].f == x[i].f-1); + assert(x2[0].c == x[i].c+1); + assert(x2[0].d == x[i].d-1); + + if (stage == 5) { + int result = 11; + REPORT_RESULT(); + } + stage++; +} + +void c1(char *data, int size, void *arg) { // tests copying + buffer enlargement + assert((int)arg == stage); + Info *x2 = (Info*)data; + assert(x2 != x && x2 != x+1 && x2 != x+2); + for (int i = 0; i < size/sizeof(Info); i++) { + printf("c1-%d\n", i); + printf(" %d, %.2f, %d, %.2f\n", x2[i].i, x2[i].f, x2[i].c, x2[i].d); + printf(" %d, %.2f, %d, %.2f\n", x[i].i, x[i].f, x[i].c, x[i].d); + assert(x2[i].i == x[i].i); + assert(x2[i].f == x[i].f); + assert(x2[i].c == x[i].c); + assert(x2[i].d == x[i].d); + } + if (stage < 2) { + emscripten_call_worker(w1, "one", (char*)x, sizeof(Info)*3, c1, (void*)2); + } else { + emscripten_call_worker(w1, "two", (char*)&x[0], sizeof(Info), c2, (void*)3); + emscripten_call_worker(w1, "two", (char*)&x[1], sizeof(Info), c2, (void*)4); + emscripten_call_worker(w1, "two", (char*)&x[2], sizeof(Info), c2, (void*)5); + } + stage++; +} + +int main() { + w1 = emscripten_create_worker("worker.js"); + + emscripten_call_worker(w1, "one", (char*)x, sizeof(Info)*2, c1, (void*)1); + + return 0; +} + diff --git a/tests/worker_api_2_worker.cpp b/tests/worker_api_2_worker.cpp new file mode 100644 index 00000000..6db704a5 --- /dev/null +++ b/tests/worker_api_2_worker.cpp @@ -0,0 +1,27 @@ +#include <assert.h> +#include <emscripten.h> + +struct Info { + int i; + float f; + char c; + double d; +}; + +extern "C" { + +void one(char *data, int size) { + emscripten_worker_respond(data, size); +} + +void two(char *data, int size) { + Info *x = (Info*)data; + x[0].i++; + x[0].f--; + x[0].c++; + x[0].d--; + emscripten_worker_respond(data, size); +} + +} + |