Skip to content

Commit

Permalink
Fix MODULARIZE with sync compilation (#12650)
Browse files Browse the repository at this point in the history
MODULARIZE + WASM_ASYNC_COMPILATION=0, that is, modularize mode but with
async compilation turned off, so that startup is synchronous, should return the
Module object from the factory function (as it would not make sense to return
a Promise without async startup).

Fixes #12647
  • Loading branch information
kripken authored Oct 30, 2020
1 parent 17fc2da commit 699996c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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
-----------------
Expand Down
4 changes: 3 additions & 1 deletion emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2835,7 +2835,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 = '{}'

Expand Down
4 changes: 4 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3322,7 +3322,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', '''
<script src="a.out.js"></script>
<script>
Expand Down
25 changes: 25 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -4842,6 +4842,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;
Expand Down

0 comments on commit 699996c

Please sign in to comment.