• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --strong-mode
6
7"use strict";
8
9function CheckSwitch() {
10  let jumpStatements = [
11    "break; ",
12    "continue; ",
13    "break foo; ",
14    "continue foo; ",
15    "return; ",
16    "throw new TypeError(); ",
17    "if(1) break; else continue; ",
18    "if(1) {1+1; {break;}} else continue; "
19  ]
20
21  let otherStatements = [
22    "null; ",
23    "1+1; ",
24    "{} ",
25    "for(;false;) {break;} ",
26    "for(;false;) {1+1; {throw new TypeError();}} ",
27    "(function(){return});",
28    "(function(){throw new TypeError();});",
29    "{break; 1+1;} ",
30    "if(1) break; ",
31    "if(1) break; else 1+1; ",
32    "if(1) 1+1; else break; ",
33  ]
34
35  let successContexts = [
36    ["switch(1) {case 1: ", "case 2: }"],
37    ["switch(1) {case 1: case 2: ", "default: }"],
38    ["switch(1) {case 1: case 2: ", "default: {}}"],
39    ["switch(1) {case 1: case 2: ", "default: 1+1}"],
40    ["switch(1) {case 1: break; case 2: ", "default: }"],
41    ["switch(1) {case 1: case 2: break; case 3: ", "case 4: default: }"],
42    ["switch(1) {case 1: if(1) break; else {", "} default: break;}"]
43  ]
44
45  let strongThrowContexts = [
46    ["switch(1) {case 1: 1+1; case 2: ", "}"],
47    ["switch(1) {case 1: bar: break foo; case 2: ", "}"],
48    ["switch(1) {case 1: bar:", " case 2: }"],
49    ["switch(1) {case 1: bar:{ ", "} case 2: }"],
50    ["switch(1) {case 1: bar:{ ", "} default: break;}"],
51    ["switch(1) {case 1: { bar:{ { ", "} } } default: break;}"],
52    ["switch(1) {case 1: { { { ", "} 1+1;} } default: break;}"],
53    ["switch(1) {case 1: if(1) {", "} default: break;}"],
54    ["switch(1) {case 1: bar:if(1) break; else {", "} default: break;}"]
55  ]
56
57  let sloppy_wrap = ["function f() { foo:for(;;) {", "}}"];
58  let strong_wrap = ["function f() { 'use strong'; foo:for(;;) {", "}}"];
59
60  for (let context of successContexts) {
61    let sloppy_prefix = sloppy_wrap[0] + context[0];
62    let sloppy_suffix = context[1] + sloppy_wrap[1];
63    let strong_prefix = strong_wrap[0] + context[0];
64    let strong_suffix = context[1] + strong_wrap[1];
65
66    for (let code of jumpStatements) {
67      assertDoesNotThrow(strong_wrap[0] + "switch(1) {case 1: " + code + "}}}");
68      assertDoesNotThrow(strong_prefix + code + strong_suffix);
69      assertDoesNotThrow(strong_prefix + "{ 1+1; " + code + "}" +
70                         strong_suffix);
71      assertDoesNotThrow(strong_prefix + "{ 1+1; { 1+1; " + code + "}}" +
72                         strong_suffix);
73      assertDoesNotThrow(strong_prefix + "if(1) " + code + "else break;" +
74                         strong_suffix);
75      assertDoesNotThrow(strong_prefix + "if(1) " + code +
76                         "else if (1) break; else " + code + strong_suffix);
77    }
78    for (let code of otherStatements) {
79        assertDoesNotThrow(sloppy_prefix + code + sloppy_suffix);
80        assertThrows(strong_prefix + code + strong_suffix, SyntaxError);
81    }
82  }
83
84  for (let context of strongThrowContexts) {
85    let sloppy_prefix = sloppy_wrap[0] + context[0];
86    let sloppy_suffix = context[1] + sloppy_wrap[1];
87    let strong_prefix = strong_wrap[0] + context[0];
88    let strong_suffix = context[1] + strong_wrap[1];
89
90    for (let code of jumpStatements.concat(otherStatements)) {
91      assertDoesNotThrow(sloppy_prefix + code + sloppy_suffix);
92      assertThrows(strong_prefix + code + strong_suffix, SyntaxError);
93    }
94  }
95
96  for (let code of otherStatements) {
97    assertDoesNotThrow("switch(1) {default: " + code + "}");
98    assertDoesNotThrow("switch(1) {case 1: " + code + "}");
99    assertDoesNotThrow("switch(1) {case 1: default: " + code + "}");
100    assertDoesNotThrow("switch(1) {case 1: break; default: " + code + "}");
101    assertDoesNotThrow("switch(1) {case 1: " + code + "break; default: }");
102  }
103}
104
105CheckSwitch();
106
107assertDoesNotThrow("'use strong'; switch(1) {}");
108assertDoesNotThrow("'use strong'; switch(1) {case 1:}");
109assertDoesNotThrow("'use strong'; switch(1) {default:}");
110assertDoesNotThrow("'use strong'; switch(1) {case 1: case 2: default:}");
111