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 577680a
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 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,30 @@ 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;
%(src)s
%(generated_js)s
return moduleRtn;
}
''' % {
'maybe_async': async_emit,
'src': src,
}
''' % {'maybe_async': async_emit, '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 +2431,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 577680a

Please sign in to comment.