Skip to content

Truly independent web browser

LibJS: Don't cache the shape of a function environment that eval fills

Shannon Booth385f1f7b5440
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index c67d905140b..b0477ce952c 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -1348,6 +1348,11 @@ impl Generator<'_> {
});
self.push_static_variable_environment(environment);
let scope = &mut self.context.scope;
+ if scope.has_parameter_expressions_with_eval {
+ // NB: The parameters have an environment of their own, so the function environment only gets what an
+ // `eval` in them declares.
+ scope.environment_binding_count = 0;
+ }
scope.function_environment_binding_count = Some(scope.environment_binding_count);
}
if self.contains_direct_call_to_eval_in_non_strict_mode && captured_count != 0 {
diff --git a/Tests/LibJS/Runtime/functions-parameter-scope.js b/Tests/LibJS/Runtime/functions-parameter-scope.js
index 6a672ba077a..fc432c48c23 100644
--- a/Tests/LibJS/Runtime/functions-parameter-scope.js
+++ b/Tests/LibJS/Runtime/functions-parameter-scope.js
@@ -215,6 +215,16 @@ test("a function in the parameters checks the dead zone of what is around for a
}).toThrowWithMessage(ReferenceError, "Binding late is not initialized");
});
+test("an eval in the parameters declares different variables on each call", () => {
+ let source = "var p = 1, q = 2";
+ function f(a = eval(source)) {
+ return [typeof p, typeof q, typeof r];
+ }
+ expect(f()).toEqual(["number", "number", "undefined"]);
+ source = "var r = 3";
+ expect(f()).toEqual(["undefined", "undefined", "number"]);
+});
+
test("a function in the parameters sees the parameter, not a var in a catch clause of the same name", () => {
function f(a = () => x, x) {
try {