| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Tests of closures. | |
| 6 | |
| 7 library closures_test; | |
| 8 | |
| 9 import 'js_backend_cps_ir.dart'; | |
| 10 | |
| 11 const List<TestEntry> tests = const [ | |
| 12 const TestEntry(""" | |
| 13 main(x) { | |
| 14 a() { | |
| 15 return x; | |
| 16 } | |
| 17 x = x + '1'; | |
| 18 print(a()); | |
| 19 } | |
| 20 """, | |
| 21 r""" | |
| 22 function(x) { | |
| 23 P.print(J.$add$ns(x, "1")); | |
| 24 }"""), | |
| 25 | |
| 26 const TestEntry(""" | |
| 27 main(x) { | |
| 28 a() { | |
| 29 return x; | |
| 30 } | |
| 31 x = x + '1'; | |
| 32 print(a()); | |
| 33 return a; | |
| 34 } | |
| 35 """, | |
| 36 r""" | |
| 37 function(x) { | |
| 38 var _box_0 = {}, a = new V.main_a(_box_0); | |
| 39 _box_0.x = x; | |
| 40 _box_0.x = J.$add$ns(_box_0.x, "1"); | |
| 41 P.print(a.call$0()); | |
| 42 return a; | |
| 43 }"""), | |
| 44 | |
| 45 const TestEntry(""" | |
| 46 main(x) { | |
| 47 a() { | |
| 48 return x; | |
| 49 } | |
| 50 print(a()); | |
| 51 } | |
| 52 """, | |
| 53 r""" | |
| 54 function(x) { | |
| 55 P.print(x); | |
| 56 }"""), | |
| 57 | |
| 58 const TestEntry(""" | |
| 59 main(x) { | |
| 60 a() { | |
| 61 return x; | |
| 62 } | |
| 63 print(a()); | |
| 64 return a; | |
| 65 } | |
| 66 """, | |
| 67 r""" | |
| 68 function(x) { | |
| 69 var a = new V.main_a(x); | |
| 70 P.print(a.call$0()); | |
| 71 return a; | |
| 72 }"""), | |
| 73 | |
| 74 const TestEntry(""" | |
| 75 main() { | |
| 76 var x = 122; | |
| 77 var a = () => x; | |
| 78 x = x + 1; | |
| 79 print(a()); | |
| 80 } | |
| 81 """, | |
| 82 r""" | |
| 83 function() { | |
| 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 } | |
| 94 }"""), | |
| 95 | |
| 96 const TestEntry(""" | |
| 97 main() { | |
| 98 var x = 122; | |
| 99 var a = () => x; | |
| 100 x = x + 1; | |
| 101 print(a()); | |
| 102 return a; | |
| 103 } | |
| 104 """, | |
| 105 r""" | |
| 106 function() { | |
| 107 var _box_0 = {}, a = new V.main_closure(_box_0), v0; | |
| 108 _box_0.x = 122; | |
| 109 ++_box_0.x; | |
| 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 } | |
| 120 return a; | |
| 121 }"""), | |
| 122 | |
| 123 const TestEntry(""" | |
| 124 main() { | |
| 125 var x = 122; | |
| 126 var a = () { | |
| 127 var y = x; | |
| 128 return () => y; | |
| 129 }; | |
| 130 x = x + 1; | |
| 131 print(a()()); | |
| 132 } | |
| 133 """, | |
| 134 r""" | |
| 135 function() { | |
| 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 } | |
| 146 }"""), | |
| 147 | |
| 148 const TestEntry(""" | |
| 149 main() { | |
| 150 var x = 122; | |
| 151 var a = () { | |
| 152 var y = x; | |
| 153 return () => y; | |
| 154 }; | |
| 155 x = x + 1; | |
| 156 print(a()()); | |
| 157 return a; | |
| 158 } | |
| 159 """, | |
| 160 r""" | |
| 161 function() { | |
| 162 var _box_0 = {}, a = new V.main_closure(_box_0), v0; | |
| 163 _box_0.x = 122; | |
| 164 ++_box_0.x; | |
| 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 } | |
| 175 return a; | |
| 176 }"""), | |
| 177 | |
| 178 const TestEntry(""" | |
| 179 main() { | |
| 180 var a; | |
| 181 for (var i=0; i<10; i++) { | |
| 182 a = () => i; | |
| 183 } | |
| 184 print(a()); | |
| 185 } | |
| 186 """, | |
| 187 r""" | |
| 188 function() { | |
| 189 var a = null, i = 0, v0; | |
| 190 for (; i < 10; a = new V.main_closure(i), ++i) | |
| 191 ; | |
| 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 } | |
| 202 }"""), | |
| 203 | |
| 204 const TestEntry(""" | |
| 205 class A { | |
| 206 a() => 1; | |
| 207 b() => () => a(); | |
| 208 } | |
| 209 main() { | |
| 210 print(new A().b()()); | |
| 211 } | |
| 212 """, | |
| 213 r""" | |
| 214 function() { | |
| 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 } | |
| 225 }"""), | |
| 226 | |
| 227 const TestEntry(""" | |
| 228 staticMethod(x) { print(x); return x; } | |
| 229 main(x) { | |
| 230 var tearOff = staticMethod; | |
| 231 print(tearOff(123)); | |
| 232 } | |
| 233 """, | |
| 234 r""" | |
| 235 function(x) { | |
| 236 P.print(123); | |
| 237 P.print(123); | |
| 238 }"""), | |
| 239 | |
| 240 const TestEntry(""" | |
| 241 class Foo { | |
| 242 instanceMethod(x) => x; | |
| 243 } | |
| 244 main(x) { | |
| 245 var tearOff = new Foo().instanceMethod; | |
| 246 print(tearOff(123)); | |
| 247 } | |
| 248 """, | |
| 249 r""" | |
| 250 function(x) { | |
| 251 V.Foo$(); | |
| 252 P.print(123); | |
| 253 }"""), | |
| 254 | |
| 255 const TestEntry(""" | |
| 256 class Foo { | |
| 257 instanceMethod(x) => x; | |
| 258 } | |
| 259 main(x) { | |
| 260 var tearOff = new Foo().instanceMethod; | |
| 261 print(tearOff(123)); | |
| 262 print(tearOff(321)); | |
| 263 } | |
| 264 """, | |
| 265 r""" | |
| 266 function(x) { | |
| 267 V.Foo$(); | |
| 268 P.print(123); | |
| 269 P.print(321); | |
| 270 }"""), | |
| 271 | |
| 272 const TestEntry(""" | |
| 273 class Foo { | |
| 274 get getter { | |
| 275 print('getter'); | |
| 276 return (x) => x; | |
| 277 } | |
| 278 } | |
| 279 main(x) { | |
| 280 var notTearOff = new Foo().getter; | |
| 281 print(notTearOff(123)); | |
| 282 print(notTearOff(321)); | |
| 283 } | |
| 284 """, | |
| 285 r""" | |
| 286 function(x) { | |
| 287 var v0 = new V.Foo_getter_closure(); | |
| 288 V.Foo$(); | |
| 289 P.print("getter"); | |
| 290 P.print(v0.call$1(123)); | |
| 291 P.print(v0.call$1(321)); | |
| 292 }"""), | |
| 293 | |
| 294 const TestEntry(""" | |
| 295 class Foo { | |
| 296 get getter { | |
| 297 print('getter'); | |
| 298 return (x) => x; | |
| 299 } | |
| 300 } | |
| 301 main(x) { | |
| 302 var notTearOff = new Foo().getter; | |
| 303 print(notTearOff(123)); | |
| 304 } | |
| 305 """, | |
| 306 r""" | |
| 307 function(x) { | |
| 308 V.Foo$(); | |
| 309 P.print("getter"); | |
| 310 P.print(new V.Foo_getter_closure().call$1(123)); | |
| 311 }"""), | |
| 312 ]; | |
| 313 | |
| 314 void main() { | |
| 315 runTests(tests); | |
| 316 } | |
| OLD | NEW |