Skip to content

Commit 8027de3

Browse files
authored
Refactor modularization code to avoid nested functions. NFC (#20962)
This also avoids the needs for an IIFE here, which makes the output code much readable. This also saves on code size.
1 parent 4f9f1e1 commit 8027de3

File tree

5 files changed

+31
-44
lines changed

5 files changed

+31
-44
lines changed

test/code_size/hello_webgl2_wasm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 569,
33
"a.html.gz": 379,
4-
"a.js": 4697,
4+
"a.js": 4691,
55
"a.js.gz": 2419,
66
"a.wasm": 10482,
77
"a.wasm.gz": 6728,
8-
"total": 15748,
8+
"total": 15742,
99
"total_gz": 9526
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 567,
33
"a.html.gz": 379,
4-
"a.js": 18022,
5-
"a.js.gz": 8135,
4+
"a.js": 18016,
5+
"a.js.gz": 8139,
66
"a.mem": 3123,
77
"a.mem.gz": 2693,
8-
"total": 21712,
9-
"total_gz": 11207
8+
"total": 21706,
9+
"total_gz": 11211
1010
}

test/code_size/hello_webgl_wasm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 569,
33
"a.html.gz": 379,
4-
"a.js": 4183,
4+
"a.js": 4177,
55
"a.js.gz": 2241,
66
"a.wasm": 10482,
77
"a.wasm.gz": 6728,
8-
"total": 15234,
8+
"total": 15228,
99
"total_gz": 9348
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 567,
33
"a.html.gz": 379,
4-
"a.js": 17499,
5-
"a.js.gz": 7958,
4+
"a.js": 17493,
5+
"a.js.gz": 7960,
66
"a.mem": 3123,
77
"a.mem.gz": 2693,
8-
"total": 21189,
9-
"total_gz": 11030
8+
"total": 21183,
9+
"total_gz": 11032
1010
}

tools/link.py

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,23 +2325,15 @@ def modularize():
23252325
if async_emit != '' and settings.EXPORT_NAME == 'config':
23262326
diagnostics.warning('emcc', 'EXPORT_NAME should not be named "config" when targeting Safari')
23272327

2328-
src = '''
2329-
%(maybe_async)sfunction(moduleArg = {}) {
2330-
2331-
%(src)s
2332-
2333-
return %(return_value)s
2334-
}
2335-
''' % {
2336-
'maybe_async': async_emit,
2337-
'src': src,
2338-
'return_value': return_value,
2339-
}
2340-
23412328
if settings.MINIMAL_RUNTIME and not settings.PTHREADS:
23422329
# Single threaded MINIMAL_RUNTIME programs do not need access to
23432330
# document.currentScript, so a simple export declaration is enough.
2344-
src = '/** @nocollapse */ var %s = %s' % (settings.EXPORT_NAME, src)
2331+
src = f'''\
2332+
var {settings.EXPORT_NAME} = {async_emit}(moduleArg = {{}}) => {{
2333+
{src}
2334+
return {return_value};
2335+
}};
2336+
'''
23452337
else:
23462338
script_url_node = ''
23472339
# When MODULARIZE this JS may be executed later,
@@ -2355,19 +2347,14 @@ def modularize():
23552347
script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined"
23562348
if shared.target_environment_may_be('node'):
23572349
script_url_node = "if (typeof __filename !== 'undefined') _scriptDir ||= __filename;"
2358-
src = '''%(node_imports)s
2359-
var %(EXPORT_NAME)s = (() => {
2360-
var _scriptDir = %(script_url)s;
2361-
%(script_url_node)s
2362-
return (%(src)s);
2363-
})();
2364-
''' % {
2365-
'node_imports': node_es6_imports(),
2366-
'EXPORT_NAME': settings.EXPORT_NAME,
2367-
'script_url': script_url,
2368-
'script_url_node': script_url_node,
2369-
'src': src,
2370-
}
2350+
src = f'''{node_es6_imports()}
2351+
var {settings.EXPORT_NAME} = {async_emit}(moduleArg = {{}}) => {{
2352+
var _scriptDir = {script_url};
2353+
{script_url_node}
2354+
{src}
2355+
return {return_value};
2356+
}};
2357+
'''
23712358
# Given the async nature of how the Module function and Module object
23722359
# come into existence in AudioWorkletGlobalScope, store the Module
23732360
# function under a different variable name so that AudioWorkletGlobalScope
@@ -2378,14 +2365,14 @@ def modularize():
23782365

23792366
# Export using a UMD style export, or ES6 exports if selected
23802367
if settings.EXPORT_ES6:
2381-
src += 'export default %s;' % settings.EXPORT_NAME
2368+
src += f'export default {settings.EXPORT_NAME};'
23822369
elif not settings.MINIMAL_RUNTIME:
2383-
src += '''\
2370+
src += f'''
23842371
if (typeof exports === 'object' && typeof module === 'object')
2385-
module.exports = %(EXPORT_NAME)s;
2372+
module.exports = {settings.EXPORT_NAME};
23862373
else if (typeof define === 'function' && define['amd'])
2387-
define([], () => %(EXPORT_NAME)s);
2388-
''' % {'EXPORT_NAME': settings.EXPORT_NAME}
2374+
define([], () => {settings.EXPORT_NAME});
2375+
'''
23892376

23902377
final_js += '.modular.js'
23912378
write_file(final_js, src)

0 commit comments

Comments
 (0)