PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/test/webkit/fast/js/basic-strict-mode.js

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