Home | History | Annotate | Download | only in js
      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 but not throw with 'in' & 'hasOwnProperty'.
    113 shouldBeTrue('"caller" in function(){"use strict"}');
    114 shouldBeTrue('(function(){"use strict";}).hasOwnProperty("caller")');
    115 shouldBeTrue('"arguments" in function(){"use strict"}');
    116 shouldBeTrue('(function(){"use strict";}).hasOwnProperty("arguments")');
    117 
    118 shouldBeSyntaxError("'use strict'; (function (){with(1){};})");
    119 shouldBeSyntaxError("'use strict'; (function (){var a; delete a;})");
    120 shouldBeSyntaxError("'use strict'; var a; (function (){ delete a;})");
    121 shouldBeSyntaxError("var a; (function (){ 'use strict'; delete a;})");
    122 shouldBeSyntaxError("'misc directive'; 'use strict'; with({}){}");
    123 shouldThrow("'use strict'; return");
    124 shouldBeSyntaxError("'use strict'; break");
    125 shouldBeSyntaxError("'use strict'; continue");
    126 shouldThrow("'use strict'; for(;;)return");
    127 shouldBeSyntaxError("'use strict'; for(;;)break missingLabel");
    128 shouldBeSyntaxError("'use strict'; for(;;)continue missingLabel");
    129 shouldBeSyntaxError("'use strict'; 007;");
    130 shouldBeSyntaxError("'use strict'; '\\007';");
    131 shouldBeSyntaxError("'\\007'; 'use strict';");
    132 
    133 var someDeclaredGlobal;
    134 aDeletableProperty = 'test';
    135 
    136 shouldBeSyntaxError("'use strict'; delete aDeletableProperty;");
    137 shouldBeSyntaxError("'use strict'; (function (){ delete someDeclaredGlobal;})");
    138 shouldBeSyntaxError("(function (){ 'use strict'; delete someDeclaredGlobal;})");
    139 shouldBeTrue("'use strict'; if (0) { someGlobal = 'Shouldn\\'t be able to assign this.'; }; true;");
    140 shouldThrow("'use strict'; someGlobal = 'Shouldn\\'t be able to assign this.'; ");
    141 shouldThrow("'use strict'; (function f(){ f = 'shouldn\\'t be able to assign to function expression name'; })()");
    142 shouldThrow("'use strict'; eval('var introducedVariable = \"FAIL: variable introduced into containing scope\";'); introducedVariable");
    143 var objectWithReadonlyProperty = {};
    144 Object.defineProperty(objectWithReadonlyProperty, "prop", {value: "value", writable:false});
    145 shouldThrow("'use strict'; objectWithReadonlyProperty.prop = 'fail'");
    146 shouldThrow("'use strict'; delete objectWithReadonlyProperty.prop");
    147 readonlyPropName = "prop";
    148 shouldThrow("'use strict'; delete objectWithReadonlyProperty[readonlyPropName]");
    149 shouldBeSyntaxError("'use strict'; ++eval");
    150 shouldBeSyntaxError("'use strict'; eval++");
    151 shouldBeSyntaxError("'use strict'; --eval");
    152 shouldBeSyntaxError("'use strict'; eval--");
    153 shouldBeSyntaxError("'use strict'; function f() { ++arguments }");
    154 shouldBeSyntaxError("'use strict'; function f() { arguments++ }");
    155 shouldBeSyntaxError("'use strict'; function f() { --arguments }");
    156 shouldBeSyntaxError("'use strict'; function f() { arguments-- }");
    157 var global = this;
    158 shouldThrow("global.eval('\"use strict\"; if (0) ++arguments; true;')");
    159 shouldBeSyntaxError("'use strict'; ++(1, eval)");
    160 shouldBeSyntaxError("'use strict'; (1, eval)++");
    161 shouldBeSyntaxError("'use strict'; --(1, eval)");
    162 shouldBeSyntaxError("'use strict'; (1, eval)--");
    163 shouldBeSyntaxError("'use strict'; function f() { ++(1, arguments) }");
    164 shouldBeSyntaxError("'use strict'; function f() { (1, arguments)++ }");
    165 shouldBeSyntaxError("'use strict'; function f() { --(1, arguments) }");
    166 shouldBeSyntaxError("'use strict'; function f() { (1, arguments)-- }");
    167 shouldBeSyntaxError("'use strict'; if (0) delete +a.b");
    168 shouldBeSyntaxError("'use strict'; if (0) delete ++a.b");
    169 shouldBeSyntaxError("'use strict'; if (0) delete void a.b");
    170 
    171 shouldBeTrue("(function (a){'use strict'; a = false; return a !== arguments[0]; })(true)");
    172 shouldBeTrue("(function (a){'use strict'; arguments[0] = false; return a !== arguments[0]; })(true)");
    173 shouldBeTrue("(function (a){'use strict'; a=false; return arguments; })(true)[0]");
    174 shouldBeTrue("(function (a){'use strict'; arguments[0]=false; return a; })(true)");
    175 shouldBeTrue("(function (a){'use strict'; arguments[0]=true; return arguments; })(false)[0]");
    176 shouldBeTrue("(function (){'use strict';  arguments[0]=true; return arguments; })(false)[0]");
    177 shouldBeTrue("(function (a){'use strict'; arguments[0]=true; a=false; return arguments; })()[0]");
    178 shouldBeTrue("(function (a){'use strict'; arguments[0]=false; a=true; return a; })()");
    179 shouldBeTrue("(function (a){'use strict'; arguments[0]=true; return arguments; })()[0]");
    180 shouldBeTrue("(function (){'use strict';  arguments[0]=true; return arguments; })()[0]");
    181 
    182 // Same tests again, this time forcing an activation to be created as well
    183 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); a = false; return a !== arguments[0]; })(true)");
    184 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0] = false; return a !== arguments[0]; })(true)");
    185 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); a=false; return arguments; })(true)[0]");
    186 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=false; return a; })(true)");
    187 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=true; return arguments; })(false)[0]");
    188 shouldBeTrue("(function (){'use strict';  var local; (function (){local;})(); arguments[0]=true; return arguments; })(false)[0]");
    189 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=true; a=false; return arguments; })()[0]");
    190 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=true; return arguments; })()[0]");
    191 shouldBeTrue("(function (a){'use strict'; var local; (function (){local;})(); arguments[0]=false; a=true; return a; })()");
    192 shouldBeTrue("(function (){'use strict';  var local; (function (){local;})(); arguments[0]=true; return arguments; })()[0]");
    193 
    194 shouldBeTrue("'use strict'; (function (){var a = true; eval('var a = false'); return a; })()");
    195 shouldBeTrue("(function (){var a = true; eval('\"use strict\"; var a = false'); return a; })()");
    196 
    197 shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(f, 'arguments').value; })()");
    198 shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(f, 'caller').value; })()");
    199 shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(arguments, 'callee').value; })()");
    200 shouldBeUndefined("(function f(arg){'use strict'; return Object.getOwnPropertyDescriptor(arguments, 'caller').value; })()");
    201 shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(arguments, 'caller'); return descriptor.get === descriptor.set; })()");
    202 shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(arguments, 'callee'); return descriptor.get === descriptor.set; })()");
    203 shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(f, 'caller'); return descriptor.get === descriptor.set; })()");
    204 shouldBeTrue("(function f(arg){'use strict'; var descriptor = Object.getOwnPropertyDescriptor(f, 'arguments'); return descriptor.get === descriptor.set; })()");
    205 shouldBeTrue("'use strict'; (function f() { for(var i in this); })(); true;")
    206 
    207 shouldBeSyntaxError("'use strict'\u033b");
    208 shouldBeSyntaxError("'use strict'5.f");
    209 shouldBeSyntaxError("'use strict';\u033b");
    210 shouldBeSyntaxError("'use strict';5.f");
    211 shouldBeSyntaxError("'use strict';1-(eval=1);");
    212 shouldBeSyntaxError("'use strict';arguments=1;");
    213 shouldBeSyntaxError("'use strict';1-(arguments=1);");
    214 shouldBeSyntaxError("'use strict';var a=(eval=1);");
    215 shouldBeSyntaxError("'use strict';var a=(arguments=1);");
    216 
    217 var aGlobal = false;
    218 shouldBeTrue("'use strict'; try { throw 1; } catch (e) { aGlobal = true; }");
    219 aGlobal = false;
    220 shouldBeTrue("'use strict'; (function () { try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;");
    221 aGlobal = false;
    222 shouldBeTrue("(function () {'use strict';  try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;");
    223 aGlobal = false;
    224 shouldBeTrue("try { throw 1; } catch (e) { aGlobal = true; }");
    225 aGlobal = false;
    226 shouldBeTrue("(function () { try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;");
    227 aGlobal = false;
    228 shouldBeTrue("(function () {try { throw 1; } catch (e) { aGlobal = true; }})(); aGlobal;");
    229 
    230 // Make sure this doesn't crash!
    231 shouldBe('String(Object.getOwnPropertyDescriptor(function() { "use strict"; }, "caller").get)', "'function () {\\n    [native code]\\n}'");
    232