Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MODULARIZE with sync compilation #12650

Merged
merged 3 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------
Expand Down
4 changes: 3 additions & 1 deletion emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '{}'

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
25 changes: 25 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down