Skip to content

Truly independent web browser

LibJS: Don't resolve the name of a var without an initializer

Shannon Booth5908d4c78585
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
index ae671cc0290..453ffc3768b 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
@@ -3879,12 +3879,10 @@ impl Generator<'_> {
}
let target = self.pop_value();
self.store_to_target(target, value, kind != DeclarationKind::Var);
- } else {
+ } else if kind != DeclarationKind::Var {
let target = self.pop_value();
- if kind != DeclarationKind::Var {
- let undefined = self.context.constants.add_undefined();
- self.store_to_target(target, undefined, true);
- }
+ let undefined = self.context.constants.add_undefined();
+ self.store_to_target(target, undefined, true);
}
if target_ident.local_type == Some(LocalType::Variable) {
self.mark_local_initialized(target_ident.local_index);
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
index 07971832d48..9afa19c8491 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
@@ -161,7 +161,10 @@ impl Generator<'_> {
self.begin_disposal(is_async);
}
let ident = self.declare_binding(Name(a), declaration_kind);
- self.target_of_declaration(ident);
+ // NB: A `var` without an initializer does nothing, not even resolve its name.
+ if declaration_kind != DeclarationKind::Var || tags[row + 1] != tag::DECLARATOR {
+ self.target_of_declaration(ident);
+ }
declared_ident = Some(ident);
}
tag::IDENT_TYPEOF => {
diff --git a/Tests/LibJS/Runtime/with-basic.js b/Tests/LibJS/Runtime/with-basic.js
index 41c3539c8e1..f01a0175fed 100644
--- a/Tests/LibJS/Runtime/with-basic.js
+++ b/Tests/LibJS/Runtime/with-basic.js
@@ -135,6 +135,26 @@ test("var patterns resolve their names before getting their values", () => {
expect(evalAddedId).toBe(2);
});
+test("var without an initializer does not resolve its name", () => {
+ const names = [];
+ const object = new Proxy(
+ {},
+ {
+ has(target, name) {
+ names.push(name);
+ return false;
+ },
+ }
+ );
+
+ with (object) {
+ var noInitializerId;
+ for (var noInitializerLoopId; false; );
+ }
+
+ expect(names).toEqual([]);
+});
+
test("with statement updates empty abrupt completions to undefined", () => {
expect(eval("1; do { 2; with ({}) { 3; break; } 4; } while (false);")).toBe(3);
expect(eval("5; do { 6; with ({}) { break; } 7; } while (false);")).toBeUndefined();