OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 | 4 |
5 // Tests of interceptors. | 5 // Tests of interceptors. |
6 | 6 |
7 library constructor_test; | 7 library constructor_test; |
8 | 8 |
9 import 'js_backend_cps_ir.dart'; | 9 import 'js_backend_cps_ir.dart'; |
10 | 10 |
11 const List<TestEntry> tests = const [ | 11 const List<TestEntry> tests = const [ |
12 const TestEntry.forMethod('generative_constructor(Sub#)', """ | 12 const TestEntry(""" |
13 class Base { | 13 class Base { |
14 var x; | 14 var x; |
15 Base(this.x); | 15 Base(this.x); |
16 } | 16 } |
17 class Sub extends Base { | 17 class Sub extends Base { |
18 var y; | 18 var y; |
19 Sub(x, this.y) : super(x); | 19 Sub(x, this.y) : super(x); |
20 } | 20 } |
21 main() { | 21 main() { |
22 print(new Sub(1, 2).x); | 22 print(new Sub(1, 2).x); |
23 }""", | 23 }""", |
24 r""" | 24 r""" |
25 function(x, y) { | 25 function() { |
26 return new V.Sub(y, x); | 26 var v0 = H.S(1); |
| 27 if (typeof dartPrint == "function") |
| 28 dartPrint(v0); |
| 29 else if (typeof console == "object" && typeof console.log != "undefined") |
| 30 console.log(v0); |
| 31 else if (!(typeof window == "object")) { |
| 32 if (!(typeof print == "function")) |
| 33 throw "Unable to print message: " + String(v0); |
| 34 print(v0); |
| 35 } |
27 }"""), | 36 }"""), |
28 | 37 |
29 const TestEntry.forMethod('generative_constructor_body(Sub#)', """ | 38 const TestEntry(""" |
30 class Base { | 39 class Base { |
31 var x; | 40 var x; |
32 Base(this.x); | 41 Base(this.x); |
33 } | 42 } |
34 class Sub extends Base { | 43 class Sub extends Base { |
35 var y; | 44 var y; |
36 Sub(x, this.y) : super(x) { | 45 Sub(x, this.y) : super(x) { |
37 print(x); | 46 print(x); |
38 } | 47 } |
39 } | 48 } |
40 main() { | 49 main() { |
41 print(new Sub(1, 2).x); | 50 print(new Sub(1, 2).x); |
42 }""", | 51 }""", |
43 r""" | 52 r""" |
44 function(x, y) { | 53 function() { |
45 P.print(x); | 54 P.print(1); |
| 55 P.print(1); |
46 }"""), | 56 }"""), |
47 | 57 |
48 const TestEntry.forMethod('generative_constructor(Sub#)', """ | 58 const TestEntry(""" |
49 class Base0 { | 59 class Base0 { |
50 Base0() { | 60 Base0() { |
51 print('Base0'); | 61 print('Base0'); |
52 } | 62 } |
53 } | 63 } |
54 class Base extends Base0 { | 64 class Base extends Base0 { |
55 var x; | 65 var x; |
56 Base(this.x); | 66 Base(this.x); |
57 } | 67 } |
58 class Sub extends Base { | 68 class Sub extends Base { |
59 var y; | 69 var y; |
60 Sub(x, this.y) : super(x) { | 70 Sub(x, this.y) : super(x) { |
61 print(x); | 71 print(x); |
62 } | 72 } |
63 } | 73 } |
64 main() { | 74 main() { |
65 print(new Sub(1, 2).x); | 75 print(new Sub(1, 2).x); |
66 }""", | 76 }""", |
67 r""" | 77 r""" |
68 function(x, y) { | 78 function() { |
69 var v0 = new V.Sub(y, x); | 79 P.print("Base0"); |
70 v0.Base0$0(); | 80 P.print(1); |
71 v0.Sub$2(x, y); | 81 P.print(1); |
72 return v0; | |
73 }"""), | 82 }"""), |
74 | 83 |
75 const TestEntry.forMethod('generative_constructor(Sub#)', """ | 84 const TestEntry(""" |
76 class Base0 { | 85 class Base0 { |
77 Base0() { | 86 Base0() { |
78 print('Base0'); | 87 print('Base0'); |
79 } | 88 } |
80 } | 89 } |
81 class Base extends Base0 { | 90 class Base extends Base0 { |
82 var x; | 91 var x; |
83 Base(x1) : x = (() => ++x1) { | 92 Base(x1) : x = (() => ++x1) { |
84 print(x1); // use boxed x1 | 93 print(x1); // use boxed x1 |
85 } | 94 } |
86 } | 95 } |
87 class Sub extends Base { | 96 class Sub extends Base { |
88 var y; | 97 var y; |
89 Sub(x, this.y) : super(x) { | 98 Sub(x, this.y) : super(x) { |
90 print(x); | 99 print(x); |
91 } | 100 } |
92 } | 101 } |
93 main() { | 102 main() { |
94 print(new Sub(1, 2).x); | 103 print(new Sub(1, 2).x); |
95 }""", | 104 }""", |
96 r""" | 105 r""" |
97 function(x, y) { | 106 function() { |
98 var _box_0 = {}, v0 = new V.Sub(y, new V.Base_closure(_box_0)); | 107 var _box_0 = {}; |
99 _box_0.x1 = x; | 108 _box_0.x1 = 1; |
100 v0.Base0$0(); | 109 P.print("Base0"); |
101 v0.Base$1(_box_0); | 110 P.print(_box_0.x1); |
102 v0.Sub$2(x, y); | 111 P.print(1); |
103 return v0; | 112 P.print(new V.Base_closure(_box_0)); |
104 }"""), | 113 }"""), |
105 | 114 |
106 const TestEntry.forMethod('generative_constructor(Sub#)', """ | 115 const TestEntry(""" |
107 foo(x) { | 116 foo(x) { |
108 print(x); | 117 print(x); |
109 } | 118 } |
110 class Base { | 119 class Base { |
111 var x1 = foo('x1'); | 120 var x1 = foo('x1'); |
112 var x2; | 121 var x2; |
113 var x3 = foo('x3'); | 122 var x3 = foo('x3'); |
114 Base() : x2 = foo('x2'); | 123 Base() : x2 = foo('x2'); |
115 } | 124 } |
116 class Sub extends Base { | 125 class Sub extends Base { |
117 var y1 = foo('y1'); | 126 var y1 = foo('y1'); |
118 var y2; | 127 var y2; |
119 var y3; | 128 var y3; |
120 Sub() : y2 = foo('y2'), super(), y3 = foo('y3'); | 129 Sub() : y2 = foo('y2'), super(), y3 = foo('y3'); |
121 } | 130 } |
122 main() { | 131 main() { |
123 new Sub(); | 132 new Sub(); |
124 } | 133 } |
125 """, | 134 """, |
126 r""" | 135 r""" |
127 function() { | 136 function() { |
128 var v0 = V.foo("y1"), v1 = V.foo("y2"), v2 = V.foo("x1"), v3 = V.foo("x3"), v4
= V.foo("x2"); | 137 V.foo("y1"); |
129 return new V.Sub(v0, v1, V.foo("y3"), v2, v4, v3); | 138 V.foo("y2"); |
| 139 V.foo("x1"); |
| 140 V.foo("x3"); |
| 141 V.foo("x2"); |
| 142 V.foo("y3"); |
130 }"""), | 143 }"""), |
131 | 144 |
132 | 145 |
133 const TestEntry.forMethod('generative_constructor(Foo#)', """ | 146 const TestEntry(""" |
134 class Bar { | 147 class Bar { |
135 Bar(x, {y, z: 'z', w: '_', q}) { | 148 Bar(x, {y, z: 'z', w: '_', q}) { |
136 print(x); | 149 print(x); |
137 print(y); | 150 print(y); |
138 print(z); | 151 print(z); |
139 print(w); | 152 print(w); |
140 print(q); | 153 print(q); |
141 } | 154 } |
142 } | 155 } |
143 class Foo extends Bar { | 156 class Foo extends Bar { |
144 Foo() : super('x', y: 'y', w: 'w'); | 157 Foo() : super('x', y: 'y', w: 'w'); |
145 } | 158 } |
146 main() { | 159 main() { |
147 new Foo(); | 160 new Foo(); |
148 } | 161 } |
149 """, | 162 """, |
150 r""" | 163 r""" |
151 function() { | 164 function() { |
152 var v0 = new V.Foo(); | 165 P.print("x"); |
153 v0.Bar$5$q$w$y$z("x", null, "w", "y", "z"); | 166 P.print("y"); |
154 return v0; | 167 P.print("z"); |
| 168 P.print("w"); |
| 169 P.print(null); |
155 }"""), | 170 }"""), |
156 const TestEntry(r""" | 171 const TestEntry(r""" |
157 class C<T> { | 172 class C<T> { |
158 foo() => T; | 173 foo() => T; |
159 } | 174 } |
160 main() { | 175 main() { |
161 print(new C<int>().foo()); | 176 print(new C<int>().foo()); |
162 }""", r""" | 177 }""", r""" |
163 function() { | 178 function() { |
164 P.print(V.C$(P.$int).foo$0()); | 179 var v0 = H.S(H.createRuntimeType(H.runtimeTypeToString(H.getTypeArgumentByInde
x(V.C$(P.$int), 0)))); |
| 180 if (typeof dartPrint == "function") |
| 181 dartPrint(v0); |
| 182 else if (typeof console == "object" && typeof console.log != "undefined") |
| 183 console.log(v0); |
| 184 else if (!(typeof window == "object")) { |
| 185 if (!(typeof print == "function")) |
| 186 throw "Unable to print message: " + String(v0); |
| 187 print(v0); |
| 188 } |
165 }"""), | 189 }"""), |
166 const TestEntry(r""" | 190 const TestEntry(r""" |
167 class C<T> { | 191 class C<T> { |
168 foo() => C; | 192 foo() => C; |
169 } | 193 } |
170 main() { | 194 main() { |
171 print(new C<int>().foo()); | 195 print(new C<int>().foo()); |
172 }""", r""" | 196 }""", r""" |
173 function() { | 197 function() { |
174 P.print(V.C$().foo$0()); | 198 var v0; |
| 199 V.C$(); |
| 200 v0 = H.S(C.Type_C_cdS); |
| 201 if (typeof dartPrint == "function") |
| 202 dartPrint(v0); |
| 203 else if (typeof console == "object" && typeof console.log != "undefined") |
| 204 console.log(v0); |
| 205 else if (!(typeof window == "object")) { |
| 206 if (!(typeof print == "function")) |
| 207 throw "Unable to print message: " + String(v0); |
| 208 print(v0); |
| 209 } |
175 }"""), | 210 }"""), |
176 const TestEntry.forMethod('generative_constructor(C#)', r""" | 211 const TestEntry.forMethod('generative_constructor(C#)', r""" |
177 class C<T> { | 212 class C<T> { |
178 C() { print(T); } | 213 C() { print(T); } |
179 foo() => print(T); | 214 foo() => print(T); |
180 } | 215 } |
181 main() { | 216 main() { |
182 new C<int>(); | 217 new C<int>(); |
183 }""", r""" | 218 }""", r""" |
184 function($T) { | 219 function($T) { |
(...skipping 10 matching lines...) Expand all Loading... |
195 foo() => T; | 230 foo() => T; |
196 } | 231 } |
197 main() { | 232 main() { |
198 print(new C<int>().x.foo()); | 233 print(new C<int>().x.foo()); |
199 }""", r""" | 234 }""", r""" |
200 function($T) { | 235 function($T) { |
201 return H.setRuntimeTypeInfo(new V.C(V.D$($T)), [$T]); | 236 return H.setRuntimeTypeInfo(new V.C(V.D$($T)), [$T]); |
202 }"""), | 237 }"""), |
203 | 238 |
204 | 239 |
205 const TestEntry.forMethod('generative_constructor(A#)', r""" | 240 const TestEntry(r""" |
206 class A { | 241 class A { |
207 var x; | 242 var x; |
208 A() : this.b(1); | 243 A() : this.b(1); |
209 A.b(this.x); | 244 A.b(this.x); |
210 } | 245 } |
211 main() { | 246 main() { |
212 print(new A().x); | 247 print(new A().x); |
213 }""", r""" | 248 }""", r""" |
214 function() { | 249 function() { |
215 return new V.A(1); | 250 var v0 = H.S(1); |
| 251 if (typeof dartPrint == "function") |
| 252 dartPrint(v0); |
| 253 else if (typeof console == "object" && typeof console.log != "undefined") |
| 254 console.log(v0); |
| 255 else if (!(typeof window == "object")) { |
| 256 if (!(typeof print == "function")) |
| 257 throw "Unable to print message: " + String(v0); |
| 258 print(v0); |
| 259 } |
216 }"""), | 260 }"""), |
217 | 261 |
218 | 262 |
219 const TestEntry.forMethod('factory_constructor(Foo#make)', r""" | 263 const TestEntry(r""" |
220 class Foo { | 264 class Foo { |
221 factory Foo.make(x) { | 265 factory Foo.make(x) { |
222 print('Foo'); | 266 print('Foo'); |
223 return new Foo.create(x); | 267 return new Foo.create(x); |
224 } | 268 } |
225 var x; | 269 var x; |
226 Foo.create(this.x); | 270 Foo.create(this.x); |
227 } | 271 } |
228 main() { | 272 main() { |
229 print(new Foo.make(5)); | 273 print(new Foo.make(5)); |
230 }""", r""" | 274 }""", r""" |
231 function(x) { | 275 function() { |
232 P.print("Foo"); | 276 P.print("Foo"); |
233 return V.Foo$create(x); | 277 P.print(new V.Foo(5)); |
234 }"""), | 278 }"""), |
235 const TestEntry(r""" | 279 const TestEntry(r""" |
236 class Foo { | 280 class Foo { |
237 factory Foo.make(x) = Foo.create; | 281 factory Foo.make(x) = Foo.create; |
238 var x; | 282 var x; |
239 Foo.create(this.x); | 283 Foo.create(this.x); |
240 } | 284 } |
241 main() { | 285 main() { |
242 print(new Foo.make(5)); | 286 print(new Foo.make(5)); |
243 }""", r""" | 287 }""", r""" |
244 function() { | 288 function() { |
245 P.print(V.Foo$create(5)); | 289 var v0 = new V.Foo(5), v1 = "Instance of '" + H.Primitives_objectTypeName(v0)
+ "'"; |
| 290 if (!(typeof v1 === "string")) |
| 291 throw H.wrapException(H.argumentErrorValue(v0)); |
| 292 v0 = v1; |
| 293 if (typeof dartPrint == "function") |
| 294 dartPrint(v0); |
| 295 else if (typeof console == "object" && typeof console.log != "undefined") |
| 296 console.log(v0); |
| 297 else if (!(typeof window == "object")) { |
| 298 if (!(typeof print == "function")) |
| 299 throw "Unable to print message: " + String(v0); |
| 300 print(v0); |
| 301 } |
246 }"""), | 302 }"""), |
247 const TestEntry(r""" | 303 const TestEntry(r""" |
248 class A { | 304 class A { |
249 factory A(x) = B<int>; | 305 factory A(x) = B<int>; |
250 get typevar; | 306 get typevar; |
251 } | 307 } |
252 class B<T> implements A { | 308 class B<T> implements A { |
253 var x; | 309 var x; |
254 B(this.x); | 310 B(this.x); |
255 | 311 |
256 get typevar => T; | 312 get typevar => T; |
257 } | 313 } |
258 main() { | 314 main() { |
259 new A(5).typevar; | 315 new A(5).typevar; |
260 }""", r""" | 316 }""", r""" |
261 function() { | 317 function() { |
262 V.B$(5, P.$int).get$typevar(); | 318 V.B$(5, P.$int); |
263 }"""), | 319 }"""), |
264 ]; | 320 ]; |
265 | 321 |
266 void main() { | 322 void main() { |
267 runTests(tests); | 323 runTests(tests); |
268 } | 324 } |
OLD | NEW |