summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library_browser.js46
-rw-r--r--src/library_gl.js4
-rw-r--r--system/include/emscripten/emscripten.h12
-rw-r--r--tests/emscripten_fs_api_browser.cpp69
-rwxr-xr-xtests/runner.py3
5 files changed, 125 insertions, 9 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index 9283913f..6015168f 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -86,6 +86,7 @@ mergeInto(LibraryManager.library, {
requestFullScreen: function() {
var canvas = Module.canvas;
function fullScreenChange() {
+ if (Module['onFullScreen']) Module['onFullScreen']();
if (document['webkitFullScreenElement'] === canvas ||
document['mozFullScreenElement'] === canvas ||
document['fullScreenElement'] === canvas) {
@@ -142,24 +143,55 @@ 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;
+ 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();
- };
- xhr.onerror = function(event) {
- assert(arrayBuffer, 'Loading data file "' + url + '" failed.');
- };
- xhr.send(null);
+ }, function(event) {
+ throw '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/src/library_gl.js b/src/library_gl.js
index 37dcf0a5..89603572 100644
--- a/src/library_gl.js
+++ b/src/library_gl.js
@@ -294,7 +294,7 @@ var LibraryGL = {
} else {
data = null;
}
- Module.ctx.compressedTexImage2D(target, level, internalformat, width, height, border, data);
+ Module.ctx['compressedTexImage2D'](target, level, internalformat, width, height, border, data);
},
glCompressedTexSubImage2D: function(target, level, xoffset, yoffset, width, height, format, imageSize, data) {
@@ -303,7 +303,7 @@ var LibraryGL = {
} else {
data = null;
}
- Module.ctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, data);
+ Module.ctx['compressedTexSubImage2D'](target, level, xoffset, yoffset, width, height, data);
},
glTexImage2D: function(target, level, internalformat, width, height, border, format, type, pixels) {
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 c195f370..c0f4e564 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -7775,6 +7775,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')