diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-12-30 16:36:28 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-12-30 16:36:28 -0800 |
commit | e97f48a2c9376354a6b8927191322322f13808ba (patch) | |
tree | 1e1f079b5569ae2d7269ff2abf4b520ad82d3cdd | |
parent | c3ed294ed249b446bf5a290981fea79a8e4111d7 (diff) |
emscripten_async_wget_data
-rw-r--r-- | src/library_browser.js | 19 | ||||
-rw-r--r-- | system/include/emscripten/emscripten.h | 24 | ||||
-rw-r--r-- | tests/emscripten_fs_api_browser.cpp | 32 |
3 files changed, 71 insertions, 4 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 00ee158c..b14099f6 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -336,11 +336,11 @@ mergeInto(LibraryManager.library, { xhr.send(null); }, - asyncLoad: function(url, onload, onerror) { + asyncLoad: function(url, onload, onerror, noRunDep) { Browser.xhrLoad(url, function(arrayBuffer) { assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).'); onload(new Uint8Array(arrayBuffer)); - removeRunDependency('al ' + url); + if (!noRunDep) removeRunDependency('al ' + url); }, function(event) { if (onerror) { onerror(); @@ -348,7 +348,7 @@ mergeInto(LibraryManager.library, { throw 'Loading data file "' + url + '" failed.'; } }); - addRunDependency('al ' + url); + if (!noRunDep) addRunDependency('al ' + url); }, resizeListeners: [], @@ -381,10 +381,21 @@ mergeInto(LibraryManager.library, { }, function() { if (onerror) FUNCTION_TABLE[onerror](file); - } + } ); }, + emscripten_async_wget_data: function(url, arg, onload, onerror) { + Browser.asyncLoad(Pointer_stringify(url), function(byteArray) { + var buffer = _malloc(byteArray.length); + HEAPU8.set(byteArray, buffer); + FUNCTION_TABLE[onload](arg, buffer, byteArray.length); + _free(buffer); + }, function() { + if (onerror) FUNCTION_TABLE[onerror](arg); + }, true /* no need for run dependency, this is async but will not do any prepare etc. step */ ); + }, + emscripten_async_prepare: function(file, onload, onerror) { var _file = Pointer_stringify(file); var data = FS.analyzePath(_file); diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h index 3eefe0b8..926fe2e5 100644 --- a/system/include/emscripten/emscripten.h +++ b/system/include/emscripten/emscripten.h @@ -187,6 +187,30 @@ float emscripten_random(); void emscripten_async_wget(const char* url, const char* file, void (*onload)(const char*), void (*onerror)(const char*)); /* + * Data version of emscripten_async_wget. Instead of writing + * to a file, it writes to a buffer directly in memory. + * This avoids the overhead of using the emulated + * filesystem, note however that since files are not used, + * It cannot do the 'prepare' stage to set things up for + * IMG_Load and so forth (IMG_Load etc. work on files). + * + * @param arg User-defined data that is passed to the callbacks, + * + * @param onload Callback on success, with the @arg that + * was provided to this function, a pointer + * to a buffer with the data, and the size + * of the buffer. As in the worker API, the + * data buffer only lives during the + * callback, so you should use it or copy + * it during that time and not later. + * + * @param onerror An optional callback on failure, with the + * @arg that was provided to this function. + * + */ +void emscripten_async_wget_data(const char* url, void *arg, void (*onload)(void*, void*, int), void (*onerror)(void*)); + +/* * Prepare a file in asynchronous way. This does just the * preparation part of emscripten_async_wget, that is, it * works on file data already present, and asynchronously diff --git a/tests/emscripten_fs_api_browser.cpp b/tests/emscripten_fs_api_browser.cpp index a2a82160..9936a09c 100644 --- a/tests/emscripten_fs_api_browser.cpp +++ b/tests/emscripten_fs_api_browser.cpp @@ -9,10 +9,42 @@ extern "C" { int result = 1; int get_count = 0; +int data_ok = 0; +int data_bad = 0; + +void onLoadedData(void *arg, void *buffer, int size) { + get_count++; + assert(size == 329895); + assert((int)arg == 135); + unsigned char *b = (unsigned char*)buffer; + assert(b[0] == 137); + assert(b[1122] == 128); + assert(b[1123] == 201); + assert(b[202125] == 218); + data_ok = 1; +} + +void onErrorData(void *arg) { + get_count++; + assert((int)arg == 246); + data_bad = 1; +} void wait_wgets() { if (get_count == 3) { + emscripten_async_wget_data( + "http://localhost:8888/screenshot.png", + (void*)135, + onLoadedData, + onErrorData); + emscripten_async_wget_data( + "http://localhost:8888/fail_me", + (void*)246, + onLoadedData, + onErrorData); + } else if (get_count == 5) { assert(IMG_Load("/tmp/screen_shot.png")); + assert(data_ok == 1 && data_bad == 1); emscripten_cancel_main_loop(); REPORT_RESULT(); } |