Skip to content

Truly independent web browser

LibJS: Read a parameter for its `var` from the start of the body

Shannon Booth990f639af914
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/bindings.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/bindings.rs
index b547a8d4442..0bb361f657a 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/bindings.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/bindings.rs
@@ -18,12 +18,24 @@ pub(super) fn emit_get_binding(
dst: Operand,
identifier: IdentifierTableIndex,
known_initialized: bool,
+) {
+ let location = generator.binding_location_for_identifier(identifier);
+ emit_get_binding_at(generator, dst, identifier, location, known_initialized);
+}
+
+/// Reads the binding at `location`, as seen from the running lexical environment.
+pub(super) fn emit_get_binding_at(
+ generator: &mut Generator<'_>,
+ dst: Operand,
+ identifier: IdentifierTableIndex,
+ location: Option<BindingLocation>,
+ known_initialized: bool,
) {
// Prefer an eagerly-computed coordinate when the generator can prove that
// the binding lives in a known declarative environment. The dynamic forms
// remain necessary for script and eval code, eval-poisoned scopes, and
// `with`.
- match (generator.binding_location_for_identifier(identifier), known_initialized) {
+ match (location, known_initialized) {
(Some(BindingLocation::Import(import_index)), _) => generator.emit(op::GetImport {
dst,
identifier,
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index 99b746308ef..b12354ce684 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -45,6 +45,7 @@ use crate::u32_from_usize;
use super::EnclosingEnvironmentScope;
use super::bindings::*;
use super::context::Generator;
+use super::environment::BindingLocation;
use super::nodes::Identifier;
use super::registers::ScopedOperand;
use super::stream::Control;
@@ -775,7 +776,11 @@ impl Generator<'_> {
Some(_) => {
let value = generator.temporary();
let identifier = generator.intern_identifier(name);
- emit_get_binding(generator, value, identifier, false);
+ // NB: This runs where the body starts, before any scope around the `var` has an environment.
+ let location = generator
+ .variable_environment_coordinate_for_identifier(identifier)
+ .map(BindingLocation::Environment);
+ emit_get_binding_at(generator, value, identifier, location, false);
value
}
};
diff --git a/Tests/LibJS/Runtime/functions-parameter-scope.js b/Tests/LibJS/Runtime/functions-parameter-scope.js
index bf6de119923..b531727ca27 100644
--- a/Tests/LibJS/Runtime/functions-parameter-scope.js
+++ b/Tests/LibJS/Runtime/functions-parameter-scope.js
@@ -57,6 +57,83 @@ test("a var of the name of a parameter starts with its value and is a binding of
expect(i(1)).toEqual([1, 1]);
});
+test("a var of the name of a parameter inside a scope that has an environment", () => {
+ function inBlock(p = "parameter", read = () => p) {
+ {
+ let q = "block";
+ const keep = () => q;
+ var p;
+ }
+ return [p, read()];
+ }
+ expect(inBlock()).toEqual(["parameter", "parameter"]);
+
+ function inNestedBlocks(p = "parameter", read = () => p) {
+ {
+ let q = "block";
+ const keepQ = () => q;
+ {
+ let s = "inner block";
+ const keepS = () => s;
+ var p;
+ }
+ }
+ return p;
+ }
+ expect(inNestedBlocks()).toBe("parameter");
+
+ function inCatch(p = "parameter", read = () => p) {
+ try {
+ throw 1;
+ } catch (e) {
+ const keep = () => e;
+ var p;
+ }
+ return p;
+ }
+ expect(inCatch()).toBe("parameter");
+
+ function inLoop(p = "parameter", read = () => p) {
+ for (let i = 0; i < 1; i++) {
+ const keep = () => i;
+ var p;
+ }
+ return p;
+ }
+ expect(inLoop()).toBe("parameter");
+
+ function inSwitch(p = "parameter", read = () => p) {
+ switch (0) {
+ case 0:
+ let q = "case";
+ const keep = () => q;
+ var p;
+ }
+ return p;
+ }
+ expect(inSwitch()).toBe("parameter");
+
+ function inClassScope(p = "parameter", read = () => p) {
+ {
+ class K {}
+ const keep = () => K;
+ var p;
+ }
+ return p;
+ }
+ expect(inClassScope()).toBe("parameter");
+
+ function withDirectEval(p = "parameter", read = () => p) {
+ {
+ let q = "block";
+ const keep = () => q;
+ var p;
+ }
+ return eval("p");
+ }
+ expect(withDirectEval()).toBe("parameter");
+});
+
test("a function declaration of the name of a parameter", () => {
function f(a, b = a) {
function a() {}