| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 --harmony-sloppy | |
| 6 | |
| 7 // Repurposing the strict mode 'eval' and 'arguments' tests to test for correct | |
| 8 // behaviour of 'undefined' as an identifier in strong mode. | |
| 9 "use strict"; | |
| 10 | |
| 11 function CheckStrongMode(code) { | |
| 12 let strictContexts = [ | |
| 13 ["'use strict';", ""], | |
| 14 ["function outer() { 'use strict';", "}"], | |
| 15 ["function outer() { 'use strict'; function inner() {", "}}"], | |
| 16 ["class C { m() {", "} }"] | |
| 17 ] | |
| 18 let strongContexts = [ | |
| 19 ["'use strong';", ""], | |
| 20 ["function outer() { 'use strong';", "}"], | |
| 21 ["function outer() { 'use strong'; function inner() {", "}}"], | |
| 22 ["class C { m() { 'use strong';", "} }"] | |
| 23 ] | |
| 24 | |
| 25 for (let context of strictContexts) { | |
| 26 assertThrows(context[0] + code + context[1] + "; throw new TypeError();", | |
| 27 TypeError); | |
| 28 } | |
| 29 for (let context of strongContexts) { | |
| 30 assertThrows(context[0] + code + context[1], SyntaxError); | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 // Binding 'undefined' | |
| 35 CheckStrongMode("var undefined;"); | |
| 36 CheckStrongMode("let undefined;"); | |
| 37 CheckStrongMode("var undefined = 0;"); | |
| 38 CheckStrongMode("let undefined = 0;"); | |
| 39 CheckStrongMode("const undefined = 0;"); | |
| 40 CheckStrongMode("var x, y = 0, undefined;"); | |
| 41 CheckStrongMode("let x, y = 0, undefined;"); | |
| 42 | |
| 43 // Function identifier is 'undefined' | |
| 44 // Function declaration | |
| 45 CheckStrongMode("function undefined() {}"); | |
| 46 assertThrows("function undefined() {'use strong';}", SyntaxError); | |
| 47 | |
| 48 // Generator function | |
| 49 CheckStrongMode("function* undefined() {}"); | |
| 50 assertThrows("function* undefined() {'use strong';}", SyntaxError); | |
| 51 | |
| 52 // Function expression | |
| 53 CheckStrongMode("(function undefined() {});"); | |
| 54 assertThrows("(function undefined() {'use strong';});", SyntaxError); | |
| 55 CheckStrongMode("{foo: (function undefined(){})};"); | |
| 56 assertThrows("{foo: (function undefined(){'use strong';})};", SyntaxError); | |
| 57 | |
| 58 //Generator function expression | |
| 59 CheckStrongMode("(function* undefined() {})"); | |
| 60 assertThrows("(function* undefined() {'use strong';})", SyntaxError); | |
| 61 CheckStrongMode("{foo: (function* undefined(){})};"); | |
| 62 assertThrows("{foo: (function* undefined(){'use strong';})};", SyntaxError); | |
| 63 | |
| 64 // Function parameter named 'undefined' | |
| 65 // Function declaration | |
| 66 CheckStrongMode("function foo(a, b, undefined, c, d) {}"); | |
| 67 assertThrows("function foo(a, b, undefined, c, d) {'use strong';}", | |
| 68 SyntaxError); | |
| 69 | |
| 70 // Generator function declaration | |
| 71 CheckStrongMode("function* foo(a, b, undefined, c, d) {}"); | |
| 72 assertThrows("function* foo(a, b, undefined, c, d) {'use strong';}", | |
| 73 SyntaxError); | |
| 74 | |
| 75 // Function expression | |
| 76 CheckStrongMode("(function foo(a, b, undefined, c, d) {});"); | |
| 77 assertThrows("(function foo(a, b, undefined, c, d) {'use strong';})", | |
| 78 SyntaxError); | |
| 79 CheckStrongMode("{foo: (function foo(a, b, undefined, c, d) {})};"); | |
| 80 assertThrows("{foo: (function foo(a, b, undefined, c, d) {'use strong';})};", | |
| 81 SyntaxError); | |
| 82 | |
| 83 // Generator function expression | |
| 84 CheckStrongMode("(function* foo(a, b, undefined, c, d) {});"); | |
| 85 assertThrows("(function* foo(a, b, undefined, c, d) {'use strong';})", | |
| 86 SyntaxError); | |
| 87 CheckStrongMode("{foo: (function* foo(a, b, undefined, c, d) {})};"); | |
| 88 assertThrows("{foo: (function* foo(a, b, undefined, c, d) {'use strong';})};", | |
| 89 SyntaxError); | |
| 90 | |
| 91 // Method parameter named 'undefined' | |
| 92 // Class method | |
| 93 CheckStrongMode("class C { foo(a, b, undefined, c, d) {} }"); | |
| 94 assertThrows("class C { foo(a, b, undefined, c, d) {'use strong';} }", | |
| 95 SyntaxError); | |
| 96 | |
| 97 //Class generator method | |
| 98 CheckStrongMode("class C { *foo(a, b, undefined, c, d) {} }"); | |
| 99 assertThrows("class C { *foo(a, b, undefined, c, d) {'use strong';} }", | |
| 100 SyntaxError); | |
| 101 | |
| 102 //Object literal method | |
| 103 CheckStrongMode("({ foo(a, b, undefined, c, d) {} });"); | |
| 104 assertThrows("({ foo(a, b, undefined, c, d) {'use strong';} });", SyntaxError); | |
| 105 | |
| 106 //Object literal generator method | |
| 107 CheckStrongMode("({ *foo(a, b, undefined, c, d) {} });"); | |
| 108 assertThrows("({ *foo(a, b, undefined, c, d) {'use strong';} });", SyntaxError); | |
| 109 | |
| 110 // Class declaration named 'undefined' | |
| 111 CheckStrongMode("class undefined {}"); | |
| 112 assertThrows("class undefined {'use strong'}", SyntaxError); | |
| 113 | |
| 114 // Class expression named 'undefined' | |
| 115 CheckStrongMode("(class undefined {});"); | |
| 116 assertThrows("(class undefined {'use strong'});", SyntaxError); | |
| 117 | |
| 118 // Binding/assigning to 'undefined' in for | |
| 119 CheckStrongMode("for(undefined = 0;false;);"); | |
| 120 CheckStrongMode("for(var undefined = 0;false;);"); | |
| 121 CheckStrongMode("for(let undefined = 0;false;);"); | |
| 122 CheckStrongMode("for(const undefined = 0;false;);"); | |
| 123 | |
| 124 // Binding/assigning to 'undefined' in for-in | |
| 125 CheckStrongMode("for(undefined in {});"); | |
| 126 CheckStrongMode("for(var undefined in {});"); | |
| 127 CheckStrongMode("for(let undefined in {});"); | |
| 128 CheckStrongMode("for(const undefined in {});"); | |
| 129 | |
| 130 // Binding/assigning to 'undefined' in for-of | |
| 131 CheckStrongMode("for(undefined of []);"); | |
| 132 CheckStrongMode("for(var undefined of []);"); | |
| 133 CheckStrongMode("for(let undefined of []);"); | |
| 134 CheckStrongMode("for(const undefined of []);"); | |
| 135 | |
| 136 // Property accessor parameter named 'undefined'. | |
| 137 CheckStrongMode("let o = { set foo(undefined) {} }"); | |
| 138 assertThrows("let o = { set foo(undefined) {'use strong';} }", SyntaxError); | |
| 139 | |
| 140 // catch(undefined) | |
| 141 CheckStrongMode("try {} catch(undefined) {};"); | |
| 142 | |
| 143 // Assignment to undefined | |
| 144 CheckStrongMode("undefined = 0;"); | |
| 145 CheckStrongMode("print(undefined = 0);"); | |
| 146 CheckStrongMode("let x = undefined = 0;"); | |
| 147 | |
| 148 // Compound assignment to undefined | |
| 149 CheckStrongMode("undefined *= 0;"); | |
| 150 CheckStrongMode("undefined /= 0;"); | |
| 151 CheckStrongMode("print(undefined %= 0);"); | |
| 152 CheckStrongMode("let x = undefined += 0;"); | |
| 153 CheckStrongMode("let x = undefined -= 0;"); | |
| 154 CheckStrongMode("undefined <<= 0;"); | |
| 155 CheckStrongMode("undefined >>= 0;"); | |
| 156 CheckStrongMode("print(undefined >>>= 0);"); | |
| 157 CheckStrongMode("print(undefined &= 0);"); | |
| 158 CheckStrongMode("let x = undefined ^= 0;"); | |
| 159 CheckStrongMode("let x = undefined |= 0;"); | |
| 160 | |
| 161 // Postfix increment with undefined | |
| 162 CheckStrongMode("undefined++;"); | |
| 163 CheckStrongMode("print(undefined++);"); | |
| 164 CheckStrongMode("let x = undefined++;"); | |
| 165 | |
| 166 // Postfix decrement with undefined | |
| 167 CheckStrongMode("undefined--;"); | |
| 168 CheckStrongMode("print(undefined--);"); | |
| 169 CheckStrongMode("let x = undefined--;"); | |
| 170 | |
| 171 // Prefix increment with undefined | |
| 172 CheckStrongMode("++undefined;"); | |
| 173 CheckStrongMode("print(++undefined);"); | |
| 174 CheckStrongMode("let x = ++undefined;"); | |
| 175 | |
| 176 // Prefix decrement with undefined | |
| 177 CheckStrongMode("--undefined;"); | |
| 178 CheckStrongMode("print(--undefined);"); | |
| 179 CheckStrongMode("let x = --undefined;"); | |
| 180 | |
| 181 // Function constructor: 'undefined' parameter name | |
| 182 assertDoesNotThrow(function() { | |
| 183 Function("undefined", ""); | |
| 184 }); | |
| 185 assertThrows(function() { | |
| 186 Function("undefined", "'use strong';"); | |
| 187 }, SyntaxError); | |
| 188 | |
| 189 // Arrow functions with undefined parameters | |
| 190 CheckStrongMode("(undefined => {return});"); | |
| 191 assertThrows("(undefined => {'use strong';});"); | |
| 192 | |
| 193 CheckStrongMode("((undefined, b, c) => {return});"); | |
| 194 assertThrows("((undefined, b, c) => {'use strong';});"); | |
| 195 | |
| 196 CheckStrongMode("((a, undefined, c) => {return});"); | |
| 197 assertThrows("((a, undefined, c) => {'use strong';});"); | |
| 198 | |
| 199 CheckStrongMode("((a, b, undefined) => {return});"); | |
| 200 assertThrows("((a, b, undefined) => {'use strong';});"); | |
| OLD | NEW |