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

LibJS: Allow statements to have multiple labels

This is a curious thing that occurs more often than you'd think in
minified JavaScript:

    a: b: c: for (...) { ... break b; ... }
This commit is contained in:
Andreas Kling 2021-09-26 18:16:06 +02:00
parent ababcc5725
commit 3252d984ae
6 changed files with 31 additions and 21 deletions

View file

@ -395,9 +395,9 @@ Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_obj
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_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
} else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) { } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
break; break;
} else { } else {
@ -421,9 +421,9 @@ Value DoWhileStatement::execute(Interpreter& interpreter, GlobalObject& global_o
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_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
} else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) { } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
break; break;
} else { } else {
@ -477,9 +477,9 @@ Value ForStatement::execute(Interpreter& interpreter, GlobalObject& global_objec
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_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
} else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) { } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
break; break;
} else { } else {
@ -498,9 +498,9 @@ Value ForStatement::execute(Interpreter& interpreter, GlobalObject& global_objec
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_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
} else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) { } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
break; break;
} else { } else {
@ -570,9 +570,9 @@ Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_obj
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_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
} else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) { } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
break; break;
} else { } else {
@ -613,9 +613,9 @@ Value ForOfStatement::execute(Interpreter& interpreter, GlobalObject& global_obj
if (interpreter.exception()) if (interpreter.exception())
return IterationDecision::Break; return IterationDecision::Break;
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_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
} else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) { } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
return IterationDecision::Break; return IterationDecision::Break;
} else { } else {
@ -2429,10 +2429,10 @@ Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_ob
if (interpreter.exception()) if (interpreter.exception())
return Value {}; return Value {};
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_labels)) {
// 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 last_value; return last_value;
} else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) { } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_labels)) {
interpreter.vm().stop_unwind(); interpreter.vm().stop_unwind();
return last_value; return last_value;
} else { } else {

View file

@ -80,11 +80,12 @@ public:
{ {
} }
FlyString const& label() const { return m_label; } HashTable<FlyString> const& labels() const { return m_labels; }
void set_label(FlyString string) { m_label = move(string); } void add_label(FlyString label) { m_labels.set(move(label)); }
bool has_label(FlyString const& label) const { return m_labels.contains(label); }
protected: protected:
FlyString m_label; HashTable<FlyString> m_labels;
}; };
class EmptyStatement final : public Statement { class EmptyStatement final : public Statement {

View file

@ -193,7 +193,7 @@ Value Interpreter::execute_statement(GlobalObject& global_object, const Statemen
if (!value.is_empty()) if (!value.is_empty())
last_value = value; last_value = value;
if (vm().should_unwind()) { if (vm().should_unwind()) {
if (!block.label().is_null() && vm().should_unwind_until(ScopeType::Breakable, block.label())) if (!block.labels().is_empty() && vm().should_unwind_until(ScopeType::Breakable, block.labels()))
vm().stop_unwind(); vm().stop_unwind();
break; break;
} }

View file

@ -621,7 +621,7 @@ RefPtr<Statement> Parser::try_parse_labelled_statement(AllowLabelledFunction all
m_state.labels_in_scope.remove(identifier); m_state.labels_in_scope.remove(identifier);
labelled_statement->set_label(identifier); labelled_statement->add_label(identifier);
state_rollback_guard.disarm(); state_rollback_guard.disarm();
discard_saved_state(); discard_saved_state();
return labelled_statement.release_nonnull(); return labelled_statement.release_nonnull();

View file

@ -193,11 +193,11 @@ public:
m_unwind_until = ScopeType::None; m_unwind_until = ScopeType::None;
m_unwind_until_label = {}; m_unwind_until_label = {};
} }
bool should_unwind_until(ScopeType type, FlyString const& label) const bool should_unwind_until(ScopeType type, HashTable<FlyString> const& labels) const
{ {
if (m_unwind_until_label.is_null()) if (m_unwind_until_label.is_null())
return m_unwind_until == type; return m_unwind_until == type;
return m_unwind_until == type && m_unwind_until_label == label; return m_unwind_until == type && labels.contains(m_unwind_until_label);
} }
bool should_unwind() const { return m_unwind_until != ScopeType::None; } bool should_unwind() const { return m_unwind_until != ScopeType::None; }

View file

@ -0,0 +1,9 @@
test("basic support for statement with many labels", () => {
function foo() {
a: b: c: for (;;) {
break b;
}
return 1;
}
expect(foo()).toBe(1);
});