Skip to content

Commit

Permalink
emscripten_async_wget_data
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Dec 31, 2012
1 parent c3ed294 commit e97f48a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/library_browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions system/include/emscripten/emscripten.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions tests/emscripten_fs_api_browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit e97f48a

Please sign in to comment.