Skip to content

Fix OOM running acorn optimizer on binaryen_js #24559

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

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/modularize.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var {{{ EXPORT_NAME }}} = (() => {
return {{{ asyncIf(WASM_ASYNC_COMPILATION || (EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE)) }}}function(moduleArg = {}) {
var moduleRtn;

<<< INNER_JS_CODE >>>
"<<< INNER_JS_CODE >>>"

return moduleRtn;
};
Expand All @@ -33,7 +33,7 @@ var {{{ EXPORT_NAME }}} = (() => {
{{{ asyncIf(WASM_ASYNC_COMPILATION || (EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE)) }}}function {{{ EXPORT_NAME }}}(moduleArg = {}) {
var moduleRtn;

<<< INNER_JS_CODE >>>
"<<< INNER_JS_CODE >>>"

return moduleRtn;
}
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1234
1249
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2551
2566
20 changes: 15 additions & 5 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2442,15 +2442,25 @@ def phase_binaryen(target, options, wasm_target):
def modularize():
global final_js
logger.debug(f'Modularizing, creating factory function called `{settings.EXPORT_NAME}`')
src = building.read_and_preprocess(utils.path_from_root('src/modularize.js'), expand_macros=True)
src = do_replace(src, '<<< INNER_JS_CODE >>>', read_file(final_js))
modularize_src = building.read_and_preprocess(utils.path_from_root('src/modularize.js'), expand_macros=True)
if settings.MINIFY_WHITESPACE:
with shared.get_temp_files().get_file(suffix='.js') as tmp:
write_file(tmp, modularize_src)
minified_file = building.acorn_optimizer(tmp, ['--minify-whitespace'])
modularize_src = read_file(minified_file)

# Replace INNER_JS_CODE in the minified code
full_src = do_replace(modularize_src, '"<<< INNER_JS_CODE >>>"', read_file(final_js))
final_js += '.modular.js'
write_file(final_js, src)
write_file(final_js, full_src)
shared.get_temp_files().note(final_js)
save_intermediate('modularized')

if settings.MINIFY_WHITESPACE:
final_js = building.acorn_optimizer(final_js, ['--minify-whitespace'])
# FIXME(https://github.com/emscripten-core/emscripten/issues/24558): Running acorn at this
# late phase seems to cause OOM (some kind of inifite loop perhaps) in node.
# Instead we minify src/modularize.js in isolation above.
#if settings.MINIFY_WHITESPACE:
# final_js = building.acorn_optimizer(final_js, ['--minify-whitespace'])


def module_export_name_substitution():
Expand Down