| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Flags: --harmony-default-parameters --harmony-arrow-functions | |
| 6 | |
| 7 function return_specified() { return "specified"; } | |
| 8 | |
| 9 var method_returns_specified = { | |
| 10 method() { return "specified"; } | |
| 11 }; | |
| 12 | |
| 13 | |
| 14 (function testDefaultFunctions() { | |
| 15 function optional_function(handler = function() { }) { | |
| 16 assertEquals("function", typeof handler); | |
| 17 | |
| 18 // TODO(caitp): infer function name correctly | |
| 19 // (https://code.google.com/p/v8/issues/detail?id=3699) | |
| 20 // assertEquals("handler", handler.name); | |
| 21 | |
| 22 return handler(); | |
| 23 } | |
| 24 assertEquals(undefined, optional_function()); | |
| 25 assertEquals(undefined, optional_function(undefined)); | |
| 26 assertEquals("specified", optional_function(return_specified)); | |
| 27 })(); | |
| 28 | |
| 29 | |
| 30 (function testDefaultFunctionReferencesParameters() { | |
| 31 function fn1(handler = function() { return value; }, value) { | |
| 32 return handler(); | |
| 33 } | |
| 34 assertEquals(undefined, fn1()); | |
| 35 assertEquals(undefined, fn1(undefined, undefined)); | |
| 36 assertEquals(1, fn1(undefined, 1)); | |
| 37 | |
| 38 function fn2(value, handler = function() { return value; }) { | |
| 39 return handler(); | |
| 40 } | |
| 41 assertEquals(undefined, fn2()); | |
| 42 assertEquals(undefined, fn2(undefined)); | |
| 43 assertEquals(1, fn2(1)); | |
| 44 })(); | |
| 45 | |
| 46 | |
| 47 (function testDefaultObjects() { | |
| 48 function optional_object(object = { method() { return "method"; } }) { | |
| 49 assertEquals("object", typeof object); | |
| 50 | |
| 51 assertEquals("function", typeof object.method); | |
| 52 return object.method(); | |
| 53 } | |
| 54 | |
| 55 assertEquals("method", optional_object()); | |
| 56 assertEquals("method", optional_object(undefined)); | |
| 57 assertEquals("specified", optional_object(method_returns_specified)); | |
| 58 | |
| 59 | |
| 60 assertEquals(4, (function(x = { a: 4 }) { return x.a; })()); | |
| 61 assertEquals(5, (function(x, y = { a: x }) { return y.a; })(5)); | |
| 62 assertEquals(6, (function(x, y = { a: eval("x") }) { return y.a; })(6)); | |
| 63 })(); | |
| 64 | |
| 65 | |
| 66 // TDZ | |
| 67 | |
| 68 (function testReferencesUninitializedParameter() { | |
| 69 assertThrows(function(a = b, b) {}, ReferenceError); | |
| 70 })(); | |
| 71 | |
| 72 | |
| 73 (function testEvalReferencesUninitializedParameter() { | |
| 74 assertThrows( function(x = { a: y }, y) { return x.a; }, ReferenceError); | |
| 75 assertThrows(function(a = eval("b"), b = 0) { return a; }, ReferenceError); | |
| 76 assertThrows( | |
| 77 function(x = { a: eval("y") }, y) { return x.a; }, ReferenceError); | |
| 78 })(); | |
| 79 | |
| 80 | |
| 81 (function testReferencesInitializedParameter() { | |
| 82 assertEquals(1, (function(a = 1, b = a) { return b; })()); | |
| 83 })(); | |
| 84 | |
| 85 | |
| 86 // Scoping | |
| 87 // | |
| 88 // TODO(caitp): fix scoping --- var declarations in function body can't be | |
| 89 // resolved in formal parameters | |
| 90 // assertThrows(function referencesVariableBodyDeclaration(a = body_var) { | |
| 91 // var body_var = true; | |
| 92 // return a; | |
| 93 // }, ReferenceError); | |
| 94 | |
| 95 | |
| 96 // TODO(caitp): default function length does not include any parameters | |
| 97 // following the first optional parameter | |
| 98 // assertEquals(0, (function(a = 1) {}).length); | |
| 99 // assertEquals(1, (function(a, b = 1) {}).length); | |
| 100 // assertEquals(2, (function(a, b, c = 1) {}).length); | |
| 101 // assertEquals(3, (function(a, b, c, d = 1) {}).length); | |
| 102 // assertEquals(1, (function(a, b = 1, c, d = 1) {}).length); | |
| 103 | |
| 104 | |
| 105 (function testInitializerReferencesThis() { | |
| 106 var O = {}; | |
| 107 function fn(x = this) { return x; } | |
| 108 assertEquals(O, fn.call(O)); | |
| 109 | |
| 110 function fn2(x = () => this) { return x(); } | |
| 111 assertEquals(O, fn2.call(O)); | |
| 112 })(); | |
| 113 | |
| 114 | |
| 115 (function testInitializerReferencesSelf() { | |
| 116 function fn(x, y = fn) { return x ? y(false) + 1 : 0 } | |
| 117 assertEquals(1, fn(true)); | |
| 118 })(); | |
| 119 | |
| 120 | |
| 121 (function testInitializerEvalParameter() { | |
| 122 assertEquals(7, (function(x, y = eval("x")) { return y; })(7)); | |
| 123 assertEquals(9, (function(x = () => eval("y"), y = 9) { return x(); })()); | |
| 124 })(); | |
| 125 | |
| 126 | |
| 127 (function testContextAllocatedUsedInBody() { | |
| 128 assertEquals("Monkey!Monkey!Monkey!", (function(x, y = eval("x")) { | |
| 129 return "Mon" + x + "Mon" + eval("y") + "Mon" + y; | |
| 130 })("key!")); | |
| 131 assertEquals("Monkey!", (function(x = "Mon", y = "key!") { | |
| 132 return eval("x") + eval("y"); | |
| 133 })()); | |
| 134 })(); | |
| 135 | |
| 136 | |
| 137 (function testContextAllocatedEscapesFunction() { | |
| 138 assertEquals("Monkey!", (function(x = "Monkey!") { | |
| 139 return function() { | |
| 140 return x; | |
| 141 }; | |
| 142 })()()); | |
| 143 })(); | |
| OLD | NEW |