Skip to content

Direct eval() in JS libraries disables Terser reduce_vars and unused across the entire file #28196

Open

Description

@sbc100

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.

Minimal reproduction

const ENOENT = 44;
const EINVAL = 28;
function foo(x) { return x === ENOENT ? EINVAL : 0; }
function run_script(s) { eval(s); }
globalThis.keep(foo, run_script);

Running Terser with Emscripten's compress settings produces:

const ENOENT=44;const EINVAL=28;function foo(x){return x===ENOENT?EINVAL:0}function run_script(s){eval(s)}globalThis.keep(foo,run_script);

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:

  1. 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)
  2. $addEmAsm and $addEmJs in src/lib/libdylink.js (included in
    -sMAIN_MODULE / dynamic linking builds)
  3. emscripten_async_wget_script in src/lib/libbrowser.js (when
    ENVIRONMENT_MAY_BE_NODE is active)
  4. -sWASM=2 fallback loader in src/shell.js and src/shell_minimal.js

Possible solutions

  1. 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.
  2. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions