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 part of dart2js.mirrors; | 5 part of dart2js.mirrors; |
6 | 6 |
7 abstract class ObjectMirrorMixin implements ObjectMirror { | 7 abstract class ObjectMirrorMixin implements ObjectMirror { |
8 InstanceMirror getField(Symbol fieldName) { | 8 InstanceMirror getField(Symbol fieldName) { |
9 throw new UnsupportedError('ObjectMirror.getField unsupported.'); | 9 throw new UnsupportedError('ObjectMirror.getField unsupported.'); |
10 } | 10 } |
11 | 11 |
12 InstanceMirror setField(Symbol fieldName, Object value) { | 12 InstanceMirror setField(Symbol fieldName, Object value) { |
13 throw new UnsupportedError('ObjectMirror.setField unsupported.'); | 13 throw new UnsupportedError('ObjectMirror.setField unsupported.'); |
14 } | 14 } |
15 | 15 |
16 InstanceMirror invoke(Symbol memberName, | 16 InstanceMirror invoke(Symbol memberName, List positionalArguments, |
17 List positionalArguments, | 17 [Map<Symbol, dynamic> namedArguments]) { |
18 [Map<Symbol, dynamic> namedArguments]) { | |
19 throw new UnsupportedError('ObjectMirror.invoke unsupported.'); | 18 throw new UnsupportedError('ObjectMirror.invoke unsupported.'); |
20 } | 19 } |
21 | 20 |
22 delegate(Invocation invocation) { | 21 delegate(Invocation invocation) { |
23 throw new UnsupportedError('ObjectMirror.delegate unsupported'); | 22 throw new UnsupportedError('ObjectMirror.delegate unsupported'); |
24 } | 23 } |
25 } | 24 } |
26 | 25 |
27 abstract class InstanceMirrorMixin implements InstanceMirror { | 26 abstract class InstanceMirrorMixin implements InstanceMirror { |
28 bool get hasReflectee => false; | 27 bool get hasReflectee => false; |
29 | 28 |
30 get reflectee { | 29 get reflectee { |
31 throw new UnsupportedError('InstanceMirror.reflectee unsupported.'); | 30 throw new UnsupportedError('InstanceMirror.reflectee unsupported.'); |
32 } | 31 } |
33 } | 32 } |
34 | 33 |
35 InstanceMirror _convertConstantToInstanceMirror( | 34 InstanceMirror _convertConstantToInstanceMirror( |
36 Dart2JsMirrorSystem mirrorSystem, | 35 Dart2JsMirrorSystem mirrorSystem, |
37 ConstantExpression constant, | 36 ConstantExpression constant, |
38 ConstantValue value) { | 37 ConstantValue value) { |
39 | |
40 if (value.isBool) { | 38 if (value.isBool) { |
41 return new Dart2JsBoolConstantMirror(mirrorSystem, constant, value); | 39 return new Dart2JsBoolConstantMirror(mirrorSystem, constant, value); |
42 } else if (value.isNum) { | 40 } else if (value.isNum) { |
43 return new Dart2JsNumConstantMirror(mirrorSystem, constant, value); | 41 return new Dart2JsNumConstantMirror(mirrorSystem, constant, value); |
44 } else if (value.isString) { | 42 } else if (value.isString) { |
45 return new Dart2JsStringConstantMirror(mirrorSystem, constant, value); | 43 return new Dart2JsStringConstantMirror(mirrorSystem, constant, value); |
46 } else if (value.isList) { | 44 } else if (value.isList) { |
47 return new Dart2JsListConstantMirror(mirrorSystem, constant, value); | 45 return new Dart2JsListConstantMirror(mirrorSystem, constant, value); |
48 } else if (value.isMap) { | 46 } else if (value.isMap) { |
49 return new Dart2JsMapConstantMirror(mirrorSystem, constant, value); | 47 return new Dart2JsMapConstantMirror(mirrorSystem, constant, value); |
50 } else if (value.isType) { | 48 } else if (value.isType) { |
51 return new Dart2JsTypeConstantMirror(mirrorSystem, constant, value); | 49 return new Dart2JsTypeConstantMirror(mirrorSystem, constant, value); |
52 } else if (value.isFunction) { | 50 } else if (value.isFunction) { |
53 return new Dart2JsConstantMirror(mirrorSystem, constant, value); | 51 return new Dart2JsConstantMirror(mirrorSystem, constant, value); |
54 } else if (value.isNull) { | 52 } else if (value.isNull) { |
55 return new Dart2JsNullConstantMirror(mirrorSystem, constant, value); | 53 return new Dart2JsNullConstantMirror(mirrorSystem, constant, value); |
56 } else if (value.isConstructedObject) { | 54 } else if (value.isConstructedObject) { |
57 return new Dart2JsConstructedConstantMirror(mirrorSystem, constant, value); | 55 return new Dart2JsConstructedConstantMirror(mirrorSystem, constant, value); |
58 } | 56 } |
59 mirrorSystem.compiler.reporter.internalError(NO_LOCATION_SPANNABLE, | 57 mirrorSystem.compiler.reporter |
60 "Unexpected constant value $value"); | 58 .internalError(NO_LOCATION_SPANNABLE, "Unexpected constant value $value"); |
61 return null; | 59 return null; |
62 } | 60 } |
63 | 61 |
64 | |
65 //////////////////////////////////////////////////////////////////////////////// | 62 //////////////////////////////////////////////////////////////////////////////// |
66 // Mirrors on constant values used for metadata. | 63 // Mirrors on constant values used for metadata. |
67 //////////////////////////////////////////////////////////////////////////////// | 64 //////////////////////////////////////////////////////////////////////////////// |
68 | 65 |
69 class Dart2JsConstantMirror extends Object | 66 class Dart2JsConstantMirror extends Object |
70 with ObjectMirrorMixin, InstanceMirrorMixin { | 67 with ObjectMirrorMixin, InstanceMirrorMixin { |
71 final Dart2JsMirrorSystem mirrorSystem; | 68 final Dart2JsMirrorSystem mirrorSystem; |
72 final ConstantExpression _constant; | 69 final ConstantExpression _constant; |
73 final ConstantValue _value; | 70 final ConstantValue _value; |
74 | 71 |
(...skipping 15 matching lines...) Expand all Loading... |
90 int get hashCode => 13 * _constant.hashCode; | 87 int get hashCode => 13 * _constant.hashCode; |
91 | 88 |
92 bool operator ==(var other) { | 89 bool operator ==(var other) { |
93 if (other is! Dart2JsConstantMirror) return false; | 90 if (other is! Dart2JsConstantMirror) return false; |
94 return _value == other._value; | 91 return _value == other._value; |
95 } | 92 } |
96 } | 93 } |
97 | 94 |
98 class Dart2JsNullConstantMirror extends Dart2JsConstantMirror { | 95 class Dart2JsNullConstantMirror extends Dart2JsConstantMirror { |
99 Dart2JsNullConstantMirror(Dart2JsMirrorSystem mirrorSystem, | 96 Dart2JsNullConstantMirror(Dart2JsMirrorSystem mirrorSystem, |
100 ConstantExpression constant, | 97 ConstantExpression constant, NullConstantValue value) |
101 NullConstantValue value) | |
102 : super(mirrorSystem, constant, value); | 98 : super(mirrorSystem, constant, value); |
103 | 99 |
104 NullConstantValue get _value => super._value; | 100 NullConstantValue get _value => super._value; |
105 | 101 |
106 bool get hasReflectee => true; | 102 bool get hasReflectee => true; |
107 | 103 |
108 get reflectee => null; | 104 get reflectee => null; |
109 } | 105 } |
110 | 106 |
111 class Dart2JsBoolConstantMirror extends Dart2JsConstantMirror { | 107 class Dart2JsBoolConstantMirror extends Dart2JsConstantMirror { |
112 Dart2JsBoolConstantMirror(Dart2JsMirrorSystem mirrorSystem, | 108 Dart2JsBoolConstantMirror(Dart2JsMirrorSystem mirrorSystem, |
113 ConstantExpression constant, | 109 ConstantExpression constant, BoolConstantValue value) |
114 BoolConstantValue value) | |
115 : super(mirrorSystem, constant, value); | 110 : super(mirrorSystem, constant, value); |
116 | 111 |
117 Dart2JsBoolConstantMirror.fromBool(Dart2JsMirrorSystem mirrorSystem, | 112 Dart2JsBoolConstantMirror.fromBool( |
118 bool value) | 113 Dart2JsMirrorSystem mirrorSystem, bool value) |
119 : super(mirrorSystem, null, | 114 : super(mirrorSystem, null, |
120 value ? new TrueConstantValue() : new FalseConstantValue()); | 115 value ? new TrueConstantValue() : new FalseConstantValue()); |
121 | 116 |
122 BoolConstantValue get _value => super._value; | 117 BoolConstantValue get _value => super._value; |
123 | 118 |
124 bool get hasReflectee => true; | 119 bool get hasReflectee => true; |
125 | 120 |
126 get reflectee => _value is TrueConstantValue; | 121 get reflectee => _value is TrueConstantValue; |
127 } | 122 } |
128 | 123 |
129 class Dart2JsStringConstantMirror extends Dart2JsConstantMirror { | 124 class Dart2JsStringConstantMirror extends Dart2JsConstantMirror { |
130 Dart2JsStringConstantMirror(Dart2JsMirrorSystem mirrorSystem, | 125 Dart2JsStringConstantMirror(Dart2JsMirrorSystem mirrorSystem, |
131 ConstantExpression constant, | 126 ConstantExpression constant, StringConstantValue value) |
132 StringConstantValue value) | |
133 : super(mirrorSystem, constant, value); | 127 : super(mirrorSystem, constant, value); |
134 | 128 |
135 Dart2JsStringConstantMirror.fromString(Dart2JsMirrorSystem mirrorSystem, | 129 Dart2JsStringConstantMirror.fromString( |
136 String text) | 130 Dart2JsMirrorSystem mirrorSystem, String text) |
137 : super(mirrorSystem, null, | 131 : super(mirrorSystem, null, |
138 new StringConstantValue(new DartString.literal(text))); | 132 new StringConstantValue(new DartString.literal(text))); |
139 | 133 |
140 StringConstantValue get _value => super._value; | 134 StringConstantValue get _value => super._value; |
141 | 135 |
142 bool get hasReflectee => true; | 136 bool get hasReflectee => true; |
143 | 137 |
144 get reflectee => _value.primitiveValue.slowToString(); | 138 get reflectee => _value.primitiveValue.slowToString(); |
145 } | 139 } |
146 | 140 |
147 class Dart2JsNumConstantMirror extends Dart2JsConstantMirror { | 141 class Dart2JsNumConstantMirror extends Dart2JsConstantMirror { |
148 Dart2JsNumConstantMirror(Dart2JsMirrorSystem mirrorSystem, | 142 Dart2JsNumConstantMirror(Dart2JsMirrorSystem mirrorSystem, |
149 ConstantExpression constant, | 143 ConstantExpression constant, NumConstantValue value) |
150 NumConstantValue value) | |
151 : super(mirrorSystem, constant, value); | 144 : super(mirrorSystem, constant, value); |
152 | 145 |
153 NumConstantValue get _value => super._value; | 146 NumConstantValue get _value => super._value; |
154 | 147 |
155 bool get hasReflectee => true; | 148 bool get hasReflectee => true; |
156 | 149 |
157 get reflectee => _value.primitiveValue; | 150 get reflectee => _value.primitiveValue; |
158 } | 151 } |
159 | 152 |
160 class Dart2JsListConstantMirror extends Dart2JsConstantMirror | 153 class Dart2JsListConstantMirror extends Dart2JsConstantMirror |
161 implements ListInstanceMirror { | 154 implements ListInstanceMirror { |
162 Dart2JsListConstantMirror(Dart2JsMirrorSystem mirrorSystem, | 155 Dart2JsListConstantMirror(Dart2JsMirrorSystem mirrorSystem, |
163 ConstantExpression constant, | 156 ConstantExpression constant, ListConstantValue value) |
164 ListConstantValue value) | |
165 : super(mirrorSystem, constant, value); | 157 : super(mirrorSystem, constant, value); |
166 | 158 |
167 ListConstantValue get _value => super._value; | 159 ListConstantValue get _value => super._value; |
168 | 160 |
169 int get length => _value.length; | 161 int get length => _value.length; |
170 | 162 |
171 InstanceMirror getElement(int index) { | 163 InstanceMirror getElement(int index) { |
172 if (index < 0) throw new RangeError('Negative index'); | 164 if (index < 0) throw new RangeError('Negative index'); |
173 if (index >= _value.length) throw new RangeError('Index out of bounds'); | 165 if (index >= _value.length) throw new RangeError('Index out of bounds'); |
174 return _convertConstantToInstanceMirror( | 166 return _convertConstantToInstanceMirror( |
175 mirrorSystem, null, _value.entries[index]); | 167 mirrorSystem, null, _value.entries[index]); |
176 } | 168 } |
177 } | 169 } |
178 | 170 |
179 class Dart2JsMapConstantMirror extends Dart2JsConstantMirror | 171 class Dart2JsMapConstantMirror extends Dart2JsConstantMirror |
180 implements MapInstanceMirror { | 172 implements MapInstanceMirror { |
181 List<String> _listCache; | 173 List<String> _listCache; |
182 | 174 |
183 Dart2JsMapConstantMirror(Dart2JsMirrorSystem mirrorSystem, | 175 Dart2JsMapConstantMirror(Dart2JsMirrorSystem mirrorSystem, |
184 ConstantExpression constant, | 176 ConstantExpression constant, MapConstantValue value) |
185 MapConstantValue value) | |
186 : super(mirrorSystem, constant, value); | 177 : super(mirrorSystem, constant, value); |
187 | 178 |
188 MapConstantValue get _value => super._value; | 179 MapConstantValue get _value => super._value; |
189 | 180 |
190 List<String> get _list { | 181 List<String> get _list { |
191 if (_listCache == null) { | 182 if (_listCache == null) { |
192 _listCache = new List<String>(_value.length); | 183 _listCache = new List<String>(_value.length); |
193 int index = 0; | 184 int index = 0; |
194 for (StringConstantValue keyConstant in _value.keys) { | 185 for (StringConstantValue keyConstant in _value.keys) { |
195 _listCache[index] = keyConstant.primitiveValue.slowToString(); | 186 _listCache[index] = keyConstant.primitiveValue.slowToString(); |
(...skipping 13 matching lines...) Expand all Loading... |
209 InstanceMirror getValue(String key) { | 200 InstanceMirror getValue(String key) { |
210 int index = _list.indexOf(key); | 201 int index = _list.indexOf(key); |
211 if (index == -1) return null; | 202 if (index == -1) return null; |
212 return _convertConstantToInstanceMirror( | 203 return _convertConstantToInstanceMirror( |
213 mirrorSystem, null, _value.values[index]); | 204 mirrorSystem, null, _value.values[index]); |
214 } | 205 } |
215 } | 206 } |
216 | 207 |
217 class Dart2JsTypeConstantMirror extends Dart2JsConstantMirror | 208 class Dart2JsTypeConstantMirror extends Dart2JsConstantMirror |
218 implements TypeInstanceMirror { | 209 implements TypeInstanceMirror { |
219 | |
220 Dart2JsTypeConstantMirror(Dart2JsMirrorSystem mirrorSystem, | 210 Dart2JsTypeConstantMirror(Dart2JsMirrorSystem mirrorSystem, |
221 ConstantExpression constant, | 211 ConstantExpression constant, TypeConstantValue value) |
222 TypeConstantValue value) | |
223 : super(mirrorSystem, constant, value); | 212 : super(mirrorSystem, constant, value); |
224 | 213 |
225 TypeConstantValue get _value => super._value; | 214 TypeConstantValue get _value => super._value; |
226 | 215 |
227 TypeMirror get representedType => | 216 TypeMirror get representedType => |
228 mirrorSystem._convertTypeToTypeMirror(_value.representedType); | 217 mirrorSystem._convertTypeToTypeMirror(_value.representedType); |
229 } | 218 } |
230 | 219 |
231 class Dart2JsConstructedConstantMirror extends Dart2JsConstantMirror { | 220 class Dart2JsConstructedConstantMirror extends Dart2JsConstantMirror { |
232 Map<String, ConstantValue> _fieldMapCache; | 221 Map<String, ConstantValue> _fieldMapCache; |
233 | 222 |
234 Dart2JsConstructedConstantMirror(Dart2JsMirrorSystem mirrorSystem, | 223 Dart2JsConstructedConstantMirror(Dart2JsMirrorSystem mirrorSystem, |
235 ConstantExpression constant, | 224 ConstantExpression constant, ConstructedConstantValue value) |
236 ConstructedConstantValue value) | |
237 : super(mirrorSystem, constant, value); | 225 : super(mirrorSystem, constant, value); |
238 | 226 |
239 ConstructedConstantValue get _value => super._value; | 227 ConstructedConstantValue get _value => super._value; |
240 | 228 |
241 Map<String, ConstantValue> get _fieldMap { | 229 Map<String, ConstantValue> get _fieldMap { |
242 if (_fieldMapCache == null) { | 230 if (_fieldMapCache == null) { |
243 _fieldMapCache = new Map<String, ConstantValue>(); | 231 _fieldMapCache = new Map<String, ConstantValue>(); |
244 if (identical(_value.type.element.kind, ElementKind.CLASS)) { | 232 if (identical(_value.type.element.kind, ElementKind.CLASS)) { |
245 _value.fields.forEach((FieldElement field, ConstantValue value) { | 233 _value.fields.forEach((FieldElement field, ConstantValue value) { |
246 _fieldMapCache[field.name] = value; | 234 _fieldMapCache[field.name] = value; |
247 }); | 235 }); |
248 } | 236 } |
249 } | 237 } |
250 return _fieldMapCache; | 238 return _fieldMapCache; |
251 } | 239 } |
252 | 240 |
253 InstanceMirror getField(Symbol fieldName) { | 241 InstanceMirror getField(Symbol fieldName) { |
254 String name = MirrorSystem.getName(fieldName); | 242 String name = MirrorSystem.getName(fieldName); |
255 ConstantValue fieldConstant = _fieldMap[name]; | 243 ConstantValue fieldConstant = _fieldMap[name]; |
256 if (fieldConstant != null) { | 244 if (fieldConstant != null) { |
257 return _convertConstantToInstanceMirror( | 245 return _convertConstantToInstanceMirror( |
258 mirrorSystem, null, fieldConstant); | 246 mirrorSystem, null, fieldConstant); |
259 } | 247 } |
260 return super.getField(fieldName); | 248 return super.getField(fieldName); |
261 } | 249 } |
262 } | 250 } |
263 | 251 |
264 class Dart2JsCommentInstanceMirror extends Object | 252 class Dart2JsCommentInstanceMirror extends Object |
265 with ObjectMirrorMixin, InstanceMirrorMixin | 253 with ObjectMirrorMixin, InstanceMirrorMixin |
266 implements CommentInstanceMirror { | 254 implements CommentInstanceMirror { |
267 final Dart2JsMirrorSystem mirrorSystem; | 255 final Dart2JsMirrorSystem mirrorSystem; |
268 final String text; | 256 final String text; |
269 String _trimmedText; | 257 String _trimmedText; |
270 | 258 |
271 Dart2JsCommentInstanceMirror(this.mirrorSystem, this.text); | 259 Dart2JsCommentInstanceMirror(this.mirrorSystem, this.text); |
272 | 260 |
273 ClassMirror get type { | 261 ClassMirror get type { |
274 return mirrorSystem._getTypeDeclarationMirror( | 262 return mirrorSystem |
275 mirrorSystem.compiler.documentClass); | 263 ._getTypeDeclarationMirror(mirrorSystem.compiler.documentClass); |
276 } | 264 } |
277 | 265 |
278 bool get isDocComment => text.startsWith('/**') || text.startsWith('///'); | 266 bool get isDocComment => text.startsWith('/**') || text.startsWith('///'); |
279 | 267 |
280 String get trimmedText { | 268 String get trimmedText { |
281 if (_trimmedText == null) { | 269 if (_trimmedText == null) { |
282 _trimmedText = stripComment(text); | 270 _trimmedText = stripComment(text); |
283 } | 271 } |
284 return _trimmedText; | 272 return _trimmedText; |
285 } | 273 } |
286 | 274 |
287 InstanceMirror getField(Symbol fieldName) { | 275 InstanceMirror getField(Symbol fieldName) { |
288 if (fieldName == #isDocComment) { | 276 if (fieldName == #isDocComment) { |
289 return new Dart2JsBoolConstantMirror.fromBool(mirrorSystem, isDocComment); | 277 return new Dart2JsBoolConstantMirror.fromBool(mirrorSystem, isDocComment); |
290 } else if (fieldName == #text) { | 278 } else if (fieldName == #text) { |
291 return new Dart2JsStringConstantMirror.fromString(mirrorSystem, text); | 279 return new Dart2JsStringConstantMirror.fromString(mirrorSystem, text); |
292 } else if (fieldName == #trimmedText) { | 280 } else if (fieldName == #trimmedText) { |
293 return new Dart2JsStringConstantMirror.fromString(mirrorSystem, | 281 return new Dart2JsStringConstantMirror.fromString( |
294 trimmedText); | 282 mirrorSystem, trimmedText); |
295 } | 283 } |
296 return super.getField(fieldName); | 284 return super.getField(fieldName); |
297 } | 285 } |
298 } | 286 } |
OLD | NEW |