mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
Merge pull request #10619 from dotCooCoo/fix/eval-default-param-annexb
JavaScript: eval in a default parameter leaves a parameter of a block function's name alone
This commit is contained in:
2 files changed
+21
-2
No files matched your search
@@ -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");
|
||||
|
||||
+10
-2
@@ -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, <https://tc39.es/ecma262/#sec-web-compat-globaldeclarationinstantiation>): 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<bool> {
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user