OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart2js.mirrors; | |
6 | |
7 abstract class ObjectMirrorMixin implements ObjectMirror { | |
8 InstanceMirror getField(Symbol fieldName) { | |
9 throw new UnsupportedError('ObjectMirror.getField unsupported.'); | |
10 } | |
11 | |
12 InstanceMirror setField(Symbol fieldName, Object value) { | |
13 throw new UnsupportedError('ObjectMirror.setField unsupported.'); | |
14 } | |
15 | |
16 InstanceMirror invoke(Symbol memberName, | |
17 List positionalArguments, | |
18 [Map<Symbol, dynamic> namedArguments]) { | |
19 throw new UnsupportedError('ObjectMirror.invoke unsupported.'); | |
20 } | |
21 } | |
22 | |
23 abstract class InstanceMirrorMixin implements InstanceMirror { | |
24 | |
25 bool get hasReflectee => false; | |
26 | |
27 get reflectee { | |
28 throw new UnsupportedError('InstanceMirror.reflectee unsupported.'); | |
29 } | |
30 | |
31 Function operator [](Symbol name) { | |
32 throw new UnsupportedError('InstanceMirror.operator [] unsupported.'); | |
33 } | |
34 | |
35 delegate(Invocation invocation) { | |
36 throw new UnsupportedError('InstanceMirror.delegate unsupported'); | |
37 } | |
38 } | |
39 | |
40 InstanceMirror _convertConstantToInstanceMirror(Dart2JsMirrorSystem mirrors, | |
41 Constant constant) { | |
42 if (constant is BoolConstant) { | |
43 return new Dart2JsBoolConstantMirror(mirrors, constant); | |
44 } else if (constant is NumConstant) { | |
45 return new Dart2JsNumConstantMirror(mirrors, constant); | |
46 } else if (constant is StringConstant) { | |
47 return new Dart2JsStringConstantMirror(mirrors, constant); | |
48 } else if (constant is ListConstant) { | |
49 return new Dart2JsListConstantMirror(mirrors, constant); | |
50 } else if (constant is MapConstant) { | |
51 return new Dart2JsMapConstantMirror(mirrors, constant); | |
52 } else if (constant is TypeConstant) { | |
53 return new Dart2JsTypeConstantMirror(mirrors, constant); | |
54 } else if (constant is FunctionConstant) { | |
55 return new Dart2JsConstantMirror(mirrors, constant); | |
56 } else if (constant is NullConstant) { | |
57 return new Dart2JsNullConstantMirror(mirrors, constant); | |
58 } else if (constant is ConstructedConstant) { | |
59 return new Dart2JsConstructedConstantMirror(mirrors, constant); | |
60 } | |
61 mirrors.compiler.internalError("Unexpected constant $constant"); | |
62 } | |
63 | |
64 | |
65 //////////////////////////////////////////////////////////////////////////////// | |
66 // Mirrors on constant values used for metadata. | |
67 //////////////////////////////////////////////////////////////////////////////// | |
68 | |
69 class Dart2JsConstantMirror extends Object | |
70 with ObjectMirrorMixin, InstanceMirrorMixin | |
71 implements InstanceMirror { | |
72 final Dart2JsMirrorSystem mirrors; | |
floitsch
2014/01/30 18:18:20
Why do we call this "mirrors" and not "mirrorSyste
Johnni Winther
2014/02/03 08:22:48
No good reason. Changed.
| |
73 final Constant _constant; | |
74 | |
75 Dart2JsConstantMirror(this.mirrors, this._constant); | |
76 | |
77 // TODO(johnniwinther): Improve the quality of this method. | |
78 String toString() => '$_constant'; | |
79 | |
80 ClassMirror get type { | |
81 return mirrors._getTypeDeclarationMirror( | |
82 _constant.computeType(mirrors.compiler).element); | |
83 } | |
84 } | |
85 | |
86 class Dart2JsNullConstantMirror extends Dart2JsConstantMirror { | |
87 Dart2JsNullConstantMirror(Dart2JsMirrorSystem mirrors, NullConstant constant) | |
88 : super(mirrors, constant); | |
89 | |
90 NullConstant get _constant => super._constant; | |
91 | |
92 bool get hasReflectee => true; | |
93 | |
94 get reflectee => null; | |
95 } | |
96 | |
97 class Dart2JsBoolConstantMirror extends Dart2JsConstantMirror { | |
98 Dart2JsBoolConstantMirror(Dart2JsMirrorSystem mirrors, BoolConstant constant) | |
99 : super(mirrors, constant); | |
100 | |
101 Dart2JsBoolConstantMirror.fromBool(Dart2JsMirrorSystem mirrors, bool value) | |
102 : super(mirrors, value ? new TrueConstant() : new FalseConstant()); | |
103 | |
104 BoolConstant get _constant => super._constant; | |
105 | |
106 bool get hasReflectee => true; | |
107 | |
108 get reflectee => _constant is TrueConstant; | |
109 } | |
110 | |
111 class Dart2JsStringConstantMirror extends Dart2JsConstantMirror { | |
112 Dart2JsStringConstantMirror(Dart2JsMirrorSystem mirrors, | |
113 StringConstant constant) | |
114 : super(mirrors, constant); | |
115 | |
116 Dart2JsStringConstantMirror.fromString(Dart2JsMirrorSystem mirrors, | |
117 String text) | |
118 : super(mirrors, new StringConstant(new DartString.literal(text))); | |
119 | |
120 StringConstant get _constant => super._constant; | |
121 | |
122 bool get hasReflectee => true; | |
123 | |
124 get reflectee => _constant.value.slowToString(); | |
125 } | |
126 | |
127 class Dart2JsNumConstantMirror extends Dart2JsConstantMirror { | |
128 Dart2JsNumConstantMirror(Dart2JsMirrorSystem mirrors, | |
129 NumConstant constant) | |
130 : super(mirrors, constant); | |
131 | |
132 NumConstant get _constant => super._constant; | |
133 | |
134 bool get hasReflectee => true; | |
135 | |
136 get reflectee => _constant.value; | |
137 } | |
138 | |
139 class Dart2JsListConstantMirror extends Dart2JsConstantMirror | |
140 implements ListInstanceMirror { | |
141 Dart2JsListConstantMirror(Dart2JsMirrorSystem mirrors, | |
142 ListConstant constant) | |
143 : super(mirrors, constant); | |
144 | |
145 ListConstant get _constant => super._constant; | |
146 | |
147 int get length => _constant.length; | |
148 | |
149 InstanceMirror getElement(int index) { | |
150 if (index < 0) throw new RangeError('Negative index'); | |
151 if (index >= _constant.length) throw new RangeError('Index out of bounds'); | |
152 return _convertConstantToInstanceMirror(mirrors, _constant.entries[index]); | |
153 } | |
154 } | |
155 | |
156 class Dart2JsMapConstantMirror extends Dart2JsConstantMirror | |
157 implements MapInstanceMirror { | |
158 List<String> _listCache; | |
159 | |
160 Dart2JsMapConstantMirror(Dart2JsMirrorSystem mirrors, | |
161 MapConstant constant) | |
162 : super(mirrors, constant); | |
163 | |
164 MapConstant get _constant => super._constant; | |
165 | |
166 List<String> get _list { | |
167 if (_listCache == null) { | |
168 _listCache = new List<String>(_constant.keys.entries.length); | |
169 int index = 0; | |
170 for (StringConstant keyConstant in _constant.keys.entries) { | |
171 _listCache[index] = keyConstant.value.slowToString(); | |
172 index++; | |
173 } | |
174 _listCache = new UnmodifiableListView<String>(_listCache); | |
175 } | |
176 return _listCache; | |
177 } | |
178 | |
179 int get length => _constant.length; | |
180 | |
181 Iterable<String> get keys { | |
182 return _list; | |
183 } | |
184 | |
185 InstanceMirror getValue(String key) { | |
186 int index = _list.indexOf(key); | |
187 if (index == -1) return null; | |
188 return _convertConstantToInstanceMirror(mirrors, _constant.values[index]); | |
189 } | |
190 } | |
191 | |
192 class Dart2JsTypeConstantMirror extends Dart2JsConstantMirror | |
193 implements TypeInstanceMirror { | |
194 | |
195 Dart2JsTypeConstantMirror(Dart2JsMirrorSystem mirrors, | |
196 TypeConstant constant) | |
197 : super(mirrors, constant); | |
198 | |
199 TypeConstant get _constant => super._constant; | |
200 | |
201 TypeMirror get representedType => | |
202 mirrors._convertTypeToTypeMirror(_constant.representedType); | |
203 } | |
204 | |
205 class Dart2JsConstructedConstantMirror extends Dart2JsConstantMirror { | |
206 Map<String,Constant> _fieldMapCache; | |
207 | |
208 Dart2JsConstructedConstantMirror(Dart2JsMirrorSystem mirrors, | |
209 ConstructedConstant constant) | |
210 : super(mirrors, constant); | |
211 | |
212 ConstructedConstant get _constant => super._constant; | |
213 | |
214 Map<String,Constant> get _fieldMap { | |
215 if (_fieldMapCache == null) { | |
216 _fieldMapCache = new Map<String,Constant>(); | |
217 if (identical(_constant.type.element.kind, ElementKind.CLASS)) { | |
218 var index = 0; | |
219 ClassElement element = _constant.type.element; | |
220 element.forEachInstanceField((_, Element field) { | |
221 String fieldName = field.name; | |
222 _fieldMapCache.putIfAbsent(fieldName, () => _constant.fields[index]); | |
223 index++; | |
224 }, includeSuperAndInjectedMembers: true); | |
225 } | |
226 } | |
227 return _fieldMapCache; | |
228 } | |
229 | |
230 InstanceMirror getField(Symbol fieldName) { | |
231 Constant fieldConstant = _fieldMap[MirrorSystem.getName(fieldName)]; | |
232 if (fieldConstant != null) { | |
233 return _convertConstantToInstanceMirror(mirrors, fieldConstant); | |
234 } | |
235 return super.getField(fieldName); | |
236 } | |
237 } | |
238 | |
239 class Dart2JsCommentInstanceMirror extends Object | |
240 with ObjectMirrorMixin, InstanceMirrorMixin | |
241 implements CommentInstanceMirror { | |
242 final Dart2JsMirrorSystem mirrors; | |
243 final String text; | |
244 String _trimmedText; | |
245 | |
246 Dart2JsCommentInstanceMirror(this.mirrors, this.text); | |
247 | |
248 ClassMirror get type { | |
249 return mirrors._getTypeDeclarationMirror(mirrors.compiler.documentClass); | |
250 } | |
251 | |
252 bool get isDocComment => text.startsWith('/**') || text.startsWith('///'); | |
253 | |
254 String get trimmedText { | |
255 if (_trimmedText == null) { | |
256 _trimmedText = stripComment(text); | |
257 } | |
258 return _trimmedText; | |
259 } | |
260 | |
261 InstanceMirror getField(Symbol fieldName) { | |
262 if (fieldName == #isDocComment) { | |
263 return new Dart2JsBoolConstantMirror.fromBool(mirrors, isDocComment); | |
264 } else if (fieldName == #text) { | |
265 return new Dart2JsStringConstantMirror.fromString(mirrors, text); | |
266 } else if (fieldName == #trimmedText) { | |
267 return new Dart2JsStringConstantMirror.fromString(mirrors, trimmedText); | |
268 } | |
269 super.getField(fieldName); | |
270 } | |
271 } | |
OLD | NEW |