From 67ccd7834fb8463b04e7f1a5df8cd17cf1a33d05 Mon Sep 17 00:00:00 2001 From: jedel1043 Date: Fri, 14 Oct 2022 15:18:29 -0500 Subject: [PATCH] Fix declaration creation for loops --- boa_engine/src/bytecompiler/mod.rs | 63 ++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/boa_engine/src/bytecompiler/mod.rs b/boa_engine/src/bytecompiler/mod.rs index b2ca5e4e8bf..d31a244cd17 100644 --- a/boa_engine/src/bytecompiler/mod.rs +++ b/boa_engine/src/bytecompiler/mod.rs @@ -2688,48 +2688,77 @@ impl<'b> ByteCompiler<'b> { has_identifier_argument } - pub(crate) fn create_decls_from_stmt_list_item(&mut self, node: &StatementListItem) -> bool { - match node { - StatementListItem::Declaration(Declaration::Lexical(decl)) => { - self.create_decls_from_lexical_decl(decl) - } - StatementListItem::Statement(Statement::Var(decl)) => { - self.create_decls_from_var_decl(decl) - } - StatementListItem::Declaration(Declaration::Class(decl)) => { - let ident = decl.name().expect("class declaration must have a name"); - self.context.create_mutable_binding(ident, false); - false - } - StatementListItem::Declaration(Declaration::Function(decl)) => { + #[inline] + pub(crate) fn create_decls_from_decl(&mut self, declaration: &Declaration) -> bool { + match declaration { + Declaration::Lexical(decl) => self.create_decls_from_lexical_decl(decl), + Declaration::Function(decl) => { let ident = decl.name().expect("function declaration must have a name"); self.context.create_mutable_binding(ident, true); ident == Sym::ARGUMENTS } - StatementListItem::Declaration(Declaration::Generator(decl)) => { + Declaration::Generator(decl) => { let ident = decl.name().expect("generator declaration must have a name"); self.context.create_mutable_binding(ident, true); ident == Sym::ARGUMENTS } - StatementListItem::Declaration(Declaration::AsyncFunction(decl)) => { + Declaration::AsyncFunction(decl) => { let ident = decl .name() .expect("async function declaration must have a name"); self.context.create_mutable_binding(ident, true); ident == Sym::ARGUMENTS } - StatementListItem::Declaration(Declaration::AsyncGenerator(decl)) => { + Declaration::AsyncGenerator(decl) => { let ident = decl .name() .expect("async generator declaration must have a name"); self.context.create_mutable_binding(ident, true); ident == Sym::ARGUMENTS } + Declaration::Class(decl) => { + let ident = decl.name().expect("class declaration must have a name"); + self.context.create_mutable_binding(ident, false); + false + } + } + } + + #[inline] + pub(crate) fn create_decls_from_stmt(&mut self, statement: &Statement) -> bool { + match statement { + Statement::Var(var) => self.create_decls_from_var_decl(var), + Statement::DoWhileLoop(do_while_loop) => { + if !matches!(do_while_loop.body(), Statement::Block(_)) { + self.create_decls_from_stmt(do_while_loop.body()); + } + false + } + Statement::ForInLoop(for_in_loop) => { + if !matches!(for_in_loop.body(), Statement::Block(_)) { + self.create_decls_from_stmt(for_in_loop.body()); + } + false + } + Statement::ForOfLoop(for_of_loop) => { + if !matches!(for_of_loop.body(), Statement::Block(_)) { + self.create_decls_from_stmt(for_of_loop.body()); + } + false + } _ => false, } } + #[inline] + pub(crate) fn create_decls_from_stmt_list_item(&mut self, item: &StatementListItem) -> bool { + match item { + StatementListItem::Declaration(decl) => self.create_decls_from_decl(decl), + StatementListItem::Statement(stmt) => self.create_decls_from_stmt(stmt), + } + } + /// This function compiles a class declaration or expression. /// /// The compilation of a class declaration and expression is mostly equal.