You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Direct eval() in JS libraries disables Terser reduce_vars and unused across the entire file #28196
In #27949 and #27987, we enabled reduce_vars: true, unused: true, and toplevel: true in the Terser compress pass in tools/acorn-optimizer.mjs.
This is what inlines top-level const declarations (including all C defines
from cDefs) into their use sites and removes the const declarations in
optimized non-Closure builds.
However, whenever any function included in the build contains a direct eval()
call, Terser marks that function's scope and every enclosing ancestor scope
(up to AST_Toplevel, or the MODULARIZE factory function) with uses_eval = true. When uses_eval is set on a scope, Terser completely
disables both reduce_vars and unused for all declarations in that scope.
Because run_script calls eval(s), ENOENT and EINVAL are neither inlined
into foo nor removed from the top-level output.
Affected library code
Any build that pulls in one of the following gets no top-level const inlining
or Terser dead-variable elimination:
emscripten_run_script, emscripten_run_script_int, and emscripten_run_script_string in src/lib/libcore.js (also included in any -sINCLUDE_FULL_LIBRARY build)
$addEmAsm and $addEmJs in src/lib/libdylink.js (included in -sMAIN_MODULE / dynamic linking builds)
emscripten_async_wget_script in src/lib/libbrowser.js (when ENVIRONMENT_MAY_BE_NODE is active)
-sWASM=2 fallback loader in src/shell.js and src/shell_minimal.js
Possible solutions
Switch to indirect (0, eval)(...) or eval?.(...) where appropriate:
Indirect eval executes in global scope and does not cause Terser to mark
enclosing scopes with uses_eval. Caveat: under -sMODULARIZE or -sEXPORT_ES6, indirect eval cannot see module-scoped internal symbols
(such as Module, HEAP8, UTF8ToString), which $addEmAsm/$addEmJs or
some emscripten_run_script callers may rely on.
Inline const primitives in tools/acorn-optimizer.mjs (or mask eval
during minify_sync):
Emscripten's own JSDCE pass already ignores dynamic eval scope effects
(and src/jsifier.mjs explicitly compensates for eval( by conservatively
adding $HEAP* dependencies). Masking direct eval from Terser (or
inlining top-level primitive const declarations in acorn-optimizer.mjs)
would preserve direct eval scoping at runtime while allowing Terser to
optimize the rest of the module.
Activity