| 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 // VMOptions=-DUSE_CPS_IR=true | 4 // VMOptions=-DUSE_CPS_IR=true |
| 5 | 5 |
| 6 // Tests of closures. | 6 // Tests of closures. |
| 7 | 7 |
| 8 library closures_test; | 8 library closures_test; |
| 9 | 9 |
| 10 import 'js_backend_cps_ir.dart'; | 10 import 'js_backend_cps_ir.dart'; |
| 11 | 11 |
| 12 const List<TestEntry> tests = const [ | 12 const List<TestEntry> tests = const [ |
| 13 const TestEntry(""" | 13 const TestEntry(""" |
| 14 main(x) { | 14 main(x) { |
| 15 a() { | 15 a() { |
| 16 return x; | 16 return x; |
| 17 } | 17 } |
| 18 x = x + '1'; | 18 x = x + '1'; |
| 19 print(a()); | 19 print(a()); |
| 20 } | 20 } |
| 21 """, | 21 """, |
| 22 r""" | 22 r""" |
| 23 function(x) { | 23 function(x) { |
| 24 var _box_0, a; | 24 var _box_0 = {}, a; |
| 25 _box_0 = {}; | |
| 26 _box_0._captured_x_0 = x; | 25 _box_0._captured_x_0 = x; |
| 27 a = new V.main_a(_box_0); | 26 a = new V.main_a(_box_0); |
| 28 x = _box_0._captured_x_0; | 27 x = _box_0._captured_x_0; |
| 29 _box_0._captured_x_0 = J.getInterceptor$ns(x).$add(x, "1"); | 28 _box_0._captured_x_0 = J.getInterceptor$ns(x).$add(x, "1"); |
| 30 P.print(a.call$0()); | 29 P.print(a.call$0()); |
| 31 return null; | 30 return null; |
| 32 }"""), | 31 }"""), |
| 33 | 32 |
| 34 const TestEntry(""" | 33 const TestEntry(""" |
| 35 main(x) { | 34 main(x) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 48 const TestEntry(""" | 47 const TestEntry(""" |
| 49 main() { | 48 main() { |
| 50 var x = 122; | 49 var x = 122; |
| 51 var a = () => x; | 50 var a = () => x; |
| 52 x = x + 1; | 51 x = x + 1; |
| 53 print(a()); | 52 print(a()); |
| 54 } | 53 } |
| 55 """, | 54 """, |
| 56 r""" | 55 r""" |
| 57 function() { | 56 function() { |
| 58 var _box_0, a, x; | 57 var _box_0 = {}, a, x; |
| 59 _box_0 = {}; | |
| 60 _box_0._captured_x_0 = 122; | 58 _box_0._captured_x_0 = 122; |
| 61 a = new V.main_closure(_box_0); | 59 a = new V.main_closure(_box_0); |
| 62 x = _box_0._captured_x_0; | 60 x = _box_0._captured_x_0; |
| 63 _box_0._captured_x_0 = J.getInterceptor$ns(x).$add(x, 1); | 61 _box_0._captured_x_0 = J.getInterceptor$ns(x).$add(x, 1); |
| 64 P.print(a.call$0()); | 62 P.print(a.call$0()); |
| 65 return null; | 63 return null; |
| 66 }"""), | 64 }"""), |
| 67 | 65 |
| 68 const TestEntry(""" | 66 const TestEntry(""" |
| 69 main() { | 67 main() { |
| 70 var x = 122; | 68 var x = 122; |
| 71 var a = () { | 69 var a = () { |
| 72 var y = x; | 70 var y = x; |
| 73 return () => y; | 71 return () => y; |
| 74 }; | 72 }; |
| 75 x = x + 1; | 73 x = x + 1; |
| 76 print(a()()); | 74 print(a()()); |
| 77 } | 75 } |
| 78 """, | 76 """, |
| 79 r""" | 77 r""" |
| 80 function() { | 78 function() { |
| 81 var _box_0, a, x; | 79 var _box_0 = {}, a, x; |
| 82 _box_0 = {}; | |
| 83 _box_0._captured_x_0 = 122; | 80 _box_0._captured_x_0 = 122; |
| 84 a = new V.main_closure(_box_0); | 81 a = new V.main_closure(_box_0); |
| 85 x = _box_0._captured_x_0; | 82 x = _box_0._captured_x_0; |
| 86 _box_0._captured_x_0 = J.getInterceptor$ns(x).$add(x, 1); | 83 _box_0._captured_x_0 = J.getInterceptor$ns(x).$add(x, 1); |
| 87 P.print(a.call$0().call$0()); | 84 P.print(a.call$0().call$0()); |
| 88 return null; | 85 return null; |
| 89 }"""), | 86 }"""), |
| 90 | 87 |
| 91 const TestEntry(""" | 88 const TestEntry(""" |
| 92 main() { | 89 main() { |
| 93 var a; | 90 var a; |
| 94 for (var i=0; i<10; i++) { | 91 for (var i=0; i<10; i++) { |
| 95 a = () => i; | 92 a = () => i; |
| 96 } | 93 } |
| 97 print(a()); | 94 print(a()); |
| 98 } | 95 } |
| 99 """, | 96 """, |
| 100 r""" | 97 r""" |
| 101 function() { | 98 function() { |
| 102 var a, i; | 99 var a = null, i = 0; |
| 103 a = null; | |
| 104 i = 0; | |
| 105 while (P.identical(J.getInterceptor$n(i).$lt(i, 10), true)) { | 100 while (P.identical(J.getInterceptor$n(i).$lt(i, 10), true)) { |
| 106 a = new V.main_closure(i); | 101 a = new V.main_closure(i); |
| 107 i = J.getInterceptor$ns(i).$add(i, 1); | 102 i = J.getInterceptor$ns(i).$add(i, 1); |
| 108 } | 103 } |
| 109 P.print(a.call$0()); | 104 P.print(a.call$0()); |
| 110 return null; | 105 return null; |
| 111 }"""), | 106 }"""), |
| 112 | 107 |
| 113 const TestEntry.forMethod('function(A#b)', """ | 108 const TestEntry.forMethod('function(A#b)', """ |
| 114 class A { | 109 class A { |
| 115 a() => 1; | 110 a() => 1; |
| 116 b() => () => a(); | 111 b() => () => a(); |
| 117 } | 112 } |
| 118 main() { | 113 main() { |
| 119 print(new A().b()()); | 114 print(new A().b()()); |
| 120 } | 115 } |
| 121 """, | 116 """, |
| 122 r""" | 117 r""" |
| 123 function() { | 118 function() { |
| 124 return new V.A_b_closure(this); | 119 return new V.A_b_closure(this); |
| 125 }"""), | 120 }"""), |
| 126 ]; | 121 ]; |
| 127 | 122 |
| 128 void main() { | 123 void main() { |
| 129 runTests(tests); | 124 runTests(tests); |
| 130 } | 125 } |
| OLD | NEW |