diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-06-21 19:46:25 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-06-21 19:46:25 -0700 |
commit | 2abffbd059e9936dbf5a12d03ea69fa47ddf10a6 (patch) | |
tree | 0efa1c4abedf93258a86e20a872e9be9e00e33eb /src/library_browser.js | |
parent | 7f402318369db24e17f5eb7ddd895f7685912703 (diff) | |
parent | 2c86cbf25bead7607256ef9bb8f36bfe07047cee (diff) |
Merge pull request #483 from caiiiycuk/emscripten_fs_api
Emscripten filesystem api
Diffstat (limited to 'src/library_browser.js')
-rw-r--r-- | src/library_browser.js | 51 |
1 files changed, 43 insertions, 8 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index ec293a13..db012e1d 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -143,24 +143,59 @@ mergeInto(LibraryManager.library, { 0; }, - asyncLoad: function(url, callback) { + xhrLoad: function(url, onload, onerror) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'arraybuffer'; xhr.onload = function() { - var arrayBuffer = xhr.response; - assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).'); - callback(new Uint8Array(arrayBuffer)); - removeRunDependency(); - }; - xhr.onerror = function(event) { - assert(arrayBuffer, 'Loading data file "' + url + '" failed.'); + if (xhr.status == 200) { + onload(xhr.response); + } else { + onerror(); + } }; + xhr.onerror = onerror; xhr.send(null); + }, + + asyncLoad: function(url, callback) { + Browser.xhrLoad(url, + function(arrayBuffer) { + assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).'); + callback(new Uint8Array(arrayBuffer)); + removeRunDependency(); + }, + function(event) { + assert(arrayBuffer, 'Loading data file "' + url + '" failed.'); + }); addRunDependency(); } }, + emscripten_async_wget: function(url, file, onload, onerror) { + url = Pointer_stringify(url); + + Browser.xhrLoad(url, + function (response) { + var absolute = Pointer_stringify(file); + var index = absolute.lastIndexOf('/'); + FS.createDataFile( + absolute.substr(0, index), + absolute.substr(index +1), + new Uint8Array(response), + true, true); + + if (onload) { + FUNCTION_TABLE[onload](file); + } + }, + function (event) { + if (onerror) { + FUNCTION_TABLE[onerror](file); + } + }); + }, + emscripten_async_run_script__deps: ['emscripten_run_script'], emscripten_async_run_script: function(script, millis) { Module['noExitRuntime'] = true; |