deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / mjsunit / strict-mode.js
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 // Flags: --turbo-deoptimization
29
30 function CheckStrictMode(code, exception) {
31   assertDoesNotThrow(code);
32   assertThrows("'use strict';\n" + code, exception);
33   assertThrows('"use strict";\n' + code, exception);
34   assertDoesNotThrow("\
35     function outer() {\
36       function inner() {\n"
37         + code +
38       "\n}\
39     }");
40   assertThrows("\
41     function outer() {\
42       'use strict';\
43       function inner() {\n"
44         + code +
45       "\n}\
46     }", exception);
47 }
48
49 function CheckFunctionConstructorStrictMode() {
50   var args = [];
51   for (var i = 0; i < arguments.length; i ++) {
52     args[i] = arguments[i];
53   }
54   // Create non-strict function. No exception.
55   args[arguments.length] = "";
56   assertDoesNotThrow(function() {
57     Function.apply(this, args);
58   });
59   // Create strict mode function. Exception expected.
60   args[arguments.length] = "'use strict';";
61   assertThrows(function() {
62     Function.apply(this, args);
63   }, SyntaxError);
64 }
65
66 // Incorrect 'use strict' directive.
67 (function UseStrictEscape() {
68   "use\\x20strict";
69   with ({}) {};
70 })();
71
72 // Incorrectly place 'use strict' directive.
73 assertThrows("function foo (x) 'use strict'; {}", SyntaxError);
74
75 // 'use strict' in non-directive position.
76 (function UseStrictNonDirective() {
77   void(0);
78   "use strict";
79   with ({}) {};
80 })();
81
82 // Multiple directives, including "use strict".
83 assertThrows('\
84 "directive 1";\
85 "another directive";\
86 "use strict";\
87 "directive after strict";\
88 "and one more";\
89 with({}) {}', SyntaxError);
90
91 // 'with' disallowed in strict mode.
92 CheckStrictMode("with({}) {}", SyntaxError);
93
94 // Function named 'eval'.
95 CheckStrictMode("function eval() {}", SyntaxError);
96
97 // Function named 'arguments'.
98 CheckStrictMode("function arguments() {}", SyntaxError);
99
100 // Function parameter named 'eval'.
101 CheckStrictMode("function foo(a, b, eval, c, d) {}", SyntaxError);
102
103 // Function parameter named 'arguments'.
104 CheckStrictMode("function foo(a, b, arguments, c, d) {}", SyntaxError);
105
106 // Property accessor parameter named 'eval'.
107 CheckStrictMode("var o = { set foo(eval) {} }", SyntaxError);
108
109 // Property accessor parameter named 'arguments'.
110 CheckStrictMode("var o = { set foo(arguments) {} }", SyntaxError);
111
112 // Duplicate function parameter name.
113 CheckStrictMode("function foo(a, b, c, d, b) {}", SyntaxError);
114
115 // Function constructor: eval parameter name.
116 CheckFunctionConstructorStrictMode("eval");
117
118 // Function constructor: arguments parameter name.
119 CheckFunctionConstructorStrictMode("arguments");
120
121 // Function constructor: duplicate parameter name.
122 CheckFunctionConstructorStrictMode("a", "b", "c", "b");
123 CheckFunctionConstructorStrictMode("a,b,c,b");
124
125 // catch(eval)
126 CheckStrictMode("try{}catch(eval){};", SyntaxError);
127
128 // catch(arguments)
129 CheckStrictMode("try{}catch(arguments){};", SyntaxError);
130
131 // var eval
132 CheckStrictMode("var eval;", SyntaxError);
133
134 // var arguments
135 CheckStrictMode("var arguments;", SyntaxError);
136
137 // Strict mode applies to the function in which the directive is used..
138 assertThrows('\
139 function foo(eval) {\
140   "use strict";\
141 }', SyntaxError);
142
143 // Strict mode doesn't affect the outer stop of strict code.
144 (function NotStrict(eval) {
145   function Strict() {
146     "use strict";
147   }
148   with ({}) {};
149 })();
150
151 // Octal literal
152 CheckStrictMode("var x = 012");
153 CheckStrictMode("012");
154 CheckStrictMode("'Hello octal\\032'");
155 CheckStrictMode("function octal() { return 012; }");
156 CheckStrictMode("function octal() { return '\\032'; }");
157
158 (function ValidEscape() {
159   "use strict";
160   var x = '\0';
161   var y = "\0";
162 })();
163
164 // Octal before "use strict"
165 assertThrows('\
166   function strict() {\
167     "octal\\032directive";\
168     "use strict";\
169   }', SyntaxError);
170
171 (function StrictModeNonDuplicate() {
172   "use strict";
173   var x = { 123 : 1, "0123" : 2 };
174   var x = {
175     123: 1,
176     '123.00000000000000000000000000000000000000000000000000000000000000000001':
177       2
178   };
179 })();
180
181 // Duplicate data properties are allowed in ES6
182 (function StrictModeDuplicateES6() {
183   'use strict';
184   var x = {
185     123: 1,
186     123.00000000000000000000000000000000000000000000000000000000000000000001: 2
187   };
188   var x = { dupe : 1, nondupe: 3, dupe : 2 };
189   var x = { '1234' : 1, '2345' : 2, '1234' : 3 };
190   var x = { '1234' : 1, '2345' : 2, 1234 : 3 };
191   var x = { 3.14 : 1, 2.71 : 2, 3.14 : 3 };
192   var x = { 3.14 : 1, '3.14' : 2 };
193
194   var x = { get foo() { }, get foo() { } };
195   var x = { get foo(){}, get 'foo'(){}};
196   var x = { get 12(){}, get '12'(){}};
197
198   // Two setters
199   var x = { set foo(v) { }, set foo(v) { } };
200   var x = { set foo(v) { }, set 'foo'(v) { } };
201   var x = { set 13(v) { }, set '13'(v) { } };
202
203   // Setter and data
204   var x = { foo: 'data', set foo(v) { } };
205   var x = { set foo(v) { }, foo: 'data' };
206   var x = { foo: 'data', set 'foo'(v) { } };
207   var x = { set foo(v) { }, 'foo': 'data' };
208   var x = { 'foo': 'data', set foo(v) { } };
209   var x = { set 'foo'(v) { }, foo: 'data' };
210   var x = { 'foo': 'data', set 'foo'(v) { } };
211   var x = { set 'foo'(v) { }, 'foo': 'data' };
212   var x = { 12: 1, set '12'(v){}};
213   var x = { 12: 1, set 12(v){}};
214   var x = { '12': 1, set '12'(v){}};
215   var x = { '12': 1, set 12(v){}};
216
217   // Getter and data
218   var x = { foo: 'data', get foo() { } };
219   var x = { get foo() { }, foo: 'data' };
220   var x = { 'foo': 'data', get foo() { } };
221   var x = { get 'foo'() { }, 'foo': 'data' };
222   var x = { '12': 1, get '12'(){}};
223   var x = { '12': 1, get 12(){}};
224 })();
225
226 // Assignment to eval or arguments
227 CheckStrictMode("function strict() { eval = undefined; }", SyntaxError);
228 CheckStrictMode("function strict() { arguments = undefined; }", SyntaxError);
229 CheckStrictMode("function strict() { print(eval = undefined); }", SyntaxError);
230 CheckStrictMode("function strict() { print(arguments = undefined); }",
231                 SyntaxError);
232 CheckStrictMode("function strict() { var x = eval = undefined; }", SyntaxError);
233 CheckStrictMode("function strict() { var x = arguments = undefined; }",
234                 SyntaxError);
235
236 // Compound assignment to eval or arguments
237 CheckStrictMode("function strict() { eval *= undefined; }", SyntaxError);
238 CheckStrictMode("function strict() { arguments /= undefined; }", SyntaxError);
239 CheckStrictMode("function strict() { print(eval %= undefined); }", SyntaxError);
240 CheckStrictMode("function strict() { print(arguments %= undefined); }",
241                 SyntaxError);
242 CheckStrictMode("function strict() { var x = eval += undefined; }",
243                 SyntaxError);
244 CheckStrictMode("function strict() { var x = arguments -= undefined; }",
245                 SyntaxError);
246 CheckStrictMode("function strict() { eval <<= undefined; }", SyntaxError);
247 CheckStrictMode("function strict() { arguments >>= undefined; }", SyntaxError);
248 CheckStrictMode("function strict() { print(eval >>>= undefined); }",
249                 SyntaxError);
250 CheckStrictMode("function strict() { print(arguments &= undefined); }",
251                 SyntaxError);
252 CheckStrictMode("function strict() { var x = eval ^= undefined; }",
253                 SyntaxError);
254 CheckStrictMode("function strict() { var x = arguments |= undefined; }",
255                 SyntaxError);
256
257 // Postfix increment with eval or arguments
258 CheckStrictMode("function strict() { eval++; }", SyntaxError);
259 CheckStrictMode("function strict() { arguments++; }", SyntaxError);
260 CheckStrictMode("function strict() { print(eval++); }", SyntaxError);
261 CheckStrictMode("function strict() { print(arguments++); }", SyntaxError);
262 CheckStrictMode("function strict() { var x = eval++; }", SyntaxError);
263 CheckStrictMode("function strict() { var x = arguments++; }", SyntaxError);
264
265 // Postfix decrement with eval or arguments
266 CheckStrictMode("function strict() { eval--; }", SyntaxError);
267 CheckStrictMode("function strict() { arguments--; }", SyntaxError);
268 CheckStrictMode("function strict() { print(eval--); }", SyntaxError);
269 CheckStrictMode("function strict() { print(arguments--); }", SyntaxError);
270 CheckStrictMode("function strict() { var x = eval--; }", SyntaxError);
271 CheckStrictMode("function strict() { var x = arguments--; }", SyntaxError);
272
273 // Prefix increment with eval or arguments
274 CheckStrictMode("function strict() { ++eval; }", SyntaxError);
275 CheckStrictMode("function strict() { ++arguments; }", SyntaxError);
276 CheckStrictMode("function strict() { print(++eval); }", SyntaxError);
277 CheckStrictMode("function strict() { print(++arguments); }", SyntaxError);
278 CheckStrictMode("function strict() { var x = ++eval; }", SyntaxError);
279 CheckStrictMode("function strict() { var x = ++arguments; }", SyntaxError);
280
281 // Prefix decrement with eval or arguments
282 CheckStrictMode("function strict() { --eval; }", SyntaxError);
283 CheckStrictMode("function strict() { --arguments; }", SyntaxError);
284 CheckStrictMode("function strict() { print(--eval); }", SyntaxError);
285 CheckStrictMode("function strict() { print(--arguments); }", SyntaxError);
286 CheckStrictMode("function strict() { var x = --eval; }", SyntaxError);
287 CheckStrictMode("function strict() { var x = --arguments; }", SyntaxError);
288
289 // Delete of an unqualified identifier
290 CheckStrictMode("delete unqualified;", SyntaxError);
291 CheckStrictMode("function strict() { delete unqualified; }", SyntaxError);
292 CheckStrictMode("function function_name() { delete function_name; }",
293                 SyntaxError);
294 CheckStrictMode("function strict(parameter) { delete parameter; }",
295                 SyntaxError);
296 CheckStrictMode("function strict() { var variable; delete variable; }",
297                 SyntaxError);
298 CheckStrictMode("var variable; delete variable;", SyntaxError);
299
300 (function TestStrictDelete() {
301   "use strict";
302   // "delete this" is allowed in strict mode and should work.
303   function strict_delete() { delete this; }
304   strict_delete();
305 })();
306
307 // Prefix unary operators other than delete, ++, -- are valid in strict mode
308 (function StrictModeUnaryOperators() {
309   "use strict";
310   var x = [void eval, typeof eval, +eval, -eval, ~eval, !eval];
311   var y = [void arguments, typeof arguments,
312            +arguments, -arguments, ~arguments, !arguments];
313 })();
314
315 // 7.6.1.2 Future Reserved Words in strict mode
316 var future_strict_reserved_words = [
317   "implements",
318   "interface",
319   "let",
320   "package",
321   "private",
322   "protected",
323   "public",
324   "static",
325   "yield" ];
326
327 function testFutureStrictReservedWord(word) {
328   // Simple use of each reserved word
329   CheckStrictMode("var " + word + " = 1;", SyntaxError);
330   CheckStrictMode("typeof (" + word + ");", SyntaxError);
331
332   // object literal properties
333   eval("var x = { " + word + " : 42 };");
334   eval("var x = { get " + word + " () {} };");
335   eval("var x = { set " + word + " (value) {} };");
336   eval("var x = { get " + word + " () { 'use strict'; } };");
337   eval("var x = { set " + word + " (value) { 'use strict'; } };");
338
339   // object literal with string literal property names
340   eval("var x = { '" + word + "' : 42 };");
341   eval("var x = { get '" + word + "' () { } };");
342   eval("var x = { set '" + word + "' (value) { } };");
343   eval("var x = { get '" + word + "' () { 'use strict'; } };");
344   eval("var x = { set '" + word + "' (value) { 'use strict'; } };");
345
346   // Function names and arguments, strict and non-strict contexts
347   CheckStrictMode("function " + word + " () {}", SyntaxError);
348   CheckStrictMode("function foo (" + word + ") {}", SyntaxError);
349   CheckStrictMode("function foo (" + word + ", " + word + ") {}", SyntaxError);
350   CheckStrictMode("function foo (a, " + word + ") {}", SyntaxError);
351   CheckStrictMode("function foo (" + word + ", a) {}", SyntaxError);
352   CheckStrictMode("function foo (a, " + word + ", b) {}", SyntaxError);
353   CheckStrictMode("var foo = function (" + word + ") {}", SyntaxError);
354
355   // Function names and arguments when the body is strict
356   assertThrows("function " + word + " () { 'use strict'; }", SyntaxError);
357   assertThrows("function foo (" + word + ", " + word + ") { 'use strict'; }",
358                SyntaxError);
359   assertThrows("function foo (a, " + word + ") { 'use strict'; }", SyntaxError);
360   assertThrows("function foo (" + word + ", a) { 'use strict'; }", SyntaxError);
361   assertThrows("function foo (a, " + word + ", b) { 'use strict'; }",
362                SyntaxError);
363   assertThrows("var foo = function (" + word + ") { 'use strict'; }",
364                SyntaxError);
365
366   // setter parameter when the body is strict
367   CheckStrictMode("var x = { set foo(" + word + ") {} };", SyntaxError);
368   assertThrows("var x = { set foo(" + word + ") { 'use strict'; } };",
369                SyntaxError);
370 }
371
372 for (var i = 0; i < future_strict_reserved_words.length; i++) {
373   testFutureStrictReservedWord(future_strict_reserved_words[i]);
374 }
375
376 function testAssignToUndefined(test, should_throw) {
377   try {
378     test();
379   } catch (e) {
380     assertTrue(should_throw, "strict mode");
381     assertInstanceof(e, ReferenceError, "strict mode");
382     return;
383   }
384   assertFalse(should_throw, "strict mode");
385 }
386
387 function repeat(n, f) {
388   for (var i = 0; i < n; i ++) { f(); }
389 }
390
391 function assignToUndefined() {
392   "use strict";
393   possibly_undefined_variable_for_strict_mode_test = "should throw?";
394 }
395
396 testAssignToUndefined(assignToUndefined, true);
397 testAssignToUndefined(assignToUndefined, true);
398 testAssignToUndefined(assignToUndefined, true);
399
400 possibly_undefined_variable_for_strict_mode_test = "value";
401
402 testAssignToUndefined(assignToUndefined, false);
403 testAssignToUndefined(assignToUndefined, false);
404 testAssignToUndefined(assignToUndefined, false);
405
406 delete possibly_undefined_variable_for_strict_mode_test;
407
408 testAssignToUndefined(assignToUndefined, true);
409 testAssignToUndefined(assignToUndefined, true);
410 testAssignToUndefined(assignToUndefined, true);
411
412 repeat(10, function() { testAssignToUndefined(assignToUndefined, true); });
413 possibly_undefined_variable_for_strict_mode_test = "value";
414 repeat(10, function() { testAssignToUndefined(assignToUndefined, false); });
415 delete possibly_undefined_variable_for_strict_mode_test;
416 repeat(10, function() { testAssignToUndefined(assignToUndefined, true); });
417 possibly_undefined_variable_for_strict_mode_test = undefined;
418 repeat(10, function() { testAssignToUndefined(assignToUndefined, false); });
419
420 function assignToUndefinedWithEval() {
421   "use strict";
422   possibly_undefined_variable_for_strict_mode_test_with_eval = "should throw?";
423   eval("");
424 }
425
426 testAssignToUndefined(assignToUndefinedWithEval, true);
427 testAssignToUndefined(assignToUndefinedWithEval, true);
428 testAssignToUndefined(assignToUndefinedWithEval, true);
429
430 possibly_undefined_variable_for_strict_mode_test_with_eval = "value";
431
432 testAssignToUndefined(assignToUndefinedWithEval, false);
433 testAssignToUndefined(assignToUndefinedWithEval, false);
434 testAssignToUndefined(assignToUndefinedWithEval, false);
435
436 delete possibly_undefined_variable_for_strict_mode_test_with_eval;
437
438 testAssignToUndefined(assignToUndefinedWithEval, true);
439 testAssignToUndefined(assignToUndefinedWithEval, true);
440 testAssignToUndefined(assignToUndefinedWithEval, true);
441
442 repeat(10, function() {
443              testAssignToUndefined(assignToUndefinedWithEval, true);
444            });
445 possibly_undefined_variable_for_strict_mode_test_with_eval = "value";
446 repeat(10, function() {
447              testAssignToUndefined(assignToUndefinedWithEval, false);
448            });
449 delete possibly_undefined_variable_for_strict_mode_test_with_eval;
450 repeat(10, function() {
451              testAssignToUndefined(assignToUndefinedWithEval, true);
452            });
453 possibly_undefined_variable_for_strict_mode_test_with_eval = undefined;
454 repeat(10, function() {
455              testAssignToUndefined(assignToUndefinedWithEval, false);
456            });
457
458
459
460 (function testDeleteNonConfigurable() {
461   function delete_property(o) {
462     "use strict";
463     delete o.property;
464   }
465   function delete_element(o, i) {
466     "use strict";
467     delete o[i];
468   }
469
470   var object = {};
471
472   Object.defineProperty(object, "property", { value: "property_value" });
473   Object.defineProperty(object, "1", { value: "one" });
474   Object.defineProperty(object, 7, { value: "seven" });
475   Object.defineProperty(object, 3.14, { value: "pi" });
476
477   assertThrows(function() { delete_property(object); }, TypeError);
478   assertEquals(object.property, "property_value");
479   assertThrows(function() { delete_element(object, "1"); }, TypeError);
480   assertThrows(function() { delete_element(object, 1); }, TypeError);
481   assertEquals(object[1], "one");
482   assertThrows(function() { delete_element(object, "7"); }, TypeError);
483   assertThrows(function() { delete_element(object, 7); }, TypeError);
484   assertEquals(object[7], "seven");
485   assertThrows(function() { delete_element(object, "3.14"); }, TypeError);
486   assertThrows(function() { delete_element(object, 3.14); }, TypeError);
487   assertEquals(object[3.14], "pi");
488 })();
489
490 // Not transforming this in Function.call and Function.apply.
491 (function testThisTransformCallApply() {
492   function non_strict() {
493     return this;
494   }
495   function strict() {
496     "use strict";
497     return this;
498   }
499
500   var global_object = (function() { return this; })();
501   var object = {};
502
503   // Non-strict call.
504   assertTrue(non_strict.call(null) === global_object);
505   assertTrue(non_strict.call(undefined) === global_object);
506   assertEquals(typeof non_strict.call(7), "object");
507   assertEquals(typeof non_strict.call("Hello"), "object");
508   assertTrue(non_strict.call(object) === object);
509
510   // Non-strict apply.
511   assertTrue(non_strict.apply(null) === global_object);
512   assertTrue(non_strict.apply(undefined) === global_object);
513   assertEquals(typeof non_strict.apply(7), "object");
514   assertEquals(typeof non_strict.apply("Hello"), "object");
515   assertTrue(non_strict.apply(object) === object);
516
517   // Strict call.
518   assertTrue(strict.call(null) === null);
519   assertTrue(strict.call(undefined) === undefined);
520   assertEquals(typeof strict.call(7), "number");
521   assertEquals(typeof strict.call("Hello"), "string");
522   assertTrue(strict.call(object) === object);
523
524   // Strict apply.
525   assertTrue(strict.apply(null) === null);
526   assertTrue(strict.apply(undefined) === undefined);
527   assertEquals(typeof strict.apply(7), "number");
528   assertEquals(typeof strict.apply("Hello"), "string");
529   assertTrue(strict.apply(object) === object);
530 })();
531
532 (function testThisTransform() {
533   try {
534     function strict() {
535       "use strict";
536       return typeof(this);
537     }
538     function nonstrict() {
539       return typeof(this);
540     }
541
542     // Concat to avoid symbol.
543     var strict_name = "str" + "ict";
544     var nonstrict_name = "non" + "str" + "ict";
545     var strict_number = 17;
546     var nonstrict_number = 19;
547     var strict_name_get = "str" + "ict" + "get";
548     var nonstrict_name_get = "non" + "str" + "ict" + "get"
549     var strict_number_get = 23;
550     var nonstrict_number_get = 29;
551
552     function install(t) {
553       t.prototype.strict = strict;
554       t.prototype.nonstrict = nonstrict;
555       t.prototype[strict_number] = strict;
556       t.prototype[nonstrict_number] = nonstrict;
557       Object.defineProperty(t.prototype, strict_name_get,
558                             { get: function() { return strict; },
559                               configurable: true });
560       Object.defineProperty(t.prototype, nonstrict_name_get,
561                             { get: function() { return nonstrict; },
562                               configurable: true });
563       Object.defineProperty(t.prototype, strict_number_get,
564                             { get: function() { return strict; },
565                               configurable: true });
566       Object.defineProperty(t.prototype, nonstrict_number_get,
567                             { get: function() { return nonstrict; },
568                               configurable: true });
569     }
570
571     function cleanup(t) {
572       delete t.prototype.strict;
573       delete t.prototype.nonstrict;
574       delete t.prototype[strict_number];
575       delete t.prototype[nonstrict_number];
576       delete t.prototype[strict_name_get];
577       delete t.prototype[nonstrict_name_get];
578       delete t.prototype[strict_number_get];
579       delete t.prototype[nonstrict_number_get];
580     }
581
582     // Set up fakes
583     install(String);
584     install(Number);
585     install(Boolean)
586
587     function callStrict(o) {
588       return o.strict();
589     }
590     function callNonStrict(o) {
591       return o.nonstrict();
592     }
593     function callKeyedStrict(o) {
594       return o[strict_name]();
595     }
596     function callKeyedNonStrict(o) {
597       return o[nonstrict_name]();
598     }
599     function callIndexedStrict(o) {
600       return o[strict_number]();
601     }
602     function callIndexedNonStrict(o) {
603       return o[nonstrict_number]();
604     }
605     function callStrictGet(o) {
606       return o.strictget();
607     }
608     function callNonStrictGet(o) {
609       return o.nonstrictget();
610     }
611     function callKeyedStrictGet(o) {
612       return o[strict_name_get]();
613     }
614     function callKeyedNonStrictGet(o) {
615       return o[nonstrict_name_get]();
616     }
617     function callIndexedStrictGet(o) {
618       return o[strict_number_get]();
619     }
620     function callIndexedNonStrictGet(o) {
621       return o[nonstrict_number_get]();
622     }
623
624     for (var i = 0; i < 10; i ++) {
625       assertEquals(("hello").strict(), "string");
626       assertEquals(("hello").nonstrict(), "object");
627       assertEquals(("hello")[strict_name](), "string");
628       assertEquals(("hello")[nonstrict_name](), "object");
629       assertEquals(("hello")[strict_number](), "string");
630       assertEquals(("hello")[nonstrict_number](), "object");
631
632       assertEquals((10 + i).strict(), "number");
633       assertEquals((10 + i).nonstrict(), "object");
634       assertEquals((10 + i)[strict_name](), "number");
635       assertEquals((10 + i)[nonstrict_name](), "object");
636       assertEquals((10 + i)[strict_number](), "number");
637       assertEquals((10 + i)[nonstrict_number](), "object");
638
639       assertEquals((true).strict(), "boolean");
640       assertEquals((true).nonstrict(), "object");
641       assertEquals((true)[strict_name](), "boolean");
642       assertEquals((true)[nonstrict_name](), "object");
643       assertEquals((true)[strict_number](), "boolean");
644       assertEquals((true)[nonstrict_number](), "object");
645
646       assertEquals((false).strict(), "boolean");
647       assertEquals((false).nonstrict(), "object");
648       assertEquals((false)[strict_name](), "boolean");
649       assertEquals((false)[nonstrict_name](), "object");
650       assertEquals((false)[strict_number](), "boolean");
651       assertEquals((false)[nonstrict_number](), "object");
652
653       assertEquals(callStrict("howdy"), "string");
654       assertEquals(callNonStrict("howdy"), "object");
655       assertEquals(callKeyedStrict("howdy"), "string");
656       assertEquals(callKeyedNonStrict("howdy"), "object");
657       assertEquals(callIndexedStrict("howdy"), "string");
658       assertEquals(callIndexedNonStrict("howdy"), "object");
659
660       assertEquals(callStrict(17 + i), "number");
661       assertEquals(callNonStrict(17 + i), "object");
662       assertEquals(callKeyedStrict(17 + i), "number");
663       assertEquals(callKeyedNonStrict(17 + i), "object");
664       assertEquals(callIndexedStrict(17 + i), "number");
665       assertEquals(callIndexedNonStrict(17 + i), "object");
666
667       assertEquals(callStrict(true), "boolean");
668       assertEquals(callNonStrict(true), "object");
669       assertEquals(callKeyedStrict(true), "boolean");
670       assertEquals(callKeyedNonStrict(true), "object");
671       assertEquals(callIndexedStrict(true), "boolean");
672       assertEquals(callIndexedNonStrict(true), "object");
673
674       assertEquals(callStrict(false), "boolean");
675       assertEquals(callNonStrict(false), "object");
676       assertEquals(callKeyedStrict(false), "boolean");
677       assertEquals(callKeyedNonStrict(false), "object");
678       assertEquals(callIndexedStrict(false), "boolean");
679       assertEquals(callIndexedNonStrict(false), "object");
680
681       // All of the above, with getters
682       assertEquals(("hello").strictget(), "string");
683       assertEquals(("hello").nonstrictget(), "object");
684       assertEquals(("hello")[strict_name_get](), "string");
685       assertEquals(("hello")[nonstrict_name_get](), "object");
686       assertEquals(("hello")[strict_number_get](), "string");
687       assertEquals(("hello")[nonstrict_number_get](), "object");
688
689       assertEquals((10 + i).strictget(), "number");
690       assertEquals((10 + i).nonstrictget(), "object");
691       assertEquals((10 + i)[strict_name_get](), "number");
692       assertEquals((10 + i)[nonstrict_name_get](), "object");
693       assertEquals((10 + i)[strict_number_get](), "number");
694       assertEquals((10 + i)[nonstrict_number_get](), "object");
695
696       assertEquals((true).strictget(), "boolean");
697       assertEquals((true).nonstrictget(), "object");
698       assertEquals((true)[strict_name_get](), "boolean");
699       assertEquals((true)[nonstrict_name_get](), "object");
700       assertEquals((true)[strict_number_get](), "boolean");
701       assertEquals((true)[nonstrict_number_get](), "object");
702
703       assertEquals((false).strictget(), "boolean");
704       assertEquals((false).nonstrictget(), "object");
705       assertEquals((false)[strict_name_get](), "boolean");
706       assertEquals((false)[nonstrict_name_get](), "object");
707       assertEquals((false)[strict_number_get](), "boolean");
708       assertEquals((false)[nonstrict_number_get](), "object");
709
710       assertEquals(callStrictGet("howdy"), "string");
711       assertEquals(callNonStrictGet("howdy"), "object");
712       assertEquals(callKeyedStrictGet("howdy"), "string");
713       assertEquals(callKeyedNonStrictGet("howdy"), "object");
714       assertEquals(callIndexedStrictGet("howdy"), "string");
715       assertEquals(callIndexedNonStrictGet("howdy"), "object");
716
717       assertEquals(callStrictGet(17 + i), "number");
718       assertEquals(callNonStrictGet(17 + i), "object");
719       assertEquals(callKeyedStrictGet(17 + i), "number");
720       assertEquals(callKeyedNonStrictGet(17 + i), "object");
721       assertEquals(callIndexedStrictGet(17 + i), "number");
722       assertEquals(callIndexedNonStrictGet(17 + i), "object");
723
724       assertEquals(callStrictGet(true), "boolean");
725       assertEquals(callNonStrictGet(true), "object");
726       assertEquals(callKeyedStrictGet(true), "boolean");
727       assertEquals(callKeyedNonStrictGet(true), "object");
728       assertEquals(callIndexedStrictGet(true), "boolean");
729       assertEquals(callIndexedNonStrictGet(true), "object");
730
731       assertEquals(callStrictGet(false), "boolean");
732       assertEquals(callNonStrictGet(false), "object");
733       assertEquals(callKeyedStrictGet(false), "boolean");
734       assertEquals(callKeyedNonStrictGet(false), "object");
735       assertEquals(callIndexedStrictGet(false), "boolean");
736       assertEquals(callIndexedNonStrictGet(false), "object");
737
738     }
739   } finally {
740     // Cleanup
741     cleanup(String);
742     cleanup(Number);
743     cleanup(Boolean);
744   }
745 })();
746
747
748 (function ObjectEnvironment() {
749   var o = {};
750   Object.defineProperty(o, "foo", { value: "FOO", writable: false });
751   assertThrows(
752     function () {
753       with (o) {
754         (function() {
755           "use strict";
756           foo = "Hello";
757         })();
758       }
759     },
760     TypeError);
761 })();
762
763
764 (function TestSetPropertyWithoutSetter() {
765   var o = { get foo() { return "Yey"; } };
766   assertThrows(
767     function broken() {
768       "use strict";
769       o.foo = (0xBADBAD00 >> 1);
770     },
771     TypeError);
772 })();
773
774
775 (function TestSetPropertyNonConfigurable() {
776   var frozen = Object.freeze({});
777   var sealed = Object.seal({});
778
779   function strict(o) {
780     "use strict";
781     o.property = "value";
782   }
783
784   assertThrows(function() { strict(frozen); }, TypeError);
785   assertThrows(function() { strict(sealed); }, TypeError);
786 })();
787
788
789 (function TestAssignmentToReadOnlyProperty() {
790   "use strict";
791
792   var o = {};
793   Object.defineProperty(o, "property", { value: 7 });
794
795   assertThrows(function() { o.property = "new value"; }, TypeError);
796   assertThrows(function() { o.property += 10; }, TypeError);
797   assertThrows(function() { o.property -= 10; }, TypeError);
798   assertThrows(function() { o.property *= 10; }, TypeError);
799   assertThrows(function() { o.property /= 10; }, TypeError);
800   assertThrows(function() { o.property++; }, TypeError);
801   assertThrows(function() { o.property--; }, TypeError);
802   assertThrows(function() { ++o.property; }, TypeError);
803   assertThrows(function() { --o.property; }, TypeError);
804
805   var name = "prop" + "erty"; // to avoid symbol path.
806   assertThrows(function() { o[name] = "new value"; }, TypeError);
807   assertThrows(function() { o[name] += 10; }, TypeError);
808   assertThrows(function() { o[name] -= 10; }, TypeError);
809   assertThrows(function() { o[name] *= 10; }, TypeError);
810   assertThrows(function() { o[name] /= 10; }, TypeError);
811   assertThrows(function() { o[name]++; }, TypeError);
812   assertThrows(function() { o[name]--; }, TypeError);
813   assertThrows(function() { ++o[name]; }, TypeError);
814   assertThrows(function() { --o[name]; }, TypeError);
815
816   assertEquals(o.property, 7);
817 })();
818
819
820 (function TestAssignmentToReadOnlyLoop() {
821   var name = "prop" + "erty"; // to avoid symbol path.
822   var o = {};
823   Object.defineProperty(o, "property", { value: 7 });
824
825   function strict(o, name) {
826     "use strict";
827     o[name] = "new value";
828   }
829
830   for (var i = 0; i < 10; i ++) {
831     var exception = false;
832     try {
833       strict(o, name);
834     } catch(e) {
835       exception = true;
836       assertInstanceof(e, TypeError);
837     }
838     assertTrue(exception);
839   }
840 })();
841
842
843 // Specialized KeyedStoreIC experiencing miss.
844 (function testKeyedStoreICStrict() {
845   var o = [9,8,7,6,5,4,3,2,1];
846
847   function test(o, i, v) {
848     "use strict";
849     o[i] = v;
850   }
851
852   for (var i = 0; i < 10; i ++) {
853     test(o, 5, 17);        // start specialized for smi indices
854     assertEquals(o[5], 17);
855     test(o, "a", 19);
856     assertEquals(o["a"], 19);
857     test(o, "5", 29);
858     assertEquals(o[5], 29);
859     test(o, 100000, 31);
860     assertEquals(o[100000], 31);
861   }
862 })();
863
864
865 (function TestSetElementWithoutSetter() {
866   "use strict";
867
868   var o = { };
869   Object.defineProperty(o, 0, { get : function() { } });
870
871   var zero_smi = 0;
872   var zero_number = new Number(0);
873   var zero_symbol = "0";
874   var zero_string = "-0-".substring(1,2);
875
876   assertThrows(function() { o[zero_smi] = "new value"; }, TypeError);
877   assertThrows(function() { o[zero_number] = "new value"; }, TypeError);
878   assertThrows(function() { o[zero_symbol] = "new value"; }, TypeError);
879   assertThrows(function() { o[zero_string] = "new value"; }, TypeError);
880 })();
881
882
883 (function TestSetElementNonConfigurable() {
884   "use strict";
885   var frozen = Object.freeze({});
886   var sealed = Object.seal({});
887
888   var zero_number = 0;
889   var zero_symbol = "0";
890   var zero_string = "-0-".substring(1,2);
891
892   assertThrows(function() { frozen[zero_number] = "value"; }, TypeError);
893   assertThrows(function() { sealed[zero_number] = "value"; }, TypeError);
894   assertThrows(function() { frozen[zero_symbol] = "value"; }, TypeError);
895   assertThrows(function() { sealed[zero_symbol] = "value"; }, TypeError);
896   assertThrows(function() { frozen[zero_string] = "value"; }, TypeError);
897   assertThrows(function() { sealed[zero_string] = "value"; }, TypeError);
898 })();
899
900
901 (function TestAssignmentToReadOnlyElement() {
902   "use strict";
903
904   var o = {};
905   Object.defineProperty(o, 7, { value: 17 });
906
907   var seven_smi = 7;
908   var seven_number = new Number(7);
909   var seven_symbol = "7";
910   var seven_string = "-7-".substring(1,2);
911
912   // Index with number.
913   assertThrows(function() { o[seven_smi] = "value"; }, TypeError);
914   assertThrows(function() { o[seven_smi] += 10; }, TypeError);
915   assertThrows(function() { o[seven_smi] -= 10; }, TypeError);
916   assertThrows(function() { o[seven_smi] *= 10; }, TypeError);
917   assertThrows(function() { o[seven_smi] /= 10; }, TypeError);
918   assertThrows(function() { o[seven_smi]++; }, TypeError);
919   assertThrows(function() { o[seven_smi]--; }, TypeError);
920   assertThrows(function() { ++o[seven_smi]; }, TypeError);
921   assertThrows(function() { --o[seven_smi]; }, TypeError);
922
923   assertThrows(function() { o[seven_number] = "value"; }, TypeError);
924   assertThrows(function() { o[seven_number] += 10; }, TypeError);
925   assertThrows(function() { o[seven_number] -= 10; }, TypeError);
926   assertThrows(function() { o[seven_number] *= 10; }, TypeError);
927   assertThrows(function() { o[seven_number] /= 10; }, TypeError);
928   assertThrows(function() { o[seven_number]++; }, TypeError);
929   assertThrows(function() { o[seven_number]--; }, TypeError);
930   assertThrows(function() { ++o[seven_number]; }, TypeError);
931   assertThrows(function() { --o[seven_number]; }, TypeError);
932
933   assertThrows(function() { o[seven_symbol] = "value"; }, TypeError);
934   assertThrows(function() { o[seven_symbol] += 10; }, TypeError);
935   assertThrows(function() { o[seven_symbol] -= 10; }, TypeError);
936   assertThrows(function() { o[seven_symbol] *= 10; }, TypeError);
937   assertThrows(function() { o[seven_symbol] /= 10; }, TypeError);
938   assertThrows(function() { o[seven_symbol]++; }, TypeError);
939   assertThrows(function() { o[seven_symbol]--; }, TypeError);
940   assertThrows(function() { ++o[seven_symbol]; }, TypeError);
941   assertThrows(function() { --o[seven_symbol]; }, TypeError);
942
943   assertThrows(function() { o[seven_string] = "value"; }, TypeError);
944   assertThrows(function() { o[seven_string] += 10; }, TypeError);
945   assertThrows(function() { o[seven_string] -= 10; }, TypeError);
946   assertThrows(function() { o[seven_string] *= 10; }, TypeError);
947   assertThrows(function() { o[seven_string] /= 10; }, TypeError);
948   assertThrows(function() { o[seven_string]++; }, TypeError);
949   assertThrows(function() { o[seven_string]--; }, TypeError);
950   assertThrows(function() { ++o[seven_string]; }, TypeError);
951   assertThrows(function() { --o[seven_string]; }, TypeError);
952
953   assertEquals(o[seven_number], 17);
954   assertEquals(o[seven_symbol], 17);
955   assertEquals(o[seven_string], 17);
956 })();
957
958
959 (function TestAssignmentToReadOnlyLoop() {
960   "use strict";
961
962   var o = {};
963   Object.defineProperty(o, 7, { value: 17 });
964
965   var seven_smi = 7;
966   var seven_number = new Number(7);
967   var seven_symbol = "7";
968   var seven_string = "-7-".substring(1,2);
969
970   for (var i = 0; i < 10; i ++) {
971     assertThrows(function() { o[seven_smi] = "value" }, TypeError);
972     assertThrows(function() { o[seven_number] = "value" }, TypeError);
973     assertThrows(function() { o[seven_symbol] = "value" }, TypeError);
974     assertThrows(function() { o[seven_string] = "value" }, TypeError);
975   }
976
977   assertEquals(o[7], 17);
978 })();
979
980
981 (function TestAssignmentToStringLength() {
982   "use strict";
983
984   var str_val = "string";
985   var str_obj = new String(str_val);
986   var str_cat = str_val + str_val + str_obj;
987
988   assertThrows(function() { str_val.length = 1; }, TypeError);
989   assertThrows(function() { str_obj.length = 1; }, TypeError);
990   assertThrows(function() { str_cat.length = 1; }, TypeError);
991 })();
992
993
994 (function TestArgumentsAliasing() {
995   function strict(a, b) {
996     "use strict";
997     a = "c";
998     b = "d";
999     return [a, b, arguments[0], arguments[1]];
1000   }
1001
1002   function nonstrict(a, b) {
1003     a = "c";
1004     b = "d";
1005     return [a, b, arguments[0], arguments[1]];
1006   }
1007
1008   assertEquals(["c", "d", "a", "b"], strict("a", "b"));
1009   assertEquals(["c", "d", "c", "d"], nonstrict("a", "b"));
1010 })();
1011
1012
1013 function CheckPillDescriptor(func, name) {
1014
1015   function CheckPill(pill) {
1016     assertEquals("function", typeof pill);
1017     assertInstanceof(pill, Function);
1018     pill.property = "value";
1019     assertEquals(pill.value, undefined);
1020     assertThrows(function() { 'use strict'; pill.property = "value"; },
1021                  TypeError);
1022     assertThrows(pill, TypeError);
1023     assertEquals(pill.prototype, (function(){}).prototype);
1024     var d = Object.getOwnPropertyDescriptor(pill, "prototype");
1025     assertFalse(d.writable);
1026     assertFalse(d.configurable);
1027     assertFalse(d.enumerable);
1028   }
1029
1030   var descriptor = Object.getOwnPropertyDescriptor(func, name);
1031   CheckPill(descriptor.get)
1032   CheckPill(descriptor.set);
1033   assertFalse(descriptor.enumerable);
1034   assertFalse(descriptor.configurable);
1035 }
1036
1037
1038 (function TestStrictFunctionPills() {
1039   function strict() {
1040     "use strict";
1041   }
1042   assertThrows(function() { strict.caller; }, TypeError);
1043   assertThrows(function() { strict.arguments; }, TypeError);
1044   assertThrows(function() { strict.caller = 42; }, TypeError);
1045   assertThrows(function() { strict.arguments = 42; }, TypeError);
1046
1047   var another = new Function("'use strict'");
1048   assertThrows(function() { another.caller; }, TypeError);
1049   assertThrows(function() { another.arguments; }, TypeError);
1050   assertThrows(function() { another.caller = 42; }, TypeError);
1051   assertThrows(function() { another.arguments = 42; }, TypeError);
1052
1053   var third = (function() { "use strict"; return function() {}; })();
1054   assertThrows(function() { third.caller; }, TypeError);
1055   assertThrows(function() { third.arguments; }, TypeError);
1056   assertThrows(function() { third.caller = 42; }, TypeError);
1057   assertThrows(function() { third.arguments = 42; }, TypeError);
1058
1059   CheckPillDescriptor(strict, "caller");
1060   CheckPillDescriptor(strict, "arguments");
1061   CheckPillDescriptor(another, "caller");
1062   CheckPillDescriptor(another, "arguments");
1063   CheckPillDescriptor(third, "caller");
1064   CheckPillDescriptor(third, "arguments");
1065 })();
1066
1067
1068 (function TestStrictFunctionWritablePrototype() {
1069   "use strict";
1070   function TheClass() {
1071   }
1072   assertThrows(function() { TheClass.caller; }, TypeError);
1073   assertThrows(function() { TheClass.arguments; }, TypeError);
1074
1075   // Strict functions must have writable prototype.
1076   TheClass.prototype = {
1077     func: function() { return "func_value"; },
1078     get accessor() { return "accessor_value"; },
1079     property: "property_value",
1080   };
1081
1082   var o = new TheClass();
1083   assertEquals(o.func(), "func_value");
1084   assertEquals(o.accessor, "accessor_value");
1085   assertEquals(o.property, "property_value");
1086 })();
1087
1088
1089 (function TestStrictArgumentPills() {
1090   function strict() {
1091     "use strict";
1092     return arguments;
1093   }
1094
1095   var args = strict();
1096   CheckPillDescriptor(args, "caller");
1097   CheckPillDescriptor(args, "callee");
1098
1099   args = strict(17, "value", strict);
1100   assertEquals(17, args[0])
1101   assertEquals("value", args[1])
1102   assertEquals(strict, args[2]);
1103   CheckPillDescriptor(args, "caller");
1104   CheckPillDescriptor(args, "callee");
1105
1106   function outer() {
1107     "use strict";
1108     function inner() {
1109       return arguments;
1110     }
1111     return inner;
1112   }
1113
1114   var args = outer()();
1115   CheckPillDescriptor(args, "caller");
1116   CheckPillDescriptor(args, "callee");
1117
1118   args = outer()(17, "value", strict);
1119   assertEquals(17, args[0])
1120   assertEquals("value", args[1])
1121   assertEquals(strict, args[2]);
1122   CheckPillDescriptor(args, "caller");
1123   CheckPillDescriptor(args, "callee");
1124 })();
1125
1126
1127 (function TestNonStrictFunctionCallerPillSimple() {
1128   function return_my_caller() {
1129     return return_my_caller.caller;
1130   }
1131
1132   function strict() {
1133     "use strict";
1134     return return_my_caller();
1135   }
1136   assertSame(null, strict());
1137
1138   function non_strict() {
1139     return return_my_caller();
1140   }
1141   assertSame(non_strict(), non_strict);
1142 })();
1143
1144
1145 (function TestNonStrictFunctionCallerPill() {
1146   function strict(n) {
1147     "use strict";
1148     return non_strict(n);
1149   }
1150
1151   function recurse(n, then) {
1152     if (n > 0) {
1153       return recurse(n - 1, then);
1154     } else {
1155       return then();
1156     }
1157   }
1158
1159   function non_strict(n) {
1160     return recurse(n, function() { return non_strict.caller; });
1161   }
1162
1163   function test(n) {
1164     return recurse(n, function() { return strict(n); });
1165   }
1166
1167   for (var i = 0; i < 10; i ++) {
1168     assertSame(null, test(i));
1169   }
1170 })();
1171
1172
1173 (function TestNonStrictFunctionCallerDescriptorPill() {
1174   function strict(n) {
1175     "use strict";
1176     return non_strict(n);
1177   }
1178
1179   function recurse(n, then) {
1180     if (n > 0) {
1181       return recurse(n - 1, then);
1182     } else {
1183       return then();
1184     }
1185   }
1186
1187   function non_strict(n) {
1188     return recurse(n, function() {
1189       return Object.getOwnPropertyDescriptor(non_strict, "caller").value;
1190     });
1191   }
1192
1193   function test(n) {
1194     return recurse(n, function() { return strict(n); });
1195   }
1196
1197   for (var i = 0; i < 10; i ++) {
1198     assertSame(null, test(i));
1199   }
1200 })();
1201
1202
1203 (function TestStrictModeEval() {
1204   "use strict";
1205   eval("var eval_local = 10;");
1206   assertThrows(function() { return eval_local; }, ReferenceError);
1207 })();