| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 | 6 |
| 7 // Bind a method to a variable that can be invoked as a function | 7 // Bind a method to a variable that can be invoked as a function |
| 8 | 8 |
| 9 class A { | 9 class A { |
| 10 int a; | 10 int a; |
| 11 | 11 |
| 12 static var func; | 12 static var func; |
| 13 | 13 |
| 14 A(this.a) { } | 14 A(this.a) {} |
| 15 | 15 |
| 16 static foo() { return 4; } | 16 static foo() { |
| 17 return 4; |
| 18 } |
| 17 | 19 |
| 18 bar() { return a; } | 20 bar() { |
| 21 return a; |
| 22 } |
| 19 | 23 |
| 20 int baz() { return a; } | 24 int baz() { |
| 25 return a; |
| 26 } |
| 21 | 27 |
| 22 getThis() { return this.bar; } | 28 getThis() { |
| 29 return this.bar; |
| 30 } |
| 23 | 31 |
| 24 getNoThis() { return bar; } | 32 getNoThis() { |
| 33 return bar; |
| 34 } |
| 25 | 35 |
| 26 methodArgs(arg) { return arg + a; } | 36 methodArgs(arg) { |
| 37 return arg + a; |
| 38 } |
| 27 | 39 |
| 28 selfReference () { return selfReference; } | 40 selfReference() { |
| 41 return selfReference; |
| 42 } |
| 29 | 43 |
| 30 invokeBaz() { return (baz)(); } | 44 invokeBaz() { |
| 45 return (baz)(); |
| 46 } |
| 31 | 47 |
| 32 invokeBar(var obj) { return (obj.bar)(); } | 48 invokeBar(var obj) { |
| 49 return (obj.bar)(); |
| 50 } |
| 33 | 51 |
| 34 invokeThisBar() { return (this.bar)(); } | 52 invokeThisBar() { |
| 53 return (this.bar)(); |
| 54 } |
| 35 | 55 |
| 36 implicitStaticRef() { return foo; } | 56 implicitStaticRef() { |
| 57 return foo; |
| 58 } |
| 37 } | 59 } |
| 38 | 60 |
| 39 class B { | 61 class B { |
| 40 static foo() { return -1; } | 62 static foo() { |
| 63 return -1; |
| 64 } |
| 41 } | 65 } |
| 42 | 66 |
| 43 class C { | 67 class C { |
| 44 C() { } | 68 C() {} |
| 45 var f; | 69 var f; |
| 46 } | 70 } |
| 47 | 71 |
| 48 topLevel99() { | 72 topLevel99() { |
| 49 return 99; | 73 return 99; |
| 50 } | 74 } |
| 51 | 75 |
| 52 var topFunc; | 76 var topFunc; |
| 53 | 77 |
| 54 class D extends A { | 78 class D extends A { |
| 55 D(a): super(a) { } | 79 D(a) : super(a) {} |
| 56 getSuper() { return super.bar; } | 80 getSuper() { |
| 81 return super.bar; |
| 82 } |
| 57 } | 83 } |
| 58 | 84 |
| 59 class MethodBindingTest { | 85 class MethodBindingTest { |
| 60 static test() { | 86 static test() { |
| 61 | |
| 62 // Create closure from global | 87 // Create closure from global |
| 63 Expect.equals(99, topLevel99()); | 88 Expect.equals(99, topLevel99()); |
| 64 Function f99 = topLevel99; | 89 Function f99 = topLevel99; |
| 65 Expect.equals(99, f99()); | 90 Expect.equals(99, f99()); |
| 66 | 91 |
| 67 // Invoke closure through a global | 92 // Invoke closure through a global |
| 68 topFunc = f99; | 93 topFunc = f99; |
| 69 Expect.equals(99, topFunc()); | 94 Expect.equals(99, topFunc()); |
| 70 | 95 |
| 71 // Create closure from static method | 96 // Create closure from static method |
| 72 Function f4 = A.foo; | 97 Function f4 = A.foo; |
| 73 Expect.equals(4, f4()); | 98 Expect.equals(4, f4()); |
| 74 | 99 |
| 75 // Create closure from instance method | 100 // Create closure from instance method |
| 76 var o5 = new A(5); | 101 var o5 = new A(5); |
| 77 Function f5 = o5.bar; | 102 Function f5 = o5.bar; |
| 78 Expect.equals(5, f5()); | 103 Expect.equals(5, f5()); |
| 79 | 104 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 } | 165 } |
| 141 | 166 |
| 142 static testMain() { | 167 static testMain() { |
| 143 test(); | 168 test(); |
| 144 } | 169 } |
| 145 } | 170 } |
| 146 | 171 |
| 147 main() { | 172 main() { |
| 148 MethodBindingTest.testMain(); | 173 MethodBindingTest.testMain(); |
| 149 } | 174 } |
| OLD | NEW |