1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:47:35 +00:00

LibJS: Make SwitchStatement::execute() return undefined for empty blocks

Previously SwitchStatement::execute() would return <empty> when hitting
break, continue or empty consequent block. This was not in line with
the standard.
This commit is contained in:
Marcin Gasperowicz 2021-06-06 20:58:10 +02:00 committed by Linus Groh
parent 949ceedaed
commit 624ceec04f

View file

@ -2075,6 +2075,7 @@ Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_ob
return {}; return {};
bool falling_through = false; bool falling_through = false;
auto last_value = js_undefined();
for (auto& switch_case : m_cases) { for (auto& switch_case : m_cases) {
if (!falling_through && switch_case.test()) { if (!falling_through && switch_case.test()) {
@ -2087,24 +2088,25 @@ Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_ob
falling_through = true; falling_through = true;
for (auto& statement : switch_case.consequent()) { for (auto& statement : switch_case.consequent()) {
auto last_value = statement.execute(interpreter, global_object); auto value = statement.execute(interpreter, global_object);
if (!value.is_empty())
last_value = value;
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
if (interpreter.vm().should_unwind()) { if (interpreter.vm().should_unwind()) {
if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) { if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
// No stop_unwind(), the outer loop will handle that - we just need to break out of the switch/case. // No stop_unwind(), the outer loop will handle that - we just need to break out of the switch/case.
return {}; return last_value;
} else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) { } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
return {}; return last_value;
} else { } else {
return last_value; return last_value;
} }
} }
} }
} }
return last_value;
return js_undefined();
} }
Value SwitchCase::execute(Interpreter& interpreter, GlobalObject&) const Value SwitchCase::execute(Interpreter& interpreter, GlobalObject&) const