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 24 description("Test behaviour of strict mode"); 25 26 var globalThisTest; 27 function testThis() { 28 "use strict"; 29 return this; 30 } 31 function testThisDotAccess() { 32 "use strict"; 33 return this.length; 34 } 35 function testThisBracketAccess(prop) { 36 "use strict"; 37 return this[prop]; 38 } 39 function testGlobalAccess() { 40 return testThis(); 41 } 42 function shouldBeSyntaxError(str) { 43 shouldThrow(str); 44 shouldThrow("(function(){" + str + "})"); 45 } 46 function testLineContinuation() { 47 "use stric\ 48 t"; 49 return this; 50 } 51 function testEscapeSequence() { 52 "use\u0020strict"; 53 return this; 54 } 55 56 shouldBe("testThis.call(null)", "null"); 57 shouldBe("testThis.call(1)", "1"); 58 shouldBe("testThis.call(true)", "true"); 59 shouldBe("testThis.call(false)", "false"); 60 shouldBe("testThis.call(undefined)", "undefined"); 61 shouldBeFalse("testLineContinuation.call(undefined) === undefined"); 62 shouldBeFalse("testEscapeSequence.call(undefined) === undefined"); 63 shouldBe("testThis.call('a string')", "'a string'"); 64 shouldBe("testThisDotAccess.call('a string')", "'a string'.length"); 65 shouldThrow("testThisDotAccess.call(null)"); 66 shouldThrow("testThisDotAccess.call(undefined)"); 67 shouldBeUndefined("testThisDotAccess.call(true)"); 68 shouldBeUndefined("testThisDotAccess.call(false)"); 69 shouldBeUndefined("testThisDotAccess.call(1)"); 70 shouldBe("testThisBracketAccess.call('a string', 'length')", "'a string'.length"); 71 shouldThrow("testThisBracketAccess.call(null, 'length')"); 72 shouldThrow("testThisBracketAccess.call(undefined, 'length')"); 73 shouldBeUndefined("testThisBracketAccess.call(true, 'length')"); 74 shouldBeUndefined("testThisBracketAccess.call(false, 'length')"); 75 shouldBeUndefined("testThisBracketAccess.call(1, 'length')"); 76 shouldBeUndefined("Function('\"use strict\"; return this;')()"); 77 shouldThrow("Function('\"use strict\"; with({});')"); 78 79 80 shouldBe("testGlobalAccess()", "undefined"); 81 shouldBe("testThis.call()", "undefined"); 82 shouldBe("testThis.apply()", "undefined"); 83 shouldBe("testThis.call(undefined)", "undefined"); 84 shouldBe("testThis.apply(undefined)", "undefined"); 85 86 shouldBeSyntaxError("(function eval(){'use strict';})"); 87 shouldBeSyntaxError("(function (eval){'use strict';})"); 88 shouldBeSyntaxError("(function arguments(){'use strict';})"); 89 shouldBeSyntaxError("(function (arguments){'use strict';})"); 90 shouldBeSyntaxError("(function (){'use strict'; var eval;})"); 91 shouldBeSyntaxError("(function (){'use strict'; var arguments;})"); 92 shouldBeSyntaxError("(function (){'use strict'; try{}catch(eval){}})"); 93 shouldBeSyntaxError("(function (){'use strict'; try{}catch(arguments){}})"); 94 shouldBeSyntaxError("(function (a, a){'use strict';})"); 95 shouldBeSyntaxError("(function (a){'use strict'; delete a;})()"); 96 shouldBeSyntaxError("(function (){'use strict'; var a; delete a;})()"); 97 shouldBeSyntaxError("(function (){var a; function f() {'use strict'; delete a;} })()"); 98 shouldBeSyntaxError("(function (){'use strict'; with(1){};})"); 99 shouldThrow("(function (){'use strict'; arguments.callee; })()"); 100 shouldThrow("(function (){'use strict'; arguments.caller; })()"); 101 shouldThrow("(function f(){'use strict'; f.arguments; })()"); 102 shouldThrow("(function f(){'use strict'; f.caller; })()"); 103 shouldThrow("(function f(){'use strict'; f.arguments=5; })()"); 104 shouldThrow("(function f(){'use strict'; f.caller=5; })()"); 105 shouldThrow("(function (arg){'use strict'; arguments.callee; })()"); 106 shouldThrow("(function (arg){'use strict'; arguments.caller; })()"); 107 shouldThrow("(function f(arg){'use strict'; f.arguments; })()"); 108 shouldThrow("(function f(arg){'use strict'; f.caller; })()"); 109 shouldThrow("(function f(arg){'use strict'; f.arguments=5; })()"); 110 shouldThrow("(function f(arg){'use strict'; f.caller=5; })()"); 111 112 // arguments/caller poisoning should be visible on the intrinsic %FunctionPrototype%, but not throw with 'in' & 'hasOwnProperty'. 113 shouldBeTrue('"caller" in function(){"use strict"}'); 114 shouldBeFalse('(function(){"use strict";}).hasOwnProperty("caller")'); 115 shouldBeTrue('(function(){"use strict";}).__proto__.hasOwnProperty("caller")'); 116 shouldBeTrue('"arguments" in function(){"use strict"}'); 117 shouldBeFalse('(function(){"use strict";}).hasOwnProperty("arguments")'); 118 shouldBeTrue('(function(){"use strict";}).__proto__.hasOwnProperty("arguments")'); 119 120 shouldBeSyntaxError("'use strict'; (function (){with(1){};})"); 121 shouldBeSyntaxError("'use strict'; (function (){var a; delete a;})"); 122 shouldBeSyntaxError("'use strict'; var a; (function (){ delete a;})"); 123 shouldBeSyntaxError("var a; (function (){ 'use strict'; delete a;})"); 124 shouldBeSyntaxError("'misc directive'; 'use strict'; with({}){}"); 125 shouldThrow("'use strict'; return"); 126 shouldBeSyntaxError("'use strict'; break"); 127 shouldBeSyntaxError("'use strict'; continue"); 128 shouldThrow("'use strict'; for(;;)return"); 129 shouldBeSyntaxError("'use strict'; for(;;)break missingLabel"); 130 shouldBeSyntaxError("'use strict'; for(;;)continue missingLabel"); 131 shouldBeSyntaxError("'use strict'; 007;"); 132 shouldBeSyntaxError("'use strict'; '\\007';"); 133 shouldBeSyntaxError("'\\007'; 'use strict';"); 134 135 var someDeclaredGlobal; 136 aDeletableProperty = 'test'; 137 138 shouldBeSyntaxError("'use strict'; delete aDeletableProperty;"); 139 shouldBeSyntaxError("'use strict'; (function (){ delete someDeclaredGlobal;})"); 140 shouldBeSyntaxError("(function (){ 'use strict'; delete someDeclaredGlobal;})"); 141 shouldBeTrue("'use strict'; if (0) { someGlobal = 'Shouldn\\'t be able to assign this.'; }; true;"); 142 shouldThrow("'use strict'; someGlobal = 'Shouldn\\'t be able to assign this.'; "); 143 shouldThrow("'use strict'; (function f(){ f = 'shouldn\\'t be able to assign to function expression name'; })()"); 144 shouldThrow("'use strict'; eval('var introducedVariable = \"FAIL: variable introduced into containing scope\";'); introducedVariable"); 145 var objectWithReadonlyProperty = {}; 146 Object.defineProperty(objectWithReadonlyProperty, "prop", {value: "value", writable:false}); 147 shouldThrow("'use strict'; objectWithReadonlyProperty.prop = 'fail'"); 148 shouldThrow("'use strict'; delete objectWithReadonlyProperty.prop"); 149 readonlyPropName = "prop"; 150 shouldThrow("'use strict'; delete objectWithReadonlyProperty[readonlyPropName]"); 151 shouldBeSyntaxError("'use strict'; ++eval"); 152 shouldBeSyntaxError("'use strict'; eval++"); 153 shouldBeSyntaxError("'use strict'; --eval"); 154 shouldBeSyntaxError("'use strict'; eval--"); 155 shouldBeSyntaxError("'use strict'; function f() { ++arguments }"); 156 shouldBeSyntaxError("'use strict'; function f() { arguments++ }"); 157 shouldBeSyntaxError("'use strict'; function f() { --arguments }"); 158 shouldBeSyntaxError("'use strict'; function f() { arguments-- }"); 159 var global = this; 160 shouldThrow("global.eval('\"use strict\"; if (0) ++arguments; true;')"); 161 shouldBeSyntaxError("'use strict'; ++(1, eval)"); 162 shouldBeSyntaxError("'use strict'; (1, eval)++"); 163 shouldBeSyntaxError("'use strict'; --(1, eval)"); 164 shouldBeSyntaxError("'use strict'; (1, eval)--"); 165 shouldBeSyntaxError("'use strict'; function f() { ++(1, arguments) }"); 166 shouldBeSyntaxError("'use strict'; function f() { (1, arguments)++ }"); 167 shouldBeSyntaxError("'use strict'; function f() { --(1, arguments) }"); 168 shouldBeSyntaxError("'use strict'; function f() { (1, arguments)-- }"); 169 shouldBeSyntaxError("'use strict'; undefined; if (0) delete +a.b"); 170 shouldBeSyntaxError("'use strict'; undefined; if (0) delete ++a.b"); 171 shouldBeSyntaxError("'use strict'; undefined; if (0) delete void a.b"); 172 173 shouldBeTrue("(function (a){'use strict'; a = false; return a !== arguments[0]; })(true)"); 174 shouldBeTrue("(function (a){'use strict'; arguments[0] = false; return a !== arguments[0]; })(true)"); 175 shouldBeTrue("(function (a){'use strict'; a=false; return arguments; })(true)[0]"); 176 shouldBeTrue("(function (a){'use strict'; arguments[0]=false; return a; })(true)"); 177 shouldBeTrue("(function (a){'use strict'; arguments[0]=true; return arguments; })(false)[0]"); 178 shouldBeTrue("(function (){'use strict'; arguments[0]=true; return arguments; })(false)[0]"); 179 shouldBeTrue("(function (a){'use strict'; arguments[0]=true; a=false; return arguments; })()[0]"); 180 shouldBeTrue("(function (a){'use strict'; arguments[0]=false; a=true; return a; })()"); 181 shouldBeTrue("(function (a){'use strict'; arguments[0]=true; return arguments; })()[0]"); 182 shouldBeTrue("(function (){'use strict'; arguments[0]=true; return arguments; })()[0]"); 183 184 // Same tests again, this time forcing an activation to be created as well 185 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); a = false; return a !== arguments[0]; })(true)"); 186 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0] = false; return a !== arguments[0]; })(true)"); 187 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); a=false; return arguments; })(true)[0]"); 188 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=false; return a; })(true)"); 189 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=true; return arguments; })(false)[0]"); 190 shouldBeTrue("(function (){'use strict'; var local; (function (){local;})(); arguments[0]=true; return arguments; })(false)[0]"); 191 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=true; a=false; return arguments; })()[0]"); 192 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=true; return arguments; })()[0]"); 193 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=false; a=true; return a; })()"); 194 shouldBeTrue("(function (){'use strict'; var local; (function (){local;})(); arguments[0]=true; return arguments; })()[0]"); 195 196 shouldBeTrue("'use strict'; (function (){var a = true; eval('var a = false'); return a; })()"); 197 shouldBeTrue("(function (){var a = true; eval('\"use strict\"; var a = false'); return a; })()"); 198 199 shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(f.__proto__, 'arguments').value; })()"); 200 shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(f.__proto__, 'caller').value; })()"); 201 shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(arguments, 'callee').value; })()"); 202 shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(arguments, 'caller').value; })()"); 203 shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(arguments, 'caller'); return descriptor.get === descriptor.set; })()"); 204 shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(arguments, 'callee'); return descriptor.get === descriptor.set; })()"); 205 shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(f.__proto__, 'caller'); return descriptor.get === descriptor.set; })()"); 206 shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(f.__proto__, 'arguments'); return descriptor.get === descriptor.set; })()"); 207 shouldBeTrue("'use strict'; (function f() { for(var i in this); })(); true;") 208 209 shouldBeSyntaxError("'use strict'\u033b"); 210 shouldBeSyntaxError("'use strict'5.f"); 211 shouldBeSyntaxError("'use strict';\u033b"); 212 shouldBeSyntaxError("'use strict';5.f"); 213 shouldBeSyntaxError("'use strict';1-(eval=1);"); 214 shouldBeSyntaxError("'use strict';arguments=1;"); 215 shouldBeSyntaxError("'use strict';1-(arguments=1);"); 216 shouldBeSyntaxError("'use strict';var a=(eval=1);"); 217 shouldBeSyntaxError("'use strict';var a=(arguments=1);"); 218 219 var aGlobal = false; 220 shouldBeTrue("'use strict'; try { throw 1; } catch (e) { aGlobal = true; }"); 221 aGlobal = false; 222 shouldBeTrue("'use strict'; (function () { try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;"); 223 aGlobal = false; 224 shouldBeTrue("(function () {'use strict'; try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;"); 225 aGlobal = false; 226 shouldBeTrue("try { throw 1; } catch (e) { aGlobal = true; }"); 227 aGlobal = false; 228 shouldBeTrue("(function () { try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;"); 229 aGlobal = false; 230 shouldBeTrue("(function () {try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;"); 231 232 // Make sure this doesn't crash! 233 shouldBe('String(Object.getOwnPropertyDescriptor((function() { "use strict"; }).__proto__, "caller").get)', "'function () {\\n [native code]\\n}'"); 234