Truly independent web browser
LibJS: Find a closure’s bindings from unmapped-arguments functions
sideshowbarker4c70410b0a1e
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rsindex 7d26613c0aa..61450bd79f5 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs@@ -463,6 +463,7 @@ impl Generator<'_> {&& flags & (function_flag::MIGHT_NEED_ARGUMENTS | function_flag::USES_ARGUMENTS) != 0)|| (!is_arrow&& flags & function_flag::ARGUMENTS_ACCESS != 0+ && flags & function_flag::SIMPLE_PARAMETERS != 0&& index.parameter_count[row.0 as usize] != 0);self.capture_saved_lexical_environment_with_coordinates(needs_function_environment);self.end_head();diff --git a/Tests/LibJS/Runtime/unmapped-arguments-closure-lookup.js b/Tests/LibJS/Runtime/unmapped-arguments-closure-lookup.jsnew file mode 100644index 00000000000..727a8cc0c3c--- /dev/null+++ b/Tests/LibJS/Runtime/unmapped-arguments-closure-lookup.js@@ -0,0 +1,64 @@+// A sloppy-mode function whose parameters aren't simple gets an unmapped arguments object, which needs no environment.+// Reading a variable of an enclosing function from such a function has to find that function's binding.+describe("closure lookup from a function with an unmapped arguments object", () => {+ function enclose(makeReader) {+ const decoy = "decoy";+ const read = makeReader("captured");+ return [read, () => decoy];+ }++ test("default parameter", () => {+ const [read] = enclose(+ value =>+ function (index, flag = false) {+ if (arguments.length) return value;+ }+ );+ expect(read(0)).toBe("captured");+ });++ test("rest parameter", () => {+ const [read] = enclose(+ value =>+ function (...rest) {+ arguments;+ return value;+ }+ );+ expect(read(0)).toBe("captured");+ });++ test("destructuring parameter", () => {+ const [read] = enclose(+ value =>+ function ({ a } = {}) {+ arguments;+ return value;+ }+ );+ expect(read()).toBe("captured");+ });++ test("default parameter reading arguments", () => {+ const [read] = enclose(+ value =>+ function (index, count = arguments.length) {+ return value;+ }+ );+ expect(read(0)).toBe("captured");+ });++ test("method with a default parameter", () => {+ const [read] = enclose(+ value =>+ ({+ method(index, flag = false) {+ arguments;+ return value;+ },+ }).method+ );+ expect(read(0)).toBe("captured");+ });+});