diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2011-06-29 17:26:51 +0200 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2011-06-29 17:26:51 +0200 |
commit | 33af2720f26c2b25bc7f75ce7eb454ff99db6d35 (patch) | |
tree | 9a38f0c96420edf503eebd6325dd8d2d8249f653 /deps/v8/test/mjsunit/strict-mode.js | |
parent | 6afdca885adeeeed9eef8cbb01c3d97af0bc084d (diff) | |
download | android-node-v8-33af2720f26c2b25bc7f75ce7eb454ff99db6d35.tar.gz android-node-v8-33af2720f26c2b25bc7f75ce7eb454ff99db6d35.tar.bz2 android-node-v8-33af2720f26c2b25bc7f75ce7eb454ff99db6d35.zip |
Upgrade V8 to 3.4.8
Diffstat (limited to 'deps/v8/test/mjsunit/strict-mode.js')
-rw-r--r-- | deps/v8/test/mjsunit/strict-mode.js | 432 |
1 files changed, 394 insertions, 38 deletions
diff --git a/deps/v8/test/mjsunit/strict-mode.js b/deps/v8/test/mjsunit/strict-mode.js index 4071232ebf..30234ba6fa 100644 --- a/deps/v8/test/mjsunit/strict-mode.js +++ b/deps/v8/test/mjsunit/strict-mode.js @@ -67,6 +67,9 @@ function CheckFunctionConstructorStrictMode() { with ({}) {}; })(); +// Incorrectly place 'use strict' directive. +assertThrows("function foo (x) 'use strict'; {}", SyntaxError); + // 'use strict' in non-directive position. (function UseStrictNonDirective() { void(0); @@ -319,14 +322,8 @@ CheckStrictMode("var variable; delete variable;", SyntaxError); +arguments, -arguments, ~arguments, !arguments]; })(); -// 7.6.1.2 Future Reserved Words -var future_reserved_words = [ - "class", - "enum", - "export", - "extends", - "import", - "super", +// 7.6.1.2 Future Reserved Words in strict mode +var future_strict_reserved_words = [ "implements", "interface", "let", @@ -337,14 +334,17 @@ var future_reserved_words = [ "static", "yield" ]; -function testFutureReservedWord(word) { +function testFutureStrictReservedWord(word) { // Simple use of each reserved word CheckStrictMode("var " + word + " = 1;", SyntaxError); + CheckStrictMode("typeof (" + word + ");", SyntaxError); // object literal properties eval("var x = { " + word + " : 42 };"); eval("var x = { get " + word + " () {} };"); eval("var x = { set " + word + " (value) {} };"); + eval("var x = { get " + word + " () { 'use strict'; } };"); + eval("var x = { set " + word + " (value) { 'use strict'; } };"); // object literal with string literal property names eval("var x = { '" + word + "' : 42 };"); @@ -364,7 +364,6 @@ function testFutureReservedWord(word) { // Function names and arguments when the body is strict assertThrows("function " + word + " () { 'use strict'; }", SyntaxError); - assertThrows("function foo (" + word + ") 'use strict'; {}", SyntaxError); assertThrows("function foo (" + word + ", " + word + ") { 'use strict'; }", SyntaxError); assertThrows("function foo (a, " + word + ") { 'use strict'; }", SyntaxError); @@ -374,23 +373,19 @@ function testFutureReservedWord(word) { assertThrows("var foo = function (" + word + ") { 'use strict'; }", SyntaxError); - // get/set when the body is strict - eval("var x = { get " + word + " () { 'use strict'; } };"); - eval("var x = { set " + word + " (value) { 'use strict'; } };"); - assertThrows("var x = { get foo(" + word + ") { 'use strict'; } };", - SyntaxError); + // setter parameter when the body is strict + CheckStrictMode("var x = { set foo(" + word + ") {} };", SyntaxError); assertThrows("var x = { set foo(" + word + ") { 'use strict'; } };", SyntaxError); } -for (var i = 0; i < future_reserved_words.length; i++) { - testFutureReservedWord(future_reserved_words[i]); +for (var i = 0; i < future_strict_reserved_words.length; i++) { + testFutureStrictReservedWord(future_strict_reserved_words[i]); } -function testAssignToUndefined(should_throw) { - "use strict"; +function testAssignToUndefined(test, should_throw) { try { - possibly_undefined_variable_for_strict_mode_test = "should throw?"; + test(); } catch (e) { assertTrue(should_throw, "strict mode"); assertInstanceof(e, ReferenceError, "strict mode"); @@ -399,33 +394,78 @@ function testAssignToUndefined(should_throw) { assertFalse(should_throw, "strict mode"); } -testAssignToUndefined(true); -testAssignToUndefined(true); -testAssignToUndefined(true); +function repeat(n, f) { + for (var i = 0; i < n; i ++) { f(); } +} + +function assignToUndefined() { + "use strict"; + possibly_undefined_variable_for_strict_mode_test = "should throw?"; +} + +testAssignToUndefined(assignToUndefined, true); +testAssignToUndefined(assignToUndefined, true); +testAssignToUndefined(assignToUndefined, true); possibly_undefined_variable_for_strict_mode_test = "value"; -testAssignToUndefined(false); -testAssignToUndefined(false); -testAssignToUndefined(false); +testAssignToUndefined(assignToUndefined, false); +testAssignToUndefined(assignToUndefined, false); +testAssignToUndefined(assignToUndefined, false); delete possibly_undefined_variable_for_strict_mode_test; -testAssignToUndefined(true); -testAssignToUndefined(true); -testAssignToUndefined(true); +testAssignToUndefined(assignToUndefined, true); +testAssignToUndefined(assignToUndefined, true); +testAssignToUndefined(assignToUndefined, true); -function repeat(n, f) { - for (var i = 0; i < n; i ++) { f(); } -} - -repeat(10, function() { testAssignToUndefined(true); }); +repeat(10, function() { testAssignToUndefined(assignToUndefined, true); }); possibly_undefined_variable_for_strict_mode_test = "value"; -repeat(10, function() { testAssignToUndefined(false); }); +repeat(10, function() { testAssignToUndefined(assignToUndefined, false); }); delete possibly_undefined_variable_for_strict_mode_test; -repeat(10, function() { testAssignToUndefined(true); }); +repeat(10, function() { testAssignToUndefined(assignToUndefined, true); }); possibly_undefined_variable_for_strict_mode_test = undefined; -repeat(10, function() { testAssignToUndefined(false); }); +repeat(10, function() { testAssignToUndefined(assignToUndefined, false); }); + +function assignToUndefinedWithEval() { + "use strict"; + possibly_undefined_variable_for_strict_mode_test_with_eval = "should throw?"; + eval(""); +} + +testAssignToUndefined(assignToUndefinedWithEval, true); +testAssignToUndefined(assignToUndefinedWithEval, true); +testAssignToUndefined(assignToUndefinedWithEval, true); + +possibly_undefined_variable_for_strict_mode_test_with_eval = "value"; + +testAssignToUndefined(assignToUndefinedWithEval, false); +testAssignToUndefined(assignToUndefinedWithEval, false); +testAssignToUndefined(assignToUndefinedWithEval, false); + +delete possibly_undefined_variable_for_strict_mode_test_with_eval; + +testAssignToUndefined(assignToUndefinedWithEval, true); +testAssignToUndefined(assignToUndefinedWithEval, true); +testAssignToUndefined(assignToUndefinedWithEval, true); + +repeat(10, function() { + testAssignToUndefined(assignToUndefinedWithEval, true); + }); +possibly_undefined_variable_for_strict_mode_test_with_eval = "value"; +repeat(10, function() { + testAssignToUndefined(assignToUndefinedWithEval, false); + }); +delete possibly_undefined_variable_for_strict_mode_test_with_eval; +repeat(10, function() { + testAssignToUndefined(assignToUndefinedWithEval, true); + }); +possibly_undefined_variable_for_strict_mode_test_with_eval = undefined; +repeat(10, function() { + testAssignToUndefined(assignToUndefinedWithEval, false); + }); + + (function testDeleteNonConfigurable() { function delete_property(o) { @@ -798,12 +838,14 @@ repeat(10, function() { testAssignToUndefined(false); }); } for (var i = 0; i < 10; i ++) { + var exception = false; try { strict(o, name); - assertUnreachable(); } catch(e) { + exception = true; assertInstanceof(e, TypeError); } + assertTrue(exception); } })(); @@ -828,3 +870,317 @@ repeat(10, function() { testAssignToUndefined(false); }); assertEquals(o[100000], 31); } })(); + + +(function TestSetElementWithoutSetter() { + "use strict"; + + var o = { }; + Object.defineProperty(o, 0, { get : function() { } }); + + var zero_smi = 0; + var zero_number = new Number(0); + var zero_symbol = "0"; + var zero_string = "-0-".substring(1,2); + + assertThrows(function() { o[zero_smi] = "new value"; }, TypeError); + assertThrows(function() { o[zero_number] = "new value"; }, TypeError); + assertThrows(function() { o[zero_symbol] = "new value"; }, TypeError); + assertThrows(function() { o[zero_string] = "new value"; }, TypeError); +})(); + + +(function TestSetElementNonConfigurable() { + "use strict"; + var frozen = Object.freeze({}); + var sealed = Object.seal({}); + + var zero_number = 0; + var zero_symbol = "0"; + var zero_string = "-0-".substring(1,2); + + assertThrows(function() { frozen[zero_number] = "value"; }, TypeError); + assertThrows(function() { sealed[zero_number] = "value"; }, TypeError); + assertThrows(function() { frozen[zero_symbol] = "value"; }, TypeError); + assertThrows(function() { sealed[zero_symbol] = "value"; }, TypeError); + assertThrows(function() { frozen[zero_string] = "value"; }, TypeError); + assertThrows(function() { sealed[zero_string] = "value"; }, TypeError); +})(); + + +(function TestAssignmentToReadOnlyElement() { + "use strict"; + + var o = {}; + Object.defineProperty(o, 7, { value: 17 }); + + var seven_smi = 7; + var seven_number = new Number(7); + var seven_symbol = "7"; + var seven_string = "-7-".substring(1,2); + + // Index with number. + assertThrows(function() { o[seven_smi] = "value"; }, TypeError); + assertThrows(function() { o[seven_smi] += 10; }, TypeError); + assertThrows(function() { o[seven_smi] -= 10; }, TypeError); + assertThrows(function() { o[seven_smi] *= 10; }, TypeError); + assertThrows(function() { o[seven_smi] /= 10; }, TypeError); + assertThrows(function() { o[seven_smi]++; }, TypeError); + assertThrows(function() { o[seven_smi]--; }, TypeError); + assertThrows(function() { ++o[seven_smi]; }, TypeError); + assertThrows(function() { --o[seven_smi]; }, TypeError); + + assertThrows(function() { o[seven_number] = "value"; }, TypeError); + assertThrows(function() { o[seven_number] += 10; }, TypeError); + assertThrows(function() { o[seven_number] -= 10; }, TypeError); + assertThrows(function() { o[seven_number] *= 10; }, TypeError); + assertThrows(function() { o[seven_number] /= 10; }, TypeError); + assertThrows(function() { o[seven_number]++; }, TypeError); + assertThrows(function() { o[seven_number]--; }, TypeError); + assertThrows(function() { ++o[seven_number]; }, TypeError); + assertThrows(function() { --o[seven_number]; }, TypeError); + + assertThrows(function() { o[seven_symbol] = "value"; }, TypeError); + assertThrows(function() { o[seven_symbol] += 10; }, TypeError); + assertThrows(function() { o[seven_symbol] -= 10; }, TypeError); + assertThrows(function() { o[seven_symbol] *= 10; }, TypeError); + assertThrows(function() { o[seven_symbol] /= 10; }, TypeError); + assertThrows(function() { o[seven_symbol]++; }, TypeError); + assertThrows(function() { o[seven_symbol]--; }, TypeError); + assertThrows(function() { ++o[seven_symbol]; }, TypeError); + assertThrows(function() { --o[seven_symbol]; }, TypeError); + + assertThrows(function() { o[seven_string] = "value"; }, TypeError); + assertThrows(function() { o[seven_string] += 10; }, TypeError); + assertThrows(function() { o[seven_string] -= 10; }, TypeError); + assertThrows(function() { o[seven_string] *= 10; }, TypeError); + assertThrows(function() { o[seven_string] /= 10; }, TypeError); + assertThrows(function() { o[seven_string]++; }, TypeError); + assertThrows(function() { o[seven_string]--; }, TypeError); + assertThrows(function() { ++o[seven_string]; }, TypeError); + assertThrows(function() { --o[seven_string]; }, TypeError); + + assertEquals(o[seven_number], 17); + assertEquals(o[seven_symbol], 17); + assertEquals(o[seven_string], 17); +})(); + + +(function TestAssignmentToReadOnlyLoop() { + "use strict"; + + var o = {}; + Object.defineProperty(o, 7, { value: 17 }); + + var seven_smi = 7; + var seven_number = new Number(7); + var seven_symbol = "7"; + var seven_string = "-7-".substring(1,2); + + for (var i = 0; i < 10; i ++) { + assertThrows(function() { o[seven_smi] = "value" }, TypeError); + assertThrows(function() { o[seven_number] = "value" }, TypeError); + assertThrows(function() { o[seven_symbol] = "value" }, TypeError); + assertThrows(function() { o[seven_string] = "value" }, TypeError); + } + + assertEquals(o[7], 17); +})(); + + +(function TestAssignmentToStringLength() { + "use strict"; + + var str_val = "string"; + var str_obj = new String(str_val); + var str_cat = str_val + str_val + str_obj; + + assertThrows(function() { str_val.length = 1; }, TypeError); + assertThrows(function() { str_obj.length = 1; }, TypeError); + assertThrows(function() { str_cat.length = 1; }, TypeError); +})(); + + +(function TestArgumentsAliasing() { + function strict(a, b) { + "use strict"; + a = "c"; + b = "d"; + return [a, b, arguments[0], arguments[1]]; + } + + function nonstrict(a, b) { + a = "c"; + b = "d"; + return [a, b, arguments[0], arguments[1]]; + } + + assertEquals(["c", "d", "a", "b"], strict("a", "b")); + assertEquals(["c", "d", "c", "d"], nonstrict("a", "b")); +})(); + + +function CheckPillDescriptor(func, name) { + + function CheckPill(pill) { + assertEquals("function", typeof pill); + assertInstanceof(pill, Function); + pill.property = "value"; + assertEquals(pill.value, undefined); + assertThrows(function() { 'use strict'; pill.property = "value"; }, + TypeError); + assertThrows(pill, TypeError); + assertEquals(pill.prototype, (function(){}).prototype); + var d = Object.getOwnPropertyDescriptor(pill, "prototype"); + assertFalse(d.writable); + assertFalse(d.configurable); + assertFalse(d.enumerable); + } + + var descriptor = Object.getOwnPropertyDescriptor(func, name); + CheckPill(descriptor.get) + CheckPill(descriptor.set); + assertFalse(descriptor.enumerable); + assertFalse(descriptor.configurable); +} + + +(function TestStrictFunctionPills() { + function strict() { + "use strict"; + } + assertThrows(function() { strict.caller; }, TypeError); + assertThrows(function() { strict.arguments; }, TypeError); + + var another = new Function("'use strict'"); + assertThrows(function() { another.caller; }, TypeError); + assertThrows(function() { another.arguments; }, TypeError); + + var third = (function() { "use strict"; return function() {}; })(); + assertThrows(function() { third.caller; }, TypeError); + assertThrows(function() { third.arguments; }, TypeError); + + CheckPillDescriptor(strict, "caller"); + CheckPillDescriptor(strict, "arguments"); + CheckPillDescriptor(another, "caller"); + CheckPillDescriptor(another, "arguments"); + CheckPillDescriptor(third, "caller"); + CheckPillDescriptor(third, "arguments"); +})(); + + +(function TestStrictFunctionWritablePrototype() { + "use strict"; + function TheClass() { + } + assertThrows(function() { TheClass.caller; }, TypeError); + assertThrows(function() { TheClass.arguments; }, TypeError); + + // Strict functions must have writable prototype. + TheClass.prototype = { + func: function() { return "func_value"; }, + get accessor() { return "accessor_value"; }, + property: "property_value", + }; + + var o = new TheClass(); + assertEquals(o.func(), "func_value"); + assertEquals(o.accessor, "accessor_value"); + assertEquals(o.property, "property_value"); +})(); + + +(function TestStrictArgumentPills() { + function strict() { + "use strict"; + return arguments; + } + + var args = strict(); + CheckPillDescriptor(args, "caller"); + CheckPillDescriptor(args, "callee"); + + args = strict(17, "value", strict); + assertEquals(17, args[0]) + assertEquals("value", args[1]) + assertEquals(strict, args[2]); + CheckPillDescriptor(args, "caller"); + CheckPillDescriptor(args, "callee"); + + function outer() { + "use strict"; + function inner() { + return arguments; + } + return inner; + } + + var args = outer()(); + CheckPillDescriptor(args, "caller"); + CheckPillDescriptor(args, "callee"); + + args = outer()(17, "value", strict); + assertEquals(17, args[0]) + assertEquals("value", args[1]) + assertEquals(strict, args[2]); + CheckPillDescriptor(args, "caller"); + CheckPillDescriptor(args, "callee"); +})(); + + +(function TestNonStrictFunctionCallerPillSimple() { + function return_my_caller() { + return return_my_caller.caller; + } + + function strict() { + "use strict"; + return_my_caller(); + } + assertThrows(strict, TypeError); + + function non_strict() { + return return_my_caller(); + } + assertSame(non_strict(), non_strict); +})(); + + +(function TestNonStrictFunctionCallerPill() { + function strict(n) { + "use strict"; + non_strict(n); + } + + function recurse(n, then) { + if (n > 0) { + recurse(n - 1); + } else { + return then(); + } + } + + function non_strict(n) { + recurse(n, function() { non_strict.caller; }); + } + + function test(n) { + try { + recurse(n, function() { strict(n); }); + } catch(e) { + return e instanceof TypeError; + } + return false; + } + + for (var i = 0; i < 10; i ++) { + assertEquals(test(i), true); + } +})(); + + +(function TestStrictModeEval() { + "use strict"; + eval("var eval_local = 10;"); + assertThrows(function() { return eval_local; }, ReferenceError); +})(); |