From 91a19b6d45e8d3efa7bf6ea1ceb64602b9e878f5 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 17 Dec 2024 16:34:41 -0800 Subject: [PATCH] Avoid excessive variable reuse in `modularize` function. NFC I think this makes it a little easier to follow. --- test/test_browser.py | 35 +++++++++++++---------------------- tools/link.py | 22 +++++++++++----------- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/test/test_browser.py b/test/test_browser.py index 04d3353faf1bc..6398a5b917d4d 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -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', ''' - - - ''' % 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', ''' + + + ''') + 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 diff --git a/tools/link.py b/tools/link.py index 2d4726bcc286d..5ea301ed50bbc 100644 --- a/tools/link.py +++ b/tools/link.py @@ -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. @@ -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; @@ -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, @@ -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