summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2022-03-14 02:39:24 +0000
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2022-03-14 21:15:27 +0330
commit750b69540ef07b6a2ebdbb15f01bd87ee9b5938a (patch)
tree4d91f7dc9d794ec9eae6b792f52962579a0dd637 /Userland/Libraries/LibJS
parent97af7654dd3d5d6917d2693ba2162d0035750f21 (diff)
downloadserenity-750b69540ef07b6a2ebdbb15f01bd87ee9b5938a.zip
LibJS/Bytecode: Setup declarative environment for lexical for statements
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp26
1 files changed, 25 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
index 4453d9d011..e770e7773b 100644
--- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
+++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
@@ -723,8 +723,29 @@ Bytecode::CodeGenerationErrorOr<void> ForStatement::generate_bytecode(Bytecode::
auto& end_block = generator.make_block();
- if (m_init)
+ bool has_lexical_environment = false;
+
+ if (m_init) {
+ if (m_init->is_variable_declaration()) {
+ auto& variable_declaration = verify_cast<VariableDeclaration>(*m_init);
+
+ if (variable_declaration.is_lexical_declaration()) {
+ has_lexical_environment = true;
+
+ // FIXME: Is Block correct?
+ generator.begin_variable_scope(Bytecode::Generator::BindingMode::Lexical, Bytecode::Generator::SurroundingScopeKind::Block);
+
+ bool is_const = variable_declaration.is_constant_declaration();
+ variable_declaration.for_each_bound_name([&](auto const& name) {
+ auto index = generator.intern_identifier(name);
+ generator.register_binding(index);
+ generator.emit<Bytecode::Op::CreateVariable>(index, Bytecode::Op::EnvironmentMode::Lexical, is_const);
+ });
+ }
+ }
+
TRY(m_init->generate_bytecode(generator));
+ }
body_block_ptr = &generator.make_block();
@@ -761,6 +782,9 @@ Bytecode::CodeGenerationErrorOr<void> ForStatement::generate_bytecode(Bytecode::
generator.end_breakable_scope();
generator.end_continuable_scope();
+ if (has_lexical_environment)
+ generator.end_variable_scope();
+
if (!generator.is_current_block_terminated()) {
if (m_update) {
generator.emit<Bytecode::Op::Jump>().set_targets(