aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Guryanov <caiiiycuk@gmail.com>2012-06-20 22:20:04 +0700
committerAleksander Guryanov <caiiiycuk@gmail.com>2012-06-21 20:38:01 +0700
commit2c86cbf25bead7607256ef9bb8f36bfe07047cee (patch)
tree98f4ea7699d6d7474dd80b4ab4037e8ff5dd3a96
parent6d34633796954f2edb225c66e62ab5a188d59bb0 (diff)
Implementation for emscripten_async_wget
-rw-r--r--src/library_browser.js51
-rw-r--r--system/include/emscripten/emscripten.h12
-rw-r--r--tests/emscripten_fs_api_browser.cpp69
-rwxr-xr-xtests/runner.py3
4 files changed, 127 insertions, 8 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index 9283913f..75dfb9d0 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -142,24 +142,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;
diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h
index 48ccd341..5b71ce6a 100644
--- a/system/include/emscripten/emscripten.h
+++ b/system/include/emscripten/emscripten.h
@@ -103,6 +103,18 @@ float emscripten_random();
//extern void EMSCRIPTEN_COMMENT(const char *text);
/*
+ * Emscripten file system api
+ */
+
+/*
+ * Load file from url in asynchronous way.
+ * When file is loaded then 'onload' callback will called.
+ * If any error occurred 'onerror' will called.
+ * The callbacks are called with the file as their argument.
+ */
+void emscripten_async_wget(const char* url, const char* file, void (*onload)(const char*), void (*onerror)(const char*));
+
+/*
* Profiling tools.
* INIT must be called first, with the maximum identifier that
* will be used. BEGIN will add some code that marks
diff --git a/tests/emscripten_fs_api_browser.cpp b/tests/emscripten_fs_api_browser.cpp
new file mode 100644
index 00000000..07469f34
--- /dev/null
+++ b/tests/emscripten_fs_api_browser.cpp
@@ -0,0 +1,69 @@
+#include<stdio.h>
+#include<emscripten.h>
+#include<assert.h>
+#include <string.h>
+
+extern "C" {
+
+int result = 1;
+int get_count = 0;
+
+void wait_wgets() {
+ if (get_count == 2) {
+ emscripten_cancel_main_loop();
+ REPORT_RESULT();
+ }
+}
+
+void onLoaded(const char* file) {
+ if (strcmp(file, "/tmp/test.html")) {
+ result = 0;
+ }
+
+ printf("loaded: %s\n", file);
+
+ if (FILE * f = fopen(file, "r")) {
+ printf("exists: %s\n", file);
+ int c = fgetc (f);
+ if (c == EOF) {
+ printf("file empty: %s\n", file);
+ result = 0;
+ }
+ fclose(f);
+ } else {
+ result = 0;
+ printf("!exists: %s\n", file);
+ }
+
+ get_count++;
+}
+
+void onError(const char* file) {
+ if (strcmp(file, "/tmp/null")) {
+ result = 0;
+ }
+
+ printf("error: %s\n", file);
+ get_count++;
+}
+
+int main() {
+ emscripten_async_wget(
+ "http://localhost:8888/this_is_not_a_file",
+ "/tmp/null",
+ onLoaded,
+ onError);
+
+ emscripten_async_wget(
+ "http://localhost:8888/test.html",
+ "/tmp/test.html",
+ onLoaded,
+ onError);
+
+ emscripten_set_main_loop(wait_wgets, 0);
+
+ return 0;
+}
+
+}
+
diff --git a/tests/runner.py b/tests/runner.py
index d840465d..f94d9bda 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -7747,6 +7747,9 @@ elif 'browser' in str(sys.argv):
def test_emscripten_api(self):
self.btest('emscripten_api_browser.cpp', '1')
+ def test_emscripten_fs_api(self):
+ self.btest('emscripten_fs_api_browser.cpp', '1')
+
def test_gc(self):
self.btest('browser_gc.cpp', '1')