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 // TODO(caitp): this is broken :( |
| 63 // assertEquals(6, (function(x, y = { a: eval("x") }) { return y.a; })(6)); |
| 64 })(); |
| 65 |
| 66 |
| 67 // TDZ |
| 68 |
| 69 (function testReferencesUninitializedParameter() { |
| 70 assertThrows(function(a = b, b) {}, ReferenceError); |
| 71 })(); |
| 72 |
| 73 |
| 74 (function testEvalReferencesUninitializedParameter() { |
| 75 assertThrows( function(x = { a: y }, y) { return x.a; }, ReferenceError); |
| 76 |
| 77 // TODO(caitp): these shouldn't fail |
| 78 // assertThrows(function(a = eval("b"), b = 0) { return a; }, ReferenceError); |
| 79 // assertThrows( |
| 80 // function(x = { a: eval("y") }, y) { return x.a; }, ReferenceError); |
| 81 })(); |
| 82 |
| 83 |
| 84 (function testReferencesInitializedParameter() { |
| 85 assertEquals(1, (function(a = 1, b = a) { return b; })()); |
| 86 })(); |
| 87 |
| 88 |
| 89 // Scoping |
| 90 // |
| 91 // TODO(caitp): fix scoping --- var declarations in function body can't be |
| 92 // resolved in formal parameters |
| 93 // assertThrows(function referencesVariableBodyDeclaration(a = body_var) { |
| 94 // var body_var = true; |
| 95 // return a; |
| 96 // }, ReferenceError); |
| 97 |
| 98 |
| 99 // TODO(caitp): default function length does not include any parameters |
| 100 // following the first optional parameter |
| 101 // assertEquals(0, (function(a = 1) {}).length); |
| 102 // assertEquals(1, (function(a, b = 1) {}).length); |
| 103 // assertEquals(2, (function(a, b, c = 1) {}).length); |
| 104 // assertEquals(3, (function(a, b, c, d = 1) {}).length); |
| 105 // assertEquals(1, (function(a, b = 1, c, d = 1) {}).length); |
| 106 |
| 107 |
| 108 (function testInitializerReferencesThis() { |
| 109 var O = {}; |
| 110 function fn(x = this) { return x; } |
| 111 assertEquals(O, fn.call(O)); |
| 112 |
| 113 function fn2(x = () => this) { return x(); } |
| 114 assertEquals(O, fn2.call(O)); |
| 115 })(); |
| 116 |
| 117 |
| 118 (function testInitializerReferencesSelf() { |
| 119 function fn(x, y = fn) { return x ? y(false) + 1 : 0 } |
| 120 assertEquals(1, fn(true)); |
| 121 })(); |
| 122 |
| 123 |
| 124 (function testInitializerEvalParameter() { |
| 125 // TODO(caitp): should not fail |
| 126 // assertEquals(7, (function(x, y = eval("x")) { return y; })(7)); |
| 127 // assertEquals(9, (function(x = () => eval("y"), y = 9) { return x(); })()); |
| 128 })(); |
OLD | NEW |