Truly independent web browser
LibJS: Initialize the local of a var that redeclares a catch parameter
Tim Ledbetter3901681a4125
diff --git a/Libraries/LibJS/Rust/src/parser/statements.rs b/Libraries/LibJS/Rust/src/parser/statements.rsindex 6e63f3d5a18..adb6d34bdec 100644--- a/Libraries/LibJS/Rust/src/parser/statements.rs+++ b/Libraries/LibJS/Rust/src/parser/statements.rs@@ -816,7 +816,7 @@ impl Parser<'_> {&arena.strings,&mut arena.scopes,);- scope_collector.add_catch_parameter_identifier(&value, id);+ scope_collector.add_catch_parameter_identifier(&value);Some(CatchBinding::Identifier(id))} else {self.expected("catch parameter");diff --git a/Libraries/LibJS/Rust/src/scope_collector.rs b/Libraries/LibJS/Rust/src/scope_collector.rsindex e241db66c90..b6831ab321a 100644--- a/Libraries/LibJS/Rust/src/scope_collector.rs+++ b/Libraries/LibJS/Rust/src/scope_collector.rs@@ -146,9 +146,9 @@ impl std::ops::BitOrAssign for VarFlags {struct ScopeVariable {/// Bit flags describing how this name was declared.flags: VarFlags,- /// The Identifier AST node for the `var` declaration (used to build- /// FunctionScopeData). None if not a var.- var_identifier: Option<IdentifierId>,+ /// The local that references to this name in this scope resolve to (used to+ /// build FunctionScopeData). None if not a local.+ local: Option<LocalBinding>,}/// Groups all Identifier AST nodes that share the same name within a scope.@@ -578,9 +578,7 @@ impl ScopeCollector {{self.already_declared_error(name, declaration_line, declaration_column);}- let var = self.records[scope_index].variable(name);- var.flags |= VarFlags::VAR;- var.var_identifier = *identifier;+ self.records[scope_index].variable(name).flags |= VarFlags::VAR;if self.records[scope_index].is_top_level() {break;}@@ -618,9 +616,7 @@ impl ScopeCollector {}if scope_level != ScopeLevel::NotTopLevel && scope_level != ScopeLevel::ModuleTopLevel {- let var = self.records[index].variable(name);- var.flags |= VarFlags::VAR;- var.var_identifier = name_identifier;+ self.records[index].variable(name).flags |= VarFlags::VAR;} else {// Check flags first, then modify. This avoids borrow checker issues// since we need to access both variables and functions_to_hoist.@@ -693,11 +689,9 @@ impl ScopeCollector {self.records[index].variable(name).flags |= VarFlags::IMPORT;}- pub fn add_catch_parameter_identifier(&mut self, name: &[u16], identifier: IdentifierId) {+ pub fn add_catch_parameter_identifier(&mut self, name: &[u16]) {let index = self.current.expect("no current scope");- let var = self.records[index].variable(name);- var.flags |= VarFlags::VAR | VarFlags::BOUND | VarFlags::CATCH_PARAMETER;- var.var_identifier = Some(identifier);+ self.records[index].variable(name).flags |= VarFlags::VAR | VarFlags::BOUND | VarFlags::CATCH_PARAMETER;}// === Identifier registration ===@@ -1265,7 +1259,7 @@ impl ScopeCollector {.and_then(|scope_id| scopes[scope_id].source_range);let sd = &mut scopes[scope_id];- if is_function_parameter {+ let local = if is_function_parameter {let argument_index = records[ls].get_parameter_index(&name);if let Some(ai) = argument_index {for id in &group.identifiers {@@ -1273,6 +1267,10 @@ impl ScopeCollector {identifier.local_index = ai;identifier.local_type = Some(crate::ast::LocalType::Argument);}+ LocalBinding {+ local_type: crate::ast::LocalType::Argument,+ index: ai,+ }} else {let lvi = u32_from_usize(sd.local_variables.len());sd.local_variables.push(LocalVariable {@@ -1286,6 +1284,10 @@ impl ScopeCollector {identifier.local_index = lvi;identifier.local_type = Some(crate::ast::LocalType::Variable);}+ LocalBinding {+ local_type: crate::ast::LocalType::Variable,+ index: lvi,+ }}} else {let kind = local_var_kind.expect("local_var_kind must be set for local variables");@@ -1301,6 +1303,13 @@ impl ScopeCollector {identifier.local_index = lvi;identifier.local_type = Some(crate::ast::LocalType::Variable);}+ LocalBinding {+ local_type: crate::ast::LocalType::Variable,+ index: lvi,+ }+ };+ if let Some(variable) = records[index].variables.get_mut(name.as_slice()) {+ variable.local = Some(local);}}}@@ -1412,19 +1421,7 @@ impl ScopeCollector {let is_parameter = var.flags.intersects(VarFlags::FORBIDDEN_LEXICAL);let is_function_name = last_position_by_slice.contains_key(name.as_slice());- let local_info = if let Some(ident_id) = var.var_identifier {- let ident = &identifiers[ident_id];- if ident.is_local() {- Some(LocalBinding {- local_type: ident.local_type.expect("is_local() implies local_type is Some"),- index: ident.local_index,- })- } else {- None- }- } else {- None- };+ let local_info = var.local;if local_info.is_none() {non_local_var_count_for_parameter_expressions += 1;diff --git a/Tests/LibJS/Runtime/try-catch-finally.js b/Tests/LibJS/Runtime/try-catch-finally.jsindex 60bd3dc5726..3b24393a0ea 100644--- a/Tests/LibJS/Runtime/try-catch-finally.js+++ b/Tests/LibJS/Runtime/try-catch-finally.js@@ -192,3 +192,25 @@ test("lexical declarations in a catch block may not redeclare the catch parameteexpect("try {} catch ({ e }) { const [e] = []; }").not.toEval();expect("try {} catch (e) { using e = null; }").not.toEval();});++test("var declaration named like the catch parameter leaves the function binding undefined", () => {+ function local() {+ try {+ throw 1;+ } catch (e) {+ var e = 2;+ }+ return typeof e;+ }+ expect(local()).toBe("undefined");++ function captured() {+ try {+ throw 1;+ } catch (e) {+ var e = 2;+ }+ return (() => e)();+ }+ expect(captured()).toBeUndefined();+});