OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 closures. | 5 // Tests of closures. |
6 | 6 |
7 library closures_test; | 7 library closures_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(""" | 12 const TestEntry(""" |
13 main(x) { | 13 main(x) { |
14 a() { | 14 a() { |
15 return x; | 15 return x; |
16 } | 16 } |
17 x = x + '1'; | 17 x = x + '1'; |
18 print(a()); | 18 print(a()); |
19 } | 19 } |
20 """, | 20 """, |
21 r""" | 21 r""" |
22 function(x) { | 22 function(x) { |
23 var _box_0 = {}, a = new V.main_a(_box_0); | 23 var _box_0 = {}, a = new V.main_a(_box_0); |
24 _box_0._captured_x_0 = x; | 24 _box_0._captured_x_0 = x; |
25 _box_0._captured_x_0 = J.getInterceptor$ns(x = _box_0._captured_x_0).$add(x, "
1"); | 25 _box_0._captured_x_0 = J.getInterceptor$ns(x = _box_0._captured_x_0).$add(x, "
1"); |
26 P.print(a.call$0()); | 26 P.print(a.call$0()); |
27 return null; | |
28 }"""), | 27 }"""), |
29 | 28 |
30 const TestEntry(""" | 29 const TestEntry(""" |
31 main(x) { | 30 main(x) { |
32 a() { | 31 a() { |
33 return x; | 32 return x; |
34 } | 33 } |
35 print(a()); | 34 print(a()); |
36 } | 35 } |
37 """, | 36 """, |
38 r""" | 37 r""" |
39 function(x) { | 38 function(x) { |
40 P.print(new V.main_a(x).call$0()); | 39 P.print(new V.main_a(x).call$0()); |
41 return null; | |
42 }"""), | 40 }"""), |
43 | 41 |
44 const TestEntry(""" | 42 const TestEntry(""" |
45 main() { | 43 main() { |
46 var x = 122; | 44 var x = 122; |
47 var a = () => x; | 45 var a = () => x; |
48 x = x + 1; | 46 x = x + 1; |
49 print(a()); | 47 print(a()); |
50 } | 48 } |
51 """, | 49 """, |
52 r""" | 50 r""" |
53 function() { | 51 function() { |
54 var _box_0 = {}, a = new V.main_closure(_box_0); | 52 var _box_0 = {}, a = new V.main_closure(_box_0); |
55 _box_0._captured_x_0 = 122; | 53 _box_0._captured_x_0 = 122; |
56 _box_0._captured_x_0 = _box_0._captured_x_0 + 1; | 54 _box_0._captured_x_0 = _box_0._captured_x_0 + 1; |
57 P.print(a.call$0()); | 55 P.print(a.call$0()); |
58 return null; | |
59 }"""), | 56 }"""), |
60 | 57 |
61 const TestEntry(""" | 58 const TestEntry(""" |
62 main() { | 59 main() { |
63 var x = 122; | 60 var x = 122; |
64 var a = () { | 61 var a = () { |
65 var y = x; | 62 var y = x; |
66 return () => y; | 63 return () => y; |
67 }; | 64 }; |
68 x = x + 1; | 65 x = x + 1; |
69 print(a()()); | 66 print(a()()); |
70 } | 67 } |
71 """, | 68 """, |
72 r""" | 69 r""" |
73 function() { | 70 function() { |
74 var _box_0 = {}, a = new V.main_closure(_box_0); | 71 var _box_0 = {}, a = new V.main_closure(_box_0); |
75 _box_0._captured_x_0 = 122; | 72 _box_0._captured_x_0 = 122; |
76 _box_0._captured_x_0 = _box_0._captured_x_0 + 1; | 73 _box_0._captured_x_0 = _box_0._captured_x_0 + 1; |
77 P.print(a.call$0().call$0()); | 74 P.print(a.call$0().call$0()); |
78 return null; | |
79 }"""), | 75 }"""), |
80 | 76 |
81 const TestEntry(""" | 77 const TestEntry(""" |
82 main() { | 78 main() { |
83 var a; | 79 var a; |
84 for (var i=0; i<10; i++) { | 80 for (var i=0; i<10; i++) { |
85 a = () => i; | 81 a = () => i; |
86 } | 82 } |
87 print(a()); | 83 print(a()); |
88 } | 84 } |
89 """, | 85 """, |
90 r""" | 86 r""" |
91 function() { | 87 function() { |
92 var a = null, i = 0; | 88 var a = null, i = 0; |
93 while (i < 10) { | 89 while (i < 10) { |
94 a = new V.main_closure(i); | 90 a = new V.main_closure(i); |
95 i = i + 1; | 91 i = i + 1; |
96 } | 92 } |
97 P.print(a.call$0()); | 93 P.print(a.call$0()); |
98 return null; | |
99 }"""), | 94 }"""), |
100 | 95 |
101 const TestEntry.forMethod('function(A#b)', """ | 96 const TestEntry.forMethod('function(A#b)', """ |
102 class A { | 97 class A { |
103 a() => 1; | 98 a() => 1; |
104 b() => () => a(); | 99 b() => () => a(); |
105 } | 100 } |
106 main() { | 101 main() { |
107 print(new A().b()()); | 102 print(new A().b()()); |
108 } | 103 } |
109 """, | 104 """, |
110 r""" | 105 r""" |
111 function() { | 106 function() { |
112 return new V.A_b_closure(this); | 107 return new V.A_b_closure(this); |
113 }"""), | 108 }"""), |
114 ]; | 109 ]; |
115 | 110 |
116 void main() { | 111 void main() { |
117 runTests(tests); | 112 runTests(tests); |
118 } | 113 } |
OLD | NEW |