From 8561f1b4b340934806d07fdd7db039d126508667 Mon Sep 17 00:00:00 2001 From: "Alon Zakai (kripken)" Date: Thu, 29 Oct 2020 20:03:21 -0700 Subject: [PATCH 1/2] fix --- ChangeLog.md | 4 ++++ emcc.py | 4 +++- src/settings.js | 4 ++++ tests/test_other.py | 25 +++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 2445c88135f99..da5bd0e3055af 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -35,6 +35,10 @@ Current Trunk modules. As one side effect of this change it is now required that JavaScript functions that are imported by address are now required to have a `__sig` specified in the library JavaScript file. +- `MODULARIZE` + `WASM_ASYNC_COMPILATION=0`, that is, modularize mode but with + async compilation turned off, so that startup is synchronous, now returns the + Module object from the factory function (as it would not make sense to return + a Promise without async startup). See #12647 2.0.8: 10/24/2020 ----------------- diff --git a/emcc.py b/emcc.py index 120f3f563822d..64440cf6e81cb 100755 --- a/emcc.py +++ b/emcc.py @@ -2841,7 +2841,9 @@ def modularize(): logger.debug('Modularizing, assigning to var ' + shared.Settings.EXPORT_NAME) src = open(final_js).read() - return_value = shared.Settings.EXPORT_NAME + '.ready' + return_value = shared.Settings.EXPORT_NAME + if shared.Settings.WASM_ASYNC_COMPILATION: + return_value += '.ready' if not shared.Settings.EXPORT_READY_PROMISE: return_value = '{}' diff --git a/src/settings.js b/src/settings.js index e7f8e0a271068..6899ea3dc140d 100644 --- a/src/settings.js +++ b/src/settings.js @@ -954,6 +954,10 @@ var DETERMINISTIC = 0; // resolved with the module instance when it is safe to run the compiled code, // similar to the `onRuntimeInitialized` callback. You do not need to use the // `onRuntimeInitialized` callback when using `MODULARIZE`. +// +// (If WASM_ASYNC_COMPILATION is off, that is, if compilation is +// *synchronous*, then it would not make sense to return a Promise, and instead +// the Module object itself is returned, which is ready to be used.) // // The default name of the function is `Module`, but can be changed using the // `EXPORT_NAME` option. We recommend renaming it to a more typical name for a diff --git a/tests/test_other.py b/tests/test_other.py index 4396c1b716768..c3a3980cd8753 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -4838,6 +4838,31 @@ def test_EXPORT_NAME_with_html(self): self.assertNotEqual(result.returncode, 0) self.assertContained('Customizing EXPORT_NAME requires that the HTML be customized to use that name', result.stdout) + def test_modularize_sync_compilation(self): + create_test_file('post.js', r''' +console.log('before'); +var result = Module(); +// It should be an object. +console.log(typeof result); +// And it should have the exports that Module has, showing it is Module in fact. +console.log(typeof result._main); +// And it should not be a Promise. +console.log(typeof result.then); +console.log('after'); +''') + self.run_process([EMCC, path_from_root('tests', 'hello_world.c'), + '-s', 'MODULARIZE=1', + '-s', 'WASM_ASYNC_COMPILATION=0', + '--extern-post-js', 'post.js']) + self.assertContained('''\ +before +hello, world! +object +function +undefined +after +''', self.run_js('a.out.js')) + def test_export_all_3142(self): create_test_file('src.cpp', r''' typedef unsigned int Bit32u; From 9e4cf4fd79cd0d28f358868fbe1241972c72a0db Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 30 Oct 2020 10:18:22 -0700 Subject: [PATCH 2/2] Fix existing test --- tests/test_browser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_browser.py b/tests/test_browser.py index 2f58aa70d459e..14d74f50aa9c7 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -3274,7 +3274,7 @@ def test_modularize(self): src = open(path_from_root('tests', 'browser_test_hello_world.c')).read() create_test_file('test.c', self.with_report_result(src)) # this test is synchronous, so avoid async startup due to wasm features - self.compile_btest(['test.c', '-s', 'MODULARIZE=1', '-s', 'WASM_ASYNC_COMPILATION=0', '-s', 'SINGLE_FILE=1'] + args + opts) + self.compile_btest(['test.c', '-s', 'MODULARIZE=1', '-s', 'SINGLE_FILE=1'] + args + opts) create_test_file('a.html', '''