14.12 The switch
Statement
Syntax
14.12.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
LexicallyDeclaredNames ofCaseBlock contains any duplicate entries. -
It is a Syntax Error if any element of the
LexicallyDeclaredNames ofCaseBlock also occurs in theVarDeclaredNames ofCaseBlock .
14.12.2 Runtime Semantics: CaseBlockEvaluation
The
- Return
undefined .
- Let V be
undefined . - Let A be the
List ofCaseClause items inCaseClauses , in source text order. - Let found be
false . - For each
CaseClause C of A, do- If found is
false , then- Set found to ?
CaseClauseIsSelected (C, input).
- Set found to ?
- If found is
true , then- Let R be
Completion (Evaluation of C). - If R.[[Value]] is not
empty , set V to R.[[Value]]. - If R is an
abrupt completion , return ?UpdateEmpty (R, V).
- Let R be
- If found is
- Return V.
- Let V be
undefined . - If the first
CaseClauses is present, then- Let A be the
List ofCaseClause items in the firstCaseClauses , in source text order.
- Let A be the
- Else,
- Let A be a new empty
List .
- Let A be a new empty
- Let found be
false . - For each
CaseClause C of A, do- If found is
false , then- Set found to ?
CaseClauseIsSelected (C, input).
- Set found to ?
- If found is
true , then- Let R be
Completion (Evaluation of C). - If R.[[Value]] is not
empty , set V to R.[[Value]]. - If R is an
abrupt completion , return ?UpdateEmpty (R, V).
- Let R be
- If found is
- Let foundInB be
false . - If the second
CaseClauses is present, then- Let B be the
List ofCaseClause items in the secondCaseClauses , in source text order.
- Let B be the
- Else,
- Let B be a new empty
List .
- Let B be a new empty
- If found is
false , then- For each
CaseClause C of B, do- If foundInB is
false , then- Set foundInB to ?
CaseClauseIsSelected (C, input).
- Set foundInB to ?
- If foundInB is
true , then- Let R be
Completion (Evaluation ofCaseClause C). - If R.[[Value]] is not
empty , set V to R.[[Value]]. - If R is an
abrupt completion , return ?UpdateEmpty (R, V).
- Let R be
- If foundInB is
- For each
- If foundInB is
true , return V. - Let defaultR be
Completion (Evaluation ofDefaultClause ). - If defaultR.[[Value]] is not
empty , set V to defaultR.[[Value]]. - If defaultR is an
abrupt completion , return ?UpdateEmpty (defaultR, V). - NOTE: The following is another complete iteration of the second
CaseClauses . - For each
CaseClause C of B, do- Let R be
Completion (Evaluation ofCaseClause C). - If R.[[Value]] is not
empty , set V to R.[[Value]]. - If R is an
abrupt completion , return ?UpdateEmpty (R, V).
- Let R be
- Return V.
14.12.3 CaseClauseIsSelected ( C, input )
The abstract operation CaseClauseIsSelected takes arguments C (a
Assert : C is an instance of the production .CaseClause : case Expression : StatementList opt - Let exprRef be ?
Evaluation of theExpression of C. - Let clauseSelector be ?
GetValue (exprRef). - Return
IsStrictlyEqual (input, clauseSelector).
This operation does not execute C's
14.12.4 Runtime Semantics: Evaluation
- Let exprRef be ?
Evaluation ofExpression . - Let switchValue be ?
GetValue (exprRef). - Let oldEnv be the
running execution context 's LexicalEnvironment. - Let blockEnv be
NewDeclarativeEnvironment (oldEnv). - Perform
BlockDeclarationInstantiation (CaseBlock , blockEnv). - Set the
running execution context 's LexicalEnvironment to blockEnv. - Let R be
Completion (CaseBlockEvaluation ofCaseBlock with argument switchValue). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return R.
No matter how control leaves the
- Return
empty .
- Return ?
Evaluation ofStatementList .
- Return
empty .
- Return ?
Evaluation ofStatementList .