diff --git a/crates/js/src/compiler.rs b/crates/js/src/compiler.rs index 74b7ca7e1b..23f0f0e2fd 100644 --- a/crates/js/src/compiler.rs +++ b/crates/js/src/compiler.rs @@ -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)), diff --git a/crates/js/src/es2015_tests.rs b/crates/js/src/es2015_tests.rs index d63c3983c5..17c89d86d8 100644 --- a/crates/js/src/es2015_tests.rs +++ b/crates/js/src/es2015_tests.rs @@ -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'"); diff --git a/crates/js/src/interp.rs b/crates/js/src/interp.rs index dd2570b11f..24a1acaf90 100644 --- a/crates/js/src/interp.rs +++ b/crates/js/src/interp.rs @@ -237,6 +237,8 @@ pub(crate) struct Interp { pub(crate) temp_roots: Vec, /// Environments native code is still building. temp_envs: Vec, + /// Global and eval code whose declarations are being made, before its frame holds it. + temp_codes: Vec>, 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, env: EnvId, var_env: EnvId, is_eval: bool) -> JsResult { - // 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 }