OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
karlklose
2014/04/01 08:32:23
2013 -> 2014.
floitsch
2014/04/01 14:03:19
Done.
| |
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 library test.invoke_closurization_test; | |
6 | |
7 import 'dart:mirrors'; | |
8 | |
9 import 'package:expect/expect.dart'; | |
10 | |
11 class A { | |
12 foo() => "foo"; | |
13 bar([x]) => "bar-$x"; | |
14 gee({named}) => "gee-$named"; | |
15 | |
16 // Methods that must be intercepted. | |
17 //---------- | |
karlklose
2014/04/01 08:32:23
Delete this line?
floitsch
2014/04/01 14:03:19
Done.
| |
18 | |
19 // Tear-offs we will also get without mirrors. | |
20 codeUnitAt(x) => "codeUnitAt-$x"; | |
21 toUpperCase() => "toUpperCase"; | |
22 // indexOf takes an optional argument in String. | |
23 indexOf(x) => "indexOf-$x"; | |
24 // lastIndexOf matches signature from String. | |
25 lastIndexOf(x, [y]) => "lastIndexOf-$x,$y"; | |
26 // splitMapJoin matches signature from String. | |
27 splitMapJoin(x, {onMatch, onNonMatch}) => | |
28 "splitMapJoin-$x,$onMatch,$onNonMatch"; | |
29 // Same name as intercepted, but with named argument. | |
30 trim({named}) => "trim-$named"; | |
31 | |
32 // Tear-offs we will not call directly. | |
33 endsWith(x) => "endsWith-$x"; | |
34 toLowerCase() => "toLowerCase"; | |
35 // matchAsPrefix matches signature from String. | |
36 matchAsPrefix(x, [y = 0]) => "matchAsPrefix-$x,$y"; | |
37 // Matches signature from List | |
38 toList({growable: true}) => "toList-$growable"; | |
39 // Same name as intercepted, but with named argument. | |
40 toSet({named}) => "toSet-$named"; | |
41 } | |
42 | |
43 // The recursive call makes inlining difficult. | |
44 // The use of DateTime.now makes the result unpredictable. | |
45 confuse(x) { | |
46 if (new DateTime.now().millisecondsSinceEpoch == 42) { | |
47 return confuse(new DateTime.now().millisecondsSinceEpoch); | |
48 } | |
49 return x; | |
50 } | |
51 | |
52 main() { | |
53 var list = ["foo", new List(), new A()]; | |
54 confuse(0); | |
karlklose
2014/04/01 08:32:23
Why these calls?
floitsch
2014/04/01 14:03:19
Initially the recursive confuse call was recursing
| |
55 confuse(1); | |
56 | |
57 getAMirror() => reflect(list[confuse(2)]); | |
58 | |
59 // Tear-off without mirrors. | |
60 var f = confuse(getAMirror().reflectee.codeUnitAt); | |
61 Expect.equals("codeUnitAt-42", f(42)); | |
62 f = confuse(getAMirror().reflectee.toUpperCase); | |
63 Expect.equals("toUpperCase", f()); | |
64 f = confuse(getAMirror().reflectee.indexOf); | |
65 Expect.equals("indexOf-499", f(499)); | |
66 f = confuse(getAMirror().reflectee.lastIndexOf); | |
67 Expect.equals("lastIndexOf-FOO,BAR", f("FOO", "BAR")); | |
68 f = confuse(getAMirror().reflectee.splitMapJoin); | |
69 Expect.equals("splitMapJoin-1,2,3", f(1, onMatch: 2, onNonMatch: 3)); | |
70 f = confuse(getAMirror().reflectee.trim); | |
71 Expect.equals("trim-true", f(named: true)); | |
72 | |
73 // Now the same thing through mirrors. | |
74 f = getAMirror().getField(#codeUnitAt).reflectee; | |
75 Expect.equals("codeUnitAt-42", f(42)); | |
76 f = getAMirror().getField(#toUpperCase).reflectee; | |
77 Expect.equals("toUpperCase", f()); | |
78 f = getAMirror().getField(#indexOf).reflectee; | |
79 Expect.equals("indexOf-499", f(499)); | |
80 f = getAMirror().getField(#lastIndexOf).reflectee; | |
81 Expect.equals("lastIndexOf-FOO,BAR", f("FOO", "BAR")); | |
82 f = getAMirror().getField(#splitMapJoin).reflectee; | |
83 Expect.equals("splitMapJoin-1,2,3", f(1, onMatch: 2, onNonMatch: 3)); | |
84 f = getAMirror().getField(#trim).reflectee; | |
85 Expect.equals("trim-true", f(named: true)); | |
86 | |
87 // Now the same thing through mirrors and mirror-invocation. | |
88 f = getAMirror().getField(#codeUnitAt); | |
89 Expect.equals("codeUnitAt-42", f.invoke(#call, [42], {}).reflectee); | |
90 f = getAMirror().getField(#toUpperCase); | |
91 Expect.equals("toUpperCase", f.invoke(#call, [], {}).reflectee); | |
92 f = getAMirror().getField(#indexOf); | |
93 Expect.equals("indexOf-499", f.invoke(#call, [499], {}).reflectee); | |
94 f = getAMirror().getField(#lastIndexOf); | |
95 Expect.equals("lastIndexOf-FOO,BAR", | |
96 f.invoke(#call, ["FOO", "BAR"]).reflectee); | |
97 f = getAMirror().getField(#splitMapJoin); | |
98 Expect.equals("splitMapJoin-1,2,3", | |
99 f.invoke(#call, [1], {#onMatch: 2, #onNonMatch: 3}).reflectee); | |
100 f = getAMirror().getField(#trim); | |
101 Expect.equals("trim-true", f.invoke(#call, [], {#named: true}).reflectee); | |
102 | |
103 // Tear-offs only through mirrors. (No direct selector in the code). | |
104 // -------- | |
105 | |
106 f = getAMirror().getField(#endsWith).reflectee; | |
107 Expect.equals("endsWith-42", f(42)); | |
108 f = getAMirror().getField(#toLowerCase).reflectee; | |
109 Expect.equals("toLowerCase", f()); | |
110 f = getAMirror().getField(#indexOf).reflectee; | |
111 Expect.equals("indexOf-499", f(499)); | |
112 f = getAMirror().getField(#matchAsPrefix).reflectee; | |
113 Expect.equals("matchAsPrefix-FOO,BAR", f("FOO", "BAR")); | |
114 f = getAMirror().getField(#toList).reflectee; | |
115 Expect.equals("toList-1", f(growable: 1)); | |
116 f = getAMirror().getField(#toSet).reflectee; | |
117 Expect.equals("toSet-true", f(named: true)); | |
118 | |
119 f = getAMirror().getField(#endsWith); | |
120 Expect.equals("endsWith-42", f.invoke(#call, [42], {}).reflectee); | |
121 f = getAMirror().getField(#toLowerCase); | |
122 Expect.equals("toLowerCase", f.invoke(#call, [], {}).reflectee); | |
123 f = getAMirror().getField(#indexOf); | |
124 Expect.equals("indexOf-499", f.invoke(#call, [499], {}).reflectee); | |
125 f = getAMirror().getField(#matchAsPrefix); | |
126 Expect.equals("matchAsPrefix-FOO,BAR", | |
127 f.invoke(#call, ["FOO", "BAR"]).reflectee); | |
128 f = getAMirror().getField(#toList); | |
129 Expect.equals("toList-1", | |
130 f.invoke(#call, [], {#growable: 1}).reflectee); | |
131 f = getAMirror().getField(#toSet); | |
132 Expect.equals("toSet-true", f.invoke(#call, [], {#named: true}).reflectee); | |
133 } | |
OLD | NEW |