Permalink
Browse files

emscripten_async_wget_data

  • Loading branch information...
1 parent c3ed294 commit e97f48a2c9376354a6b8927191322322f13808ba @kripken committed Dec 31, 2012
Showing with 71 additions and 4 deletions.
  1. +15 −4 src/library_browser.js
  2. +24 −0 system/include/emscripten/emscripten.h
  3. +32 −0 tests/emscripten_fs_api_browser.cpp
View
@@ -336,19 +336,19 @@ 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();
} else {
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);
@@ -186,6 +186,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
@@ -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();
}

0 comments on commit e97f48a

Please sign in to comment.