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 --noharmony-scoping
29 // Flags: --noharmony-classes --noharmony-object-literals
31 function CheckStrictMode(code, exception) {
32 assertDoesNotThrow(code);
33 assertThrows("'use strict';\n" + code, exception);
34 assertThrows('"use strict";\n' + code, exception);
50 function CheckFunctionConstructorStrictMode() {
52 for (var i = 0; i < arguments.length; i ++) {
53 args[i] = arguments[i];
55 // Create non-strict function. No exception.
56 args[arguments.length] = "";
57 assertDoesNotThrow(function() {
58 Function.apply(this, args);
60 // Create strict mode function. Exception expected.
61 args[arguments.length] = "'use strict';";
62 assertThrows(function() {
63 Function.apply(this, args);
67 // Incorrect 'use strict' directive.
68 (function UseStrictEscape() {
73 // Incorrectly place 'use strict' directive.
74 assertThrows("function foo (x) 'use strict'; {}", SyntaxError);
76 // 'use strict' in non-directive position.
77 (function UseStrictNonDirective() {
83 // Multiple directives, including "use strict".
88 "directive after strict";\
90 with({}) {}', SyntaxError);
92 // 'with' disallowed in strict mode.
93 CheckStrictMode("with({}) {}", SyntaxError);
95 // Function named 'eval'.
96 CheckStrictMode("function eval() {}", SyntaxError);
98 // Function named 'arguments'.
99 CheckStrictMode("function arguments() {}", SyntaxError);
101 // Function parameter named 'eval'.
102 CheckStrictMode("function foo(a, b, eval, c, d) {}", SyntaxError);
104 // Function parameter named 'arguments'.
105 CheckStrictMode("function foo(a, b, arguments, c, d) {}", SyntaxError);
107 // Property accessor parameter named 'eval'.
108 CheckStrictMode("var o = { set foo(eval) {} }", SyntaxError);
110 // Property accessor parameter named 'arguments'.
111 CheckStrictMode("var o = { set foo(arguments) {} }", SyntaxError);
113 // Duplicate function parameter name.
114 CheckStrictMode("function foo(a, b, c, d, b) {}", SyntaxError);
116 // Function constructor: eval parameter name.
117 CheckFunctionConstructorStrictMode("eval");
119 // Function constructor: arguments parameter name.
120 CheckFunctionConstructorStrictMode("arguments");
122 // Function constructor: duplicate parameter name.
123 CheckFunctionConstructorStrictMode("a", "b", "c", "b");
124 CheckFunctionConstructorStrictMode("a,b,c,b");
127 CheckStrictMode("try{}catch(eval){};", SyntaxError);
130 CheckStrictMode("try{}catch(arguments){};", SyntaxError);
133 CheckStrictMode("var eval;", SyntaxError);
136 CheckStrictMode("var arguments;", SyntaxError);
138 // Strict mode applies to the function in which the directive is used..
140 function foo(eval) {\
144 // Strict mode doesn't affect the outer stop of strict code.
145 (function NotStrict(eval) {
153 CheckStrictMode("var x = 012");
154 CheckStrictMode("012");
155 CheckStrictMode("'Hello octal\\032'");
156 CheckStrictMode("function octal() { return 012; }");
157 CheckStrictMode("function octal() { return '\\032'; }");
159 (function ValidEscape() {
165 // Octal before "use strict"
168 "octal\\032directive";\
172 (function StrictModeNonDuplicate() {
174 var x = { 123 : 1, "0123" : 2 };
177 '123.00000000000000000000000000000000000000000000000000000000000000000001':
182 // Duplicate data properties are allowed in ES6
183 (function StrictModeDuplicateES6() {
187 123.00000000000000000000000000000000000000000000000000000000000000000001: 2
189 var x = { dupe : 1, nondupe: 3, dupe : 2 };
190 var x = { '1234' : 1, '2345' : 2, '1234' : 3 };
191 var x = { '1234' : 1, '2345' : 2, 1234 : 3 };
192 var x = { 3.14 : 1, 2.71 : 2, 3.14 : 3 };
193 var x = { 3.14 : 1, '3.14' : 2 };
195 var x = { get foo() { }, get foo() { } };
196 var x = { get foo(){}, get 'foo'(){}};
197 var x = { get 12(){}, get '12'(){}};
200 var x = { set foo(v) { }, set foo(v) { } };
201 var x = { set foo(v) { }, set 'foo'(v) { } };
202 var x = { set 13(v) { }, set '13'(v) { } };
205 var x = { foo: 'data', set foo(v) { } };
206 var x = { set foo(v) { }, foo: 'data' };
207 var x = { foo: 'data', set 'foo'(v) { } };
208 var x = { set foo(v) { }, 'foo': 'data' };
209 var x = { 'foo': 'data', set foo(v) { } };
210 var x = { set 'foo'(v) { }, foo: 'data' };
211 var x = { 'foo': 'data', set 'foo'(v) { } };
212 var x = { set 'foo'(v) { }, 'foo': 'data' };
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 var x = { '12': 1, set 12(v){}};
219 var x = { foo: 'data', get foo() { } };
220 var x = { get foo() { }, foo: 'data' };
221 var x = { 'foo': 'data', get foo() { } };
222 var x = { get 'foo'() { }, 'foo': 'data' };
223 var x = { '12': 1, get '12'(){}};
224 var x = { '12': 1, get 12(){}};
227 // Assignment to eval or arguments
228 CheckStrictMode("function strict() { eval = undefined; }", SyntaxError);
229 CheckStrictMode("function strict() { arguments = undefined; }", SyntaxError);
230 CheckStrictMode("function strict() { print(eval = undefined); }", SyntaxError);
231 CheckStrictMode("function strict() { print(arguments = undefined); }",
233 CheckStrictMode("function strict() { var x = eval = undefined; }", SyntaxError);
234 CheckStrictMode("function strict() { var x = arguments = undefined; }",
237 // Compound assignment to eval or arguments
238 CheckStrictMode("function strict() { eval *= undefined; }", SyntaxError);
239 CheckStrictMode("function strict() { arguments /= undefined; }", SyntaxError);
240 CheckStrictMode("function strict() { print(eval %= undefined); }", SyntaxError);
241 CheckStrictMode("function strict() { print(arguments %= undefined); }",
243 CheckStrictMode("function strict() { var x = eval += undefined; }",
245 CheckStrictMode("function strict() { var x = arguments -= undefined; }",
247 CheckStrictMode("function strict() { eval <<= undefined; }", SyntaxError);
248 CheckStrictMode("function strict() { arguments >>= undefined; }", SyntaxError);
249 CheckStrictMode("function strict() { print(eval >>>= undefined); }",
251 CheckStrictMode("function strict() { print(arguments &= undefined); }",
253 CheckStrictMode("function strict() { var x = eval ^= undefined; }",
255 CheckStrictMode("function strict() { var x = arguments |= undefined; }",
258 // Postfix increment with eval or arguments
259 CheckStrictMode("function strict() { eval++; }", SyntaxError);
260 CheckStrictMode("function strict() { arguments++; }", SyntaxError);
261 CheckStrictMode("function strict() { print(eval++); }", SyntaxError);
262 CheckStrictMode("function strict() { print(arguments++); }", SyntaxError);
263 CheckStrictMode("function strict() { var x = eval++; }", SyntaxError);
264 CheckStrictMode("function strict() { var x = arguments++; }", SyntaxError);
266 // Postfix decrement with eval or arguments
267 CheckStrictMode("function strict() { eval--; }", SyntaxError);
268 CheckStrictMode("function strict() { arguments--; }", SyntaxError);
269 CheckStrictMode("function strict() { print(eval--); }", SyntaxError);
270 CheckStrictMode("function strict() { print(arguments--); }", SyntaxError);
271 CheckStrictMode("function strict() { var x = eval--; }", SyntaxError);
272 CheckStrictMode("function strict() { var x = arguments--; }", SyntaxError);
274 // Prefix increment with eval or arguments
275 CheckStrictMode("function strict() { ++eval; }", SyntaxError);
276 CheckStrictMode("function strict() { ++arguments; }", SyntaxError);
277 CheckStrictMode("function strict() { print(++eval); }", SyntaxError);
278 CheckStrictMode("function strict() { print(++arguments); }", SyntaxError);
279 CheckStrictMode("function strict() { var x = ++eval; }", SyntaxError);
280 CheckStrictMode("function strict() { var x = ++arguments; }", SyntaxError);
282 // Prefix decrement with eval or arguments
283 CheckStrictMode("function strict() { --eval; }", SyntaxError);
284 CheckStrictMode("function strict() { --arguments; }", SyntaxError);
285 CheckStrictMode("function strict() { print(--eval); }", SyntaxError);
286 CheckStrictMode("function strict() { print(--arguments); }", SyntaxError);
287 CheckStrictMode("function strict() { var x = --eval; }", SyntaxError);
288 CheckStrictMode("function strict() { var x = --arguments; }", SyntaxError);
290 // Use of const in strict mode is disallowed in anticipation of ES Harmony.
291 CheckStrictMode("const x = 0;", SyntaxError);
292 CheckStrictMode("for (const x = 0; false;) {}", SyntaxError);
293 CheckStrictMode("function strict() { const x = 0; }", SyntaxError);
295 // Strict mode only allows functions in StatementList
296 CheckStrictMode("if (true) { function invalid() {} }", SyntaxError);
297 CheckStrictMode("for (;false;) { function invalid() {} }", SyntaxError);
298 CheckStrictMode("{ function invalid() {} }", SyntaxError);
299 CheckStrictMode("try { function invalid() {} } catch(e) {}", SyntaxError);
300 CheckStrictMode("try { } catch(e) { function invalid() {} }", SyntaxError);
301 CheckStrictMode("function outer() {{ function invalid() {} }}", SyntaxError);
303 // Delete of an unqualified identifier
304 CheckStrictMode("delete unqualified;", SyntaxError);
305 CheckStrictMode("function strict() { delete unqualified; }", SyntaxError);
306 CheckStrictMode("function function_name() { delete function_name; }",
308 CheckStrictMode("function strict(parameter) { delete parameter; }",
310 CheckStrictMode("function strict() { var variable; delete variable; }",
312 CheckStrictMode("var variable; delete variable;", SyntaxError);
314 (function TestStrictDelete() {
316 // "delete this" is allowed in strict mode and should work.
317 function strict_delete() { delete this; }
321 // Prefix unary operators other than delete, ++, -- are valid in strict mode
322 (function StrictModeUnaryOperators() {
324 var x = [void eval, typeof eval, +eval, -eval, ~eval, !eval];
325 var y = [void arguments, typeof arguments,
326 +arguments, -arguments, ~arguments, !arguments];
329 // 7.6.1.2 Future Reserved Words in strict mode
330 var future_strict_reserved_words = [
341 function testFutureStrictReservedWord(word) {
342 // Simple use of each reserved word
343 CheckStrictMode("var " + word + " = 1;", SyntaxError);
344 CheckStrictMode("typeof (" + word + ");", SyntaxError);
346 // object literal properties
347 eval("var x = { " + word + " : 42 };");
348 eval("var x = { get " + word + " () {} };");
349 eval("var x = { set " + word + " (value) {} };");
350 eval("var x = { get " + word + " () { 'use strict'; } };");
351 eval("var x = { set " + word + " (value) { 'use strict'; } };");
353 // object literal with string literal property names
354 eval("var x = { '" + word + "' : 42 };");
355 eval("var x = { get '" + word + "' () { } };");
356 eval("var x = { set '" + word + "' (value) { } };");
357 eval("var x = { get '" + word + "' () { 'use strict'; } };");
358 eval("var x = { set '" + word + "' (value) { 'use strict'; } };");
360 // Function names and arguments, strict and non-strict contexts
361 CheckStrictMode("function " + word + " () {}", SyntaxError);
362 CheckStrictMode("function foo (" + word + ") {}", SyntaxError);
363 CheckStrictMode("function foo (" + word + ", " + word + ") {}", SyntaxError);
364 CheckStrictMode("function foo (a, " + word + ") {}", SyntaxError);
365 CheckStrictMode("function foo (" + word + ", a) {}", SyntaxError);
366 CheckStrictMode("function foo (a, " + word + ", b) {}", SyntaxError);
367 CheckStrictMode("var foo = function (" + word + ") {}", SyntaxError);
369 // Function names and arguments when the body is strict
370 assertThrows("function " + word + " () { 'use strict'; }", SyntaxError);
371 assertThrows("function foo (" + word + ", " + word + ") { 'use strict'; }",
373 assertThrows("function foo (a, " + word + ") { 'use strict'; }", SyntaxError);
374 assertThrows("function foo (" + word + ", a) { 'use strict'; }", SyntaxError);
375 assertThrows("function foo (a, " + word + ", b) { 'use strict'; }",
377 assertThrows("var foo = function (" + word + ") { 'use strict'; }",
380 // setter parameter when the body is strict
381 CheckStrictMode("var x = { set foo(" + word + ") {} };", SyntaxError);
382 assertThrows("var x = { set foo(" + word + ") { 'use strict'; } };",
386 for (var i = 0; i < future_strict_reserved_words.length; i++) {
387 testFutureStrictReservedWord(future_strict_reserved_words[i]);
390 function testAssignToUndefined(test, should_throw) {
394 assertTrue(should_throw, "strict mode");
395 assertInstanceof(e, ReferenceError, "strict mode");
398 assertFalse(should_throw, "strict mode");
401 function repeat(n, f) {
402 for (var i = 0; i < n; i ++) { f(); }
405 function assignToUndefined() {
407 possibly_undefined_variable_for_strict_mode_test = "should throw?";
410 testAssignToUndefined(assignToUndefined, true);
411 testAssignToUndefined(assignToUndefined, true);
412 testAssignToUndefined(assignToUndefined, true);
414 possibly_undefined_variable_for_strict_mode_test = "value";
416 testAssignToUndefined(assignToUndefined, false);
417 testAssignToUndefined(assignToUndefined, false);
418 testAssignToUndefined(assignToUndefined, false);
420 delete possibly_undefined_variable_for_strict_mode_test;
422 testAssignToUndefined(assignToUndefined, true);
423 testAssignToUndefined(assignToUndefined, true);
424 testAssignToUndefined(assignToUndefined, true);
426 repeat(10, function() { testAssignToUndefined(assignToUndefined, true); });
427 possibly_undefined_variable_for_strict_mode_test = "value";
428 repeat(10, function() { testAssignToUndefined(assignToUndefined, false); });
429 delete possibly_undefined_variable_for_strict_mode_test;
430 repeat(10, function() { testAssignToUndefined(assignToUndefined, true); });
431 possibly_undefined_variable_for_strict_mode_test = undefined;
432 repeat(10, function() { testAssignToUndefined(assignToUndefined, false); });
434 function assignToUndefinedWithEval() {
436 possibly_undefined_variable_for_strict_mode_test_with_eval = "should throw?";
440 testAssignToUndefined(assignToUndefinedWithEval, true);
441 testAssignToUndefined(assignToUndefinedWithEval, true);
442 testAssignToUndefined(assignToUndefinedWithEval, true);
444 possibly_undefined_variable_for_strict_mode_test_with_eval = "value";
446 testAssignToUndefined(assignToUndefinedWithEval, false);
447 testAssignToUndefined(assignToUndefinedWithEval, false);
448 testAssignToUndefined(assignToUndefinedWithEval, false);
450 delete possibly_undefined_variable_for_strict_mode_test_with_eval;
452 testAssignToUndefined(assignToUndefinedWithEval, true);
453 testAssignToUndefined(assignToUndefinedWithEval, true);
454 testAssignToUndefined(assignToUndefinedWithEval, true);
456 repeat(10, function() {
457 testAssignToUndefined(assignToUndefinedWithEval, true);
459 possibly_undefined_variable_for_strict_mode_test_with_eval = "value";
460 repeat(10, function() {
461 testAssignToUndefined(assignToUndefinedWithEval, false);
463 delete possibly_undefined_variable_for_strict_mode_test_with_eval;
464 repeat(10, function() {
465 testAssignToUndefined(assignToUndefinedWithEval, true);
467 possibly_undefined_variable_for_strict_mode_test_with_eval = undefined;
468 repeat(10, function() {
469 testAssignToUndefined(assignToUndefinedWithEval, false);
474 (function testDeleteNonConfigurable() {
475 function delete_property(o) {
479 function delete_element(o, i) {
486 Object.defineProperty(object, "property", { value: "property_value" });
487 Object.defineProperty(object, "1", { value: "one" });
488 Object.defineProperty(object, 7, { value: "seven" });
489 Object.defineProperty(object, 3.14, { value: "pi" });
491 assertThrows(function() { delete_property(object); }, TypeError);
492 assertEquals(object.property, "property_value");
493 assertThrows(function() { delete_element(object, "1"); }, TypeError);
494 assertThrows(function() { delete_element(object, 1); }, TypeError);
495 assertEquals(object[1], "one");
496 assertThrows(function() { delete_element(object, "7"); }, TypeError);
497 assertThrows(function() { delete_element(object, 7); }, TypeError);
498 assertEquals(object[7], "seven");
499 assertThrows(function() { delete_element(object, "3.14"); }, TypeError);
500 assertThrows(function() { delete_element(object, 3.14); }, TypeError);
501 assertEquals(object[3.14], "pi");
504 // Not transforming this in Function.call and Function.apply.
505 (function testThisTransformCallApply() {
506 function non_strict() {
514 var global_object = (function() { return this; })();
518 assertTrue(non_strict.call(null) === global_object);
519 assertTrue(non_strict.call(undefined) === global_object);
520 assertEquals(typeof non_strict.call(7), "object");
521 assertEquals(typeof non_strict.call("Hello"), "object");
522 assertTrue(non_strict.call(object) === object);
525 assertTrue(non_strict.apply(null) === global_object);
526 assertTrue(non_strict.apply(undefined) === global_object);
527 assertEquals(typeof non_strict.apply(7), "object");
528 assertEquals(typeof non_strict.apply("Hello"), "object");
529 assertTrue(non_strict.apply(object) === object);
532 assertTrue(strict.call(null) === null);
533 assertTrue(strict.call(undefined) === undefined);
534 assertEquals(typeof strict.call(7), "number");
535 assertEquals(typeof strict.call("Hello"), "string");
536 assertTrue(strict.call(object) === object);
539 assertTrue(strict.apply(null) === null);
540 assertTrue(strict.apply(undefined) === undefined);
541 assertEquals(typeof strict.apply(7), "number");
542 assertEquals(typeof strict.apply("Hello"), "string");
543 assertTrue(strict.apply(object) === object);
546 (function testThisTransform() {
552 function nonstrict() {
556 // Concat to avoid symbol.
557 var strict_name = "str" + "ict";
558 var nonstrict_name = "non" + "str" + "ict";
559 var strict_number = 17;
560 var nonstrict_number = 19;
561 var strict_name_get = "str" + "ict" + "get";
562 var nonstrict_name_get = "non" + "str" + "ict" + "get"
563 var strict_number_get = 23;
564 var nonstrict_number_get = 29;
566 function install(t) {
567 t.prototype.strict = strict;
568 t.prototype.nonstrict = nonstrict;
569 t.prototype[strict_number] = strict;
570 t.prototype[nonstrict_number] = nonstrict;
571 Object.defineProperty(t.prototype, strict_name_get,
572 { get: function() { return strict; },
573 configurable: true });
574 Object.defineProperty(t.prototype, nonstrict_name_get,
575 { get: function() { return nonstrict; },
576 configurable: true });
577 Object.defineProperty(t.prototype, strict_number_get,
578 { get: function() { return strict; },
579 configurable: true });
580 Object.defineProperty(t.prototype, nonstrict_number_get,
581 { get: function() { return nonstrict; },
582 configurable: true });
585 function cleanup(t) {
586 delete t.prototype.strict;
587 delete t.prototype.nonstrict;
588 delete t.prototype[strict_number];
589 delete t.prototype[nonstrict_number];
590 delete t.prototype[strict_name_get];
591 delete t.prototype[nonstrict_name_get];
592 delete t.prototype[strict_number_get];
593 delete t.prototype[nonstrict_number_get];
601 function callStrict(o) {
604 function callNonStrict(o) {
605 return o.nonstrict();
607 function callKeyedStrict(o) {
608 return o[strict_name]();
610 function callKeyedNonStrict(o) {
611 return o[nonstrict_name]();
613 function callIndexedStrict(o) {
614 return o[strict_number]();
616 function callIndexedNonStrict(o) {
617 return o[nonstrict_number]();
619 function callStrictGet(o) {
620 return o.strictget();
622 function callNonStrictGet(o) {
623 return o.nonstrictget();
625 function callKeyedStrictGet(o) {
626 return o[strict_name_get]();
628 function callKeyedNonStrictGet(o) {
629 return o[nonstrict_name_get]();
631 function callIndexedStrictGet(o) {
632 return o[strict_number_get]();
634 function callIndexedNonStrictGet(o) {
635 return o[nonstrict_number_get]();
638 for (var i = 0; i < 10; i ++) {
639 assertEquals(("hello").strict(), "string");
640 assertEquals(("hello").nonstrict(), "object");
641 assertEquals(("hello")[strict_name](), "string");
642 assertEquals(("hello")[nonstrict_name](), "object");
643 assertEquals(("hello")[strict_number](), "string");
644 assertEquals(("hello")[nonstrict_number](), "object");
646 assertEquals((10 + i).strict(), "number");
647 assertEquals((10 + i).nonstrict(), "object");
648 assertEquals((10 + i)[strict_name](), "number");
649 assertEquals((10 + i)[nonstrict_name](), "object");
650 assertEquals((10 + i)[strict_number](), "number");
651 assertEquals((10 + i)[nonstrict_number](), "object");
653 assertEquals((true).strict(), "boolean");
654 assertEquals((true).nonstrict(), "object");
655 assertEquals((true)[strict_name](), "boolean");
656 assertEquals((true)[nonstrict_name](), "object");
657 assertEquals((true)[strict_number](), "boolean");
658 assertEquals((true)[nonstrict_number](), "object");
660 assertEquals((false).strict(), "boolean");
661 assertEquals((false).nonstrict(), "object");
662 assertEquals((false)[strict_name](), "boolean");
663 assertEquals((false)[nonstrict_name](), "object");
664 assertEquals((false)[strict_number](), "boolean");
665 assertEquals((false)[nonstrict_number](), "object");
667 assertEquals(callStrict("howdy"), "string");
668 assertEquals(callNonStrict("howdy"), "object");
669 assertEquals(callKeyedStrict("howdy"), "string");
670 assertEquals(callKeyedNonStrict("howdy"), "object");
671 assertEquals(callIndexedStrict("howdy"), "string");
672 assertEquals(callIndexedNonStrict("howdy"), "object");
674 assertEquals(callStrict(17 + i), "number");
675 assertEquals(callNonStrict(17 + i), "object");
676 assertEquals(callKeyedStrict(17 + i), "number");
677 assertEquals(callKeyedNonStrict(17 + i), "object");
678 assertEquals(callIndexedStrict(17 + i), "number");
679 assertEquals(callIndexedNonStrict(17 + i), "object");
681 assertEquals(callStrict(true), "boolean");
682 assertEquals(callNonStrict(true), "object");
683 assertEquals(callKeyedStrict(true), "boolean");
684 assertEquals(callKeyedNonStrict(true), "object");
685 assertEquals(callIndexedStrict(true), "boolean");
686 assertEquals(callIndexedNonStrict(true), "object");
688 assertEquals(callStrict(false), "boolean");
689 assertEquals(callNonStrict(false), "object");
690 assertEquals(callKeyedStrict(false), "boolean");
691 assertEquals(callKeyedNonStrict(false), "object");
692 assertEquals(callIndexedStrict(false), "boolean");
693 assertEquals(callIndexedNonStrict(false), "object");
695 // All of the above, with getters
696 assertEquals(("hello").strictget(), "string");
697 assertEquals(("hello").nonstrictget(), "object");
698 assertEquals(("hello")[strict_name_get](), "string");
699 assertEquals(("hello")[nonstrict_name_get](), "object");
700 assertEquals(("hello")[strict_number_get](), "string");
701 assertEquals(("hello")[nonstrict_number_get](), "object");
703 assertEquals((10 + i).strictget(), "number");
704 assertEquals((10 + i).nonstrictget(), "object");
705 assertEquals((10 + i)[strict_name_get](), "number");
706 assertEquals((10 + i)[nonstrict_name_get](), "object");
707 assertEquals((10 + i)[strict_number_get](), "number");
708 assertEquals((10 + i)[nonstrict_number_get](), "object");
710 assertEquals((true).strictget(), "boolean");
711 assertEquals((true).nonstrictget(), "object");
712 assertEquals((true)[strict_name_get](), "boolean");
713 assertEquals((true)[nonstrict_name_get](), "object");
714 assertEquals((true)[strict_number_get](), "boolean");
715 assertEquals((true)[nonstrict_number_get](), "object");
717 assertEquals((false).strictget(), "boolean");
718 assertEquals((false).nonstrictget(), "object");
719 assertEquals((false)[strict_name_get](), "boolean");
720 assertEquals((false)[nonstrict_name_get](), "object");
721 assertEquals((false)[strict_number_get](), "boolean");
722 assertEquals((false)[nonstrict_number_get](), "object");
724 assertEquals(callStrictGet("howdy"), "string");
725 assertEquals(callNonStrictGet("howdy"), "object");
726 assertEquals(callKeyedStrictGet("howdy"), "string");
727 assertEquals(callKeyedNonStrictGet("howdy"), "object");
728 assertEquals(callIndexedStrictGet("howdy"), "string");
729 assertEquals(callIndexedNonStrictGet("howdy"), "object");
731 assertEquals(callStrictGet(17 + i), "number");
732 assertEquals(callNonStrictGet(17 + i), "object");
733 assertEquals(callKeyedStrictGet(17 + i), "number");
734 assertEquals(callKeyedNonStrictGet(17 + i), "object");
735 assertEquals(callIndexedStrictGet(17 + i), "number");
736 assertEquals(callIndexedNonStrictGet(17 + i), "object");
738 assertEquals(callStrictGet(true), "boolean");
739 assertEquals(callNonStrictGet(true), "object");
740 assertEquals(callKeyedStrictGet(true), "boolean");
741 assertEquals(callKeyedNonStrictGet(true), "object");
742 assertEquals(callIndexedStrictGet(true), "boolean");
743 assertEquals(callIndexedNonStrictGet(true), "object");
745 assertEquals(callStrictGet(false), "boolean");
746 assertEquals(callNonStrictGet(false), "object");
747 assertEquals(callKeyedStrictGet(false), "boolean");
748 assertEquals(callKeyedNonStrictGet(false), "object");
749 assertEquals(callIndexedStrictGet(false), "boolean");
750 assertEquals(callIndexedNonStrictGet(false), "object");
762 (function ObjectEnvironment() {
764 Object.defineProperty(o, "foo", { value: "FOO", writable: false });
778 (function TestSetPropertyWithoutSetter() {
779 var o = { get foo() { return "Yey"; } };
783 o.foo = (0xBADBAD00 >> 1);
789 (function TestSetPropertyNonConfigurable() {
790 var frozen = Object.freeze({});
791 var sealed = Object.seal({});
795 o.property = "value";
798 assertThrows(function() { strict(frozen); }, TypeError);
799 assertThrows(function() { strict(sealed); }, TypeError);
803 (function TestAssignmentToReadOnlyProperty() {
807 Object.defineProperty(o, "property", { value: 7 });
809 assertThrows(function() { o.property = "new value"; }, TypeError);
810 assertThrows(function() { o.property += 10; }, TypeError);
811 assertThrows(function() { o.property -= 10; }, TypeError);
812 assertThrows(function() { o.property *= 10; }, TypeError);
813 assertThrows(function() { o.property /= 10; }, TypeError);
814 assertThrows(function() { o.property++; }, TypeError);
815 assertThrows(function() { o.property--; }, TypeError);
816 assertThrows(function() { ++o.property; }, TypeError);
817 assertThrows(function() { --o.property; }, TypeError);
819 var name = "prop" + "erty"; // to avoid symbol path.
820 assertThrows(function() { o[name] = "new value"; }, TypeError);
821 assertThrows(function() { o[name] += 10; }, TypeError);
822 assertThrows(function() { o[name] -= 10; }, TypeError);
823 assertThrows(function() { o[name] *= 10; }, TypeError);
824 assertThrows(function() { o[name] /= 10; }, TypeError);
825 assertThrows(function() { o[name]++; }, TypeError);
826 assertThrows(function() { o[name]--; }, TypeError);
827 assertThrows(function() { ++o[name]; }, TypeError);
828 assertThrows(function() { --o[name]; }, TypeError);
830 assertEquals(o.property, 7);
834 (function TestAssignmentToReadOnlyLoop() {
835 var name = "prop" + "erty"; // to avoid symbol path.
837 Object.defineProperty(o, "property", { value: 7 });
839 function strict(o, name) {
841 o[name] = "new value";
844 for (var i = 0; i < 10; i ++) {
845 var exception = false;
850 assertInstanceof(e, TypeError);
852 assertTrue(exception);
857 // Specialized KeyedStoreIC experiencing miss.
858 (function testKeyedStoreICStrict() {
859 var o = [9,8,7,6,5,4,3,2,1];
861 function test(o, i, v) {
866 for (var i = 0; i < 10; i ++) {
867 test(o, 5, 17); // start specialized for smi indices
868 assertEquals(o[5], 17);
870 assertEquals(o["a"], 19);
872 assertEquals(o[5], 29);
874 assertEquals(o[100000], 31);
879 (function TestSetElementWithoutSetter() {
883 Object.defineProperty(o, 0, { get : function() { } });
886 var zero_number = new Number(0);
887 var zero_symbol = "0";
888 var zero_string = "-0-".substring(1,2);
890 assertThrows(function() { o[zero_smi] = "new value"; }, TypeError);
891 assertThrows(function() { o[zero_number] = "new value"; }, TypeError);
892 assertThrows(function() { o[zero_symbol] = "new value"; }, TypeError);
893 assertThrows(function() { o[zero_string] = "new value"; }, TypeError);
897 (function TestSetElementNonConfigurable() {
899 var frozen = Object.freeze({});
900 var sealed = Object.seal({});
903 var zero_symbol = "0";
904 var zero_string = "-0-".substring(1,2);
906 assertThrows(function() { frozen[zero_number] = "value"; }, TypeError);
907 assertThrows(function() { sealed[zero_number] = "value"; }, TypeError);
908 assertThrows(function() { frozen[zero_symbol] = "value"; }, TypeError);
909 assertThrows(function() { sealed[zero_symbol] = "value"; }, TypeError);
910 assertThrows(function() { frozen[zero_string] = "value"; }, TypeError);
911 assertThrows(function() { sealed[zero_string] = "value"; }, TypeError);
915 (function TestAssignmentToReadOnlyElement() {
919 Object.defineProperty(o, 7, { value: 17 });
922 var seven_number = new Number(7);
923 var seven_symbol = "7";
924 var seven_string = "-7-".substring(1,2);
926 // Index with number.
927 assertThrows(function() { o[seven_smi] = "value"; }, TypeError);
928 assertThrows(function() { o[seven_smi] += 10; }, TypeError);
929 assertThrows(function() { o[seven_smi] -= 10; }, TypeError);
930 assertThrows(function() { o[seven_smi] *= 10; }, TypeError);
931 assertThrows(function() { o[seven_smi] /= 10; }, TypeError);
932 assertThrows(function() { o[seven_smi]++; }, TypeError);
933 assertThrows(function() { o[seven_smi]--; }, TypeError);
934 assertThrows(function() { ++o[seven_smi]; }, TypeError);
935 assertThrows(function() { --o[seven_smi]; }, TypeError);
937 assertThrows(function() { o[seven_number] = "value"; }, TypeError);
938 assertThrows(function() { o[seven_number] += 10; }, TypeError);
939 assertThrows(function() { o[seven_number] -= 10; }, TypeError);
940 assertThrows(function() { o[seven_number] *= 10; }, TypeError);
941 assertThrows(function() { o[seven_number] /= 10; }, TypeError);
942 assertThrows(function() { o[seven_number]++; }, TypeError);
943 assertThrows(function() { o[seven_number]--; }, TypeError);
944 assertThrows(function() { ++o[seven_number]; }, TypeError);
945 assertThrows(function() { --o[seven_number]; }, TypeError);
947 assertThrows(function() { o[seven_symbol] = "value"; }, TypeError);
948 assertThrows(function() { o[seven_symbol] += 10; }, TypeError);
949 assertThrows(function() { o[seven_symbol] -= 10; }, TypeError);
950 assertThrows(function() { o[seven_symbol] *= 10; }, TypeError);
951 assertThrows(function() { o[seven_symbol] /= 10; }, TypeError);
952 assertThrows(function() { o[seven_symbol]++; }, TypeError);
953 assertThrows(function() { o[seven_symbol]--; }, TypeError);
954 assertThrows(function() { ++o[seven_symbol]; }, TypeError);
955 assertThrows(function() { --o[seven_symbol]; }, TypeError);
957 assertThrows(function() { o[seven_string] = "value"; }, TypeError);
958 assertThrows(function() { o[seven_string] += 10; }, TypeError);
959 assertThrows(function() { o[seven_string] -= 10; }, TypeError);
960 assertThrows(function() { o[seven_string] *= 10; }, TypeError);
961 assertThrows(function() { o[seven_string] /= 10; }, TypeError);
962 assertThrows(function() { o[seven_string]++; }, TypeError);
963 assertThrows(function() { o[seven_string]--; }, TypeError);
964 assertThrows(function() { ++o[seven_string]; }, TypeError);
965 assertThrows(function() { --o[seven_string]; }, TypeError);
967 assertEquals(o[seven_number], 17);
968 assertEquals(o[seven_symbol], 17);
969 assertEquals(o[seven_string], 17);
973 (function TestAssignmentToReadOnlyLoop() {
977 Object.defineProperty(o, 7, { value: 17 });
980 var seven_number = new Number(7);
981 var seven_symbol = "7";
982 var seven_string = "-7-".substring(1,2);
984 for (var i = 0; i < 10; i ++) {
985 assertThrows(function() { o[seven_smi] = "value" }, TypeError);
986 assertThrows(function() { o[seven_number] = "value" }, TypeError);
987 assertThrows(function() { o[seven_symbol] = "value" }, TypeError);
988 assertThrows(function() { o[seven_string] = "value" }, TypeError);
991 assertEquals(o[7], 17);
995 (function TestAssignmentToStringLength() {
998 var str_val = "string";
999 var str_obj = new String(str_val);
1000 var str_cat = str_val + str_val + str_obj;
1002 assertThrows(function() { str_val.length = 1; }, TypeError);
1003 assertThrows(function() { str_obj.length = 1; }, TypeError);
1004 assertThrows(function() { str_cat.length = 1; }, TypeError);
1008 (function TestArgumentsAliasing() {
1009 function strict(a, b) {
1013 return [a, b, arguments[0], arguments[1]];
1016 function nonstrict(a, b) {
1019 return [a, b, arguments[0], arguments[1]];
1022 assertEquals(["c", "d", "a", "b"], strict("a", "b"));
1023 assertEquals(["c", "d", "c", "d"], nonstrict("a", "b"));
1027 function CheckPillDescriptor(func, name) {
1029 function CheckPill(pill) {
1030 assertEquals("function", typeof pill);
1031 assertInstanceof(pill, Function);
1032 pill.property = "value";
1033 assertEquals(pill.value, undefined);
1034 assertThrows(function() { 'use strict'; pill.property = "value"; },
1036 assertThrows(pill, TypeError);
1037 assertEquals(pill.prototype, (function(){}).prototype);
1038 var d = Object.getOwnPropertyDescriptor(pill, "prototype");
1039 assertFalse(d.writable);
1040 assertFalse(d.configurable);
1041 assertFalse(d.enumerable);
1044 var descriptor = Object.getOwnPropertyDescriptor(func, name);
1045 CheckPill(descriptor.get)
1046 CheckPill(descriptor.set);
1047 assertFalse(descriptor.enumerable);
1048 assertFalse(descriptor.configurable);
1052 (function TestStrictFunctionPills() {
1056 assertThrows(function() { strict.caller; }, TypeError);
1057 assertThrows(function() { strict.arguments; }, TypeError);
1058 assertThrows(function() { strict.caller = 42; }, TypeError);
1059 assertThrows(function() { strict.arguments = 42; }, TypeError);
1061 var another = new Function("'use strict'");
1062 assertThrows(function() { another.caller; }, TypeError);
1063 assertThrows(function() { another.arguments; }, TypeError);
1064 assertThrows(function() { another.caller = 42; }, TypeError);
1065 assertThrows(function() { another.arguments = 42; }, TypeError);
1067 var third = (function() { "use strict"; return function() {}; })();
1068 assertThrows(function() { third.caller; }, TypeError);
1069 assertThrows(function() { third.arguments; }, TypeError);
1070 assertThrows(function() { third.caller = 42; }, TypeError);
1071 assertThrows(function() { third.arguments = 42; }, TypeError);
1073 CheckPillDescriptor(strict, "caller");
1074 CheckPillDescriptor(strict, "arguments");
1075 CheckPillDescriptor(another, "caller");
1076 CheckPillDescriptor(another, "arguments");
1077 CheckPillDescriptor(third, "caller");
1078 CheckPillDescriptor(third, "arguments");
1082 (function TestStrictFunctionWritablePrototype() {
1084 function TheClass() {
1086 assertThrows(function() { TheClass.caller; }, TypeError);
1087 assertThrows(function() { TheClass.arguments; }, TypeError);
1089 // Strict functions must have writable prototype.
1090 TheClass.prototype = {
1091 func: function() { return "func_value"; },
1092 get accessor() { return "accessor_value"; },
1093 property: "property_value",
1096 var o = new TheClass();
1097 assertEquals(o.func(), "func_value");
1098 assertEquals(o.accessor, "accessor_value");
1099 assertEquals(o.property, "property_value");
1103 (function TestStrictArgumentPills() {
1109 var args = strict();
1110 CheckPillDescriptor(args, "caller");
1111 CheckPillDescriptor(args, "callee");
1113 args = strict(17, "value", strict);
1114 assertEquals(17, args[0])
1115 assertEquals("value", args[1])
1116 assertEquals(strict, args[2]);
1117 CheckPillDescriptor(args, "caller");
1118 CheckPillDescriptor(args, "callee");
1128 var args = outer()();
1129 CheckPillDescriptor(args, "caller");
1130 CheckPillDescriptor(args, "callee");
1132 args = outer()(17, "value", strict);
1133 assertEquals(17, args[0])
1134 assertEquals("value", args[1])
1135 assertEquals(strict, args[2]);
1136 CheckPillDescriptor(args, "caller");
1137 CheckPillDescriptor(args, "callee");
1141 (function TestNonStrictFunctionCallerPillSimple() {
1142 function return_my_caller() {
1143 return return_my_caller.caller;
1148 return return_my_caller();
1150 assertSame(null, strict());
1152 function non_strict() {
1153 return return_my_caller();
1155 assertSame(non_strict(), non_strict);
1159 (function TestNonStrictFunctionCallerPill() {
1160 function strict(n) {
1162 return non_strict(n);
1165 function recurse(n, then) {
1167 return recurse(n - 1, then);
1173 function non_strict(n) {
1174 return recurse(n, function() { return non_strict.caller; });
1178 return recurse(n, function() { return strict(n); });
1181 for (var i = 0; i < 10; i ++) {
1182 assertSame(null, test(i));
1187 (function TestNonStrictFunctionCallerDescriptorPill() {
1188 function strict(n) {
1190 return non_strict(n);
1193 function recurse(n, then) {
1195 return recurse(n - 1, then);
1201 function non_strict(n) {
1202 return recurse(n, function() {
1203 return Object.getOwnPropertyDescriptor(non_strict, "caller").value;
1208 return recurse(n, function() { return strict(n); });
1211 for (var i = 0; i < 10; i ++) {
1212 assertSame(null, test(i));
1217 (function TestStrictModeEval() {
1219 eval("var eval_local = 10;");
1220 assertThrows(function() { return eval_local; }, ReferenceError);