Truly independent web browser
LibJS: Create the function environment that variable lookups count
Tim Ledbetter22a816feee96
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rsindex 976ddb41eed..2e54f3f31ba 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs@@ -225,6 +225,8 @@ pub struct CodeScope {var_of_arguments_is_captured: bool,/// Whether a lexical declaration of the name `arguments` takes the place of the arguments object.has_lexical_arguments: bool,+ /// Whether a call of the function creates an environment of its own, which the environment coordinates count.+ has_function_environment: bool,/// Whether the lexical declarations of the function have an environment of their own.has_lexical_environment: bool,/// Whether the parameters have an environment of their own, for what an `eval` in them declares.@@ -465,6 +467,7 @@ impl Generator<'_> {&& flags & function_flag::ARGUMENTS_ACCESS != 0&& flags & function_flag::SIMPLE_PARAMETERS != 0&& index.parameter_count[row.0 as usize] != 0);+ self.context.scope.has_function_environment = needs_function_environment;self.capture_saved_lexical_environment_with_coordinates(needs_function_environment);self.end_head();self.begin_stream_temporaries();@@ -501,6 +504,7 @@ impl Generator<'_> {scope.has_function_in_parameters = false;scope.var_of_arguments_is_captured = false;scope.has_lexical_arguments = false;+ scope.has_function_environment = false;scope.has_mapped_parameters = false;scope.deferred_early_bindings = None;scope.has_parameter_expressions_with_eval = false;@@ -1877,7 +1881,6 @@ impl Generator<'_> {let row = self.context.scope.row;let flags = self.context.scope.flags;let is_program = self.context.scope.program.is_some();- let is_arrow = flags & function_flag::ARROW != 0;let has_direct_eval = self.has_direct_eval();let contains_eval = flags & function_flag::CONTAINS_EVAL != 0;@@ -2022,6 +2025,8 @@ impl Generator<'_> {.function_environment_binding_count.unwrap_or(environment_binding_count);let has_mapped_parameters = scope.has_mapped_parameters;+ let has_function_environment = scope.has_function_environment;+ assert!(has_function_environment || environment_binding_count == 0 || is_program);let parameter_names: Box<[ak::Utf16FlyString]> = if has_mapped_parameters {let names = &context.scope.simple_parameters;names@@ -2064,9 +2069,7 @@ impl Generator<'_> {uses_this: this_from_environment|| flags & (function_flag::SCOPE_USES_THIS | function_flag::INSIGHT_USES_THIS) != 0,this_value_needs_environment_resolution: this_from_environment,- function_environment_needed: environment_binding_count > 0- || (!is_arrow && this_from_environment)- || contains_eval,+ function_environment_needed: has_function_environment,creates_environment: self.creates_environment,function_environment_bindings_count: function_environment_binding_count as usize,var_environment_bindings_count: (environment_binding_count - function_environment_binding_count)diff --git a/Tests/LibJS/Runtime/lexical-arguments-closure-lookup.js b/Tests/LibJS/Runtime/lexical-arguments-closure-lookup.jsnew file mode 100644index 00000000000..bd2e795c9fb--- /dev/null+++ b/Tests/LibJS/Runtime/lexical-arguments-closure-lookup.js@@ -0,0 +1,60 @@+describe("closure lookup from a function that creates no arguments object", () => {+ function enclose(makeFunction) {+ const decoy = "decoy";+ const inner = makeFunction("captured");+ return [inner, () => decoy];+ }++ test("let declaration after a read of arguments", () => {+ const [read] = enclose(+ value =>+ function (index) {+ if (index === 0) arguments;+ let arguments = 1;+ return value;+ }+ );+ expect(read(1)).toBe("captured");+ });++ test("const declaration after typeof arguments", () => {+ const [read] = enclose(+ value =>+ function (index) {+ if (index === 0) typeof arguments;+ const arguments = 1;+ return value;+ }+ );+ expect(read(1)).toBe("captured");+ });++ test("assignment after a read of arguments", () => {+ let readValue;+ const [write, readDecoy] = enclose(value => {+ readValue = () => value;+ return function (index) {+ if (index === 0) arguments;+ let arguments = 1;+ value = "written";+ };+ });+ write(1);+ expect(readValue()).toBe("written");+ expect(readDecoy()).toBe("decoy");+ });++ test("parameter named arguments and an arrow function that declares arguments", () => {+ const [read] = enclose(+ value =>+ function (arguments) {+ (() => {+ if (false) arguments;+ let arguments = 1;+ })();+ return value;+ }+ );+ expect(read(1)).toBe("captured");+ });+});