Chromium Code Reviews| 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 nbArguments = 0; | |
|
kasperl
2012/09/26 13:23:43
nbArguments -> argumentCount?
ngeoffray
2012/09/26 13:40:45
Done.
| |
| 10 StringBuffer buffer = new StringBuffer(); | |
| 11 List arguments = []; | |
| 12 | |
| 13 if (args.positionalArguments != null) { | |
| 14 nbArguments += 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. | |
|
kasperl
2012/09/26 13:23:43
Maybe add a comment that explains why you've decid
ngeoffray
2012/09/26 13:40:45
Done.
| |
| 20 if (args.namedArguments != null && !args.namedArguments.isEmpty()) { | |
| 21 List<String> namedArguments = args.namedArguments.getKeys(); | |
|
kasperl
2012/09/26 13:23:43
Are we assuming too much about what is returned fr
ngeoffray
2012/09/26 13:40:45
Yes, it will crash. I changed the code to do new L
| |
| 22 nbArguments += namedArguments.length; | |
| 23 JS('void', '#.sort()', namedArguments); | |
| 24 namedArguments.forEach((String name) { | |
| 25 buffer.add('\$$name'); | |
| 26 arguments.add(args.namedArguments[name]); | |
| 27 }); | |
| 28 } | |
| 29 | |
| 30 String selectorName = 'call\$$nbArguments$buffer'; | |
| 31 var jsFunction = JS('var', '#[#]', function, selectorName); | |
| 32 if (jsFunction == null) { | |
| 33 throw new NoSuchMethodError(function, selectorName, arguments); | |
| 34 } | |
| 35 return JS('var', '#.apply(#, #)', jsFunction, function, arguments); | |
|
kasperl
2012/09/26 13:23:43
Add a comment that explains why you need 'this' to
ngeoffray
2012/09/26 13:40:45
Done.
| |
| 36 } | |
| 37 | |
| 38 class ArgumentDescriptor { | |
| 39 final List positionalArguments; | |
| 40 final Map<String, Dynamic> namedArguments; | |
| 41 | |
| 42 ArgumentDescriptor(this.positionalArguments, this.namedArguments); | |
| 43 } | |
| 44 | |
| 45 void throwsNSME(function) { | |
| 46 Expect.throws(function, (e) => e is NoSuchMethodError); | |
| 47 } | |
| 48 | |
| 49 main() { | |
| 50 var c1 = () => 'c1'; | |
| 51 var c2 = (a) => 'c2 $a'; | |
| 52 var c3 = ([a = 1]) => 'c3 $a'; | |
| 53 var c4 = ({a: 1}) => 'c4 $a'; | |
| 54 var c5 = ({a: 1, b: 2}) => 'c5 $a $b'; | |
| 55 var c6 = ({b: 1, a: 2}) => 'c6 $a $b'; | |
| 56 | |
| 57 // TODO(ngeoffray): Remove these calls. They are currently needed | |
| 58 // because otherwise we would not generate the stubs. Once apply is | |
| 59 // in the specification, we should change the compiler to emit stubs | |
| 60 // for closures once it sees an 'apply' selector. | |
| 61 c1(); | |
| 62 c2(1); | |
| 63 c3(); c3(1); | |
| 64 c4(); c4(a: 1); | |
| 65 c5(); c5(a: 1); c5(b: 2); c5(a:1, b: 2); | |
| 66 c6(); c6(a: 1); c6(b: 2); c6(a:1, b: 2); | |
| 67 | |
| 68 Expect.equals('c1', apply(c1, new ArgumentDescriptor(null, null))); | |
| 69 Expect.equals('c1', apply(c1, new ArgumentDescriptor([], null))); | |
| 70 Expect.equals('c1', apply(c1, new ArgumentDescriptor([], {}))); | |
| 71 Expect.equals('c1', apply(c1, new ArgumentDescriptor(null, {}))); | |
| 72 throwsNSME(() => apply(c1, new ArgumentDescriptor([1], null))); | |
| 73 throwsNSME(() => apply(c1, new ArgumentDescriptor([1], {'a': 2}))); | |
| 74 throwsNSME(() => apply(c1, new ArgumentDescriptor(null, {'a': 2}))); | |
| 75 | |
| 76 Expect.equals('c2 1', apply(c2, new ArgumentDescriptor([1], null))); | |
| 77 Expect.equals('c2 1', apply(c2, new ArgumentDescriptor([1], {}))); | |
| 78 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, null))); | |
| 79 throwsNSME(() => apply(c2, new ArgumentDescriptor([], null))); | |
| 80 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, {}))); | |
| 81 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, {'a': 1}))); | |
| 82 | |
| 83 Expect.equals('c3 1', apply(c3, new ArgumentDescriptor([], null))); | |
| 84 Expect.equals('c3 2', apply(c3, new ArgumentDescriptor([2], {}))); | |
| 85 throwsNSME(() => apply(c3, new ArgumentDescriptor([1, 2], null))); | |
| 86 // TODO(ngeoffray): Should be throwsNSME with the new parameter | |
| 87 // specification. | |
| 88 Expect.equals('c3 1', apply(c3, new ArgumentDescriptor(null, {'a': 1}))); | |
| 89 | |
| 90 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor([], null))); | |
| 91 Expect.equals('c4 2', apply(c4, new ArgumentDescriptor([], {'a': 2}))); | |
| 92 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor(null, null))); | |
| 93 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor([], {}))); | |
| 94 throwsNSME(() => apply(c4, new ArgumentDescriptor([1], {'a': 1}))); | |
| 95 throwsNSME(() => apply(c4, new ArgumentDescriptor([1], {}))); | |
| 96 throwsNSME(() => apply(c4, new ArgumentDescriptor([], {'a': 1, 'b': 2}))); | |
| 97 | |
| 98 Expect.equals('c5 1 2', apply(c5, new ArgumentDescriptor([], null))); | |
| 99 Expect.equals('c5 3 2', apply(c5, new ArgumentDescriptor([], {'a': 3}))); | |
| 100 Expect.equals('c5 1 2', apply(c5, new ArgumentDescriptor(null, null))); | |
| 101 Expect.equals('c5 1 2', apply(c5, new ArgumentDescriptor([], {}))); | |
| 102 Expect.equals('c5 3 4', | |
| 103 apply(c5, new ArgumentDescriptor([], {'a': 3, 'b': 4}))); | |
| 104 Expect.equals('c5 4 3', | |
| 105 apply(c5, new ArgumentDescriptor([], {'b': 3, 'a': 4}))); | |
| 106 Expect.equals('c5 1 3', | |
| 107 apply(c5, new ArgumentDescriptor([], {'b': 3}))); | |
| 108 throwsNSME(() => apply(c5, new ArgumentDescriptor([1], {'a': 1}))); | |
| 109 throwsNSME(() => apply(c5, new ArgumentDescriptor([1], {}))); | |
| 110 throwsNSME(() => | |
| 111 apply(c5, new ArgumentDescriptor([], {'a': 1, 'b': 2, 'c': 3}))); | |
| 112 | |
| 113 Expect.equals('c6 2 1', apply(c6, new ArgumentDescriptor([], null))); | |
| 114 Expect.equals('c6 3 1', apply(c6, new ArgumentDescriptor([], {'a': 3}))); | |
| 115 Expect.equals('c6 2 1', apply(c6, new ArgumentDescriptor(null, null))); | |
| 116 Expect.equals('c6 2 1', apply(c6, new ArgumentDescriptor([], {}))); | |
| 117 Expect.equals('c6 3 4', | |
| 118 apply(c6, new ArgumentDescriptor([], {'a': 3, 'b': 4}))); | |
| 119 Expect.equals('c6 4 3', | |
| 120 apply(c6, new ArgumentDescriptor([], {'b': 3, 'a': 4}))); | |
| 121 Expect.equals('c6 2 3', | |
| 122 apply(c6, new ArgumentDescriptor([], {'b': 3}))); | |
| 123 throwsNSME(() => apply(c6, new ArgumentDescriptor([1], {'a': 1}))); | |
| 124 throwsNSME(() => apply(c6, new ArgumentDescriptor([1], {}))); | |
| 125 throwsNSME(() => | |
| 126 apply(c6, new ArgumentDescriptor([], {'a': 1, 'b': 2, 'c': 3}))); | |
| 127 } | |
| OLD | NEW |