handle no-data worker api calls, and more tests

This commit is contained in:
Alon Zakai 2012-10-22 16:26:30 -07:00
Родитель c11c08094b
Коммит 97f9f26f4b
5 изменённых файлов: 71 добавлений и 19 удалений

Просмотреть файл

@ -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
});
}
});

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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 = []

Просмотреть файл

@ -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++;
}

Просмотреть файл

@ -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));
}
}