aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/strong/switch.js
blob: 96ee1eec795a1c4fbc95c74a866599cf4d100646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --strong-mode

"use strict";

function CheckSwitch() {
  let jumpStatements = [
    "break; ",
    "continue; ",
    "break foo; ",
    "continue foo; ",
    "return; ",
    "throw new TypeError(); ",
    "if(1) break; else continue; ",
    "if(1) {1+1; {break;}} else continue; "
  ]

  let otherStatements = [
    "null; ",
    "1+1; ",
    "{} ",
    "for(;false;) {break;} ",
    "for(;false;) {1+1; {throw new TypeError();}} ",
    "(function(){return});",
    "(function(){throw new TypeError();});",
    "{break; 1+1;} ",
    "if(1) break; ",
    "if(1) break; else 1+1; ",
    "if(1) 1+1; else break; ",
  ]

  let successContexts = [
    ["switch(1) {case 1: ", "case 2: }"],
    ["switch(1) {case 1: case 2: ", "default: }"],
    ["switch(1) {case 1: case 2: ", "default: {}}"],
    ["switch(1) {case 1: case 2: ", "default: 1+1}"],
    ["switch(1) {case 1: break; case 2: ", "default: }"],
    ["switch(1) {case 1: case 2: break; case 3: ", "case 4: default: }"],
    ["switch(1) {case 1: if(1) break; else {", "} default: break;}"]
  ]

  let strongThrowContexts = [
    ["switch(1) {case 1: 1+1; case 2: ", "}"],
    ["switch(1) {case 1: bar: break foo; case 2: ", "}"],
    ["switch(1) {case 1: bar:", " case 2: }"],
    ["switch(1) {case 1: bar:{ ", "} case 2: }"],
    ["switch(1) {case 1: bar:{ ", "} default: break;}"],
    ["switch(1) {case 1: { bar:{ { ", "} } } default: break;}"],
    ["switch(1) {case 1: { { { ", "} 1+1;} } default: break;}"],
    ["switch(1) {case 1: if(1) {", "} default: break;}"],
    ["switch(1) {case 1: bar:if(1) break; else {", "} default: break;}"]
  ]

  let sloppy_wrap = ["function f() { foo:for(;;) {", "}}"];
  let strong_wrap = ["function f() { 'use strong'; foo:for(;;) {", "}}"];

  for (let context of successContexts) {
    let sloppy_prefix = sloppy_wrap[0] + context[0];
    let sloppy_suffix = context[1] + sloppy_wrap[1];
    let strong_prefix = strong_wrap[0] + context[0];
    let strong_suffix = context[1] + strong_wrap[1];

    for (let code of jumpStatements) {
      assertDoesNotThrow(strong_wrap[0] + "switch(1) {case 1: " + code + "}}}");
      assertDoesNotThrow(strong_prefix + code + strong_suffix);
      assertDoesNotThrow(strong_prefix + "{ 1+1; " + code + "}" +
                         strong_suffix);
      assertDoesNotThrow(strong_prefix + "{ 1+1; { 1+1; " + code + "}}" +
                         strong_suffix);
      assertDoesNotThrow(strong_prefix + "if(1) " + code + "else break;" +
                         strong_suffix);
      assertDoesNotThrow(strong_prefix + "if(1) " + code +
                         "else if (1) break; else " + code + strong_suffix);
    }
    for (let code of otherStatements) {
        assertDoesNotThrow(sloppy_prefix + code + sloppy_suffix);
        assertThrows(strong_prefix + code + strong_suffix, SyntaxError);
    }
  }

  for (let context of strongThrowContexts) {
    let sloppy_prefix = sloppy_wrap[0] + context[0];
    let sloppy_suffix = context[1] + sloppy_wrap[1];
    let strong_prefix = strong_wrap[0] + context[0];
    let strong_suffix = context[1] + strong_wrap[1];

    for (let code of jumpStatements.concat(otherStatements)) {
      assertDoesNotThrow(sloppy_prefix + code + sloppy_suffix);
      assertThrows(strong_prefix + code + strong_suffix, SyntaxError);
    }
  }

  for (let code of otherStatements) {
    assertDoesNotThrow("switch(1) {default: " + code + "}");
    assertDoesNotThrow("switch(1) {case 1: " + code + "}");
    assertDoesNotThrow("switch(1) {case 1: default: " + code + "}");
    assertDoesNotThrow("switch(1) {case 1: break; default: " + code + "}");
    assertDoesNotThrow("switch(1) {case 1: " + code + "break; default: }");
  }
}

CheckSwitch();

assertDoesNotThrow("'use strong'; switch(1) {}");
assertDoesNotThrow("'use strong'; switch(1) {case 1:}");
assertDoesNotThrow("'use strong'; switch(1) {default:}");
assertDoesNotThrow("'use strong'; switch(1) {case 1: case 2: default:}");