Skip to content

Commit bb3e172

Browse files
committed
Refactored checklabel
1 parent d7cc252 commit bb3e172

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

lib/Parser/Parse.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10510,36 +10510,31 @@ ParseNodePtr Parser::ParseStatement()
1051010510
{
1051110511
this->GetScanner()->Scan();
1051210512
// Check if label is found within the current label id list.
10513-
for (LabelId* pLabelId = pLabelIdList; pLabelId; pLabelId = pLabelId->next)
10513+
auto checkLabelList = [&](LabelId* list, OpCode checkOp)
1051410514
{
10515-
if (pid == pLabelId->pid)
10516-
{
10517-
if (fnop == fnopContinue &&
10518-
!(ParseNode::Grfnop(m_pstmtCur->op) & fnop))
10519-
{
10520-
Error(ERRbadContinue);
10521-
}
10522-
goto LNeedTerminator;
10523-
}
10524-
}
10525-
for (pstmt = m_pstmtCur; pstmt; pstmt = pstmt->pstmtOuter)
10526-
{
10527-
LabelId* pLabelId;
10528-
for (pLabelId = pstmt->pLabelId; pLabelId; pLabelId = pLabelId->next)
10515+
for (LabelId* pLabelId = list; pLabelId; pLabelId = pLabelId->next)
1052910516
{
1053010517
if (pid == pLabelId->pid)
1053110518
{
1053210519
// Found the label. Make sure we can use it. We can
1053310520
// break out of any statement, but we can only
1053410521
// continue loops.
1053510522
if (fnop == fnopContinue &&
10536-
!(ParseNode::Grfnop(pstmt->op) & fnop))
10523+
!(ParseNode::Grfnop(checkOp) & fnop))
1053710524
{
1053810525
Error(ERRbadContinue);
1053910526
}
10540-
goto LNeedTerminator;
10527+
return true;
1054110528
}
1054210529
}
10530+
return false;
10531+
};
10532+
10533+
if (checkLabelList(pLabelIdList, m_pstmtCur->op)) goto LNeedTerminator;
10534+
10535+
for (pstmt = m_pstmtCur; pstmt; pstmt = pstmt->pstmtOuter)
10536+
{
10537+
if (checkLabelList(pstmt->pLabelId, pstmt->op)) goto LNeedTerminator;
1054310538
}
1054410539
}
1054510540
Error(ERRnoLabel);

test/Basics/Labels.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,13 @@ testSyntaxErrorWithContinueAfterLabel=
343343
`function fn() {
344344
L:
345345
continue L;
346-
}
347-
fn1()`
346+
}`
348347

349-
testReferenceErrorWithBreakAfterLabel=
348+
testNoLabelNotFoundWithBreakAfterLabel=
350349
`function fn() {
351350
L:
352351
break L;
353-
}
354-
fn1()`
352+
}`
355353

356354

357355
function testModuleScript(source, message, shouldFail = false) {
@@ -483,7 +481,7 @@ var tests = [
483481
assert.throws(() => eval(testRuntimeErrorWithDanglingStaticAfterLabel), ReferenceError, "Expected reference error from stranded static being used after label", "'static' is not defined")
484482
assert.doesNotThrow(() => eval(testNoSyntaxErrorWithDanglingStaticAfterLabel), "Expected no issue parsing since static is viewed as an identifier")
485483
assert.throws(() => eval(testSyntaxErrorWithContinueAfterLabel), SyntaxError, "Expected syntax error from having continue outside of loop", "Can't have 'continue' outside of loop")
486-
assert.throws(() => eval(testReferenceErrorWithBreakAfterLabel), ReferenceError, "Expected reference error from fn1 not being defined", "'fn1' is not defined")
484+
assert.doesNotThrow(() => eval(testNoLabelNotFoundWithBreakAfterLabel), "Expected no issue from finding label")
487485
}
488486
}
489487
];

0 commit comments

Comments
 (0)