Truly independent web browser
LibJS: Store Annex B functions named arguments into arguments binding
Andreas Kling7d87f8d14695
diff --git a/Libraries/LibJS/Rust/src/scope_collector.rs b/Libraries/LibJS/Rust/src/scope_collector.rsindex 6dcb9ad6664..e241db66c90 100644--- a/Libraries/LibJS/Rust/src/scope_collector.rs+++ b/Libraries/LibJS/Rust/src/scope_collector.rs@@ -461,9 +461,11 @@ impl ScopeCollector {self.records[parent_index].contains_await_expression |= contains_await;}// NB: An arrow function has no arguments object of its own, so the function it is in provides the one it uses.+ // A direct eval in the arrow function can also reach it.else if let Some(parent_index) = self.records[index].parent&& self.records[index].is_arrow_function- && self.records[index].contains_access_to_arguments_object_in_non_strict_mode+ && (self.records[index].contains_access_to_arguments_object_in_non_strict_mode+ || self.records[index].contains_direct_call_to_eval){self.records[parent_index].contains_access_to_arguments_object_in_non_strict_mode = true;}@@ -1515,16 +1517,16 @@ impl ScopeCollector {if records[index].has_flag(&function.name, VarFlags::FORBIDDEN_LEXICAL) {continue;}- // AnnexB.3.3.1: Skip hoisting if the function name is "arguments"- // and the function needs an arguments object.- if function.name == utf16!("arguments")- && records[index].contains_access_to_arguments_object_in_non_strict_mode- && !records[index].has_flag(utf16!("arguments"), VarFlags::FORBIDDEN_LEXICAL)- {- continue;- }+ // NB: FunctionDeclarationInstantiation creates no var binding for a hoisted function named+ // "arguments". When the function declaration is evaluated, it is stored into the arguments+ // object binding instead. A direct eval can reach the arguments object too.+ let uses_existing_arguments_binding = function.name == utf16!("arguments")+ && (records[index].contains_access_to_arguments_object_in_non_strict_mode+ || (records[index].scope_type == ScopeType::Function+ && !records[index].is_arrow_function+ && records[index].contains_direct_call_to_eval));// Reached function/program scope — register the hoisted function name.- if let Some(scope_id) = records[index].scope_data {+ if !uses_existing_arguments_binding && let Some(scope_id) = records[index].scope_data {let sd = &mut scopes[scope_id];if !sd.annexb_function_names.contains(&function.name) {sd.annexb_function_names.push(function.name.clone());diff --git a/Tests/LibJS/Runtime/arguments-object.js b/Tests/LibJS/Runtime/arguments-object.jsindex 98ac3aa62a2..801d4258580 100644--- a/Tests/LibJS/Runtime/arguments-object.js+++ b/Tests/LibJS/Runtime/arguments-object.js@@ -146,3 +146,23 @@ test("functions whose arguments object is never used", () => {}expect(withParametersAndArguments(1)).toEqual([1, undefined, 1]);});++test("block-level function named arguments with arguments object reached through direct eval", () => {+ function f() {+ const before = eval("typeof arguments");+ {+ function arguments() {}+ }+ return [before, eval("typeof arguments")];+ }+ expect(f(1)).toEqual(["object", "function"]);++ function g() {+ const before = (() => eval("arguments.length"))();+ {+ function arguments() {}+ }+ return [before, (() => eval("typeof arguments"))()];+ }+ expect(g(1, 2)).toEqual([2, "function"]);+});