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 --harmony-arrow-functions |
| 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 // Arrow function expression with 'undefined' param |
| 111 // TODO(conradw): Checking arrow function heads is hard to modify just now |
| 112 // CheckStrongMode("(undefined => {})"); |
| 113 // assertThrows("(undefined => {'use strong';})"); |
| 114 |
| 115 // Class declaration named 'undefined' |
| 116 CheckStrongMode("class undefined {}"); |
| 117 assertThrows("class undefined {'use strong'}", SyntaxError); |
| 118 |
| 119 // Class expression named 'undefined' |
| 120 CheckStrongMode("(class undefined {});"); |
| 121 assertThrows("(class undefined {'use strong'});", SyntaxError); |
| 122 |
| 123 // Binding/assigning to 'undefined' in for |
| 124 CheckStrongMode("for(undefined = 0;false;);"); |
| 125 CheckStrongMode("for(var undefined = 0;false;);"); |
| 126 CheckStrongMode("for(let undefined = 0;false;);"); |
| 127 CheckStrongMode("for(const undefined = 0;false;);"); |
| 128 |
| 129 // Binding/assigning to 'undefined' in for-in |
| 130 CheckStrongMode("for(undefined in {});"); |
| 131 CheckStrongMode("for(var undefined in {});"); |
| 132 CheckStrongMode("for(let undefined in {});"); |
| 133 CheckStrongMode("for(const undefined in {});"); |
| 134 |
| 135 // Binding/assigning to 'undefined' in for-of |
| 136 CheckStrongMode("for(undefined of []);"); |
| 137 CheckStrongMode("for(var undefined of []);"); |
| 138 CheckStrongMode("for(let undefined of []);"); |
| 139 CheckStrongMode("for(const undefined of []);"); |
| 140 |
| 141 // Property accessor parameter named 'undefined'. |
| 142 CheckStrongMode("let o = { set foo(undefined) {} }"); |
| 143 assertThrows("let o = { set foo(undefined) {'use strong';} }", SyntaxError); |
| 144 |
| 145 // catch(undefined) |
| 146 CheckStrongMode("try {} catch(undefined) {};"); |
| 147 |
| 148 // Assignment to undefined |
| 149 CheckStrongMode("undefined = 0;"); |
| 150 CheckStrongMode("print(undefined = 0);"); |
| 151 CheckStrongMode("let x = undefined = 0;"); |
| 152 |
| 153 // Compound assignment to undefined |
| 154 CheckStrongMode("undefined *= 0;"); |
| 155 CheckStrongMode("undefined /= 0;"); |
| 156 CheckStrongMode("print(undefined %= 0);"); |
| 157 CheckStrongMode("let x = undefined += 0;"); |
| 158 CheckStrongMode("let x = undefined -= 0;"); |
| 159 CheckStrongMode("undefined <<= 0;"); |
| 160 CheckStrongMode("undefined >>= 0;"); |
| 161 CheckStrongMode("print(undefined >>>= 0);"); |
| 162 CheckStrongMode("print(undefined &= 0);"); |
| 163 CheckStrongMode("let x = undefined ^= 0;"); |
| 164 CheckStrongMode("let x = undefined |= 0;"); |
| 165 |
| 166 // Postfix increment with undefined |
| 167 CheckStrongMode("undefined++;"); |
| 168 CheckStrongMode("print(undefined++);"); |
| 169 CheckStrongMode("let x = undefined++;"); |
| 170 |
| 171 // Postfix decrement with undefined |
| 172 CheckStrongMode("undefined--;"); |
| 173 CheckStrongMode("print(undefined--);"); |
| 174 CheckStrongMode("let x = undefined--;"); |
| 175 |
| 176 // Prefix increment with undefined |
| 177 CheckStrongMode("++undefined;"); |
| 178 CheckStrongMode("print(++undefined);"); |
| 179 CheckStrongMode("let x = ++undefined;"); |
| 180 |
| 181 // Prefix decrement with undefined |
| 182 CheckStrongMode("--undefined;"); |
| 183 CheckStrongMode("print(--undefined);"); |
| 184 CheckStrongMode("let x = --undefined;"); |
| 185 |
| 186 // Function constructor: 'undefined' parameter name |
| 187 assertDoesNotThrow(function() { |
| 188 Function("undefined", ""); |
| 189 }); |
| 190 assertThrows(function() { |
| 191 Function("undefined", "'use strong';"); |
| 192 }, SyntaxError); |
OLD | NEW |