Truly independent web browser
LibJS: Keep parameters mapped after a `var arguments` declaration
Shannon Booth159e288ee23a
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rsindex 06de1b63dc8..0700a26e22e 100644--- a/Libraries/LibJS/Rust/src/parser/base.rs+++ b/Libraries/LibJS/Rust/src/parser/base.rs@@ -1411,9 +1411,14 @@ impl<'a, U: Unit> Parser<'a, U> {pub(crate) fn is_declared_in_current_function(&self, name: u32) -> bool {let mut d = self.names.get(name as usize).map_or(NONE, |state| state.decl_head);let mut function_scope = 0;+ let mut declaring_flags = decl::LEXICAL | decl::VAR | decl::PARAMETER | decl::HOISTABLE;for scope in self.scopes.iter().rev() {if scope.flags & (scope_flag::FUNCTION | scope_flag::STATIC_BLOCK) != 0 {function_scope = scope.id;+ if scope.flags & (scope_flag::FUNCTION | scope_flag::ARROW) == scope_flag::FUNCTION {+ // NB: A `var` in a function with an arguments object of its own is the binding of that object.+ declaring_flags = decl::LEXICAL | decl::FUNCTION | decl::PARAMETER | decl::HOISTABLE;+ }break;}}@@ -1422,7 +1427,7 @@ impl<'a, U: Unit> Parser<'a, U> {if row.scope < function_scope {break;}- if row.flags & (decl::LEXICAL | decl::VAR | decl::PARAMETER | decl::HOISTABLE) != 0 {+ if row.flags & declaring_flags != 0 {return true;}d = row.next_same_name;diff --git a/Tests/LibJS/Runtime/arguments-object.js b/Tests/LibJS/Runtime/arguments-object.jsindex 20987e5e42c..50328a70b1d 100644--- a/Tests/LibJS/Runtime/arguments-object.js+++ b/Tests/LibJS/Runtime/arguments-object.js@@ -245,6 +245,86 @@ test("var named arguments shares the binding of the arguments object", () => {expect(laterBindingsKeepTheirPlaces()).toEqual([2, 2]);});+test("var named arguments before its first use keeps the parameters mapped", () => {+ function writeThroughArguments(a) {+ var arguments;+ arguments[0] = 2;+ return a;+ }+ expect(writeThroughArguments(1)).toBe(2);+ expect(Object.hasOwn(globalThis, "a")).toBeFalse();++ function writeThroughParameter(b) {+ var arguments;+ b = 3;+ return arguments[0];+ }+ expect(writeThroughParameter(1)).toBe(3);++ function declaredInBlock(c) {+ {+ var arguments;+ }+ arguments[0] = 4;+ return c;+ }+ expect(declaredInBlock(1)).toBe(4);++ function outer() {+ var d = "outer";+ const keep = () => d;+ function inner(d) {+ var arguments;+ const before = arguments[0];+ arguments[0] = "inner";+ return [before, d];+ }+ return [...inner(1), d];+ }+ expect(outer()).toEqual([1, "inner", "outer"]);++ function outerWithoutTheName() {+ var unrelated = 1;+ const keep = () => unrelated;+ function inner(e) {+ var arguments;+ return arguments[0];+ }+ return inner(5);+ }+ expect(outerWithoutTheName()).toBe(5);++ const object = {+ method(f) {+ var arguments;+ arguments[0] = 6;+ return f;+ },+ };+ expect(object.method(1)).toBe(6);++ function* generator(g) {+ var arguments;+ arguments[0] = 7;+ yield g;+ }+ expect(generator(1).next().value).toBe(7);++ function arrowFunctionVar(h) {+ return (() => {+ var arguments;+ return typeof arguments;+ })();+ }+ expect(arrowFunctionVar(1)).toBe("undefined");++ function generatorDeclaration(i) {+ function* arguments() {}+ return typeof arguments;+ }+ expect(generatorDeclaration(1)).toBe("function");+});+test("lexical declaration named arguments takes the place of the arguments object", () => {function captured() {const before = (() => {