OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Bind a method to a variable that can be invoked as a function | |
6 | |
7 class A { | |
8 int a; | |
9 | |
10 static var func; | |
11 | |
12 A(this.a) { } | |
13 | |
14 static foo() { return 4; } | |
15 | |
16 bar() { return a; } | |
17 | |
18 int baz() { return a; } | |
19 | |
20 getThis() { return this.bar; } | |
21 | |
22 getNoThis() { return bar; } | |
23 | |
24 methodArgs(arg) { return arg + a; } | |
25 | |
26 selfReference () { return selfReference; } | |
27 | |
28 invokeBaz() { return (baz)(); } | |
29 | |
30 invokeBar(var obj) { return (obj.bar)(); } | |
31 | |
32 invokeThisBar() { return (this.bar)(); } | |
33 | |
34 implicitStaticRef() { return foo; } | |
35 } | |
36 | |
37 class B { | |
38 static foo() { return -1; } | |
39 } | |
40 | |
41 class C { | |
42 C() { } | |
43 var f; | |
44 } | |
45 | |
46 topLevel99() { | |
47 return 99; | |
48 } | |
49 | |
50 var topFunc; | |
51 | |
52 class D extends A { | |
53 D(a): super(a) { } | |
54 getSuper() { return super.bar; } | |
55 } | |
56 | |
57 class MethodBindingTest { | |
58 static test() { | |
59 | |
60 // Create closure from global | |
61 Expect.equals(99, topLevel99()); | |
62 Function f99 = topLevel99; | |
63 Expect.equals(99, f99()); | |
64 | |
65 // Invoke closure through a global | |
66 topFunc = f99; | |
67 Expect.equals(99, topFunc()); | |
68 | |
69 // Create closure from static method | |
70 Function f4 = A.foo; | |
71 Expect.equals(4, f4()); | |
72 | |
73 // Create closure from instance method | |
74 var o5 = new A(5); | |
75 Function f5 = o5.bar; | |
76 Expect.equals(5, f5()); | |
77 | |
78 // Assign closure to field and invoke it | |
79 var c = new C(); | |
80 c.f = () => "success"; | |
81 Expect.equals("success", c.f()); | |
82 | |
83 // referencing instance method with explicit 'this' qualiier | |
84 var o6 = new A(6); | |
85 var f6 = o6.getThis(); | |
86 Expect.equals(6, f6()); | |
87 | |
88 // referencing an instance method with no qualifier | |
89 var o7 = new A(7); | |
90 var f7 = o7.getNoThis(); | |
91 Expect.equals(7, f7()); | |
92 | |
93 // bind a method that takes arguments | |
94 var o8 = new A(8); | |
95 Function f8 = o8.methodArgs; | |
96 Expect.equals(9, f8(1)); | |
97 | |
98 // Self referential method | |
99 var o9 = new A(9); | |
100 Function f9 = o9.selfReference; | |
101 | |
102 // invoking a known method as if it were a bound closure... | |
103 var o10 = new A(10); | |
104 Expect.equals(10, o10.invokeBaz()); | |
105 | |
106 // invoking a known method as if it were a bound closure... | |
107 var o11 = new A(11); | |
108 Expect.equals(10, o11.invokeBar(o10)); | |
109 | |
110 // invoking a known method as if it were a bound closure... | |
111 var o12 = new A(12); | |
112 Expect.equals(12, o12.invokeThisBar()); | |
113 | |
114 // bind to a static variable with no explicit class qualifier | |
115 var o13 = new A(13); | |
116 Function f13 = o13.implicitStaticRef(); | |
117 Expect.equals(4, f13()); | |
118 | |
119 var o14 = new D(14); | |
120 Function f14 = o14.getSuper(); | |
121 Expect.equals(14, f14()); | |
122 | |
123 // Assign static field to a function and invoke it. | |
124 A.func = A.foo; | |
125 Expect.equals(4, A.func()); | |
126 | |
127 // bind a function that is possibly native in Javascript. | |
128 String o15 = 'hithere'; | |
129 var f15 = o15.substring; | |
130 Expect.equals('i', f15(1, 2)); | |
131 | |
132 var o16 = 'hithere'; | |
133 var f16 = o16.substring; | |
134 Expect.equals('i', f16(1, 2)); | |
135 | |
136 var f17 = 'hithere'.substring; | |
137 Expect.equals('i', f17(1, 2)); | |
138 } | |
139 | |
140 static testMain() { | |
141 test(); | |
142 } | |
143 } | |
144 | |
145 main() { | |
146 MethodBindingTest.testMain(); | |
147 } | |
OLD | NEW |