OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "package:expect/expect.dart"; |
| 6 import "package:compiler/src/js/js.dart"; |
| 7 import "package:compiler/src/js/rewrite_async.dart"; |
| 8 |
| 9 import "backend_dart/dart_printer_test.dart" show PrintDiagnosticListener; |
| 10 |
| 11 void testTransform(String source, String expected) { |
| 12 Fun fun = js(source); |
| 13 Fun rewritten = new AsyncRewriter().rewrite(fun); |
| 14 Printer printer = new Printer(new PrintDiagnosticListener(), null); |
| 15 printer.visit(rewritten); |
| 16 Expect.stringEquals(expected, printer.outBuffer.getText()); |
| 17 } |
| 18 |
| 19 main() { |
| 20 testTransform(""" |
| 21 function(c) async { |
| 22 try { |
| 23 var x = c ? await foo() : foo(); |
| 24 var y = {}; |
| 25 } catch (error) { |
| 26 try { |
| 27 x = c ? await fooError(error) : fooError(error); |
| 28 } catch (error) { |
| 29 y.x = foo(error); |
| 30 } finally { |
| 31 foo(x); |
| 32 } |
| 33 } |
| 34 } |
| 35 """, """ |
| 36 function(c) { |
| 37 var __goto = 0, __handler = null, x, y, __error, __error1; |
| 38 function __helper(__result) { |
| 39 while (true) |
| 40 try { |
| 41 switch (__goto) { |
| 42 case 0: |
| 43 // Function start |
| 44 __handler = 1; |
| 45 __goto = c ? 3 : 5; |
| 46 break; |
| 47 case 3: |
| 48 // then |
| 49 __goto = 6; |
| 50 return foo().then(__helper, function(__error) { |
| 51 __goto = 1; |
| 52 __helper(__error); |
| 53 }); |
| 54 case 6: |
| 55 // Returning from await. |
| 56 __result = __result; |
| 57 // goto join |
| 58 __goto = 4; |
| 59 break; |
| 60 case 5: |
| 61 // else |
| 62 __result = foo(); |
| 63 case 4: |
| 64 // join |
| 65 x = __result; |
| 66 y = {}; |
| 67 __handler = null; |
| 68 // goto finally |
| 69 __goto = 2; |
| 70 break; |
| 71 case 1: |
| 72 // catch |
| 73 __error = __result; |
| 74 __handler = 7; |
| 75 __goto = c ? 9 : 11; |
| 76 break; |
| 77 case 9: |
| 78 // then |
| 79 __goto = 12; |
| 80 return fooError(__error).then(__helper, function(__error) { |
| 81 __goto = 7; |
| 82 __helper(__error); |
| 83 }); |
| 84 case 12: |
| 85 // Returning from await. |
| 86 __result = __result; |
| 87 // goto join |
| 88 __goto = 10; |
| 89 break; |
| 90 case 11: |
| 91 // else |
| 92 __result = fooError(__error); |
| 93 case 10: |
| 94 // join |
| 95 x = __result; |
| 96 __handler = null; |
| 97 // goto finally |
| 98 __goto = 8; |
| 99 break; |
| 100 case 7: |
| 101 // catch |
| 102 __error1 = __result; |
| 103 y.x = foo(__error1); |
| 104 case 8: |
| 105 // finally |
| 106 foo(x); |
| 107 case 2: |
| 108 // finally |
| 109 } |
| 110 } catch (__error) { |
| 111 if (__handler === null) |
| 112 throw __error; |
| 113 __result = __error; |
| 114 __goto = __handler; |
| 115 } |
| 116 |
| 117 } |
| 118 return new Future.microtask(__helper); |
| 119 }"""); |
| 120 testTransform(""" |
| 121 function(x, y) async { |
| 122 print(await(foo(x))); |
| 123 (await print)(foo(x)); |
| 124 print(foo(await x)); |
| 125 await (print(foo(await x))); |
| 126 print(foo(x, await y, z)); |
| 127 } |
| 128 """, """ |
| 129 function(x, y) { |
| 130 var __goto = 0, __handler = null, __temp1, __temp2, __temp3; |
| 131 function __helper(__result) { |
| 132 while (true) |
| 133 switch (__goto) { |
| 134 case 0: |
| 135 // Function start |
| 136 __temp1 = print; |
| 137 __goto = 1; |
| 138 return foo(x).then(__helper, function(__error) { |
| 139 __goto = null; |
| 140 __helper(__error); |
| 141 }); |
| 142 case 1: |
| 143 // Returning from await. |
| 144 __temp1(__result); |
| 145 __goto = 2; |
| 146 return print.then(__helper, function(__error) { |
| 147 __goto = null; |
| 148 __helper(__error); |
| 149 }); |
| 150 case 2: |
| 151 // Returning from await. |
| 152 __result(foo(x)); |
| 153 __temp1 = print; |
| 154 __temp2 = foo; |
| 155 __goto = 3; |
| 156 return x.then(__helper, function(__error) { |
| 157 __goto = null; |
| 158 __helper(__error); |
| 159 }); |
| 160 case 3: |
| 161 // Returning from await. |
| 162 __temp1(__temp2(__result)); |
| 163 __temp1 = print; |
| 164 __temp2 = foo; |
| 165 __goto = 5; |
| 166 return x.then(__helper, function(__error) { |
| 167 __goto = null; |
| 168 __helper(__error); |
| 169 }); |
| 170 case 5: |
| 171 // Returning from await. |
| 172 __goto = 4; |
| 173 return __temp1(__temp2(__result)).then(__helper, function(__error) { |
| 174 __goto = null; |
| 175 __helper(__error); |
| 176 }); |
| 177 case 4: |
| 178 // Returning from await. |
| 179 __result; |
| 180 __temp1 = print; |
| 181 __temp2 = foo; |
| 182 __temp3 = x; |
| 183 __goto = 6; |
| 184 return y.then(__helper, function(__error) { |
| 185 __goto = null; |
| 186 __helper(__error); |
| 187 }); |
| 188 case 6: |
| 189 // Returning from await. |
| 190 __temp1(__temp2(__temp3, __result, z)); |
| 191 } |
| 192 } |
| 193 return new Future.microtask(__helper); |
| 194 }"""); |
| 195 testTransform(""" |
| 196 function(x, y) async { |
| 197 while (await(foo())) { |
| 198 lab: { |
| 199 switch(y) { |
| 200 case 0: |
| 201 foo(); |
| 202 case 1: |
| 203 print(await foo1(x)); |
| 204 return y; |
| 205 case await bar(): |
| 206 print(await foobar(x)); |
| 207 return y; |
| 208 case x: |
| 209 if (a) { |
| 210 throw new Error(); |
| 211 } else { |
| 212 continue; |
| 213 } |
| 214 default: |
| 215 break lab; |
| 216 } |
| 217 foo(); |
| 218 } |
| 219 } |
| 220 }""", """ |
| 221 function(x, y) { |
| 222 var __goto = 0, __handler = null, __temp1; |
| 223 function __helper(__result) { |
| 224 while (true) |
| 225 switch (__goto) { |
| 226 case 0: |
| 227 // Function start |
| 228 case 1: |
| 229 // while condition |
| 230 __goto = 4; |
| 231 return foo().then(__helper, function(__error) { |
| 232 __goto = null; |
| 233 __helper(__error); |
| 234 }); |
| 235 case 4: |
| 236 // Returning from await. |
| 237 __goto = __result ? 2 : 3; |
| 238 break; |
| 239 case 2: |
| 240 // while body |
| 241 case 6: |
| 242 // continue lab |
| 243 case 7: |
| 244 // switch |
| 245 __temp1 = y; |
| 246 if (__temp1 === 0) { |
| 247 // goto case |
| 248 __goto = 9; |
| 249 break; |
| 250 } |
| 251 if (__temp1 === 1) { |
| 252 // goto case |
| 253 __goto = 10; |
| 254 break; |
| 255 } |
| 256 __goto = 12; |
| 257 return bar().then(__helper, function(__error) { |
| 258 __goto = null; |
| 259 __helper(__error); |
| 260 }); |
| 261 case 12: |
| 262 // Returning from await. |
| 263 if (__temp1 === __result) { |
| 264 // goto case |
| 265 __goto = 11; |
| 266 break; |
| 267 } |
| 268 if (__temp1 === x) { |
| 269 // goto case |
| 270 __goto = 13; |
| 271 break; |
| 272 } |
| 273 // goto default |
| 274 __goto = 14; |
| 275 break; |
| 276 case 9: |
| 277 // case |
| 278 foo(); |
| 279 case 10: |
| 280 // case |
| 281 __temp1 = print; |
| 282 __goto = 15; |
| 283 return foo1(x).then(__helper, function(__error) { |
| 284 __goto = null; |
| 285 __helper(__error); |
| 286 }); |
| 287 case 15: |
| 288 // Returning from await. |
| 289 __temp1(__result); |
| 290 return y; |
| 291 case 11: |
| 292 // case |
| 293 __temp1 = print; |
| 294 __goto = 16; |
| 295 return foobar(x).then(__helper, function(__error) { |
| 296 __goto = null; |
| 297 __helper(__error); |
| 298 }); |
| 299 case 16: |
| 300 // Returning from await. |
| 301 __temp1(__result); |
| 302 return y; |
| 303 case 13: |
| 304 // case |
| 305 if (a) { |
| 306 throw new Error(); |
| 307 } else { |
| 308 // goto while condition |
| 309 __goto = 1; |
| 310 break; |
| 311 } |
| 312 case 14: |
| 313 // default |
| 314 // goto break lab |
| 315 __goto = 5; |
| 316 break; |
| 317 case 8: |
| 318 // after switch |
| 319 foo(); |
| 320 case 5: |
| 321 // break lab |
| 322 // goto while condition |
| 323 __goto = 1; |
| 324 break; |
| 325 case 3: |
| 326 // after while |
| 327 } |
| 328 } |
| 329 return new Future.microtask(__helper); |
| 330 }"""); |
| 331 } |
OLD | NEW |