Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(248)

Side by Side Diff: tests/corelib/apply2_test.dart

Issue 11093015: Implement Function.apply in dart2js. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 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 4
8 apply(Function function, ArgumentDescriptor args) { 5 apply(Function function, ArgumentDescriptor args) {
9 int argumentCount = 0; 6 return Function.apply(
10 StringBuffer buffer = new StringBuffer(); 7 function, args.positionalArguments, args.namedArguments);
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 } 8 }
45 9
46 class ArgumentDescriptor { 10 class ArgumentDescriptor {
47 final List positionalArguments; 11 final List positionalArguments;
48 final Map<String, Dynamic> namedArguments; 12 final Map<String, Dynamic> namedArguments;
49 13
50 ArgumentDescriptor(this.positionalArguments, this.namedArguments); 14 ArgumentDescriptor(this.positionalArguments, this.namedArguments);
51 } 15 }
52 16
53 void throwsNSME(function) { 17 void throwsNSME(function) {
54 Expect.throws(function, (e) => e is NoSuchMethodError); 18 Expect.throws(function, (e) => e is NoSuchMethodError);
55 } 19 }
56 20
57 main() { 21 main() {
58 var c1 = () => 'c1'; 22 var c1 = () => 'c1';
59 var c2 = (a) => 'c2 $a'; 23 var c2 = (a) => 'c2 $a';
60 var c3 = ([a = 1]) => 'c3 $a'; 24 var c3 = ([a = 1]) => 'c3 $a';
61 var c4 = ({a: 1}) => 'c4 $a'; 25 var c4 = ({a: 1}) => 'c4 $a';
62 var c5 = ({a: 1, b: 2}) => 'c5 $a $b'; 26 var c5 = ({a: 1, b: 2}) => 'c5 $a $b';
63 var c6 = ({b: 1, a: 2}) => 'c6 $a $b'; 27 var c6 = ({b: 1, a: 2}) => 'c6 $a $b';
64 28
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))); 29 Expect.equals('c1', apply(c1, new ArgumentDescriptor(null, null)));
77 Expect.equals('c1', apply(c1, new ArgumentDescriptor([], null))); 30 Expect.equals('c1', apply(c1, new ArgumentDescriptor([], null)));
78 Expect.equals('c1', apply(c1, new ArgumentDescriptor([], {}))); 31 Expect.equals('c1', apply(c1, new ArgumentDescriptor([], {})));
79 Expect.equals('c1', apply(c1, new ArgumentDescriptor(null, {}))); 32 Expect.equals('c1', apply(c1, new ArgumentDescriptor(null, {})));
80 throwsNSME(() => apply(c1, new ArgumentDescriptor([1], null))); 33 throwsNSME(() => apply(c1, new ArgumentDescriptor([1], null)));
81 throwsNSME(() => apply(c1, new ArgumentDescriptor([1], {'a': 2}))); 34 throwsNSME(() => apply(c1, new ArgumentDescriptor([1], {'a': 2})));
82 throwsNSME(() => apply(c1, new ArgumentDescriptor(null, {'a': 2}))); 35 throwsNSME(() => apply(c1, new ArgumentDescriptor(null, {'a': 2})));
83 36
84 Expect.equals('c2 1', apply(c2, new ArgumentDescriptor([1], null))); 37 Expect.equals('c2 1', apply(c2, new ArgumentDescriptor([1], null)));
85 Expect.equals('c2 1', apply(c2, new ArgumentDescriptor([1], {}))); 38 Expect.equals('c2 1', apply(c2, new ArgumentDescriptor([1], {})));
86 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, null))); 39 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, null)));
87 throwsNSME(() => apply(c2, new ArgumentDescriptor([], null))); 40 throwsNSME(() => apply(c2, new ArgumentDescriptor([], null)));
88 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, {}))); 41 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, {})));
89 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, {'a': 1}))); 42 throwsNSME(() => apply(c2, new ArgumentDescriptor(null, {'a': 1})));
90 43
91 Expect.equals('c3 1', apply(c3, new ArgumentDescriptor([], null))); 44 Expect.equals('c3 1', apply(c3, new ArgumentDescriptor([], null)));
92 Expect.equals('c3 2', apply(c3, new ArgumentDescriptor([2], {}))); 45 Expect.equals('c3 2', apply(c3, new ArgumentDescriptor([2], {})));
93 throwsNSME(() => apply(c3, new ArgumentDescriptor([1, 2], null))); 46 throwsNSME(() => apply(c3, new ArgumentDescriptor([1, 2], null)));
94 // TODO(ngeoffray): Should be throwsNSME with the new parameter 47 throwsNSME(() => apply(c3, new ArgumentDescriptor(null, {'a': 1})));
95 // specification.
96 Expect.equals('c3 1', apply(c3, new ArgumentDescriptor(null, {'a': 1})));
97 48
98 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor([], null))); 49 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor([], null)));
99 Expect.equals('c4 2', apply(c4, new ArgumentDescriptor([], {'a': 2}))); 50 Expect.equals('c4 2', apply(c4, new ArgumentDescriptor([], {'a': 2})));
100 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor(null, null))); 51 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor(null, null)));
101 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor([], {}))); 52 Expect.equals('c4 1', apply(c4, new ArgumentDescriptor([], {})));
102 throwsNSME(() => apply(c4, new ArgumentDescriptor([1], {'a': 1}))); 53 throwsNSME(() => apply(c4, new ArgumentDescriptor([1], {'a': 1})));
103 throwsNSME(() => apply(c4, new ArgumentDescriptor([1], {}))); 54 throwsNSME(() => apply(c4, new ArgumentDescriptor([1], {})));
104 throwsNSME(() => apply(c4, new ArgumentDescriptor([], {'a': 1, 'b': 2}))); 55 throwsNSME(() => apply(c4, new ArgumentDescriptor([], {'a': 1, 'b': 2})));
105 56
106 Expect.equals('c5 1 2', apply(c5, new ArgumentDescriptor([], null))); 57 Expect.equals('c5 1 2', apply(c5, new ArgumentDescriptor([], null)));
(...skipping 19 matching lines...) Expand all
126 apply(c6, new ArgumentDescriptor([], {'a': 3, 'b': 4}))); 77 apply(c6, new ArgumentDescriptor([], {'a': 3, 'b': 4})));
127 Expect.equals('c6 4 3', 78 Expect.equals('c6 4 3',
128 apply(c6, new ArgumentDescriptor([], {'b': 3, 'a': 4}))); 79 apply(c6, new ArgumentDescriptor([], {'b': 3, 'a': 4})));
129 Expect.equals('c6 2 3', 80 Expect.equals('c6 2 3',
130 apply(c6, new ArgumentDescriptor([], {'b': 3}))); 81 apply(c6, new ArgumentDescriptor([], {'b': 3})));
131 throwsNSME(() => apply(c6, new ArgumentDescriptor([1], {'a': 1}))); 82 throwsNSME(() => apply(c6, new ArgumentDescriptor([1], {'a': 1})));
132 throwsNSME(() => apply(c6, new ArgumentDescriptor([1], {}))); 83 throwsNSME(() => apply(c6, new ArgumentDescriptor([1], {})));
133 throwsNSME(() => 84 throwsNSME(() =>
134 apply(c6, new ArgumentDescriptor([], {'a': 1, 'b': 2, 'c': 3}))); 85 apply(c6, new ArgumentDescriptor([], {'a': 1, 'b': 2, 'c': 3})));
135 } 86 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698