mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:27:45 +00:00
LibJS: Elide empty declarative environments inside switch statements
Most switch statements don't have any lexically scoped declarations, so let's avoid allocating an environment in the common case where we don't have to.
This commit is contained in:
parent
9d06448bc7
commit
eafbf372d0
1 changed files with 8 additions and 7 deletions
|
@ -2856,14 +2856,15 @@ Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_ob
|
|||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
auto* old_environment = interpreter.lexical_environment();
|
||||
ScopeGuard restore_environment = [&] {
|
||||
interpreter.vm().running_execution_context().lexical_environment = old_environment;
|
||||
};
|
||||
auto* block_environment = new_declarative_environment(*old_environment);
|
||||
block_declaration_instantiation(global_object, block_environment);
|
||||
// Optimization: Avoid creating a lexical environment if there are no lexical declarations.
|
||||
Optional<TemporaryChange<Environment*>> lexical_environment_changer;
|
||||
if (has_lexical_declarations()) {
|
||||
auto* old_environment = interpreter.lexical_environment();
|
||||
auto* block_environment = new_declarative_environment(*old_environment);
|
||||
block_declaration_instantiation(global_object, block_environment);
|
||||
lexical_environment_changer.emplace(interpreter.vm().running_execution_context().lexical_environment, block_environment);
|
||||
}
|
||||
|
||||
interpreter.vm().running_execution_context().lexical_environment = block_environment;
|
||||
Optional<size_t> first_passing_case;
|
||||
for (size_t i = 0; i < m_cases.size(); ++i) {
|
||||
auto& switch_case = m_cases[i];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue