1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 07:05:08 +00:00

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.
This commit is contained in:
Andreas Kling 2023-06-16 16:43:24 +02:00
parent 12ce0789da
commit dbfe1311ef
6 changed files with 25 additions and 45 deletions

View file

@ -2226,7 +2226,7 @@ Bytecode::CodeGenerationErrorOr<void> WithStatement::generate_bytecode(Bytecode:
generator.end_boundary(Bytecode::Generator::BlockBoundaryType::LeaveLexicalEnvironment);
if (!generator.is_current_block_terminated())
generator.emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
generator.emit<Bytecode::Op::LeaveLexicalEnvironment>();
return {};
}
@ -2282,7 +2282,7 @@ static Bytecode::CodeGenerationErrorOr<ForInOfHeadEvaluationResult> for_in_of_he
generator.emit<Bytecode::Op::CreateVariable>(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:

View file

@ -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::CreateEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
emit<Bytecode::Op::CreateLexicalEnvironment>();
}
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::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
emit<Bytecode::Op::LeaveLexicalEnvironment>();
}
}
@ -313,7 +313,7 @@ void Generator::generate_break()
last_was_finally = false;
break;
case LeaveLexicalEnvironment:
emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
emit<Bytecode::Op::LeaveLexicalEnvironment>();
break;
case Continue:
break;
@ -341,7 +341,7 @@ void Generator::generate_break(DeprecatedFlyString const& break_label)
emit<Bytecode::Op::LeaveUnwindContext>();
last_was_finally = false;
} else if (boundary == BlockBoundaryType::LeaveLexicalEnvironment) {
emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
emit<Bytecode::Op::LeaveLexicalEnvironment>();
} else if (boundary == BlockBoundaryType::ReturnToFinally) {
auto& block = make_block(DeprecatedString::formatted("{}.break", current_block().name()));
emit<Op::ScheduleJump>(Label { block });
@ -381,7 +381,7 @@ void Generator::generate_continue()
last_was_finally = false;
break;
case LeaveLexicalEnvironment:
emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
emit<Bytecode::Op::LeaveLexicalEnvironment>();
break;
case Break:
break;
@ -409,7 +409,7 @@ void Generator::generate_continue(DeprecatedFlyString const& continue_label)
emit<Bytecode::Op::LeaveUnwindContext>();
last_was_finally = false;
} else if (boundary == BlockBoundaryType::LeaveLexicalEnvironment) {
emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
emit<Bytecode::Op::LeaveLexicalEnvironment>();
} else if (boundary == BlockBoundaryType::ReturnToFinally) {
auto& block = make_block(DeprecatedString::formatted("{}.continue", current_block().name()));
emit<Op::ScheduleJump>(Label { block });

View file

@ -164,7 +164,7 @@ public:
emit<Bytecode::Op::LeaveUnwindContext>();
break;
case LeaveLexicalEnvironment:
emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
emit<Bytecode::Op::LeaveLexicalEnvironment>();
break;
case Break:
case Continue:

View file

@ -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) \

View file

@ -411,17 +411,14 @@ ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& inte
return {};
}
ThrowCompletionOr<void> CreateEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
ThrowCompletionOr<void> CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto make_and_swap_envs = [&](auto& old_environment) {
GCPtr<Environment> 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));
return {};
}
@ -831,12 +828,9 @@ ThrowCompletionOr<void> ScheduleJump::execute_impl(Bytecode::Interpreter& interp
return {};
}
ThrowCompletionOr<void> LeaveEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
ThrowCompletionOr<void> 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();
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

View file

@ -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 {