aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xemcc11
-rw-r--r--tests/mem_init.cpp24
-rw-r--r--tests/test_browser.py17
3 files changed, 51 insertions, 1 deletions
diff --git a/emcc b/emcc
index f7d1a939..3cc294ce 100755
--- a/emcc
+++ b/emcc
@@ -470,7 +470,16 @@ Options that are modified or new in %s include:
1: Emit a separate memory initialization file
in binary format. This is more efficient than
storing it as text inside JavaScript, but does
- mean you have another file to publish.
+ mean you have another file to publish. The
+ binary file will also be loaded asynchronously,
+ which means main() will not be called until
+ the file is downloaded and applied; you cannot
+ call any C functions until it arrives. (Call
+ yourself from main() to know when all async
+ stuff has happened and it is safe to call
+ library functions, as main() will only be
+ called at that time. You can also call
+ addOnPreMain from a preRun.)
-Wno-warn-absolute-paths If not specified, the compiler will warn about any
uses of absolute paths in -I and -L command line
diff --git a/tests/mem_init.cpp b/tests/mem_init.cpp
new file mode 100644
index 00000000..e642bfc9
--- /dev/null
+++ b/tests/mem_init.cpp
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <emscripten.h>
+
+extern "C" {
+
+int noted = 0;
+
+void EMSCRIPTEN_KEEPALIVE note(int n) {
+ EM_ASM_({ Module.print([$0, $1]) }, n, noted);
+ noted = noted | n;
+ EM_ASM_({ Module.print(['noted is now', $0]) }, noted);
+ if (noted == 3) {
+ int result = noted;
+ REPORT_RESULT();
+ }
+}
+
+}
+
+int main() {
+ EM_ASM( myJSCallback() ); // calls a global JS func
+ return 0;
+}
+
diff --git a/tests/test_browser.py b/tests/test_browser.py
index dfb1ef69..7ffe56c8 100644
--- a/tests/test_browser.py
+++ b/tests/test_browser.py
@@ -1752,6 +1752,23 @@ void *getBindBuffer() {
for mem in [0, 1]:
self.btest('pre_run_deps.cpp', expected='10', args=['--pre-js', 'pre.js', '--memory-init-file', str(mem)])
+ def test_mem_init(self):
+ open(os.path.join(self.get_dir(), 'pre.js'), 'w').write('''
+ function myJSCallback() { // called from main()
+ Module._note(1);
+ }
+ Module.preRun = function() {
+ addOnPreMain(function() {
+ Module._note(2);
+ });
+ };
+ ''')
+ open(os.path.join(self.get_dir(), 'post.js'), 'w').write('''
+ Module._note(4); // this happens too early! and is overwritten when the mem init arrives
+ ''')
+
+ self.btest('mem_init.cpp', expected='3', args=['--pre-js', 'pre.js', '--post-js', 'post.js', '--memory-init-file', '1'])
+
def test_worker_api(self):
Popen([PYTHON, EMCC, path_from_root('tests', 'worker_api_worker.cpp'), '-o', 'worker.js', '-s', 'BUILD_AS_WORKER=1', '-s', 'EXPORTED_FUNCTIONS=["_one"]']).communicate()
self.btest('worker_api_main.cpp', expected='566')