aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-09-13 15:13:30 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-09-13 15:13:30 -0700
commit1fc6762e3176843ffc378c8b4f43dd73fed25ebe (patch)
tree67b1b4dfb930b3e6ee234e98c778e217aceb27f6
parentee51b2bd829447fb83c5ebdbc3f8d157355c0fdc (diff)
emscripten_async_load_script
-rw-r--r--src/library_browser.js19
-rw-r--r--system/include/emscripten/emscripten.h18
-rw-r--r--tests/emscripten_api_browser2.cpp53
-rw-r--r--tests/test_browser.py11
4 files changed, 101 insertions, 0 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index e4966e15..cba8ecdf 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -688,6 +688,25 @@ mergeInto(LibraryManager.library, {
}, millis);
},
+ emscripten_async_load_script: function(url, onload, onerror) {
+ Module['noExitRuntime'] = true;
+
+ onload = Runtime.getFuncWrapper(onload, 'v');
+
+ assert(runDependencies === 0, 'async_load_script must be run when no other dependencies are active');
+ var script = document.createElement('script');
+ script.onload = function() {
+ if (runDependencies > 0) {
+ dependenciesFulfilled = onload;
+ } else {
+ onload();
+ }
+ };
+ script.onerror = onerror;
+ script.src = Pointer_stringify(url);
+ document.body.appendChild(script);
+ },
+
emscripten_set_main_loop: function(func, fps, simulateInfiniteLoop) {
Module['noExitRuntime'] = true;
diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h
index 1b9a8f25..430fbc1c 100644
--- a/system/include/emscripten/emscripten.h
+++ b/system/include/emscripten/emscripten.h
@@ -45,9 +45,27 @@ extern "C" {
extern void emscripten_run_script(const char *script);
extern int emscripten_run_script_int(const char *script);
extern char *emscripten_run_script_string(const char *script); // uses a single buffer - shared between calls!
+
+/*
+ * Asynchronously run a script, after a specified amount of
+ * time.
+ */
extern void emscripten_async_run_script(const char *script, int millis);
/*
+ * Asynchronously loads a script from a URL.
+ *
+ * This integrates with the run dependencies system, so your
+ * script can call addRunDependency multiple times, prepare
+ * various asynchronous tasks, and call removeRunDependency
+ * on them; when all are complete (or there were no run
+ * dependencies to begin with), onload is called. An example use
+ * for this is to load an asset module, that is, the output of the
+ * file packager.
+ */
+extern void emscripten_async_load_script(const char *script, void (*onload)(), void (*onerror)());
+
+/*
* Set a C function as the main event loop. The JS environment
* will call that function at a specified number of frames per
* second. Setting 0 or a negative value as the fps will use
diff --git a/tests/emscripten_api_browser2.cpp b/tests/emscripten_api_browser2.cpp
new file mode 100644
index 00000000..569ed710
--- /dev/null
+++ b/tests/emscripten_api_browser2.cpp
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include<emscripten.h>
+
+int value = 0;
+
+extern "C" {
+ void set(int x) {
+ printf("set! %d\n", x);
+ value = x;
+ }
+}
+
+void load2() {
+ printf("load2\n");
+
+ char buffer[10];
+ memset(buffer, 0, 10);
+ FILE *f = fopen("file1.txt", "r");
+ fread(buffer, 1, 5, f);
+ fclose(f);
+ assert(strcmp(buffer, "first") == 0);
+
+ memset(buffer, 0, 10);
+ f = fopen("file2.txt", "r");
+ fread(buffer, 1, 6, f);
+ fclose(f);
+ assert(strcmp(buffer, "second") == 0);
+
+ int result = 1;
+ REPORT_RESULT();
+}
+void error2() {
+ printf("fail2\n");
+}
+
+void load1() {
+ printf("load1\n");
+ assert(value == 456);
+ emscripten_async_load_script("script2.js", load2, error2);
+}
+void error1() {
+ printf("fail1\n");
+}
+
+int main() {
+ emscripten_async_load_script("script1.js", load1, error1);
+
+ return 1;
+}
+
diff --git a/tests/test_browser.py b/tests/test_browser.py
index e6fd6544..6a23b41c 100644
--- a/tests/test_browser.py
+++ b/tests/test_browser.py
@@ -1230,6 +1230,17 @@ keydown(100);keyup(100); // trigger the end
def test_emscripten_api(self):
self.btest('emscripten_api_browser.cpp', '1', args=['-s', '''EXPORTED_FUNCTIONS=['_main', '_third']'''])
+ def test_emscripten_api2(self):
+ open('script1.js', 'w').write('''
+ Module._set(456);
+ ''')
+
+ open('file1.txt', 'w').write('first');
+ open('file2.txt', 'w').write('second');
+ Popen([PYTHON, FILE_PACKAGER, 'test.data', '--preload', 'file1.txt', 'file2.txt'], stdout=open('script2.js', 'w')).communicate()
+
+ self.btest('emscripten_api_browser2.cpp', '1', args=['-s', '''EXPORTED_FUNCTIONS=['_main', '_set']'''])
+
def test_emscripten_api_infloop(self):
self.btest('emscripten_api_browser_infloop.cpp', '7')