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 // Duplicate data properties.
172 CheckStrictMode("var x = { dupe : 1, nondupe: 3, dupe : 2 };", SyntaxError);
173 CheckStrictMode("var x = { '1234' : 1, '2345' : 2, '1234' : 3 };", SyntaxError);
174 CheckStrictMode("var x = { '1234' : 1, '2345' : 2, 1234 : 3 };", SyntaxError);
175 CheckStrictMode("var x = { 3.14 : 1, 2.71 : 2, 3.14 : 3 };", SyntaxError);
176 CheckStrictMode("var x = { 3.14 : 1, '3.14' : 2 };", SyntaxError);
177 CheckStrictMode("var x = { \
179 123.00000000000000000000000000000000000000000000000000000000000000000001: 2 \
182 // Non-conflicting data properties.
183 (function StrictModeNonDuplicate() {
185 var x = { 123 : 1, "0123" : 2 };
188 '123.00000000000000000000000000000000000000000000000000000000000000000001':
193 // Two getters (non-strict)
194 assertThrows("var x = { get foo() { }, get foo() { } };", SyntaxError);
195 assertThrows("var x = { get foo(){}, get 'foo'(){}};", SyntaxError);
196 assertThrows("var x = { get 12(){}, get '12'(){}};", SyntaxError);
198 // Two setters (non-strict)
199 assertThrows("var x = { set foo(v) { }, set foo(v) { } };", SyntaxError);
200 assertThrows("var x = { set foo(v) { }, set 'foo'(v) { } };", SyntaxError);
201 assertThrows("var x = { set 13(v) { }, set '13'(v) { } };", SyntaxError);
203 // Setter and data (non-strict)
204 assertThrows("var x = { foo: 'data', set foo(v) { } };", SyntaxError);
205 assertThrows("var x = { set foo(v) { }, foo: 'data' };", SyntaxError);
206 assertThrows("var x = { foo: 'data', set 'foo'(v) { } };", SyntaxError);
207 assertThrows("var x = { set foo(v) { }, 'foo': 'data' };", SyntaxError);
208 assertThrows("var x = { 'foo': 'data', set foo(v) { } };", SyntaxError);
209 assertThrows("var x = { set 'foo'(v) { }, foo: 'data' };", SyntaxError);
210 assertThrows("var x = { 'foo': 'data', set 'foo'(v) { } };", SyntaxError);
211 assertThrows("var x = { set 'foo'(v) { }, 'foo': 'data' };", SyntaxError);
212 assertThrows("var x = { 12: 1, set '12'(v){}};", SyntaxError);
213 assertThrows("var x = { 12: 1, set 12(v){}};", SyntaxError);
214 assertThrows("var x = { '12': 1, set '12'(v){}};", SyntaxError);
215 assertThrows("var x = { '12': 1, set 12(v){}};", SyntaxError);
217 // Getter and data (non-strict)
218 assertThrows("var x = { foo: 'data', get foo() { } };", SyntaxError);
219 assertThrows("var x = { get foo() { }, foo: 'data' };", SyntaxError);
220 assertThrows("var x = { 'foo': 'data', get foo() { } };", SyntaxError);
221 assertThrows("var x = { get 'foo'() { }, 'foo': 'data' };", SyntaxError);
222 assertThrows("var x = { '12': 1, get '12'(){}};", SyntaxError);
223 assertThrows("var x = { '12': 1, get 12(){}};", SyntaxError);
225 // Assignment to eval or arguments
226 CheckStrictMode("function strict() { eval = undefined; }", SyntaxError);
227 CheckStrictMode("function strict() { arguments = undefined; }", SyntaxError);
228 CheckStrictMode("function strict() { print(eval = undefined); }", SyntaxError);
229 CheckStrictMode("function strict() { print(arguments = undefined); }",
231 CheckStrictMode("function strict() { var x = eval = undefined; }", SyntaxError);
232 CheckStrictMode("function strict() { var x = arguments = undefined; }",
235 // Compound assignment to eval or arguments
236 CheckStrictMode("function strict() { eval *= undefined; }", SyntaxError);
237 CheckStrictMode("function strict() { arguments /= undefined; }", SyntaxError);
238 CheckStrictMode("function strict() { print(eval %= undefined); }", SyntaxError);
239 CheckStrictMode("function strict() { print(arguments %= undefined); }",
241 CheckStrictMode("function strict() { var x = eval += undefined; }",
243 CheckStrictMode("function strict() { var x = arguments -= undefined; }",
245 CheckStrictMode("function strict() { eval <<= undefined; }", SyntaxError);
246 CheckStrictMode("function strict() { arguments >>= undefined; }", SyntaxError);
247 CheckStrictMode("function strict() { print(eval >>>= undefined); }",
249 CheckStrictMode("function strict() { print(arguments &= undefined); }",
251 CheckStrictMode("function strict() { var x = eval ^= undefined; }",
253 CheckStrictMode("function strict() { var x = arguments |= undefined; }",
256 // Postfix increment with eval or arguments
257 CheckStrictMode("function strict() { eval++; }", SyntaxError);
258 CheckStrictMode("function strict() { arguments++; }", SyntaxError);
259 CheckStrictMode("function strict() { print(eval++); }", SyntaxError);
260 CheckStrictMode("function strict() { print(arguments++); }", SyntaxError);
261 CheckStrictMode("function strict() { var x = eval++; }", SyntaxError);
262 CheckStrictMode("function strict() { var x = arguments++; }", SyntaxError);
264 // Postfix decrement with eval or arguments
265 CheckStrictMode("function strict() { eval--; }", SyntaxError);
266 CheckStrictMode("function strict() { arguments--; }", SyntaxError);
267 CheckStrictMode("function strict() { print(eval--); }", SyntaxError);
268 CheckStrictMode("function strict() { print(arguments--); }", SyntaxError);
269 CheckStrictMode("function strict() { var x = eval--; }", SyntaxError);
270 CheckStrictMode("function strict() { var x = arguments--; }", SyntaxError);
272 // Prefix increment with eval or arguments
273 CheckStrictMode("function strict() { ++eval; }", SyntaxError);
274 CheckStrictMode("function strict() { ++arguments; }", SyntaxError);
275 CheckStrictMode("function strict() { print(++eval); }", SyntaxError);
276 CheckStrictMode("function strict() { print(++arguments); }", SyntaxError);
277 CheckStrictMode("function strict() { var x = ++eval; }", SyntaxError);
278 CheckStrictMode("function strict() { var x = ++arguments; }", SyntaxError);
280 // Prefix decrement with eval or arguments
281 CheckStrictMode("function strict() { --eval; }", SyntaxError);
282 CheckStrictMode("function strict() { --arguments; }", SyntaxError);
283 CheckStrictMode("function strict() { print(--eval); }", SyntaxError);
284 CheckStrictMode("function strict() { print(--arguments); }", SyntaxError);
285 CheckStrictMode("function strict() { var x = --eval; }", SyntaxError);
286 CheckStrictMode("function strict() { var x = --arguments; }", SyntaxError);
288 // Use of const in strict mode is disallowed in anticipation of ES Harmony.
289 CheckStrictMode("const x = 0;", SyntaxError);
290 CheckStrictMode("for (const x = 0; false;) {}", SyntaxError);
291 CheckStrictMode("function strict() { const x = 0; }", SyntaxError);
293 // Strict mode only allows functions in SourceElements
294 CheckStrictMode("if (true) { function invalid() {} }", SyntaxError);
295 CheckStrictMode("for (;false;) { function invalid() {} }", SyntaxError);
296 CheckStrictMode("{ function invalid() {} }", SyntaxError);
297 CheckStrictMode("try { function invalid() {} } catch(e) {}", SyntaxError);
298 CheckStrictMode("try { } catch(e) { function invalid() {} }", SyntaxError);
299 CheckStrictMode("function outer() {{ function invalid() {} }}", SyntaxError);
301 // Delete of an unqualified identifier
302 CheckStrictMode("delete unqualified;", SyntaxError);
303 CheckStrictMode("function strict() { delete unqualified; }", SyntaxError);
304 CheckStrictMode("function function_name() { delete function_name; }",
306 CheckStrictMode("function strict(parameter) { delete parameter; }",
308 CheckStrictMode("function strict() { var variable; delete variable; }",
310 CheckStrictMode("var variable; delete variable;", SyntaxError);
312 (function TestStrictDelete() {
314 // "delete this" is allowed in strict mode and should work.
315 function strict_delete() { delete this; }
319 // Prefix unary operators other than delete, ++, -- are valid in strict mode
320 (function StrictModeUnaryOperators() {
322 var x = [void eval, typeof eval, +eval, -eval, ~eval, !eval];
323 var y = [void arguments, typeof arguments,
324 +arguments, -arguments, ~arguments, !arguments];
327 // 7.6.1.2 Future Reserved Words in strict mode
328 var future_strict_reserved_words = [
339 function testFutureStrictReservedWord(word) {
340 // Simple use of each reserved word
341 CheckStrictMode("var " + word + " = 1;", SyntaxError);
342 CheckStrictMode("typeof (" + word + ");", SyntaxError);
344 // object literal properties
345 eval("var x = { " + word + " : 42 };");
346 eval("var x = { get " + word + " () {} };");
347 eval("var x = { set " + word + " (value) {} };");
348 eval("var x = { get " + word + " () { 'use strict'; } };");
349 eval("var x = { set " + word + " (value) { 'use strict'; } };");
351 // object literal with string literal property names
352 eval("var x = { '" + word + "' : 42 };");
353 eval("var x = { get '" + word + "' () { } };");
354 eval("var x = { set '" + word + "' (value) { } };");
355 eval("var x = { get '" + word + "' () { 'use strict'; } };");
356 eval("var x = { set '" + word + "' (value) { 'use strict'; } };");
358 // Function names and arguments, strict and non-strict contexts
359 CheckStrictMode("function " + word + " () {}", SyntaxError);
360 CheckStrictMode("function foo (" + word + ") {}", SyntaxError);
361 CheckStrictMode("function foo (" + word + ", " + word + ") {}", SyntaxError);
362 CheckStrictMode("function foo (a, " + word + ") {}", SyntaxError);
363 CheckStrictMode("function foo (" + word + ", a) {}", SyntaxError);
364 CheckStrictMode("function foo (a, " + word + ", b) {}", SyntaxError);
365 CheckStrictMode("var foo = function (" + word + ") {}", SyntaxError);
367 // Function names and arguments when the body is strict
368 assertThrows("function " + word + " () { 'use strict'; }", SyntaxError);
369 assertThrows("function foo (" + word + ", " + word + ") { 'use strict'; }",
371 assertThrows("function foo (a, " + word + ") { 'use strict'; }", SyntaxError);
372 assertThrows("function foo (" + word + ", a) { 'use strict'; }", SyntaxError);
373 assertThrows("function foo (a, " + word + ", b) { 'use strict'; }",
375 assertThrows("var foo = function (" + word + ") { 'use strict'; }",
378 // setter parameter when the body is strict
379 CheckStrictMode("var x = { set foo(" + word + ") {} };", SyntaxError);
380 assertThrows("var x = { set foo(" + word + ") { 'use strict'; } };",
384 for (var i = 0; i < future_strict_reserved_words.length; i++) {
385 testFutureStrictReservedWord(future_strict_reserved_words[i]);
388 function testAssignToUndefined(test, should_throw) {
392 assertTrue(should_throw, "strict mode");
393 assertInstanceof(e, ReferenceError, "strict mode");
396 assertFalse(should_throw, "strict mode");
399 function repeat(n, f) {
400 for (var i = 0; i < n; i ++) { f(); }
403 function assignToUndefined() {
405 possibly_undefined_variable_for_strict_mode_test = "should throw?";
408 testAssignToUndefined(assignToUndefined, true);
409 testAssignToUndefined(assignToUndefined, true);
410 testAssignToUndefined(assignToUndefined, true);
412 possibly_undefined_variable_for_strict_mode_test = "value";
414 testAssignToUndefined(assignToUndefined, false);
415 testAssignToUndefined(assignToUndefined, false);
416 testAssignToUndefined(assignToUndefined, false);
418 delete possibly_undefined_variable_for_strict_mode_test;
420 testAssignToUndefined(assignToUndefined, true);
421 testAssignToUndefined(assignToUndefined, true);
422 testAssignToUndefined(assignToUndefined, true);
424 repeat(10, function() { testAssignToUndefined(assignToUndefined, true); });
425 possibly_undefined_variable_for_strict_mode_test = "value";
426 repeat(10, function() { testAssignToUndefined(assignToUndefined, false); });
427 delete possibly_undefined_variable_for_strict_mode_test;
428 repeat(10, function() { testAssignToUndefined(assignToUndefined, true); });
429 possibly_undefined_variable_for_strict_mode_test = undefined;
430 repeat(10, function() { testAssignToUndefined(assignToUndefined, false); });
432 function assignToUndefinedWithEval() {
434 possibly_undefined_variable_for_strict_mode_test_with_eval = "should throw?";
438 testAssignToUndefined(assignToUndefinedWithEval, true);
439 testAssignToUndefined(assignToUndefinedWithEval, true);
440 testAssignToUndefined(assignToUndefinedWithEval, true);
442 possibly_undefined_variable_for_strict_mode_test_with_eval = "value";
444 testAssignToUndefined(assignToUndefinedWithEval, false);
445 testAssignToUndefined(assignToUndefinedWithEval, false);
446 testAssignToUndefined(assignToUndefinedWithEval, false);
448 delete possibly_undefined_variable_for_strict_mode_test_with_eval;
450 testAssignToUndefined(assignToUndefinedWithEval, true);
451 testAssignToUndefined(assignToUndefinedWithEval, true);
452 testAssignToUndefined(assignToUndefinedWithEval, true);
454 repeat(10, function() {
455 testAssignToUndefined(assignToUndefinedWithEval, true);
457 possibly_undefined_variable_for_strict_mode_test_with_eval = "value";
458 repeat(10, function() {
459 testAssignToUndefined(assignToUndefinedWithEval, false);
461 delete possibly_undefined_variable_for_strict_mode_test_with_eval;
462 repeat(10, function() {
463 testAssignToUndefined(assignToUndefinedWithEval, true);
465 possibly_undefined_variable_for_strict_mode_test_with_eval = undefined;
466 repeat(10, function() {
467 testAssignToUndefined(assignToUndefinedWithEval, false);
472 (function testDeleteNonConfigurable() {
473 function delete_property(o) {
477 function delete_element(o, i) {
484 Object.defineProperty(object, "property", { value: "property_value" });
485 Object.defineProperty(object, "1", { value: "one" });
486 Object.defineProperty(object, 7, { value: "seven" });
487 Object.defineProperty(object, 3.14, { value: "pi" });
489 assertThrows(function() { delete_property(object); }, TypeError);
490 assertEquals(object.property, "property_value");
491 assertThrows(function() { delete_element(object, "1"); }, TypeError);
492 assertThrows(function() { delete_element(object, 1); }, TypeError);
493 assertEquals(object[1], "one");
494 assertThrows(function() { delete_element(object, "7"); }, TypeError);
495 assertThrows(function() { delete_element(object, 7); }, TypeError);
496 assertEquals(object[7], "seven");
497 assertThrows(function() { delete_element(object, "3.14"); }, TypeError);
498 assertThrows(function() { delete_element(object, 3.14); }, TypeError);
499 assertEquals(object[3.14], "pi");
502 // Not transforming this in Function.call and Function.apply.
503 (function testThisTransformCallApply() {
504 function non_strict() {
512 var global_object = (function() { return this; })();
516 assertTrue(non_strict.call(null) === global_object);
517 assertTrue(non_strict.call(undefined) === global_object);
518 assertEquals(typeof non_strict.call(7), "object");
519 assertEquals(typeof non_strict.call("Hello"), "object");
520 assertTrue(non_strict.call(object) === object);
523 assertTrue(non_strict.apply(null) === global_object);
524 assertTrue(non_strict.apply(undefined) === global_object);
525 assertEquals(typeof non_strict.apply(7), "object");
526 assertEquals(typeof non_strict.apply("Hello"), "object");
527 assertTrue(non_strict.apply(object) === object);
530 assertTrue(strict.call(null) === null);
531 assertTrue(strict.call(undefined) === undefined);
532 assertEquals(typeof strict.call(7), "number");
533 assertEquals(typeof strict.call("Hello"), "string");
534 assertTrue(strict.call(object) === object);
537 assertTrue(strict.apply(null) === null);
538 assertTrue(strict.apply(undefined) === undefined);
539 assertEquals(typeof strict.apply(7), "number");
540 assertEquals(typeof strict.apply("Hello"), "string");
541 assertTrue(strict.apply(object) === object);
544 (function testThisTransform() {
550 function nonstrict() {
554 // Concat to avoid symbol.
555 var strict_name = "str" + "ict";
556 var nonstrict_name = "non" + "str" + "ict";
557 var strict_number = 17;
558 var nonstrict_number = 19;
559 var strict_name_get = "str" + "ict" + "get";
560 var nonstrict_name_get = "non" + "str" + "ict" + "get"
561 var strict_number_get = 23;
562 var nonstrict_number_get = 29;
564 function install(t) {
565 t.prototype.strict = strict;
566 t.prototype.nonstrict = nonstrict;
567 t.prototype[strict_number] = strict;
568 t.prototype[nonstrict_number] = nonstrict;
569 Object.defineProperty(t.prototype, strict_name_get,
570 { get: function() { return strict; },
571 configurable: true });
572 Object.defineProperty(t.prototype, nonstrict_name_get,
573 { get: function() { return nonstrict; },
574 configurable: true });
575 Object.defineProperty(t.prototype, strict_number_get,
576 { get: function() { return strict; },
577 configurable: true });
578 Object.defineProperty(t.prototype, nonstrict_number_get,
579 { get: function() { return nonstrict; },
580 configurable: true });
583 function cleanup(t) {
584 delete t.prototype.strict;
585 delete t.prototype.nonstrict;
586 delete t.prototype[strict_number];
587 delete t.prototype[nonstrict_number];
588 delete t.prototype[strict_name_get];
589 delete t.prototype[nonstrict_name_get];
590 delete t.prototype[strict_number_get];
591 delete t.prototype[nonstrict_number_get];
599 function callStrict(o) {
602 function callNonStrict(o) {
603 return o.nonstrict();
605 function callKeyedStrict(o) {
606 return o[strict_name]();
608 function callKeyedNonStrict(o) {
609 return o[nonstrict_name]();
611 function callIndexedStrict(o) {
612 return o[strict_number]();
614 function callIndexedNonStrict(o) {
615 return o[nonstrict_number]();
617 function callStrictGet(o) {
618 return o.strictget();
620 function callNonStrictGet(o) {
621 return o.nonstrictget();
623 function callKeyedStrictGet(o) {
624 return o[strict_name_get]();
626 function callKeyedNonStrictGet(o) {
627 return o[nonstrict_name_get]();
629 function callIndexedStrictGet(o) {
630 return o[strict_number_get]();
632 function callIndexedNonStrictGet(o) {
633 return o[nonstrict_number_get]();
636 for (var i = 0; i < 10; i ++) {
637 assertEquals(("hello").strict(), "string");
638 assertEquals(("hello").nonstrict(), "object");
639 assertEquals(("hello")[strict_name](), "string");
640 assertEquals(("hello")[nonstrict_name](), "object");
641 assertEquals(("hello")[strict_number](), "string");
642 assertEquals(("hello")[nonstrict_number](), "object");
644 assertEquals((10 + i).strict(), "number");
645 assertEquals((10 + i).nonstrict(), "object");
646 assertEquals((10 + i)[strict_name](), "number");
647 assertEquals((10 + i)[nonstrict_name](), "object");
648 assertEquals((10 + i)[strict_number](), "number");
649 assertEquals((10 + i)[nonstrict_number](), "object");
651 assertEquals((true).strict(), "boolean");
652 assertEquals((true).nonstrict(), "object");
653 assertEquals((true)[strict_name](), "boolean");
654 assertEquals((true)[nonstrict_name](), "object");
655 assertEquals((true)[strict_number](), "boolean");
656 assertEquals((true)[nonstrict_number](), "object");
658 assertEquals((false).strict(), "boolean");
659 assertEquals((false).nonstrict(), "object");
660 assertEquals((false)[strict_name](), "boolean");
661 assertEquals((false)[nonstrict_name](), "object");
662 assertEquals((false)[strict_number](), "boolean");
663 assertEquals((false)[nonstrict_number](), "object");
665 assertEquals(callStrict("howdy"), "string");
666 assertEquals(callNonStrict("howdy"), "object");
667 assertEquals(callKeyedStrict("howdy"), "string");
668 assertEquals(callKeyedNonStrict("howdy"), "object");
669 assertEquals(callIndexedStrict("howdy"), "string");
670 assertEquals(callIndexedNonStrict("howdy"), "object");
672 assertEquals(callStrict(17 + i), "number");
673 assertEquals(callNonStrict(17 + i), "object");
674 assertEquals(callKeyedStrict(17 + i), "number");
675 assertEquals(callKeyedNonStrict(17 + i), "object");
676 assertEquals(callIndexedStrict(17 + i), "number");
677 assertEquals(callIndexedNonStrict(17 + i), "object");
679 assertEquals(callStrict(true), "boolean");
680 assertEquals(callNonStrict(true), "object");
681 assertEquals(callKeyedStrict(true), "boolean");
682 assertEquals(callKeyedNonStrict(true), "object");
683 assertEquals(callIndexedStrict(true), "boolean");
684 assertEquals(callIndexedNonStrict(true), "object");
686 assertEquals(callStrict(false), "boolean");
687 assertEquals(callNonStrict(false), "object");
688 assertEquals(callKeyedStrict(false), "boolean");
689 assertEquals(callKeyedNonStrict(false), "object");
690 assertEquals(callIndexedStrict(false), "boolean");
691 assertEquals(callIndexedNonStrict(false), "object");
693 // All of the above, with getters
694 assertEquals(("hello").strictget(), "string");
695 assertEquals(("hello").nonstrictget(), "object");
696 assertEquals(("hello")[strict_name_get](), "string");
697 assertEquals(("hello")[nonstrict_name_get](), "object");
698 assertEquals(("hello")[strict_number_get](), "string");
699 assertEquals(("hello")[nonstrict_number_get](), "object");
701 assertEquals((10 + i).strictget(), "number");
702 assertEquals((10 + i).nonstrictget(), "object");
703 assertEquals((10 + i)[strict_name_get](), "number");
704 assertEquals((10 + i)[nonstrict_name_get](), "object");
705 assertEquals((10 + i)[strict_number_get](), "number");
706 assertEquals((10 + i)[nonstrict_number_get](), "object");
708 assertEquals((true).strictget(), "boolean");
709 assertEquals((true).nonstrictget(), "object");
710 assertEquals((true)[strict_name_get](), "boolean");
711 assertEquals((true)[nonstrict_name_get](), "object");
712 assertEquals((true)[strict_number_get](), "boolean");
713 assertEquals((true)[nonstrict_number_get](), "object");
715 assertEquals((false).strictget(), "boolean");
716 assertEquals((false).nonstrictget(), "object");
717 assertEquals((false)[strict_name_get](), "boolean");
718 assertEquals((false)[nonstrict_name_get](), "object");
719 assertEquals((false)[strict_number_get](), "boolean");
720 assertEquals((false)[nonstrict_number_get](), "object");
722 assertEquals(callStrictGet("howdy"), "string");
723 assertEquals(callNonStrictGet("howdy"), "object");
724 assertEquals(callKeyedStrictGet("howdy"), "string");
725 assertEquals(callKeyedNonStrictGet("howdy"), "object");
726 assertEquals(callIndexedStrictGet("howdy"), "string");
727 assertEquals(callIndexedNonStrictGet("howdy"), "object");
729 assertEquals(callStrictGet(17 + i), "number");
730 assertEquals(callNonStrictGet(17 + i), "object");
731 assertEquals(callKeyedStrictGet(17 + i), "number");
732 assertEquals(callKeyedNonStrictGet(17 + i), "object");
733 assertEquals(callIndexedStrictGet(17 + i), "number");
734 assertEquals(callIndexedNonStrictGet(17 + i), "object");
736 assertEquals(callStrictGet(true), "boolean");
737 assertEquals(callNonStrictGet(true), "object");
738 assertEquals(callKeyedStrictGet(true), "boolean");
739 assertEquals(callKeyedNonStrictGet(true), "object");
740 assertEquals(callIndexedStrictGet(true), "boolean");
741 assertEquals(callIndexedNonStrictGet(true), "object");
743 assertEquals(callStrictGet(false), "boolean");
744 assertEquals(callNonStrictGet(false), "object");
745 assertEquals(callKeyedStrictGet(false), "boolean");
746 assertEquals(callKeyedNonStrictGet(false), "object");
747 assertEquals(callIndexedStrictGet(false), "boolean");
748 assertEquals(callIndexedNonStrictGet(false), "object");
760 (function ObjectEnvironment() {
762 Object.defineProperty(o, "foo", { value: "FOO", writable: false });
776 (function TestSetPropertyWithoutSetter() {
777 var o = { get foo() { return "Yey"; } };
781 o.foo = (0xBADBAD00 >> 1);
787 (function TestSetPropertyNonConfigurable() {
788 var frozen = Object.freeze({});
789 var sealed = Object.seal({});
793 o.property = "value";
796 assertThrows(function() { strict(frozen); }, TypeError);
797 assertThrows(function() { strict(sealed); }, TypeError);
801 (function TestAssignmentToReadOnlyProperty() {
805 Object.defineProperty(o, "property", { value: 7 });
807 assertThrows(function() { o.property = "new value"; }, TypeError);
808 assertThrows(function() { o.property += 10; }, TypeError);
809 assertThrows(function() { o.property -= 10; }, TypeError);
810 assertThrows(function() { o.property *= 10; }, TypeError);
811 assertThrows(function() { o.property /= 10; }, TypeError);
812 assertThrows(function() { o.property++; }, TypeError);
813 assertThrows(function() { o.property--; }, TypeError);
814 assertThrows(function() { ++o.property; }, TypeError);
815 assertThrows(function() { --o.property; }, TypeError);
817 var name = "prop" + "erty"; // to avoid symbol path.
818 assertThrows(function() { o[name] = "new value"; }, TypeError);
819 assertThrows(function() { o[name] += 10; }, TypeError);
820 assertThrows(function() { o[name] -= 10; }, TypeError);
821 assertThrows(function() { o[name] *= 10; }, TypeError);
822 assertThrows(function() { o[name] /= 10; }, TypeError);
823 assertThrows(function() { o[name]++; }, TypeError);
824 assertThrows(function() { o[name]--; }, TypeError);
825 assertThrows(function() { ++o[name]; }, TypeError);
826 assertThrows(function() { --o[name]; }, TypeError);
828 assertEquals(o.property, 7);
832 (function TestAssignmentToReadOnlyLoop() {
833 var name = "prop" + "erty"; // to avoid symbol path.
835 Object.defineProperty(o, "property", { value: 7 });
837 function strict(o, name) {
839 o[name] = "new value";
842 for (var i = 0; i < 10; i ++) {
843 var exception = false;
848 assertInstanceof(e, TypeError);
850 assertTrue(exception);
855 // Specialized KeyedStoreIC experiencing miss.
856 (function testKeyedStoreICStrict() {
857 var o = [9,8,7,6,5,4,3,2,1];
859 function test(o, i, v) {
864 for (var i = 0; i < 10; i ++) {
865 test(o, 5, 17); // start specialized for smi indices
866 assertEquals(o[5], 17);
868 assertEquals(o["a"], 19);
870 assertEquals(o[5], 29);
872 assertEquals(o[100000], 31);
877 (function TestSetElementWithoutSetter() {
881 Object.defineProperty(o, 0, { get : function() { } });
884 var zero_number = new Number(0);
885 var zero_symbol = "0";
886 var zero_string = "-0-".substring(1,2);
888 assertThrows(function() { o[zero_smi] = "new value"; }, TypeError);
889 assertThrows(function() { o[zero_number] = "new value"; }, TypeError);
890 assertThrows(function() { o[zero_symbol] = "new value"; }, TypeError);
891 assertThrows(function() { o[zero_string] = "new value"; }, TypeError);
895 (function TestSetElementNonConfigurable() {
897 var frozen = Object.freeze({});
898 var sealed = Object.seal({});
901 var zero_symbol = "0";
902 var zero_string = "-0-".substring(1,2);
904 assertThrows(function() { frozen[zero_number] = "value"; }, TypeError);
905 assertThrows(function() { sealed[zero_number] = "value"; }, TypeError);
906 assertThrows(function() { frozen[zero_symbol] = "value"; }, TypeError);
907 assertThrows(function() { sealed[zero_symbol] = "value"; }, TypeError);
908 assertThrows(function() { frozen[zero_string] = "value"; }, TypeError);
909 assertThrows(function() { sealed[zero_string] = "value"; }, TypeError);
913 (function TestAssignmentToReadOnlyElement() {
917 Object.defineProperty(o, 7, { value: 17 });
920 var seven_number = new Number(7);
921 var seven_symbol = "7";
922 var seven_string = "-7-".substring(1,2);
924 // Index with number.
925 assertThrows(function() { o[seven_smi] = "value"; }, TypeError);
926 assertThrows(function() { o[seven_smi] += 10; }, TypeError);
927 assertThrows(function() { o[seven_smi] -= 10; }, TypeError);
928 assertThrows(function() { o[seven_smi] *= 10; }, TypeError);
929 assertThrows(function() { o[seven_smi] /= 10; }, TypeError);
930 assertThrows(function() { o[seven_smi]++; }, TypeError);
931 assertThrows(function() { o[seven_smi]--; }, TypeError);
932 assertThrows(function() { ++o[seven_smi]; }, TypeError);
933 assertThrows(function() { --o[seven_smi]; }, TypeError);
935 assertThrows(function() { o[seven_number] = "value"; }, TypeError);
936 assertThrows(function() { o[seven_number] += 10; }, TypeError);
937 assertThrows(function() { o[seven_number] -= 10; }, TypeError);
938 assertThrows(function() { o[seven_number] *= 10; }, TypeError);
939 assertThrows(function() { o[seven_number] /= 10; }, TypeError);
940 assertThrows(function() { o[seven_number]++; }, TypeError);
941 assertThrows(function() { o[seven_number]--; }, TypeError);
942 assertThrows(function() { ++o[seven_number]; }, TypeError);
943 assertThrows(function() { --o[seven_number]; }, TypeError);
945 assertThrows(function() { o[seven_symbol] = "value"; }, TypeError);
946 assertThrows(function() { o[seven_symbol] += 10; }, TypeError);
947 assertThrows(function() { o[seven_symbol] -= 10; }, TypeError);
948 assertThrows(function() { o[seven_symbol] *= 10; }, TypeError);
949 assertThrows(function() { o[seven_symbol] /= 10; }, TypeError);
950 assertThrows(function() { o[seven_symbol]++; }, TypeError);
951 assertThrows(function() { o[seven_symbol]--; }, TypeError);
952 assertThrows(function() { ++o[seven_symbol]; }, TypeError);
953 assertThrows(function() { --o[seven_symbol]; }, TypeError);
955 assertThrows(function() { o[seven_string] = "value"; }, TypeError);
956 assertThrows(function() { o[seven_string] += 10; }, TypeError);
957 assertThrows(function() { o[seven_string] -= 10; }, TypeError);
958 assertThrows(function() { o[seven_string] *= 10; }, TypeError);
959 assertThrows(function() { o[seven_string] /= 10; }, TypeError);
960 assertThrows(function() { o[seven_string]++; }, TypeError);
961 assertThrows(function() { o[seven_string]--; }, TypeError);
962 assertThrows(function() { ++o[seven_string]; }, TypeError);
963 assertThrows(function() { --o[seven_string]; }, TypeError);
965 assertEquals(o[seven_number], 17);
966 assertEquals(o[seven_symbol], 17);
967 assertEquals(o[seven_string], 17);
971 (function TestAssignmentToReadOnlyLoop() {
975 Object.defineProperty(o, 7, { value: 17 });
978 var seven_number = new Number(7);
979 var seven_symbol = "7";
980 var seven_string = "-7-".substring(1,2);
982 for (var i = 0; i < 10; i ++) {
983 assertThrows(function() { o[seven_smi] = "value" }, TypeError);
984 assertThrows(function() { o[seven_number] = "value" }, TypeError);
985 assertThrows(function() { o[seven_symbol] = "value" }, TypeError);
986 assertThrows(function() { o[seven_string] = "value" }, TypeError);
989 assertEquals(o[7], 17);
993 (function TestAssignmentToStringLength() {
996 var str_val = "string";
997 var str_obj = new String(str_val);
998 var str_cat = str_val + str_val + str_obj;
1000 assertThrows(function() { str_val.length = 1; }, TypeError);
1001 assertThrows(function() { str_obj.length = 1; }, TypeError);
1002 assertThrows(function() { str_cat.length = 1; }, TypeError);
1006 (function TestArgumentsAliasing() {
1007 function strict(a, b) {
1011 return [a, b, arguments[0], arguments[1]];
1014 function nonstrict(a, b) {
1017 return [a, b, arguments[0], arguments[1]];
1020 assertEquals(["c", "d", "a", "b"], strict("a", "b"));
1021 assertEquals(["c", "d", "c", "d"], nonstrict("a", "b"));
1025 function CheckPillDescriptor(func, name) {
1027 function CheckPill(pill) {
1028 assertEquals("function", typeof pill);
1029 assertInstanceof(pill, Function);
1030 pill.property = "value";
1031 assertEquals(pill.value, undefined);
1032 assertThrows(function() { 'use strict'; pill.property = "value"; },
1034 assertThrows(pill, TypeError);
1035 assertEquals(pill.prototype, (function(){}).prototype);
1036 var d = Object.getOwnPropertyDescriptor(pill, "prototype");
1037 assertFalse(d.writable);
1038 assertFalse(d.configurable);
1039 assertFalse(d.enumerable);
1042 var descriptor = Object.getOwnPropertyDescriptor(func, name);
1043 CheckPill(descriptor.get)
1044 CheckPill(descriptor.set);
1045 assertFalse(descriptor.enumerable);
1046 assertFalse(descriptor.configurable);
1050 (function TestStrictFunctionPills() {
1054 assertThrows(function() { strict.caller; }, TypeError);
1055 assertThrows(function() { strict.arguments; }, TypeError);
1056 assertThrows(function() { strict.caller = 42; }, TypeError);
1057 assertThrows(function() { strict.arguments = 42; }, TypeError);
1059 var another = new Function("'use strict'");
1060 assertThrows(function() { another.caller; }, TypeError);
1061 assertThrows(function() { another.arguments; }, TypeError);
1062 assertThrows(function() { another.caller = 42; }, TypeError);
1063 assertThrows(function() { another.arguments = 42; }, TypeError);
1065 var third = (function() { "use strict"; return function() {}; })();
1066 assertThrows(function() { third.caller; }, TypeError);
1067 assertThrows(function() { third.arguments; }, TypeError);
1068 assertThrows(function() { third.caller = 42; }, TypeError);
1069 assertThrows(function() { third.arguments = 42; }, TypeError);
1071 CheckPillDescriptor(strict, "caller");
1072 CheckPillDescriptor(strict, "arguments");
1073 CheckPillDescriptor(another, "caller");
1074 CheckPillDescriptor(another, "arguments");
1075 CheckPillDescriptor(third, "caller");
1076 CheckPillDescriptor(third, "arguments");
1080 (function TestStrictFunctionWritablePrototype() {
1082 function TheClass() {
1084 assertThrows(function() { TheClass.caller; }, TypeError);
1085 assertThrows(function() { TheClass.arguments; }, TypeError);
1087 // Strict functions must have writable prototype.
1088 TheClass.prototype = {
1089 func: function() { return "func_value"; },
1090 get accessor() { return "accessor_value"; },
1091 property: "property_value",
1094 var o = new TheClass();
1095 assertEquals(o.func(), "func_value");
1096 assertEquals(o.accessor, "accessor_value");
1097 assertEquals(o.property, "property_value");
1101 (function TestStrictArgumentPills() {
1107 var args = strict();
1108 CheckPillDescriptor(args, "caller");
1109 CheckPillDescriptor(args, "callee");
1111 args = strict(17, "value", strict);
1112 assertEquals(17, args[0])
1113 assertEquals("value", args[1])
1114 assertEquals(strict, args[2]);
1115 CheckPillDescriptor(args, "caller");
1116 CheckPillDescriptor(args, "callee");
1126 var args = outer()();
1127 CheckPillDescriptor(args, "caller");
1128 CheckPillDescriptor(args, "callee");
1130 args = outer()(17, "value", strict);
1131 assertEquals(17, args[0])
1132 assertEquals("value", args[1])
1133 assertEquals(strict, args[2]);
1134 CheckPillDescriptor(args, "caller");
1135 CheckPillDescriptor(args, "callee");
1139 (function TestNonStrictFunctionCallerPillSimple() {
1140 function return_my_caller() {
1141 return return_my_caller.caller;
1146 return return_my_caller();
1148 assertSame(null, strict());
1150 function non_strict() {
1151 return return_my_caller();
1153 assertSame(non_strict(), non_strict);
1157 (function TestNonStrictFunctionCallerPill() {
1158 function strict(n) {
1160 return non_strict(n);
1163 function recurse(n, then) {
1165 return recurse(n - 1, then);
1171 function non_strict(n) {
1172 return recurse(n, function() { return non_strict.caller; });
1176 return recurse(n, function() { return strict(n); });
1179 for (var i = 0; i < 10; i ++) {
1180 assertSame(null, test(i));
1185 (function TestNonStrictFunctionCallerDescriptorPill() {
1186 function strict(n) {
1188 return non_strict(n);
1191 function recurse(n, then) {
1193 return recurse(n - 1, then);
1199 function non_strict(n) {
1200 return recurse(n, function() {
1201 return Object.getOwnPropertyDescriptor(non_strict, "caller").value;
1206 return recurse(n, function() { return strict(n); });
1209 for (var i = 0; i < 10; i ++) {
1210 assertSame(null, test(i));
1215 (function TestStrictModeEval() {
1217 eval("var eval_local = 10;");
1218 assertThrows(function() { return eval_local; }, ReferenceError);