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
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.
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.
28 // Flags: --turbo-deoptimization
30 function CheckStrictMode(code, exception) {
31 assertDoesNotThrow(code);
32 assertThrows("'use strict';\n" + code, exception);
33 assertThrows('"use strict";\n' + code, exception);
49 function CheckFunctionConstructorStrictMode() {
51 for (var i = 0; i < arguments.length; i ++) {
52 args[i] = arguments[i];
54 // Create non-strict function. No exception.
55 args[arguments.length] = "";
56 assertDoesNotThrow(function() {
57 Function.apply(this, args);
59 // Create strict mode function. Exception expected.
60 args[arguments.length] = "'use strict';";
61 assertThrows(function() {
62 Function.apply(this, args);
66 // Incorrect 'use strict' directive.
67 (function UseStrictEscape() {
72 // Incorrectly place 'use strict' directive.
73 assertThrows("function foo (x) 'use strict'; {}", SyntaxError);
75 // 'use strict' in non-directive position.
76 (function UseStrictNonDirective() {
82 // Multiple directives, including "use strict".
87 "directive after strict";\
89 with({}) {}', SyntaxError);
91 // 'with' disallowed in strict mode.
92 CheckStrictMode("with({}) {}", SyntaxError);
94 // Function named 'eval'.
95 CheckStrictMode("function eval() {}", SyntaxError);
97 // Function named 'arguments'.
98 CheckStrictMode("function arguments() {}", SyntaxError);
100 // Function parameter named 'eval'.
101 CheckStrictMode("function foo(a, b, eval, c, d) {}", SyntaxError);
103 // Function parameter named 'arguments'.
104 CheckStrictMode("function foo(a, b, arguments, c, d) {}", SyntaxError);
106 // Property accessor parameter named 'eval'.
107 CheckStrictMode("var o = { set foo(eval) {} }", SyntaxError);
109 // Property accessor parameter named 'arguments'.
110 CheckStrictMode("var o = { set foo(arguments) {} }", SyntaxError);
112 // Duplicate function parameter name.
113 CheckStrictMode("function foo(a, b, c, d, b) {}", SyntaxError);
115 // Function constructor: eval parameter name.
116 CheckFunctionConstructorStrictMode("eval");
118 // Function constructor: arguments parameter name.
119 CheckFunctionConstructorStrictMode("arguments");
121 // Function constructor: duplicate parameter name.
122 CheckFunctionConstructorStrictMode("a", "b", "c", "b");
123 CheckFunctionConstructorStrictMode("a,b,c,b");
126 CheckStrictMode("try{}catch(eval){};", SyntaxError);
129 CheckStrictMode("try{}catch(arguments){};", SyntaxError);
132 CheckStrictMode("var eval;", SyntaxError);
135 CheckStrictMode("var arguments;", SyntaxError);
137 // Strict mode applies to the function in which the directive is used..
139 function foo(eval) {\
143 // Strict mode doesn't affect the outer stop of strict code.
144 (function NotStrict(eval) {
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'; }");
158 (function ValidEscape() {
164 // Octal before "use strict"
167 "octal\\032directive";\
171 (function StrictModeNonDuplicate() {
173 var x = { 123 : 1, "0123" : 2 };
176 '123.00000000000000000000000000000000000000000000000000000000000000000001':
181 // Duplicate data properties are allowed in ES6
182 (function StrictModeDuplicateES6() {
186 123.00000000000000000000000000000000000000000000000000000000000000000001: 2
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 };
194 var x = { get foo() { }, get foo() { } };
195 var x = { get foo(){}, get 'foo'(){}};
196 var x = { get 12(){}, get '12'(){}};
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) { } };
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){}};
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(){}};
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); }",
232 CheckStrictMode("function strict() { var x = eval = undefined; }", SyntaxError);
233 CheckStrictMode("function strict() { var x = arguments = undefined; }",
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); }",
242 CheckStrictMode("function strict() { var x = eval += undefined; }",
244 CheckStrictMode("function strict() { var x = arguments -= undefined; }",
246 CheckStrictMode("function strict() { eval <<= undefined; }", SyntaxError);
247 CheckStrictMode("function strict() { arguments >>= undefined; }", SyntaxError);
248 CheckStrictMode("function strict() { print(eval >>>= undefined); }",
250 CheckStrictMode("function strict() { print(arguments &= undefined); }",
252 CheckStrictMode("function strict() { var x = eval ^= undefined; }",
254 CheckStrictMode("function strict() { var x = arguments |= undefined; }",
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);
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);
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);
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);
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; }",
294 CheckStrictMode("function strict(parameter) { delete parameter; }",
296 CheckStrictMode("function strict() { var variable; delete variable; }",
298 CheckStrictMode("var variable; delete variable;", SyntaxError);
300 (function TestStrictDelete() {
302 // "delete this" is allowed in strict mode and should work.
303 function strict_delete() { delete this; }
307 // Prefix unary operators other than delete, ++, -- are valid in strict mode
308 (function StrictModeUnaryOperators() {
310 var x = [void eval, typeof eval, +eval, -eval, ~eval, !eval];
311 var y = [void arguments, typeof arguments,
312 +arguments, -arguments, ~arguments, !arguments];
315 // 7.6.1.2 Future Reserved Words in strict mode
316 var future_strict_reserved_words = [
327 function testFutureStrictReservedWord(word) {
328 // Simple use of each reserved word
329 CheckStrictMode("var " + word + " = 1;", SyntaxError);
330 CheckStrictMode("typeof (" + word + ");", SyntaxError);
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'; } };");
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'; } };");
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);
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'; }",
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'; }",
363 assertThrows("var foo = function (" + word + ") { 'use strict'; }",
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'; } };",
372 for (var i = 0; i < future_strict_reserved_words.length; i++) {
373 testFutureStrictReservedWord(future_strict_reserved_words[i]);
376 function testAssignToUndefined(test, should_throw) {
380 assertTrue(should_throw, "strict mode");
381 assertInstanceof(e, ReferenceError, "strict mode");
384 assertFalse(should_throw, "strict mode");
387 function repeat(n, f) {
388 for (var i = 0; i < n; i ++) { f(); }
391 function assignToUndefined() {
393 possibly_undefined_variable_for_strict_mode_test = "should throw?";
396 testAssignToUndefined(assignToUndefined, true);
397 testAssignToUndefined(assignToUndefined, true);
398 testAssignToUndefined(assignToUndefined, true);
400 possibly_undefined_variable_for_strict_mode_test = "value";
402 testAssignToUndefined(assignToUndefined, false);
403 testAssignToUndefined(assignToUndefined, false);
404 testAssignToUndefined(assignToUndefined, false);
406 delete possibly_undefined_variable_for_strict_mode_test;
408 testAssignToUndefined(assignToUndefined, true);
409 testAssignToUndefined(assignToUndefined, true);
410 testAssignToUndefined(assignToUndefined, true);
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); });
420 function assignToUndefinedWithEval() {
422 possibly_undefined_variable_for_strict_mode_test_with_eval = "should throw?";
426 testAssignToUndefined(assignToUndefinedWithEval, true);
427 testAssignToUndefined(assignToUndefinedWithEval, true);
428 testAssignToUndefined(assignToUndefinedWithEval, true);
430 possibly_undefined_variable_for_strict_mode_test_with_eval = "value";
432 testAssignToUndefined(assignToUndefinedWithEval, false);
433 testAssignToUndefined(assignToUndefinedWithEval, false);
434 testAssignToUndefined(assignToUndefinedWithEval, false);
436 delete possibly_undefined_variable_for_strict_mode_test_with_eval;
438 testAssignToUndefined(assignToUndefinedWithEval, true);
439 testAssignToUndefined(assignToUndefinedWithEval, true);
440 testAssignToUndefined(assignToUndefinedWithEval, true);
442 repeat(10, function() {
443 testAssignToUndefined(assignToUndefinedWithEval, true);
445 possibly_undefined_variable_for_strict_mode_test_with_eval = "value";
446 repeat(10, function() {
447 testAssignToUndefined(assignToUndefinedWithEval, false);
449 delete possibly_undefined_variable_for_strict_mode_test_with_eval;
450 repeat(10, function() {
451 testAssignToUndefined(assignToUndefinedWithEval, true);
453 possibly_undefined_variable_for_strict_mode_test_with_eval = undefined;
454 repeat(10, function() {
455 testAssignToUndefined(assignToUndefinedWithEval, false);
460 (function testDeleteNonConfigurable() {
461 function delete_property(o) {
465 function delete_element(o, i) {
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" });
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");
490 // Not transforming this in Function.call and Function.apply.
491 (function testThisTransformCallApply() {
492 function non_strict() {
500 var global_object = (function() { return this; })();
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);
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);
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);
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);
532 (function testThisTransform() {
538 function nonstrict() {
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;
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 });
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];
587 function callStrict(o) {
590 function callNonStrict(o) {
591 return o.nonstrict();
593 function callKeyedStrict(o) {
594 return o[strict_name]();
596 function callKeyedNonStrict(o) {
597 return o[nonstrict_name]();
599 function callIndexedStrict(o) {
600 return o[strict_number]();
602 function callIndexedNonStrict(o) {
603 return o[nonstrict_number]();
605 function callStrictGet(o) {
606 return o.strictget();
608 function callNonStrictGet(o) {
609 return o.nonstrictget();
611 function callKeyedStrictGet(o) {
612 return o[strict_name_get]();
614 function callKeyedNonStrictGet(o) {
615 return o[nonstrict_name_get]();
617 function callIndexedStrictGet(o) {
618 return o[strict_number_get]();
620 function callIndexedNonStrictGet(o) {
621 return o[nonstrict_number_get]();
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
748 (function ObjectEnvironment() {
750 Object.defineProperty(o, "foo", { value: "FOO", writable: false });
764 (function TestSetPropertyWithoutSetter() {
765 var o = { get foo() { return "Yey"; } };
769 o.foo = (0xBADBAD00 >> 1);
775 (function TestSetPropertyNonConfigurable() {
776 var frozen = Object.freeze({});
777 var sealed = Object.seal({});
781 o.property = "value";
784 assertThrows(function() { strict(frozen); }, TypeError);
785 assertThrows(function() { strict(sealed); }, TypeError);
789 (function TestAssignmentToReadOnlyProperty() {
793 Object.defineProperty(o, "property", { value: 7 });
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);
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);
816 assertEquals(o.property, 7);
820 (function TestAssignmentToReadOnlyLoop() {
821 var name = "prop" + "erty"; // to avoid symbol path.
823 Object.defineProperty(o, "property", { value: 7 });
825 function strict(o, name) {
827 o[name] = "new value";
830 for (var i = 0; i < 10; i ++) {
831 var exception = false;
836 assertInstanceof(e, TypeError);
838 assertTrue(exception);
843 // Specialized KeyedStoreIC experiencing miss.
844 (function testKeyedStoreICStrict() {
845 var o = [9,8,7,6,5,4,3,2,1];
847 function test(o, i, v) {
852 for (var i = 0; i < 10; i ++) {
853 test(o, 5, 17); // start specialized for smi indices
854 assertEquals(o[5], 17);
856 assertEquals(o["a"], 19);
858 assertEquals(o[5], 29);
860 assertEquals(o[100000], 31);
865 (function TestSetElementWithoutSetter() {
869 Object.defineProperty(o, 0, { get : function() { } });
872 var zero_number = new Number(0);
873 var zero_symbol = "0";
874 var zero_string = "-0-".substring(1,2);
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);
883 (function TestSetElementNonConfigurable() {
885 var frozen = Object.freeze({});
886 var sealed = Object.seal({});
889 var zero_symbol = "0";
890 var zero_string = "-0-".substring(1,2);
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);
901 (function TestAssignmentToReadOnlyElement() {
905 Object.defineProperty(o, 7, { value: 17 });
908 var seven_number = new Number(7);
909 var seven_symbol = "7";
910 var seven_string = "-7-".substring(1,2);
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);
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);
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);
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);
953 assertEquals(o[seven_number], 17);
954 assertEquals(o[seven_symbol], 17);
955 assertEquals(o[seven_string], 17);
959 (function TestAssignmentToReadOnlyLoop() {
963 Object.defineProperty(o, 7, { value: 17 });
966 var seven_number = new Number(7);
967 var seven_symbol = "7";
968 var seven_string = "-7-".substring(1,2);
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);
977 assertEquals(o[7], 17);
981 (function TestAssignmentToStringLength() {
984 var str_val = "string";
985 var str_obj = new String(str_val);
986 var str_cat = str_val + str_val + str_obj;
988 assertThrows(function() { str_val.length = 1; }, TypeError);
989 assertThrows(function() { str_obj.length = 1; }, TypeError);
990 assertThrows(function() { str_cat.length = 1; }, TypeError);
994 (function TestArgumentsAliasing() {
995 function strict(a, b) {
999 return [a, b, arguments[0], arguments[1]];
1002 function nonstrict(a, b) {
1005 return [a, b, arguments[0], arguments[1]];
1008 assertEquals(["c", "d", "a", "b"], strict("a", "b"));
1009 assertEquals(["c", "d", "c", "d"], nonstrict("a", "b"));
1013 function CheckPillDescriptor(func, name) {
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"; },
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);
1030 var descriptor = Object.getOwnPropertyDescriptor(func, name);
1031 CheckPill(descriptor.get)
1032 CheckPill(descriptor.set);
1033 assertFalse(descriptor.enumerable);
1034 assertFalse(descriptor.configurable);
1038 (function TestStrictFunctionPills() {
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);
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);
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);
1059 CheckPillDescriptor(strict, "caller");
1060 CheckPillDescriptor(strict, "arguments");
1061 CheckPillDescriptor(another, "caller");
1062 CheckPillDescriptor(another, "arguments");
1063 CheckPillDescriptor(third, "caller");
1064 CheckPillDescriptor(third, "arguments");
1068 (function TestStrictFunctionWritablePrototype() {
1070 function TheClass() {
1072 assertThrows(function() { TheClass.caller; }, TypeError);
1073 assertThrows(function() { TheClass.arguments; }, TypeError);
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",
1082 var o = new TheClass();
1083 assertEquals(o.func(), "func_value");
1084 assertEquals(o.accessor, "accessor_value");
1085 assertEquals(o.property, "property_value");
1089 (function TestStrictArgumentPills() {
1095 var args = strict();
1096 CheckPillDescriptor(args, "caller");
1097 CheckPillDescriptor(args, "callee");
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");
1114 var args = outer()();
1115 CheckPillDescriptor(args, "caller");
1116 CheckPillDescriptor(args, "callee");
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");
1127 (function TestNonStrictFunctionCallerPillSimple() {
1128 function return_my_caller() {
1129 return return_my_caller.caller;
1134 return return_my_caller();
1136 assertSame(null, strict());
1138 function non_strict() {
1139 return return_my_caller();
1141 assertSame(non_strict(), non_strict);
1145 (function TestNonStrictFunctionCallerPill() {
1146 function strict(n) {
1148 return non_strict(n);
1151 function recurse(n, then) {
1153 return recurse(n - 1, then);
1159 function non_strict(n) {
1160 return recurse(n, function() { return non_strict.caller; });
1164 return recurse(n, function() { return strict(n); });
1167 for (var i = 0; i < 10; i ++) {
1168 assertSame(null, test(i));
1173 (function TestNonStrictFunctionCallerDescriptorPill() {
1174 function strict(n) {
1176 return non_strict(n);
1179 function recurse(n, then) {
1181 return recurse(n - 1, then);
1187 function non_strict(n) {
1188 return recurse(n, function() {
1189 return Object.getOwnPropertyDescriptor(non_strict, "caller").value;
1194 return recurse(n, function() { return strict(n); });
1197 for (var i = 0; i < 10; i ++) {
1198 assertSame(null, test(i));
1203 (function TestStrictModeEval() {
1205 eval("var eval_local = 10;");
1206 assertThrows(function() { return eval_local; }, ReferenceError);