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