| 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 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 const TestEntry(""" | 74 const TestEntry(""" |
| 75 main() { | 75 main() { |
| 76 var x = 122; | 76 var x = 122; |
| 77 var a = () => x; | 77 var a = () => x; |
| 78 x = x + 1; | 78 x = x + 1; |
| 79 print(a()); | 79 print(a()); |
| 80 } | 80 } |
| 81 """, | 81 """, |
| 82 r""" | 82 r""" |
| 83 function() { | 83 function() { |
| 84 P.print(122 + 1); | 84 var v0 = H.S(122 + 1); |
| 85 if (typeof dartPrint == "function") |
| 86 dartPrint(v0); |
| 87 else if (typeof console == "object" && typeof console.log != "undefined") |
| 88 console.log(v0); |
| 89 else if (!(typeof window == "object")) { |
| 90 if (!(typeof print == "function")) |
| 91 throw "Unable to print message: " + String(v0); |
| 92 print(v0); |
| 93 } |
| 85 }"""), | 94 }"""), |
| 86 | 95 |
| 87 const TestEntry(""" | 96 const TestEntry(""" |
| 88 main() { | 97 main() { |
| 89 var x = 122; | 98 var x = 122; |
| 90 var a = () => x; | 99 var a = () => x; |
| 91 x = x + 1; | 100 x = x + 1; |
| 92 print(a()); | 101 print(a()); |
| 93 return a; | 102 return a; |
| 94 } | 103 } |
| 95 """, | 104 """, |
| 96 r""" | 105 r""" |
| 97 function() { | 106 function() { |
| 98 var _box_0 = {}, a = new V.main_closure(_box_0); | 107 var _box_0 = {}, a = new V.main_closure(_box_0), v0; |
| 99 _box_0.x = 122; | 108 _box_0.x = 122; |
| 100 _box_0.x = _box_0.x + 1; | 109 _box_0.x = _box_0.x + 1; |
| 101 P.print(a.call$0()); | 110 v0 = H.S(a.call$0()); |
| 111 if (typeof dartPrint == "function") |
| 112 dartPrint(v0); |
| 113 else if (typeof console == "object" && typeof console.log != "undefined") |
| 114 console.log(v0); |
| 115 else if (!(typeof window == "object")) { |
| 116 if (!(typeof print == "function")) |
| 117 throw "Unable to print message: " + String(v0); |
| 118 print(v0); |
| 119 } |
| 102 return a; | 120 return a; |
| 103 }"""), | 121 }"""), |
| 104 | 122 |
| 105 const TestEntry(""" | 123 const TestEntry(""" |
| 106 main() { | 124 main() { |
| 107 var x = 122; | 125 var x = 122; |
| 108 var a = () { | 126 var a = () { |
| 109 var y = x; | 127 var y = x; |
| 110 return () => y; | 128 return () => y; |
| 111 }; | 129 }; |
| 112 x = x + 1; | 130 x = x + 1; |
| 113 print(a()()); | 131 print(a()()); |
| 114 } | 132 } |
| 115 """, | 133 """, |
| 116 r""" | 134 r""" |
| 117 function() { | 135 function() { |
| 118 P.print(122 + 1); | 136 var v0 = H.S(122 + 1); |
| 137 if (typeof dartPrint == "function") |
| 138 dartPrint(v0); |
| 139 else if (typeof console == "object" && typeof console.log != "undefined") |
| 140 console.log(v0); |
| 141 else if (!(typeof window == "object")) { |
| 142 if (!(typeof print == "function")) |
| 143 throw "Unable to print message: " + String(v0); |
| 144 print(v0); |
| 145 } |
| 119 }"""), | 146 }"""), |
| 120 | 147 |
| 121 const TestEntry(""" | 148 const TestEntry(""" |
| 122 main() { | 149 main() { |
| 123 var x = 122; | 150 var x = 122; |
| 124 var a = () { | 151 var a = () { |
| 125 var y = x; | 152 var y = x; |
| 126 return () => y; | 153 return () => y; |
| 127 }; | 154 }; |
| 128 x = x + 1; | 155 x = x + 1; |
| 129 print(a()()); | 156 print(a()()); |
| 130 return a; | 157 return a; |
| 131 } | 158 } |
| 132 """, | 159 """, |
| 133 r""" | 160 r""" |
| 134 function() { | 161 function() { |
| 135 var _box_0 = {}, a = new V.main_closure(_box_0); | 162 var _box_0 = {}, a = new V.main_closure(_box_0), v0; |
| 136 _box_0.x = 122; | 163 _box_0.x = 122; |
| 137 _box_0.x = _box_0.x + 1; | 164 _box_0.x = _box_0.x + 1; |
| 138 P.print(a.call$0().call$0()); | 165 v0 = H.S(a.call$0().call$0()); |
| 166 if (typeof dartPrint == "function") |
| 167 dartPrint(v0); |
| 168 else if (typeof console == "object" && typeof console.log != "undefined") |
| 169 console.log(v0); |
| 170 else if (!(typeof window == "object")) { |
| 171 if (!(typeof print == "function")) |
| 172 throw "Unable to print message: " + String(v0); |
| 173 print(v0); |
| 174 } |
| 139 return a; | 175 return a; |
| 140 }"""), | 176 }"""), |
| 141 | 177 |
| 142 const TestEntry(""" | 178 const TestEntry(""" |
| 143 main() { | 179 main() { |
| 144 var a; | 180 var a; |
| 145 for (var i=0; i<10; i++) { | 181 for (var i=0; i<10; i++) { |
| 146 a = () => i; | 182 a = () => i; |
| 147 } | 183 } |
| 148 print(a()); | 184 print(a()); |
| 149 } | 185 } |
| 150 """, | 186 """, |
| 151 r""" | 187 r""" |
| 152 function() { | 188 function() { |
| 153 var a = null, i = 0; | 189 var a = null, i = 0, v0; |
| 154 for (; i < 10; a = new V.main_closure(i), i = i + 1) | 190 for (; i < 10; a = new V.main_closure(i), i = i + 1) |
| 155 ; | 191 ; |
| 156 P.print(a.call$0()); | 192 v0 = H.S(a.call$0()); |
| 193 if (typeof dartPrint == "function") |
| 194 dartPrint(v0); |
| 195 else if (typeof console == "object" && typeof console.log != "undefined") |
| 196 console.log(v0); |
| 197 else if (!(typeof window == "object")) { |
| 198 if (!(typeof print == "function")) |
| 199 throw "Unable to print message: " + String(v0); |
| 200 print(v0); |
| 201 } |
| 157 }"""), | 202 }"""), |
| 158 | 203 |
| 159 const TestEntry.forMethod('function(A#b)', """ | 204 const TestEntry(""" |
| 160 class A { | 205 class A { |
| 161 a() => 1; | 206 a() => 1; |
| 162 b() => () => a(); | 207 b() => () => a(); |
| 163 } | 208 } |
| 164 main() { | 209 main() { |
| 165 print(new A().b()()); | 210 print(new A().b()()); |
| 166 } | 211 } |
| 167 """, | 212 """, |
| 168 r""" | 213 r""" |
| 169 function() { | 214 function() { |
| 170 return new V.A_b_closure(this); | 215 var v0 = H.S(new V.A_b_closure(V.A$()).call$0()); |
| 216 if (typeof dartPrint == "function") |
| 217 dartPrint(v0); |
| 218 else if (typeof console == "object" && typeof console.log != "undefined") |
| 219 console.log(v0); |
| 220 else if (!(typeof window == "object")) { |
| 221 if (!(typeof print == "function")) |
| 222 throw "Unable to print message: " + String(v0); |
| 223 print(v0); |
| 224 } |
| 171 }"""), | 225 }"""), |
| 172 | 226 |
| 173 const TestEntry(""" | 227 const TestEntry(""" |
| 174 staticMethod(x) { print(x); return x; } | 228 staticMethod(x) { print(x); return x; } |
| 175 main(x) { | 229 main(x) { |
| 176 var tearOff = staticMethod; | 230 var tearOff = staticMethod; |
| 177 print(tearOff(123)); | 231 print(tearOff(123)); |
| 178 } | 232 } |
| 179 """, | 233 """, |
| 180 r""" | 234 r""" |
| 181 function(x) { | 235 function(x) { |
| 182 P.print(V.staticMethod(123)); | 236 P.print(123); |
| 237 P.print(123); |
| 183 }"""), | 238 }"""), |
| 184 | 239 |
| 185 const TestEntry(""" | 240 const TestEntry(""" |
| 186 class Foo { | 241 class Foo { |
| 187 instanceMethod(x) => x; | 242 instanceMethod(x) => x; |
| 188 } | 243 } |
| 189 main(x) { | 244 main(x) { |
| 190 var tearOff = new Foo().instanceMethod; | 245 var tearOff = new Foo().instanceMethod; |
| 191 print(tearOff(123)); | 246 print(tearOff(123)); |
| 192 } | 247 } |
| 193 """, | 248 """, |
| 194 r""" | 249 r""" |
| 195 function(x) { | 250 function(x) { |
| 196 P.print(V.Foo$().instanceMethod$1(123)); | 251 V.Foo$(); |
| 252 P.print(123); |
| 197 }"""), | 253 }"""), |
| 198 | 254 |
| 199 const TestEntry(""" | 255 const TestEntry(""" |
| 200 class Foo { | 256 class Foo { |
| 201 instanceMethod(x) => x; | 257 instanceMethod(x) => x; |
| 202 } | 258 } |
| 203 main(x) { | 259 main(x) { |
| 204 var tearOff = new Foo().instanceMethod; | 260 var tearOff = new Foo().instanceMethod; |
| 205 print(tearOff(123)); | 261 print(tearOff(123)); |
| 206 print(tearOff(321)); | 262 print(tearOff(321)); |
| 207 } | 263 } |
| 208 """, | 264 """, |
| 209 r""" | 265 r""" |
| 210 function(x) { | 266 function(x) { |
| 211 var v0 = V.Foo$(); | 267 V.Foo$(); |
| 212 P.print(v0.instanceMethod$1(123)); | 268 P.print(123); |
| 213 P.print(v0.instanceMethod$1(321)); | 269 P.print(321); |
| 214 }"""), | 270 }"""), |
| 215 | 271 |
| 216 const TestEntry(""" | 272 const TestEntry(""" |
| 217 class Foo { | 273 class Foo { |
| 218 get getter { | 274 get getter { |
| 219 print('getter'); | 275 print('getter'); |
| 220 return (x) => x; | 276 return (x) => x; |
| 221 } | 277 } |
| 222 } | 278 } |
| 223 main(x) { | 279 main(x) { |
| 224 var notTearOff = new Foo().getter; | 280 var notTearOff = new Foo().getter; |
| 225 print(notTearOff(123)); | 281 print(notTearOff(123)); |
| 226 print(notTearOff(321)); | 282 print(notTearOff(321)); |
| 227 } | 283 } |
| 228 """, | 284 """, |
| 229 r""" | 285 r""" |
| 230 function(x) { | 286 function(x) { |
| 231 var notTearOff = V.Foo$().get$getter(); | 287 var v0 = new V.Foo_getter_closure(); |
| 232 P.print(notTearOff.call$1(123)); | 288 V.Foo$(); |
| 233 P.print(notTearOff.call$1(321)); | 289 P.print("getter"); |
| 290 P.print(v0.call$1(123)); |
| 291 P.print(v0.call$1(321)); |
| 234 }"""), | 292 }"""), |
| 235 | 293 |
| 236 const TestEntry(""" | 294 const TestEntry(""" |
| 237 class Foo { | 295 class Foo { |
| 238 get getter { | 296 get getter { |
| 239 print('getter'); | 297 print('getter'); |
| 240 return (x) => x; | 298 return (x) => x; |
| 241 } | 299 } |
| 242 } | 300 } |
| 243 main(x) { | 301 main(x) { |
| 244 var notTearOff = new Foo().getter; | 302 var notTearOff = new Foo().getter; |
| 245 print(notTearOff(123)); | 303 print(notTearOff(123)); |
| 246 } | 304 } |
| 247 """, | 305 """, |
| 248 r""" | 306 r""" |
| 249 function(x) { | 307 function(x) { |
| 250 P.print(V.Foo$().get$getter().call$1(123)); | 308 V.Foo$(); |
| 309 P.print("getter"); |
| 310 P.print(new V.Foo_getter_closure().call$1(123)); |
| 251 }"""), | 311 }"""), |
| 252 ]; | 312 ]; |
| 253 | 313 |
| 254 void main() { | 314 void main() { |
| 255 runTests(tests); | 315 runTests(tests); |
| 256 } | 316 } |
| OLD | NEW |