| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // This test should move to a language test once the apply method |
| 6 // gets into the specification. |
| 7 |
| 8 apply(Function function, ArgumentDescriptor args) { |
| 9 int argumentCount = 0; |
| 10 StringBuffer buffer = new StringBuffer(); |
| 11 List arguments = []; |
| 12 |
| 13 if (args.positionalArguments != null) { |
| 14 argumentCount += args.positionalArguments.length; |
| 15 arguments.addAll(args.positionalArguments); |
| 16 } |
| 17 |
| 18 // Sort the named arguments to get the right selector name and |
| 19 // arguments order. |
| 20 if (args.namedArguments != null && !args.namedArguments.isEmpty()) { |
| 21 // Call new List.from to make sure we get a JavaScript array. |
| 22 List<String> namedArguments = |
| 23 new List<String>.from(args.namedArguments.getKeys()); |
| 24 argumentCount += namedArguments.length; |
| 25 // We're sorting on strings, and the behavior is the same between |
| 26 // Dart string sort and JS String sort. To avoid needing the Dart |
| 27 // sort implementation, we use the JavaScript one instead. |
| 28 JS('void', '#.sort()', namedArguments); |
| 29 namedArguments.forEach((String name) { |
| 30 buffer.add('\$$name'); |
| 31 arguments.add(args.namedArguments[name]); |
| 32 }); |
| 33 } |
| 34 |
| 35 String selectorName = 'call\$$argumentCount$buffer'; |
| 36 var jsFunction = JS('var', '#[#]', function, selectorName); |
| 37 if (jsFunction == null) { |
| 38 throw new NoSuchMethodError(function, selectorName, arguments); |
| 39 } |
| 40 // We bound 'this' to [function] because of how we compile |
| 41 // closures: escaped local variables are stored and accessed through |
| 42 // [function]. |
| 43 return JS('var', '#.apply(#, #)', jsFunction, function, arguments); |
| 44 } |
| 45 |
| 46 class ArgumentDescriptor { |
| 47 final List positionalArguments; |
| 48 final Map<String, Dynamic> namedArguments; |
| 49 |
| 50 ArgumentDescriptor(this.positionalArguments, this.namedArguments); |
| 51 } |
| 52 |
| 53 void throwsNSME(function) { |
| 54 Expect.throws(function, (e) => e is NoSuchMethodError); |
| 55 } |
| 56 |
| 57 main() { |
| 58 var c1 = () => 'c1'; |
| 59 var c2 = (a) => 'c2 $a'; |
| 60 var c3 = ([a = 1]) => 'c3 $a'; |
| 61 var c4 = ({a: 1}) => 'c4 $a'; |
| 62 var c5 = ({a: 1, b: 2}) => 'c5 $a $b'; |
| 63 var c6 = ({b: 1, a: 2}) => 'c6 $a $b'; |
| 64 |
| 65 // TODO(ngeoffray): Remove these calls. They are currently needed |
| 66 // because otherwise we would not generate the stubs. Once apply is |
| 67 // in the specification, we should change the compiler to emit stubs |
| 68 // for closures once it sees an 'apply' selector. |
| 69 c1(); |
| 70 c2(1); |
| 71 c3(); c3(1); |
| 72 c4(); c4(a: 1); |
| 73 c5(); c5(a: 1); c5(b: 2); c5(a:1, b: 2); |
| 74 c6(); c6(a: 1); c6(b: 2); c6(a:1, b: 2); |
| 75 |
| 76 Expect.equals('c1', apply(c1, new ArgumentDescriptor(null, null))); |
| 77 Expect.equals('c1', apply(c1, new ArgumentDescriptor([], null))); |
| 78 Expect.equals('c1', apply(c1, new ArgumentDescriptor([], {}))); |
| 79 Expect.equals('c1', apply(c1, new ArgumentDescriptor(null, {}))); |
| 80 throwsNSME(() => apply(c1, new ArgumentDescriptor([1], null))); |
| 81 throwsNSME(() => apply(c1, new ArgumentDescriptor([1], {'a': 2}))); |
| 82 throwsNSME(() => apply(c1, new ArgumentDescriptor(null, {'a': 2}))); |
| 83 |
| 84 Expect.equals('c2 1', apply(c2, new ArgumentDescriptor([1], null))); |
| 85 Expect.equals('c2 1', apply(c2, new ArgumentDescriptor([1], {}))); |
| 86 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, null))); |
| 87 throwsNSME(() => apply(c2, new ArgumentDescriptor([], null))); |
| 88 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, {}))); |
| 89 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, {'a': 1}))); |
| 90 |
| 91 Expect.equals('c3 1', apply(c3, new ArgumentDescriptor([], null))); |
| 92 Expect.equals('c3 2', apply(c3, new ArgumentDescriptor([2], {}))); |
| 93 throwsNSME(() => apply(c3, new ArgumentDescriptor([1, 2], null))); |
| 94 // TODO(ngeoffray): Should be throwsNSME with the new parameter |
| 95 // specification. |
| 96 Expect.equals('c3 1', apply(c3, new ArgumentDescriptor(null, {'a': 1}))); |
| 97 |
| 98 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor([], null))); |
| 99 Expect.equals('c4 2', apply(c4, new ArgumentDescriptor([], {'a': 2}))); |
| 100 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor(null, null))); |
| 101 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor([], {}))); |
| 102 throwsNSME(() => apply(c4, new ArgumentDescriptor([1], {'a': 1}))); |
| 103 throwsNSME(() => apply(c4, new ArgumentDescriptor([1], {}))); |
| 104 throwsNSME(() => apply(c4, new ArgumentDescriptor([], {'a': 1, 'b': 2}))); |
| 105 |
| 106 Expect.equals('c5 1 2', apply(c5, new ArgumentDescriptor([], null))); |
| 107 Expect.equals('c5 3 2', apply(c5, new ArgumentDescriptor([], {'a': 3}))); |
| 108 Expect.equals('c5 1 2', apply(c5, new ArgumentDescriptor(null, null))); |
| 109 Expect.equals('c5 1 2', apply(c5, new ArgumentDescriptor([], {}))); |
| 110 Expect.equals('c5 3 4', |
| 111 apply(c5, new ArgumentDescriptor([], {'a': 3, 'b': 4}))); |
| 112 Expect.equals('c5 4 3', |
| 113 apply(c5, new ArgumentDescriptor([], {'b': 3, 'a': 4}))); |
| 114 Expect.equals('c5 1 3', |
| 115 apply(c5, new ArgumentDescriptor([], {'b': 3}))); |
| 116 throwsNSME(() => apply(c5, new ArgumentDescriptor([1], {'a': 1}))); |
| 117 throwsNSME(() => apply(c5, new ArgumentDescriptor([1], {}))); |
| 118 throwsNSME(() => |
| 119 apply(c5, new ArgumentDescriptor([], {'a': 1, 'b': 2, 'c': 3}))); |
| 120 |
| 121 Expect.equals('c6 2 1', apply(c6, new ArgumentDescriptor([], null))); |
| 122 Expect.equals('c6 3 1', apply(c6, new ArgumentDescriptor([], {'a': 3}))); |
| 123 Expect.equals('c6 2 1', apply(c6, new ArgumentDescriptor(null, null))); |
| 124 Expect.equals('c6 2 1', apply(c6, new ArgumentDescriptor([], {}))); |
| 125 Expect.equals('c6 3 4', |
| 126 apply(c6, new ArgumentDescriptor([], {'a': 3, 'b': 4}))); |
| 127 Expect.equals('c6 4 3', |
| 128 apply(c6, new ArgumentDescriptor([], {'b': 3, 'a': 4}))); |
| 129 Expect.equals('c6 2 3', |
| 130 apply(c6, new ArgumentDescriptor([], {'b': 3}))); |
| 131 throwsNSME(() => apply(c6, new ArgumentDescriptor([1], {'a': 1}))); |
| 132 throwsNSME(() => apply(c6, new ArgumentDescriptor([1], {}))); |
| 133 throwsNSME(() => |
| 134 apply(c6, new ArgumentDescriptor([], {'a': 1, 'b': 2, 'c': 3}))); |
| 135 } |
| OLD | NEW |