1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:38:11 +00:00

LibJS: Implement basic execution of "switch" statements

The "break" keyword now unwinds to the nearest ScopeType::Breakable.
There's no support for break labels yet, but we'll get there too.
This commit is contained in:
Andreas Kling 2020-03-29 14:34:25 +02:00
parent 1923051c5b
commit 2285f84596
6 changed files with 57 additions and 2 deletions

View file

@ -873,7 +873,31 @@ Value ThrowStatement::execute(Interpreter& interpreter) const
Value SwitchStatement::execute(Interpreter& interpreter) const
{
(void)interpreter;
auto discriminant_result = m_discriminant->execute(interpreter);
if (interpreter.exception())
return {};
bool falling_through = false;
for (auto& switch_case : m_cases) {
if (!falling_through && switch_case.test()) {
auto test_result = switch_case.test()->execute(interpreter);
if (interpreter.exception())
return {};
if (!eq(discriminant_result, test_result).to_boolean())
continue;
}
falling_through = true;
for (auto& statement : switch_case.consequent()) {
statement.execute(interpreter);
if (interpreter.exception())
return {};
if (interpreter.should_unwind())
return {};
}
}
return {};
}
@ -885,7 +909,7 @@ Value SwitchCase::execute(Interpreter& interpreter) const
Value BreakStatement::execute(Interpreter& interpreter) const
{
(void)interpreter;
interpreter.unwind(ScopeType::Breakable);
return {};
}