Skip to content

Commit

Permalink
Avoid excessive variable reuse in modularize function. NFC
Browse files Browse the repository at this point in the history
I think this makes it a little easier to follow.
  • Loading branch information
sbc100 committed Dec 18, 2024
1 parent 2ac4c4f commit 91a19b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
35 changes: 13 additions & 22 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4795,28 +4795,19 @@ def test_browser_run_from_different_directory(self):

# Similar to `test_browser_run_from_different_directory`, but asynchronous because of `-sMODULARIZE`
def test_browser_run_from_different_directory_async(self):
for args, creations in [
(['-sMODULARIZE'], [
'Module();', # documented way for using modularize
'new Module();' # not documented as working, but we support it
]),
]:
print(args)
# compile the code with the modularize feature and the preload-file option enabled
self.compile_btest('browser_test_hello_world.c', ['-o', 'test.js', '-O3'] + args)
ensure_dir('subdir')
shutil.move('test.js', Path('subdir/test.js'))
shutil.move('test.wasm', Path('subdir/test.wasm'))
for creation in creations:
print(creation)
# Make sure JS is loaded from subdirectory
create_file('test-subdir.html', '''
<script src="subdir/test.js"></script>
<script>
%s
</script>
''' % creation)
self.run_browser('test-subdir.html', '/report_result?0')
# compile the code with the modularize feature and the preload-file option enabled
self.compile_btest('browser_test_hello_world.c', ['-o', 'test.js', '-O3', '-sMODULARIZE'])
ensure_dir('subdir')
shutil.move('test.js', Path('subdir/test.js'))
shutil.move('test.wasm', Path('subdir/test.wasm'))
# Make sure JS is loaded from subdirectory
create_file('test-subdir.html', '''
<script src="subdir/test.js"></script>
<script>
Module();
</script>
''')
self.run_browser('test-subdir.html', '/report_result?0')

# Similar to `test_browser_run_from_different_directory`, but
# also also we eval the initial code, so currentScript is not present. That prevents us
Expand Down
22 changes: 11 additions & 11 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2377,7 +2377,7 @@ def node_pthread_detection():
def modularize():
global final_js
logger.debug(f'Modularizing, assigning to var {settings.EXPORT_NAME}')
src = read_file(final_js)
generated_js = read_file(final_js)

# When targetting node and ES6 we use `await import ..` in the generated code
# so the outer function needs to be marked as async.
Expand All @@ -2390,19 +2390,19 @@ def modularize():
diagnostics.warning('emcc', 'EXPORT_NAME should not be named "config" when targeting Safari')

if settings.MODULARIZE == 'instance':
src = '''
wrapper_funcion = '''
export default async function init(moduleArg = {}) {
var moduleRtn;
%(src)s
%(generated_js)s
return await moduleRtn;
}
''' % {
'src': src,
'generated_js': generated_js,
}
else:
src = '''
wrapper_funcion = '''
%(maybe_async)sfunction(moduleArg = {}) {
var moduleRtn;
Expand All @@ -2412,13 +2412,13 @@ def modularize():
}
''' % {
'maybe_async': async_emit,
'src': src,
'generated_js': generated_js,
}

if settings.MINIMAL_RUNTIME and not settings.PTHREADS:
# Single threaded MINIMAL_RUNTIME programs do not need access to
# document.currentScript, so a simple export declaration is enough.
src = '/** @nocollapse */ var %s = %s' % (settings.EXPORT_NAME, src)
src = '/** @nocollapse */ var %s = %s;' % (settings.EXPORT_NAME, wrapper_funcion)
else:
script_url_node = ''
# When MODULARIZE this JS may be executed later,
Expand All @@ -2436,24 +2436,24 @@ def modularize():
src = '''\
var _scriptName = %(script_url)s;
%(script_url_node)s
%(src)s
%(wrapper_funcion)s
''' % {
'script_url': script_url,
'script_url_node': script_url_node,
'src': src,
'wrapper_funcion': wrapper_funcion,
}
else:
src = '''\
var %(EXPORT_NAME)s = (() => {
var _scriptName = %(script_url)s;
%(script_url_node)s
return (%(src)s);
return (%(wrapper_funcion)s);
})();
''' % {
'EXPORT_NAME': settings.EXPORT_NAME,
'script_url': script_url,
'script_url_node': script_url_node,
'src': src,
'wrapper_funcion': wrapper_funcion,
}

# Given the async nature of how the Module function and Module object
Expand Down

0 comments on commit 91a19b6

Please sign in to comment.