1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description("Test behaviour of strict mode");
25
26var globalThisTest;
27function testThis() {
28    "use strict";
29    return this;
30}
31function testThisDotAccess() {
32    "use strict";
33    return this.length;
34}
35function testThisBracketAccess(prop) {
36    "use strict";
37    return this[prop];
38}
39function testGlobalAccess() {
40    return testThis();
41}
42function shouldBeSyntaxError(str) {
43    shouldThrow(str);
44    shouldThrow("(function(){" + str + "})");
45}
46function testLineContinuation() {
47    "use stric\
48t";
49    return this;
50}
51function testEscapeSequence() {
52    "use\u0020strict";
53    return this;
54}
55
56shouldBe("testThis.call(null)", "null");
57shouldBe("testThis.call(1)", "1");
58shouldBe("testThis.call(true)", "true");
59shouldBe("testThis.call(false)", "false");
60shouldBe("testThis.call(undefined)", "undefined");
61shouldBeFalse("testLineContinuation.call(undefined) === undefined");
62shouldBeFalse("testEscapeSequence.call(undefined) === undefined");
63shouldBe("testThis.call('a string')", "'a string'");
64shouldBe("testThisDotAccess.call('a string')", "'a string'.length");
65shouldThrow("testThisDotAccess.call(null)");
66shouldThrow("testThisDotAccess.call(undefined)");
67shouldBeUndefined("testThisDotAccess.call(true)");
68shouldBeUndefined("testThisDotAccess.call(false)");
69shouldBeUndefined("testThisDotAccess.call(1)");
70shouldBe("testThisBracketAccess.call('a string', 'length')", "'a string'.length");
71shouldThrow("testThisBracketAccess.call(null, 'length')");
72shouldThrow("testThisBracketAccess.call(undefined, 'length')");
73shouldBeUndefined("testThisBracketAccess.call(true, 'length')");
74shouldBeUndefined("testThisBracketAccess.call(false, 'length')");
75shouldBeUndefined("testThisBracketAccess.call(1, 'length')");
76shouldBeUndefined("Function('\"use strict\"; return this;')()");
77shouldThrow("Function('\"use strict\"; with({});')");
78
79
80shouldBe("testGlobalAccess()", "undefined");
81shouldBe("testThis.call()", "undefined");
82shouldBe("testThis.apply()", "undefined");
83shouldBe("testThis.call(undefined)", "undefined");
84shouldBe("testThis.apply(undefined)", "undefined");
85
86shouldBeSyntaxError("(function eval(){'use strict';})");
87shouldBeSyntaxError("(function (eval){'use strict';})");
88shouldBeSyntaxError("(function arguments(){'use strict';})");
89shouldBeSyntaxError("(function (arguments){'use strict';})");
90shouldBeSyntaxError("(function (){'use strict'; var eval;})");
91shouldBeSyntaxError("(function (){'use strict'; var arguments;})");
92shouldBeSyntaxError("(function (){'use strict'; try{}catch(eval){}})");
93shouldBeSyntaxError("(function (){'use strict'; try{}catch(arguments){}})");
94shouldBeSyntaxError("(function (a, a){'use strict';})");
95shouldBeSyntaxError("(function (a){'use strict'; delete a;})()");
96shouldBeSyntaxError("(function (){'use strict'; var a; delete a;})()");
97shouldBeSyntaxError("(function (){var a; function f() {'use strict'; delete a;} })()");
98shouldBeSyntaxError("(function (){'use strict'; with(1){};})");
99shouldThrow("(function (){'use strict'; arguments.callee; })()");
100shouldThrow("(function (){'use strict'; arguments.caller; })()");
101shouldThrow("(function f(){'use strict'; f.arguments; })()");
102shouldThrow("(function f(){'use strict'; f.caller; })()");
103shouldThrow("(function f(){'use strict'; f.arguments=5; })()");
104shouldThrow("(function f(){'use strict'; f.caller=5; })()");
105shouldThrow("(function (arg){'use strict'; arguments.callee; })()");
106shouldThrow("(function (arg){'use strict'; arguments.caller; })()");
107shouldThrow("(function f(arg){'use strict'; f.arguments; })()");
108shouldThrow("(function f(arg){'use strict'; f.caller; })()");
109shouldThrow("(function f(arg){'use strict'; f.arguments=5; })()");
110shouldThrow("(function f(arg){'use strict'; f.caller=5; })()");
111
112// arguments/caller poisoning should be visible but not throw with 'in' & 'hasOwnProperty'.
113shouldBeTrue('"caller" in function(){"use strict"}');
114shouldBeTrue('(function(){"use strict";}).hasOwnProperty("caller")');
115shouldBeTrue('"arguments" in function(){"use strict"}');
116shouldBeTrue('(function(){"use strict";}).hasOwnProperty("arguments")');
117
118shouldBeSyntaxError("'use strict'; (function (){with(1){};})");
119shouldBeSyntaxError("'use strict'; (function (){var a; delete a;})");
120shouldBeSyntaxError("'use strict'; var a; (function (){ delete a;})");
121shouldBeSyntaxError("var a; (function (){ 'use strict'; delete a;})");
122shouldBeSyntaxError("'misc directive'; 'use strict'; with({}){}");
123shouldThrow("'use strict'; return");
124shouldBeSyntaxError("'use strict'; break");
125shouldBeSyntaxError("'use strict'; continue");
126shouldThrow("'use strict'; for(;;)return");
127shouldBeSyntaxError("'use strict'; for(;;)break missingLabel");
128shouldBeSyntaxError("'use strict'; for(;;)continue missingLabel");
129shouldBeSyntaxError("'use strict'; 007;");
130shouldBeSyntaxError("'use strict'; '\\007';");
131shouldBeSyntaxError("'\\007'; 'use strict';");
132
133var someDeclaredGlobal;
134aDeletableProperty = 'test';
135
136shouldBeSyntaxError("'use strict'; delete aDeletableProperty;");
137shouldBeSyntaxError("'use strict'; (function (){ delete someDeclaredGlobal;})");
138shouldBeSyntaxError("(function (){ 'use strict'; delete someDeclaredGlobal;})");
139shouldBeTrue("'use strict'; if (0) { someGlobal = 'Shouldn\\'t be able to assign this.'; }; true;");
140shouldThrow("'use strict'; someGlobal = 'Shouldn\\'t be able to assign this.'; ");
141shouldThrow("'use strict'; (function f(){ f = 'shouldn\\'t be able to assign to function expression name'; })()");
142shouldThrow("'use strict'; eval('var introducedVariable = \"FAIL: variable introduced into containing scope\";'); introducedVariable");
143var objectWithReadonlyProperty = {};
144Object.defineProperty(objectWithReadonlyProperty, "prop", {value: "value", writable:false});
145shouldThrow("'use strict'; objectWithReadonlyProperty.prop = 'fail'");
146shouldThrow("'use strict'; delete objectWithReadonlyProperty.prop");
147readonlyPropName = "prop";
148shouldThrow("'use strict'; delete objectWithReadonlyProperty[readonlyPropName]");
149shouldBeSyntaxError("'use strict'; ++eval");
150shouldBeSyntaxError("'use strict'; eval++");
151shouldBeSyntaxError("'use strict'; --eval");
152shouldBeSyntaxError("'use strict'; eval--");
153shouldBeSyntaxError("'use strict'; function f() { ++arguments }");
154shouldBeSyntaxError("'use strict'; function f() { arguments++ }");
155shouldBeSyntaxError("'use strict'; function f() { --arguments }");
156shouldBeSyntaxError("'use strict'; function f() { arguments-- }");
157var global = this;
158shouldThrow("global.eval('\"use strict\"; if (0) ++arguments; true;')");
159shouldBeSyntaxError("'use strict'; ++(1, eval)");
160shouldBeSyntaxError("'use strict'; (1, eval)++");
161shouldBeSyntaxError("'use strict'; --(1, eval)");
162shouldBeSyntaxError("'use strict'; (1, eval)--");
163shouldBeSyntaxError("'use strict'; function f() { ++(1, arguments) }");
164shouldBeSyntaxError("'use strict'; function f() { (1, arguments)++ }");
165shouldBeSyntaxError("'use strict'; function f() { --(1, arguments) }");
166shouldBeSyntaxError("'use strict'; function f() { (1, arguments)-- }");
167shouldBeSyntaxError("'use strict'; if (0) delete +a.b");
168shouldBeSyntaxError("'use strict'; if (0) delete ++a.b");
169shouldBeSyntaxError("'use strict'; if (0) delete void a.b");
170
171shouldBeTrue("(function (a){'use strict'; a = false; return a !== arguments[0]; })(true)");
172shouldBeTrue("(function (a){'use strict'; arguments[0] = false; return a !== arguments[0]; })(true)");
173shouldBeTrue("(function (a){'use strict'; a=false; return arguments; })(true)[0]");
174shouldBeTrue("(function (a){'use strict'; arguments[0]=false; return a; })(true)");
175shouldBeTrue("(function (a){'use strict'; arguments[0]=true; return arguments; })(false)[0]");
176shouldBeTrue("(function (){'use strict';  arguments[0]=true; return arguments; })(false)[0]");
177shouldBeTrue("(function (a){'use strict'; arguments[0]=true; a=false; return arguments; })()[0]");
178shouldBeTrue("(function (a){'use strict'; arguments[0]=false; a=true; return a; })()");
179shouldBeTrue("(function (a){'use strict'; arguments[0]=true; return arguments; })()[0]");
180shouldBeTrue("(function (){'use strict';  arguments[0]=true; return arguments; })()[0]");
181
182// Same tests again, this time forcing an activation to be created as well
183shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); a = false; return a !== arguments[0]; })(true)");
184shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0] = false; return a !== arguments[0]; })(true)");
185shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); a=false; return arguments; })(true)[0]");
186shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=false; return a; })(true)");
187shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=true; return arguments; })(false)[0]");
188shouldBeTrue("(function (){'use strict';  var local; (function (){local;})(); arguments[0]=true; return arguments; })(false)[0]");
189shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=true; a=false; return arguments; })()[0]");
190shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=true; return arguments; })()[0]");
191shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=false; a=true; return a; })()");
192shouldBeTrue("(function (){'use strict';  var local; (function (){local;})(); arguments[0]=true; return arguments; })()[0]");
193
194shouldBeTrue("'use strict'; (function (){var a = true; eval('var a = false'); return a; })()");
195shouldBeTrue("(function (){var a = true; eval('\"use strict\"; var a = false'); return a; })()");
196
197shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(f, 'arguments').value; })()");
198shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(f, 'caller').value; })()");
199shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(arguments, 'callee').value; })()");
200shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(arguments, 'caller').value; })()");
201shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(arguments, 'caller'); return descriptor.get === descriptor.set; })()");
202shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(arguments, 'callee'); return descriptor.get === descriptor.set; })()");
203shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(f, 'caller'); return descriptor.get === descriptor.set; })()");
204shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(f, 'arguments'); return descriptor.get === descriptor.set; })()");
205shouldBeTrue("'use strict'; (function f() { for(var i in this); })(); true;")
206
207shouldBeSyntaxError("'use strict'\u033b");
208shouldBeSyntaxError("'use strict'5.f");
209shouldBeSyntaxError("'use strict';\u033b");
210shouldBeSyntaxError("'use strict';5.f");
211shouldBeSyntaxError("'use strict';1-(eval=1);");
212shouldBeSyntaxError("'use strict';arguments=1;");
213shouldBeSyntaxError("'use strict';1-(arguments=1);");
214shouldBeSyntaxError("'use strict';var a=(eval=1);");
215shouldBeSyntaxError("'use strict';var a=(arguments=1);");
216
217var aGlobal = false;
218shouldBeTrue("'use strict'; try { throw 1; } catch (e) { aGlobal = true; }");
219aGlobal = false;
220shouldBeTrue("'use strict'; (function () { try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;");
221aGlobal = false;
222shouldBeTrue("(function () {'use strict';  try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;");
223aGlobal = false;
224shouldBeTrue("try { throw 1; } catch (e) { aGlobal = true; }");
225aGlobal = false;
226shouldBeTrue("(function () { try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;");
227aGlobal = false;
228shouldBeTrue("(function () {try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;");
229
230// Make sure this doesn't crash!
231shouldBe('String(Object.getOwnPropertyDescriptor(function() { "use strict"; }, "caller").get)', "'function () {\\n    [native code]\\n}'");
232