JavaScript: module code drops each expression statement's value

Module code was compiled as eval code, so every expression statement
stored its value as the completion value. A module never reads that value,
and the stored value stayed reachable until the module finished, so a large
temporary result counted against the heap limit for the rest of the module.
Module code now pops the value, as function code does. Script and eval code
keep their completion values, including through finally blocks.
This commit is contained in:
dotCooCoo committed 2026-11-24 22:41:14 -08:00
1 parent 1975975a71
commit 6cf8b4606a
2 files changed
+33 -2

No files matched your search

+12 -2
View File
@@ -90,6 +90,8 @@ struct FnCompiler<'a> {
recursion: u32,
/// The code was read with [`crate::Options::es2015`]: blocks have scopes of their own.
es2015: bool,
/// The code is a module's ([`compile_module`]), which has no completion value.
module: bool,
scopes: Vec<BlockScope>,
templates: Vec<Template>,
uses_this: bool,
@@ -143,6 +145,7 @@ impl<'a> FnCompiler<'a> {
uses_arguments: false,
recursion,
es2015: false,
module: false,
scopes: Vec::new(),
templates: Vec::new(),
uses_this: false,
@@ -153,6 +156,12 @@ impl<'a> FnCompiler<'a> {
}
}
/// Script and eval code keep the value of the last expression statement as their
/// completion value; function and module code do not.
fn keeps_completion(&self) -> bool {
self.kind != CodeKind::Function && !self.module
}
// ---- emitting ----
fn emit(&mut self, op: Op) {
@@ -511,7 +520,7 @@ impl<'a> FnCompiler<'a> {
Stmt::Empty | Stmt::Debugger => Ok(()),
Stmt::Expr(e) => {
self.expr(*e)?;
self.emit(if self.kind == CodeKind::Function { Op::Pop } else { Op::SetCompletion });
self.emit(if self.keeps_completion() { Op::SetCompletion } else { Op::Pop });
Ok(())
}
Stmt::Block(list) => self.block(list),
@@ -1113,7 +1122,7 @@ impl<'a> FnCompiler<'a> {
self.bind(fin_entry);
// In script and eval code the finally block keeps the completion value of the try or
// catch block when it ends normally (ECMA-262 5.1 section 12.14).
let keeps_completion = self.kind != CodeKind::Function;
let keeps_completion = self.keeps_completion();
if keeps_completion {
self.emit(Op::GetCompletion);
}
@@ -2025,6 +2034,7 @@ pub(crate) fn compile_program(program: &crate::Program, source: &Rc<Source>, kin
pub(crate) fn compile_module(program: &crate::Program, source: &Rc<Source>) -> CResult<Rc<Code>> {
let mut c = FnCompiler::new(&program.ast, source, CodeKind::Eval, true, 0);
c.es2015 = true;
c.module = true;
let hoisted = c.hoist(&program.body, &[])?;
let top_scope = c.list_scope(&program.body, false)?;
c.stmts(&program.body)?;
+21
View File
@@ -506,3 +506,24 @@ fn the_heap_counts_each_module_from_when_it_loads() {
assert!(matches!(r, Err(RunError { kind: RunErrorKind::Limit(LimitKind::Heap), .. })), "{r:?}");
assert_eq!(*asked.borrow(), ["./a.mjs", "./b.mjs"]);
}
#[test]
fn a_module_keeps_no_expression_value_alive_after_its_statement() {
// The heap holds one of these arrays and not two, so each must be collectable once the
// expression statement that made it ends.
let big = "function big() { var a = []; for (var i = 0; i < 50000; i++) a[i] = { i: i }; return a; }";
let mut realm = es();
assert!(realm.run(&format!("{big} var held = big();")).is_ok());
let with_one = realm.collect_garbage();
assert!(realm.run("held = null;").is_ok());
let without = realm.collect_garbage();
let one = with_one - without;
realm.set_limits(Limits { heap_bytes: without + one * 3 / 2, ..Limits::default() });
let entry = format!("{big} big(); big(); try {{ big(); }} finally {{ big(); }} out.push('done');");
let (r, out) = run_with(&mut realm, &entry, &[], ModuleLimits::default());
assert_eq!((r, out.as_str()), (Ok(JsValue::Undefined), "done"));
// Script and eval code still complete with the value of their last expression statement.
assert_eq!(realm.run("1; 2;"), Ok(JsValue::Number(2.0)));
assert_eq!(realm.run("eval('3; 4;')"), Ok(JsValue::Number(4.0)));
assert_eq!(realm.run("try { 5; } finally { 6; }"), Ok(JsValue::Number(5.0)));
}