OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "dart:async"; | 5 import "dart:async"; |
6 import "dart:_js_helper"; | 6 import "dart:_js_helper"; |
7 import "package:async_helper/async_helper.dart"; | 7 import "package:async_helper/async_helper.dart"; |
8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
9 | 9 |
10 typedef void Callback0(); | 10 typedef void Callback0(); |
11 | 11 |
12 @Native("A") | 12 @Native("A") |
13 class A { | 13 class A { |
14 foo(Callback0 f) native; | 14 foo(Callback0 f) native ; |
15 } | 15 } |
16 | 16 |
17 makeA() native; | 17 makeA() native ; |
18 | 18 |
19 void setup() native r""" | 19 void setup() native r""" |
20 function A() {} | 20 function A() {} |
21 A.prototype.foo = function(f) { return f(); }; | 21 A.prototype.foo = function(f) { return f(); }; |
22 makeA = function() { return new A; }; | 22 makeA = function() { return new A; }; |
23 """; | 23 """; |
24 | 24 |
25 main() { | 25 main() { |
26 setup(); | 26 setup(); |
27 | 27 |
28 // Makes sure that we don't run the event-loop when we have a reentrant | 28 // Makes sure that we don't run the event-loop when we have a reentrant |
29 // call from JS to Dart code. | 29 // call from JS to Dart code. |
30 // We start by setting up a microtask that should only run after main has | 30 // We start by setting up a microtask that should only run after main has |
31 // finished. We then pass a closure into JavaScript. That closure is | 31 // finished. We then pass a closure into JavaScript. That closure is |
32 // immediately invoked. Dart2js had a bug, that it would start the event-loop | 32 // immediately invoked. Dart2js had a bug, that it would start the event-loop |
33 // at this moment (a JS->Dart transition), and execute the scheduled | 33 // at this moment (a JS->Dart transition), and execute the scheduled |
34 // microtask. | 34 // microtask. |
35 | 35 |
36 var events = []; | 36 var events = []; |
37 asyncStart(); | 37 asyncStart(); |
38 var a = makeA(); | 38 var a = makeA(); |
39 new Future.microtask(() { events.add("scheduleMicrotask"); }) | 39 new Future.microtask(() { |
40 .whenComplete(asyncEnd); | 40 events.add("scheduleMicrotask"); |
| 41 }).whenComplete(asyncEnd); |
41 | 42 |
42 Expect.equals(499, a.foo(() { | 43 Expect.equals(499, a.foo(() { |
43 events.add("closure to foo"); | 44 events.add("closure to foo"); |
44 return 499; | 45 return 499; |
45 })); | 46 })); |
46 | 47 |
47 events.add("after native call"); | 48 events.add("after native call"); |
48 Expect.listEquals(["closure to foo", "after native call"], events); | 49 Expect.listEquals(["closure to foo", "after native call"], events); |
49 } | 50 } |
OLD | NEW |