From dbfe1311eff2c3d7d15aefc5cc068c6f118c87b8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 16 Jun 2023 16:43:24 +0200 Subject: [PATCH] LibJS/Bytecode: Simplify creating/leaving lexical environment Since we no longer need to create or leave var environments directly in bytecode, we can streamline the two instructions by making them always operate on the lexical environment. --- .../Libraries/LibJS/Bytecode/ASTCodegen.cpp | 4 +-- .../Libraries/LibJS/Bytecode/Generator.cpp | 12 ++++---- Userland/Libraries/LibJS/Bytecode/Generator.h | 2 +- .../Libraries/LibJS/Bytecode/Instruction.h | 4 +-- Userland/Libraries/LibJS/Bytecode/Op.cpp | 28 ++++++------------- Userland/Libraries/LibJS/Bytecode/Op.h | 20 ++++--------- 6 files changed, 25 insertions(+), 45 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 45178bc216..8b90d2e67a 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -2226,7 +2226,7 @@ Bytecode::CodeGenerationErrorOr WithStatement::generate_bytecode(Bytecode: generator.end_boundary(Bytecode::Generator::BlockBoundaryType::LeaveLexicalEnvironment); if (!generator.is_current_block_terminated()) - generator.emit(Bytecode::Op::EnvironmentMode::Lexical); + generator.emit(); return {}; } @@ -2282,7 +2282,7 @@ static Bytecode::CodeGenerationErrorOr for_in_of_he generator.emit(identifier, Bytecode::Op::EnvironmentMode::Lexical, false); })); // d. Set the running execution context's LexicalEnvironment to newEnv. - // NOTE: Done by CreateEnvironment. + // NOTE: Done by CreateLexicalEnvironment. } } else { // Runtime Semantics: ForInOfLoopEvaluation, for any of: diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.cpp b/Userland/Libraries/LibJS/Bytecode/Generator.cpp index 9d10e4c09d..1c0897afde 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Generator.cpp @@ -96,7 +96,7 @@ void Generator::block_declaration_instantiation(ScopeNode const& scope_node) void Generator::begin_variable_scope() { start_boundary(BlockBoundaryType::LeaveLexicalEnvironment); - emit(Bytecode::Op::EnvironmentMode::Lexical); + emit(); } void Generator::end_variable_scope() @@ -104,7 +104,7 @@ void Generator::end_variable_scope() end_boundary(BlockBoundaryType::LeaveLexicalEnvironment); if (!m_current_basic_block->is_terminated()) { - emit(Bytecode::Op::EnvironmentMode::Lexical); + emit(); } } @@ -313,7 +313,7 @@ void Generator::generate_break() last_was_finally = false; break; case LeaveLexicalEnvironment: - emit(Bytecode::Op::EnvironmentMode::Lexical); + emit(); break; case Continue: break; @@ -341,7 +341,7 @@ void Generator::generate_break(DeprecatedFlyString const& break_label) emit(); last_was_finally = false; } else if (boundary == BlockBoundaryType::LeaveLexicalEnvironment) { - emit(Bytecode::Op::EnvironmentMode::Lexical); + emit(); } else if (boundary == BlockBoundaryType::ReturnToFinally) { auto& block = make_block(DeprecatedString::formatted("{}.break", current_block().name())); emit(Label { block }); @@ -381,7 +381,7 @@ void Generator::generate_continue() last_was_finally = false; break; case LeaveLexicalEnvironment: - emit(Bytecode::Op::EnvironmentMode::Lexical); + emit(); break; case Break: break; @@ -409,7 +409,7 @@ void Generator::generate_continue(DeprecatedFlyString const& continue_label) emit(); last_was_finally = false; } else if (boundary == BlockBoundaryType::LeaveLexicalEnvironment) { - emit(Bytecode::Op::EnvironmentMode::Lexical); + emit(); } else if (boundary == BlockBoundaryType::ReturnToFinally) { auto& block = make_block(DeprecatedString::formatted("{}.continue", current_block().name())); emit(Label { block }); diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.h b/Userland/Libraries/LibJS/Bytecode/Generator.h index e12077c21e..4434e61cf4 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.h +++ b/Userland/Libraries/LibJS/Bytecode/Generator.h @@ -164,7 +164,7 @@ public: emit(); break; case LeaveLexicalEnvironment: - emit(Bytecode::Op::EnvironmentMode::Lexical); + emit(); break; case Break: case Continue: diff --git a/Userland/Libraries/LibJS/Bytecode/Instruction.h b/Userland/Libraries/LibJS/Bytecode/Instruction.h index 3b36c4c364..bc6a1373e4 100644 --- a/Userland/Libraries/LibJS/Bytecode/Instruction.h +++ b/Userland/Libraries/LibJS/Bytecode/Instruction.h @@ -22,7 +22,7 @@ O(ConcatString) \ O(ContinuePendingUnwind) \ O(CopyObjectExcludingProperties) \ - O(CreateEnvironment) \ + O(CreateLexicalEnvironment) \ O(CreateVariable) \ O(Decrement) \ O(DeleteById) \ @@ -53,7 +53,7 @@ O(JumpConditional) \ O(JumpNullish) \ O(JumpUndefined) \ - O(LeaveEnvironment) \ + O(LeaveLexicalEnvironment) \ O(LeaveUnwindContext) \ O(LeftShift) \ O(LessThan) \ diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index 4534eb18a0..c33ecb421d 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -411,17 +411,14 @@ ThrowCompletionOr DeleteVariable::execute_impl(Bytecode::Interpreter& inte return {}; } -ThrowCompletionOr CreateEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const +ThrowCompletionOr CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const { auto make_and_swap_envs = [&](auto& old_environment) { GCPtr environment = new_declarative_environment(*old_environment).ptr(); swap(old_environment, environment); return environment; }; - if (m_mode == EnvironmentMode::Lexical) - interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment)); - else if (m_mode == EnvironmentMode::Var) - interpreter.saved_variable_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().variable_environment)); + interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment)); return {}; } @@ -831,12 +828,9 @@ ThrowCompletionOr ScheduleJump::execute_impl(Bytecode::Interpreter& interp return {}; } -ThrowCompletionOr LeaveEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const +ThrowCompletionOr LeaveLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const { - if (m_mode == EnvironmentMode::Lexical) - interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last(); - if (m_mode == EnvironmentMode::Var) - interpreter.vm().running_execution_context().variable_environment = interpreter.saved_variable_environment_stack().take_last(); + interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last(); return {}; } @@ -1191,12 +1185,9 @@ DeprecatedString DeleteVariable::to_deprecated_string_impl(Bytecode::Executable return DeprecatedString::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier)); } -DeprecatedString CreateEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const +DeprecatedString CreateLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const { - auto mode_string = m_mode == EnvironmentMode::Lexical - ? "Lexical" - : "Variable"; - return DeprecatedString::formatted("CreateEnvironment mode:{}", mode_string); + return "CreateLexicalEnvironment"sv; } DeprecatedString CreateVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const @@ -1344,12 +1335,9 @@ DeprecatedString ScheduleJump::to_deprecated_string_impl(Bytecode::Executable co return DeprecatedString::formatted("ScheduleJump {}", m_target); } -DeprecatedString LeaveEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const +DeprecatedString LeaveLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const { - auto mode_string = m_mode == EnvironmentMode::Lexical - ? "Lexical" - : "Variable"; - return DeprecatedString::formatted("LeaveEnvironment env:{}", mode_string); + return "LeaveLexicalEnvironment"sv; } DeprecatedString LeaveUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const diff --git a/Userland/Libraries/LibJS/Bytecode/Op.h b/Userland/Libraries/LibJS/Bytecode/Op.h index 5ee823e958..83fb61dfca 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.h +++ b/Userland/Libraries/LibJS/Bytecode/Op.h @@ -383,11 +383,10 @@ enum class EnvironmentMode { Var, }; -class CreateEnvironment final : public Instruction { +class CreateLexicalEnvironment final : public Instruction { public: - explicit CreateEnvironment(EnvironmentMode mode) - : Instruction(Type::CreateEnvironment) - , m_mode(mode) + explicit CreateLexicalEnvironment() + : Instruction(Type::CreateLexicalEnvironment) { } @@ -395,9 +394,6 @@ public: DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; void replace_references_impl(BasicBlock const&, BasicBlock const&) { } void replace_references_impl(Register, Register) { } - -private: - EnvironmentMode m_mode { EnvironmentMode::Lexical }; }; class EnterObjectEnvironment final : public Instruction { @@ -955,11 +951,10 @@ private: Label m_target; }; -class LeaveEnvironment final : public Instruction { +class LeaveLexicalEnvironment final : public Instruction { public: - LeaveEnvironment(EnvironmentMode mode) - : Instruction(Type::LeaveEnvironment) - , m_mode(mode) + LeaveLexicalEnvironment() + : Instruction(Type::LeaveLexicalEnvironment) { } @@ -967,9 +962,6 @@ public: DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; void replace_references_impl(BasicBlock const&, BasicBlock const&) { } void replace_references_impl(Register, Register) { } - -private: - EnvironmentMode m_mode { EnvironmentMode::Lexical }; }; class LeaveUnwindContext final : public Instruction {