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