diff --git a/crates/js/src/es2015_tests.rs b/crates/js/src/es2015_tests.rs index 1a076c43c1..ec43bfe327 100644 --- a/crates/js/src/es2015_tests.rs +++ b/crates/js/src/es2015_tests.rs @@ -203,6 +203,17 @@ fn a_block_function_whose_variable_cannot_be_declared_stays_in_its_block() { assert_eq!(realm.run("(0, eval)('{ function m() { return 7; } out = m(); }'); out + typeof m"), Ok(JsValue::String("7undefined".into()))); } +#[test] +fn a_block_function_in_eval_in_a_default_parameter_leaves_a_parameter_of_its_name_alone() { + // A parameter between the eval code and its variable environment has the name, so the block + // function gets no variable (Annex B.3.2.3) and the parameter keeps its value. + truth("function f(a = eval('{ function a() {} }'), b = a) { return typeof b; } f() === 'undefined'"); + truth("function f(a = 1, b = eval('{ function a() {} }'), c = a) { return c + typeof a; } f() === '1number'"); + truth("function f(a = 2, b = eval('{ function a() {} }')) { return a; } f() === 2"); + // A name that no parameter has still gets the variable. + truth("function f(x = eval('{ function a() { return 3; } }'), b = a) { return b(); } f() === 3"); +} + #[test] fn a_labelled_function_in_a_block_is_made_when_the_block_is_entered() { truth("let f = 0; { var value = f(); label: function f() { return 7; } } value === 7 && f === 0"); diff --git a/crates/js/src/interp.rs b/crates/js/src/interp.rs index 0639d52d25..3784b3a515 100644 --- a/crates/js/src/interp.rs +++ b/crates/js/src/interp.rs @@ -4813,7 +4813,8 @@ impl Interp { /// A block function's variable in script or eval code can be declared (Annex B.3.3.2 and /// B.3.3.3, ): no /// `let`, `const` or block function from `env` out to `var_env` has the name, a `catch` - /// parameter does not count, and on the global object the name is its own property or the + /// parameter does not count, no parameter has the name when the code is in a default + /// parameter's value (B.3.2.3), and on the global object the name is its own property or the /// object is extensible. Each environment passed takes a unit of fuel. fn annex_var_declarable(&mut self, name: &Key, env: EnvId, var_env: EnvId) -> JsResult { let mut cur = Some(env).filter(|&e| e != var_env); @@ -4832,7 +4833,14 @@ impl Interp { .filter(|&o| o != var_env); } if var_env != self.global_env { - return Ok(true); + // Eval code in a default parameter's value declares into the parameters' environment, + // where the parameters are lexical bindings. + self.use_fuel()?; + return match self.heap.env(var_env) { + Some(Env::Decl { bindings, .. }) => Ok(!bindings.get(name).is_some_and(|b| b.lexical)), + Some(Env::Obj { .. }) => Ok(true), + None => internal("environment out of range"), + }; } let global = self.global; Ok(self.get_own_property(global, name)?.is_some() || self.object(global)?.extensible)