| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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:_js_helper"; | 5 import 'native_testing.dart'; |
| 6 import "package:expect/expect.dart"; | |
| 7 | 6 |
| 8 typedef void MyFunctionType(); | 7 typedef void MyFunctionType(); |
| 9 | 8 |
| 10 @Native("A") | 9 @Native("A") |
| 11 class A { | 10 class A { |
| 12 setClosure(MyFunctionType f) native ; | 11 setClosure(MyFunctionType f) native ; |
| 13 check(MyFunctionType f) native ; | 12 check(MyFunctionType f) native ; |
| 14 invoke() native ; | 13 invoke() native ; |
| 15 } | 14 } |
| 16 | 15 |
| 17 makeA() native ; | 16 makeA() native ; |
| 18 | 17 |
| 19 void setup() native """ | 18 void setup() native """ |
| 20 function A() {} | 19 function A() {} |
| 21 A.prototype.setClosure = function(f) { this.f = f; }; | 20 A.prototype.setClosure = function(f) { this.f = f; }; |
| 22 A.prototype.check = function(f) { return this.f === f; }; | 21 A.prototype.check = function(f) { return this.f === f; }; |
| 23 A.prototype.invoke = function() { return this.f(); }; | 22 A.prototype.invoke = function() { return this.f(); }; |
| 24 makeA = function(){return new A;}; | 23 makeA = function(){return new A;}; |
| 24 |
| 25 self.nativeConstructor(A); |
| 25 """; | 26 """; |
| 26 | 27 |
| 27 var staticClosure; | 28 var staticClosure; |
| 28 staticMethod() => 42; | 29 staticMethod() => 42; |
| 29 | 30 |
| 30 class B { | 31 class B { |
| 31 var instanceClosure; | 32 var instanceClosure; |
| 32 instanceMethod() => 43; | 33 instanceMethod() => 43; |
| 33 } | 34 } |
| 34 | 35 |
| 35 checkUntyped(a, closure) { | 36 checkUntyped(a, closure) { |
| 36 a.setClosure(closure); | 37 a.setClosure(closure); |
| 37 Expect.isTrue(a.check(closure)); | 38 Expect.isTrue(a.check(closure)); |
| 38 Expect.equals(closure(), a.invoke()); | 39 Expect.equals(closure(), a.invoke()); |
| 39 } | 40 } |
| 40 | 41 |
| 41 checkTyped(A a, MyFunctionType closure) { | 42 checkTyped(A a, MyFunctionType closure) { |
| 42 a.setClosure(closure); | 43 a.setClosure(closure); |
| 43 Expect.isTrue(a.check(closure)); | 44 Expect.isTrue(a.check(closure)); |
| 44 Expect.equals(closure(), a.invoke()); | 45 Expect.equals(closure(), a.invoke()); |
| 45 } | 46 } |
| 46 | 47 |
| 47 main() { | 48 main() { |
| 49 nativeTesting(); |
| 48 setup(); | 50 setup(); |
| 49 | 51 |
| 50 staticClosure = () => 44; | 52 staticClosure = () => 44; |
| 51 B b = new B(); | 53 B b = new B(); |
| 52 b.instanceClosure = () => 45; | 54 b.instanceClosure = () => 45; |
| 53 | 55 |
| 54 closureStatement() => 46; | 56 closureStatement() => 46; |
| 55 var closureExpression = () => 47; | 57 var closureExpression = () => 47; |
| 56 | 58 |
| 57 checkUntyped(makeA(), staticClosure); | 59 checkUntyped(makeA(), staticClosure); |
| 58 checkTyped(makeA(), staticClosure); | 60 checkTyped(makeA(), staticClosure); |
| 59 | 61 |
| 60 checkUntyped(makeA(), staticMethod); | 62 checkUntyped(makeA(), staticMethod); |
| 61 checkTyped(makeA(), staticMethod); | 63 checkTyped(makeA(), staticMethod); |
| 62 | 64 |
| 63 checkUntyped(makeA(), b.instanceClosure); | 65 checkUntyped(makeA(), b.instanceClosure); |
| 64 checkTyped(makeA(), b.instanceClosure); | 66 checkTyped(makeA(), b.instanceClosure); |
| 65 | 67 |
| 66 checkUntyped(makeA(), b.instanceMethod); | 68 checkUntyped(makeA(), b.instanceMethod); |
| 67 checkTyped(makeA(), b.instanceMethod); | 69 checkTyped(makeA(), b.instanceMethod); |
| 68 | 70 |
| 69 checkUntyped(makeA(), closureStatement); | 71 checkUntyped(makeA(), closureStatement); |
| 70 checkTyped(makeA(), closureStatement); | 72 checkTyped(makeA(), closureStatement); |
| 71 | 73 |
| 72 checkUntyped(makeA(), closureExpression); | 74 checkUntyped(makeA(), closureExpression); |
| 73 checkTyped(makeA(), closureExpression); | 75 checkTyped(makeA(), closureExpression); |
| 74 } | 76 } |
| OLD | NEW |