Scripts: an if-branch function stands in a block of its own

A function declaration standing alone as an if branch outside strict
mode code is in a block of its own (Annex B.3.4), so a function of the
same name in the block around the if keeps it from assigning the outer
variable: var f; { function f() { return 1; } if (true) function f()
{ return 2; } } f() gives 1.

Global and eval code stays in the collector's roots while its
declarations are made, so a collection in that time keeps the code's
heap charge.
This commit is contained in:
dotCooCoo committed 2026-11-22 07:05:23 -08:00
1 parent 029482d0d7
commit b0a7959bf9
3 files changed
+18 -4

No files matched your search

+7 -2
View File
@@ -290,8 +290,13 @@ impl<'a> FnCompiler<'a> {
work.extend(list.iter().rev().map(|&st| (st, nested || inner, s)));
}
Stmt::If { consequent, alternate, .. } => {
work.extend(alternate.iter().map(|&s| (s, nested || inner, scope)));
work.push((*consequent, nested || inner, scope));
// A function declaration standing alone as a branch is in a block of its own
// (Annex B.3.4).
for &branch in alternate.iter().chain([consequent]) {
let s =
if matches!(self.ast.stmt(branch), Some(Stmt::FunctionDecl(_))) { enter(HashSet::new(), HashSet::new()) } else { scope };
work.push((branch, nested || inner, s));
}
}
Stmt::Labeled { body, .. } => work.push((*body, nested, scope)),
Stmt::DoWhile { body, .. } | Stmt::While { body, .. } | Stmt::With { body, .. } => work.push((*body, nested || inner, scope)),
+3
View File
@@ -130,6 +130,9 @@ fn functions_declared_in_blocks_are_scoped_to_the_block() {
// Only a `let` or `const` around the declaration, a function declared in a block around it, or
// a parameter keeps the name from being a variable.
truth("var f; { function f() { return 1; } { function f() { return 2; } } } f() === 1");
// A function standing alone as an `if` branch is in a block of its own.
truth("var f; { function f() { return 1; } if (true) function f() { return 2; } } f() === 1");
truth("var f; if (true) function f() { return 2; } else function f() { return 3; } f() === 2");
// A labeled function in a block is made when the block is entered.
truth("{ var result = typeof lf; L: function lf() {} } result === 'function'");
truth("function f() { { let g; } { function g() {} } return typeof g; } f() === 'function'");
+8 -2
View File
@@ -237,6 +237,8 @@ pub(crate) struct Interp {
pub(crate) temp_roots: Vec<Value>,
/// Environments native code is still building.
temp_envs: Vec<EnvId>,
/// Global and eval code whose declarations are being made, before its frame holds it.
temp_codes: Vec<Rc<Code>>,
pub(crate) global: ObjId,
pub(crate) global_env: EnvId,
/// The declarative environment inside `global_env` that holds the `let` and `const` names of
@@ -525,6 +527,7 @@ impl Interp {
frames: Vec::new(),
temp_roots: Vec::new(),
temp_envs: Vec::new(),
temp_codes: Vec::new(),
global,
global_env,
global_lex,
@@ -809,6 +812,7 @@ impl Interp {
roots.envs.push(self.global_env);
roots.envs.push(self.global_lex);
roots.envs.extend(self.temp_envs.iter().copied());
roots.codes.extend(self.temp_codes.iter().map(|c| &**c));
// A template object stays while its code can still run: while something other than the
// entries for its code's sites holds the code.
let mut sites: std::collections::HashMap<*const Code, usize> = std::collections::HashMap::new();
@@ -3285,11 +3289,13 @@ impl Interp {
/// environment of its `let` and `const` names. A script's `let` and `const` names go in the
/// global scope ([`Self::declare_global_lexical`]).
fn instantiate_global(&mut self, code: &Rc<Code>, env: EnvId, var_env: EnvId, is_eval: bool) -> JsResult<EnvId> {
// A strict eval's own environment is in no frame yet, so the environments stay reachable
// until the code's frame holds them.
// A strict eval's own environment and the code are in no frame yet, so they stay
// reachable, and the code's charge counted, until the code's frame holds them.
let mark = self.temp_envs.len();
self.temp_envs.extend([env, var_env]);
self.temp_codes.push(code.clone());
let r = self.instantiate_global_in(code, env, var_env, is_eval);
self.temp_codes.pop();
self.temp_envs.truncate(mark);
r
}