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

Avoid excessive variable reuse in modularize function. NFC #23208

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
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
24 changes: 12 additions & 12 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,35 +2390,35 @@ def modularize():
diagnostics.warning('emcc', 'EXPORT_NAME should not be named "config" when targeting Safari')

if settings.MODULARIZE == 'instance':
src = '''
wrapper_function = '''
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_function = '''
%(maybe_async)sfunction(moduleArg = {}) {
var moduleRtn;
%(src)s
%(generated_js)s
return moduleRtn;
}
''' % {
'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 = f'/** @nocollapse */ var {settings.EXPORT_NAME} = {wrapper_function};'
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_function)s
''' % {
'script_url': script_url,
'script_url_node': script_url_node,
'src': src,
'wrapper_function': wrapper_function,
}
else:
src = '''\
var %(EXPORT_NAME)s = (() => {
var _scriptName = %(script_url)s;
%(script_url_node)s
return (%(src)s);
return (%(wrapper_function)s);
})();
''' % {
'EXPORT_NAME': settings.EXPORT_NAME,
'script_url': script_url,
'script_url_node': script_url_node,
'src': src,
'wrapper_function': wrapper_function,
}

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