Skip to content

Commit

Permalink
Refactor modularization code to avoid nested functions. NFC (emscript…
Browse files Browse the repository at this point in the history
…en-core#20962)

This also avoids the needs for an IIFE here, which makes the output code
much readable.

This also saves on code size.
  • Loading branch information
sbc100 authored Dec 20, 2023
1 parent 4f9f1e1 commit 8027de3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 44 deletions.
4 changes: 2 additions & 2 deletions test/code_size/hello_webgl2_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 569,
"a.html.gz": 379,
"a.js": 4697,
"a.js": 4691,
"a.js.gz": 2419,
"a.wasm": 10482,
"a.wasm.gz": 6728,
"total": 15748,
"total": 15742,
"total_gz": 9526
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 18022,
"a.js.gz": 8135,
"a.js": 18016,
"a.js.gz": 8139,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21712,
"total_gz": 11207
"total": 21706,
"total_gz": 11211
}
4 changes: 2 additions & 2 deletions test/code_size/hello_webgl_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 569,
"a.html.gz": 379,
"a.js": 4183,
"a.js": 4177,
"a.js.gz": 2241,
"a.wasm": 10482,
"a.wasm.gz": 6728,
"total": 15234,
"total": 15228,
"total_gz": 9348
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17499,
"a.js.gz": 7958,
"a.js": 17493,
"a.js.gz": 7960,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21189,
"total_gz": 11030
"total": 21183,
"total_gz": 11032
}
51 changes: 19 additions & 32 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2325,23 +2325,15 @@ def modularize():
if async_emit != '' and settings.EXPORT_NAME == 'config':
diagnostics.warning('emcc', 'EXPORT_NAME should not be named "config" when targeting Safari')

src = '''
%(maybe_async)sfunction(moduleArg = {}) {
%(src)s
return %(return_value)s
}
''' % {
'maybe_async': async_emit,
'src': src,
'return_value': return_value,
}

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 = f'''\
var {settings.EXPORT_NAME} = {async_emit}(moduleArg = {{}}) => {{
{src}
return {return_value};
}};
'''
else:
script_url_node = ''
# When MODULARIZE this JS may be executed later,
Expand All @@ -2355,19 +2347,14 @@ def modularize():
script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined"
if shared.target_environment_may_be('node'):
script_url_node = "if (typeof __filename !== 'undefined') _scriptDir ||= __filename;"
src = '''%(node_imports)s
var %(EXPORT_NAME)s = (() => {
var _scriptDir = %(script_url)s;
%(script_url_node)s
return (%(src)s);
})();
''' % {
'node_imports': node_es6_imports(),
'EXPORT_NAME': settings.EXPORT_NAME,
'script_url': script_url,
'script_url_node': script_url_node,
'src': src,
}
src = f'''{node_es6_imports()}
var {settings.EXPORT_NAME} = {async_emit}(moduleArg = {{}}) => {{
var _scriptDir = {script_url};
{script_url_node}
{src}
return {return_value};
}};
'''
# Given the async nature of how the Module function and Module object
# come into existence in AudioWorkletGlobalScope, store the Module
# function under a different variable name so that AudioWorkletGlobalScope
Expand All @@ -2378,14 +2365,14 @@ def modularize():

# Export using a UMD style export, or ES6 exports if selected
if settings.EXPORT_ES6:
src += 'export default %s;' % settings.EXPORT_NAME
src += f'export default {settings.EXPORT_NAME};'
elif not settings.MINIMAL_RUNTIME:
src += '''\
src += f'''
if (typeof exports === 'object' && typeof module === 'object')
module.exports = %(EXPORT_NAME)s;
module.exports = {settings.EXPORT_NAME};
else if (typeof define === 'function' && define['amd'])
define([], () => %(EXPORT_NAME)s);
''' % {'EXPORT_NAME': settings.EXPORT_NAME}
define([], () => {settings.EXPORT_NAME});
'''

final_js += '.modular.js'
write_file(final_js, src)
Expand Down

0 comments on commit 8027de3

Please sign in to comment.