OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 import 'dart:_foreign_helper' show JS; | 6 import 'dart:_foreign_helper' show JS; |
7 import 'dart:_js_helper' show Native, Creates, setNativeSubclassDispatchRecord; | 7 import 'dart:_js_helper' show Native, Creates, setNativeSubclassDispatchRecord; |
8 import 'dart:_interceptors' show | 8 import 'dart:_interceptors' |
9 findInterceptorForType, findConstructorForNativeSubclassType; | 9 show findInterceptorForType, findConstructorForNativeSubclassType; |
10 | 10 |
11 // Test that subclasses of native classes can be initialized by calling the | 11 // Test that subclasses of native classes can be initialized by calling the |
12 // 'upgrade' constructor. | 12 // 'upgrade' constructor. |
13 | 13 |
14 var trace = []; | 14 var trace = []; |
15 | 15 |
16 var log; | 16 var log; |
17 | 17 |
18 @Native("A") | 18 @Native("A") |
19 class A { | 19 class A { |
20 final a1 = log(101); // Only initialized IF named constructor called. | 20 final a1 = log(101); // Only initialized IF named constructor called. |
21 final a2; // Initialized by native constructor. | 21 final a2; // Initialized by native constructor. |
22 final a3; // Initialized only by A.two. | 22 final a3; // Initialized only by A.two. |
23 var a4 = log(104); | 23 var a4 = log(104); |
24 | 24 |
25 A.one(); | 25 A.one(); |
26 | 26 |
27 A.two() : a3 = log(103) { | 27 A.two() : a3 = log(103) { |
28 log('body(A.two)'); | 28 log('body(A.two)'); |
29 log(a4 += increment); | 29 log(a4 += increment); |
30 } | 30 } |
31 | 31 |
32 A.three(x, this.a4) { | 32 A.three(x, this.a4) { |
33 log('body(A.three)'); | 33 log('body(A.three)'); |
34 log(a4 = '($a4, $x)'); | 34 log(a4 = '($a4, $x)'); |
35 } | 35 } |
36 | 36 |
37 get increment => 10; | 37 get increment => 10; |
38 } | 38 } |
39 | 39 |
40 class B extends A { | 40 class B extends A { |
41 final b1; | 41 final b1; |
42 final b2 = log(202); | 42 final b2 = log(202); |
43 var b3; | 43 var b3; |
44 | 44 |
45 B.one() : super.one(); | 45 B.one() : super.one(); |
46 | 46 |
47 B.two() : b1 = log(201), super.two(), b3 = log(203) { | 47 B.two() |
| 48 : b1 = log(201), |
| 49 super.two(), |
| 50 b3 = log(203) { |
48 log('body(B.two)'); | 51 log('body(B.two)'); |
49 } | 52 } |
50 | 53 |
51 B.three([x]) : super.three(205, x); | 54 B.three([x]) : super.three(205, x); |
52 | 55 |
53 get increment => 20; | 56 get increment => 20; |
54 } | 57 } |
55 | 58 |
56 | 59 makeB() native ; |
57 makeB() native; | |
58 | 60 |
59 @Creates('=Object') | 61 @Creates('=Object') |
60 getBPrototype() native; | 62 getBPrototype() native ; |
61 | 63 |
62 void setup() native r""" | 64 void setup() native r""" |
63 function B() { this.a2 = 102; } | 65 function B() { this.a2 = 102; } |
64 | 66 |
65 makeB = function(){return new B;}; | 67 makeB = function(){return new B;}; |
66 | 68 |
67 getBPrototype = function(){return B.prototype;}; | 69 getBPrototype = function(){return B.prototype;}; |
68 """; | 70 """; |
69 | 71 |
70 | |
71 test_one() { | 72 test_one() { |
72 trace = []; | 73 trace = []; |
73 var constructor = findConstructorForNativeSubclassType(B, 'one'); | 74 var constructor = findConstructorForNativeSubclassType(B, 'one'); |
74 Expect.isNotNull(constructor); | 75 Expect.isNotNull(constructor); |
75 Expect.isNull(findConstructorForNativeSubclassType(B, 'Missing')); | 76 Expect.isNull(findConstructorForNativeSubclassType(B, 'Missing')); |
76 | 77 |
77 var b = makeB(); | 78 var b = makeB(); |
78 Expect.isTrue(b is B); | 79 Expect.isTrue(b is B); |
79 // Call constructor to initialize native object. | 80 // Call constructor to initialize native object. |
80 var b2 = JS('', '#(#)', constructor, b); | 81 var b2 = JS('', '#(#)', constructor, b); |
(...skipping 23 matching lines...) Expand all Loading... |
104 Expect.isTrue(b is B); | 105 Expect.isTrue(b is B); |
105 | 106 |
106 Expect.equals(101, b.a1); | 107 Expect.equals(101, b.a1); |
107 Expect.equals(102, b.a2); | 108 Expect.equals(102, b.a2); |
108 Expect.equals(103, b.a3); | 109 Expect.equals(103, b.a3); |
109 Expect.equals(124, b.a4); | 110 Expect.equals(124, b.a4); |
110 Expect.equals(201, b.b1); | 111 Expect.equals(201, b.b1); |
111 Expect.equals(202, b.b2); | 112 Expect.equals(202, b.b2); |
112 Expect.equals(203, b.b3); | 113 Expect.equals(203, b.b3); |
113 | 114 |
114 Expect.equals( | 115 Expect.equals('[202, 201, 101, 104, 103, 203, body(A.two), 124, body(B.two)]', |
115 '[202, 201, 101, 104, 103, 203, body(A.two), 124, body(B.two)]', | |
116 '$trace'); | 116 '$trace'); |
117 } | 117 } |
118 | 118 |
119 test_three() { | 119 test_three() { |
120 trace = []; | 120 trace = []; |
121 var constructor = findConstructorForNativeSubclassType(B, 'three'); | 121 var constructor = findConstructorForNativeSubclassType(B, 'three'); |
122 Expect.isNotNull(constructor); | 122 Expect.isNotNull(constructor); |
123 | 123 |
124 var b = makeB(); | 124 var b = makeB(); |
125 Expect.isTrue(b is B); | 125 Expect.isTrue(b is B); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 return message; | 168 return message; |
169 }; | 169 }; |
170 | 170 |
171 setNativeSubclassDispatchRecord(getBPrototype(), findInterceptorForType(B)); | 171 setNativeSubclassDispatchRecord(getBPrototype(), findInterceptorForType(B)); |
172 | 172 |
173 test_one(); | 173 test_one(); |
174 test_two(); | 174 test_two(); |
175 test_three(); | 175 test_three(); |
176 test_new(); | 176 test_new(); |
177 } | 177 } |
OLD | NEW |