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