Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: pkg/checked_mirrors/lib/src/wrappers.dart

Issue 111643015: Preliminary checked mirrors (not ready for review yet) (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/checked_mirrors/lib/src/utils.dart ('k') | pkg/checked_mirrors/lib/transformer.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 /// This part of checked_mirrors contains definitions that wrap each type in the
6 /// mirror system with an equivalent type that performs a check before accessing
7 /// symbols.
8 part of checked_mirrors;
9
10 /// An expando to store existing wrappers, since some APIs are expected to
11 /// return the same instance (at least according to the mirror tests).
12 Expando _wrappers = new Expando();
13
14 /// Returns a wrapper for [original], an object from the base mirror system. We
15 /// internally try to store the wrapper in an expando to ensure we use the same
16 /// wrapper whenever it's possible.
17 _wrap(original) {
18 if (original == null) return null;
19 var value = _wrappers[original];
20 if (value != null) return value;
21 value = _createWrapper(original);
22 _wrappers[original] = value;
23 return value;
24 }
25
26 /// Creates a wrapper for [original] assuming one doesn't exist already. The
27 /// wrapper will be a corresponding object in this library, for instance
28 /// _ClosureMirror for ClosureMirror. This function also wraps maps and lists
29 /// whose values are mirror system objects.
30 _createWrapper(original) {
31 // Note: the order below is important to guarnatee we create the most precise
32 // wrapper for a given type (e.g. FunctionTypeMirror is a ClassMirror too).
33
34 if (original is Map) return new _MapWrapper(original);
35 if (original is List) return new _ListWrapper(original);
36 if (original is FunctionTypeMirror) return new _FunctionTypeMirror(original);
37 if (original is ClassMirror) return new _ClassMirror(original);
38
39 if (original is ClosureMirror) return new _ClosureMirror(original);
40 if (original is InstanceMirror) return new _InstanceMirror(original);
41 if (original is LibraryMirror) return new _LibraryMirror(original);
42 if (original is ObjectMirror) return new _ObjectMirror(original);
43
44 if (original is ParameterMirror) return new _ParameterMirror(original);
45 if (original is VariableMirror) return new _VariableMirror(original);
46
47 if (original is TypeVariableMirror) return new _TypeVariableMirror(original);
48 if (original is IsolateMirror) return new _IsolateMirror(original);
49 if (original is MethodMirror) return new _MethodMirror(original);
50 if (original is mirrors.MirrorSystem) return new MirrorSystem(original);
51 if (original is TypedefMirror) return new _TypedefMirror(original);
52
53 if (original is TypeMirror) return new _TypeMirror(original);
54 if (original is DeclarationMirror) return new _DeclarationMirror(original);
55 throw "Unknown mirror type: ${original.runtimeType}";
56 }
57
58 MirrorSystem _current = _wrap(mirrors.currentMirrorSystem());
59 MirrorSystem currentMirrorSystem() => _current;
60 InstanceMirror reflect(Object reflectee) => _wrap(mirrors.reflect(reflectee));
61 ClassMirror reflectClass(Type key) => _wrap(mirrors.reflectClass(key));
62 TypeMirror reflectType(Type key) => _wrap(mirrors.reflectType(key));
63
64 class _Mirror {
65 dynamic _original;
66 _Mirror(this._original);
67
68 toString() => _original.toString();
69 }
70
71 // We use the original name for this class because it exposes static methods
72 // that could be used externally.
73 // TODO(sigmund): technically the top-level symbols exposed here could be used
74 // too, like #LibraryMirror. Maybe we need to override the names of all these
75 // symbols?
76 class MirrorSystem extends _Mirror with mirrors.MirrorSystem {
77 MirrorSystem(original) : super(original);
78
79 Map<Uri, LibraryMirror> get libraries => _wrap(_original.libraries);
80
81 IsolateMirror get isolate => _wrap(_original.isolate);
82
83 TypeMirror get dynamicType => _wrap(_original.dynamicType);
84
85 TypeMirror get voidType => _wrap(_original.voidType);
86
87 static String getName(Symbol symbol) => mirrors.MirrorSystem.getName(symbol);
88 static Symbol getSymbol(String name, [LibraryMirror library]) {
89 if (library == null) return mirrors.MirrorSystem.getSymbol(name);
90 if (library is! _LibraryMirror) {
91 throw new ArgumentError("$library is not a LibraryMirror");
92 }
93 _LibraryMirror lib = library; // type-check
94 return mirrors.MirrorSystem.getSymbol(name, lib._original);
95 }
96 }
97
98 class _IsolateMirror extends _Mirror implements mirrors.IsolateMirror {
99 _IsolateMirror(original) : super(original);
100
101 String get debugName => _original.debugName;
102 bool get isCurrent => _original.isCurrent;
103 LibraryMirror get rootLibrary => _wrap(_original.rootLibrary);
104 bool operator == (other) {
105 if (other == null || other is! _IsolateMirror) return false;
106 return _original == other._original;
107 }
108 int get hashCode => _original.hashCode;
109 }
110
111 abstract class _DeclarationMirrorMixin implements mirrors.DeclarationMirror {
112 get _original;
113 Symbol get simpleName => _original.simpleName;
114 Symbol get qualifiedName => _original.qualifiedName;
115 DeclarationMirror get owner => _wrap(_original.owner);
116 bool get isPrivate => _original.isPrivate;
117 bool get isTopLevel => _original.isTopLevel;
118 SourceLocation get location => _original.location;
119 List<InstanceMirror> get metadata => _wrap(_original.metadata);
120 }
121
122 class _DeclarationMirror extends _Mirror with _DeclarationMirrorMixin
123 implements mirrors.DeclarationMirror {
124 _DeclarationMirror(original) : super(original);
125 get _original => super._original; // to get rid of static warnings
126
127 }
128
129 abstract class _ObjectMirrorMixin implements mirrors.ObjectMirror {
130 get _original;
131
132 InstanceMirror invoke(Symbol memberName,
133 List positionalArguments,
134 [Map<Symbol,dynamic> namedArguments]) =>
135 _wrap(_original.invoke(memberName, positionalArguments, namedArguments));
136
137 InstanceMirror getField(Symbol fieldName) {
138 _check(this, fieldName);
139 return _wrap(_original.getField(fieldName));
140 }
141
142 InstanceMirror setField(Symbol fieldName, Object value) {
143 _check(this, fieldName);
144 return _wrap(_original.setField(fieldName, value));
145 }
146 }
147
148 class _ObjectMirror extends _Mirror with _ObjectMirrorMixin
149 implements mirrors.ObjectMirror {
150 _ObjectMirror(original) : super(original);
151 get _original => super._original; // to get rid of static warnings
152 }
153
154 class _InstanceMirror extends _ObjectMirror implements mirrors.InstanceMirror {
155 _InstanceMirror(original) : super(original);
156
157 ClassMirror get type => _wrap(_original.type);
158 bool get hasReflectee => _original.hasReflectee;
159 get reflectee => _original.reflectee;
160 bool operator == (other) {
161 if (other == null || other is! _InstanceMirror) return false;
162 return _original == other._original;
163 }
164 int get hashCode => _original.hashCode;
165
166 delegate(Invocation invocation) => _original.delegate(invocation);
167 Function operator [](Symbol name) {
168 print('warning: this cannot be debugged: InstanceMirror.operator[]');
169 // TODO(sigmund): we'd like to wrap the result inside the function, but we
170 // don't have a way to dynamically take arguments.
171 return _original[name];
172 }
173 }
174
175 class _ClosureMirror extends _InstanceMirror implements mirrors.ClosureMirror {
176 _ClosureMirror(original) : super(original);
177
178 MethodMirror get function => _wrap(_original.function);
179 InstanceMirror apply(List positionalArguments,
180 [Map<Symbol, dynamic> namedArguments]) =>
181 _wrap(_original.apply(positionalArguments, namedArguments));
182 InstanceMirror findInContext(Symbol name, {ifAbsent: null}) =>
183 _wrap(_original.findInContext(name, ifAbsent: ifAbsent));
184 }
185
186 class _LibraryMirror extends _DeclarationMirror with _ObjectMirrorMixin
187 implements mirrors.LibraryMirror {
188 _LibraryMirror(original) : super(original);
189 get _original => super._original; // to get rid of static warnings
190
191 Uri get uri => _original.uri;
192 Map<Symbol, DeclarationMirror> get declarations =>
193 _wrap(_original.declarations);
194
195 Map<Symbol, MethodMirror> get topLevelMembers =>
196 _wrap(_original.topLevelMembers);
197
198 bool operator == (other) {
199 if (other == null || other is! _LibraryMirror) return false;
200 return _original == other._original;
201 }
202 int get hashCode => _original.hashCode;
203
204 Function operator [](Symbol name) {
205 print('warning: this cannot be debugged: LibraryMirror.operator[]');
206 // TODO(sigmund): we'd like to wrap the result inside the function, but we
207 // don't have a way to dynamically take arguments.
208 return _original[name];
209 }
210 }
211
212 class _TypeMirror extends _DeclarationMirror implements mirrors.TypeMirror {
213 _TypeMirror(original) : super(original);
214
215 List<TypeVariableMirror> get typeVariables =>
216 _wrap(_original.typeVariables);
217 List<TypeMirror> get typeArguments =>
218 _wrap(_original.typeArguments);
219 bool get isOriginalDeclaration => _original.isOriginalDeclaration;
220 TypeMirror get originalDeclaration =>
221 _wrap(_original.originalDeclaration);
222
223 bool operator == (other) {
224 if (other == null || other is! _TypeMirror) return false;
225 return _original == other._original;
226 }
227 int get hashCode => _original.hashCode;
228 }
229
230 class _ClassMirror extends _TypeMirror with _ObjectMirrorMixin
231 implements ClassMirror {
232 _ClassMirror(original) : super(original);
233
234 bool get hasReflectedType => _original.hasReflectedType;
235 Type get reflectedType => _original.reflectedType;
236 ClassMirror get superclass => _wrap(_original.superclass);
237 List<ClassMirror> get superinterfaces =>
238 _wrap(_original.superinterfaces);
239
240 Map<Symbol, DeclarationMirror> get declarations =>
241 _wrap(_original.declarations);
242 Map<Symbol, MethodMirror> get instanceMembers =>
243 _wrap(_original.instanceMembers);
244 Map<Symbol, MethodMirror> get staticMembers =>
245 _wrap(_original.staticMembers);
246 ClassMirror get mixin => _wrap(_original.mixin);
247 InstanceMirror newInstance(Symbol constructorName,
248 List positionalArguments,
249 [Map<Symbol,dynamic> namedArguments]) =>
250 _wrap(_original.newInstance(constructorName,
251 positionalArguments, namedArguments));
252
253 bool operator == (other) {
254 if (other == null || other is! _ClassMirror) return false;
255 return _original == other._original;
256 }
257 int get hashCode => _original.hashCode;
258
259 Function operator [](Symbol name) {
260 print('warning: this cannot be debugged: ClassMirror.operator[]');
261 // TODO(sigmund): we'd like to wrap the result inside the function, but we
262 // don't have a way to dynamically take arguments.
263 return _original[name];
264 }
265 }
266
267 class _FunctionTypeMirror extends _ClassMirror implements FunctionTypeMirror {
268 _FunctionTypeMirror(original) : super(original);
269 get _original => super._original; // to get rid of static warnings
270
271 TypeMirror get returnType => _wrap(_original.returnType);
272 List<ParameterMirror> get parameters =>
273 _wrap(_original.parameters);
274 MethodMirror get callMethod => _wrap(_original.callMethod);
275 }
276
277 class _TypeVariableMirror extends _TypeMirror implements TypeVariableMirror {
278 _TypeVariableMirror(original) : super(original);
279
280 TypeMirror get upperBound => _wrap(_original.upperBound);
281 bool get isStatic => _original.isStatic;
282 bool operator == (other) {
283 if (other == null || other is! _TypeVariableMirror) return false;
284 return _original == other._original;
285 }
286 int get hashCode => _original.hashCode;
287 }
288
289 class _TypedefMirror extends _TypeMirror implements TypedefMirror {
290 _TypedefMirror(original) : super(original);
291
292 FunctionTypeMirror get referent => _wrap(_original.referent);
293 }
294
295 class _MethodMirror extends _DeclarationMirror implements MethodMirror {
296 _MethodMirror(original) : super(original);
297
298 TypeMirror get returnType => _wrap(_original.returnType);
299 String get source => _original.source;
300 List<ParameterMirror> get parameters =>
301 _wrap(_original.parameters);
302 bool get isStatic => _original.isStatic;
303 bool get isAbstract => _original.isAbstract;
304 bool get isSynthetic => _original.isSynthetic;
305 bool get isRegularMethod => _original.isRegularMethod;
306 bool get isOperator => _original.isOperator;
307 bool get isGetter => _original.isGetter;
308 bool get isSetter => _original.isSetter;
309 bool get isConstructor => _original.isConstructor;
310 Symbol get constructorName => _original.constructorName;
311 bool get isConstConstructor => _original.isConstConstructor;
312 bool get isGenerativeConstructor => _original.isGenerativeConstructor;
313 bool get isRedirectingConstructor => _original.isRedirectingConstructor;
314 bool get isFactoryConstructor => _original.isFactoryConstructor;
315 bool operator == (other) {
316 if (other == null || other is! _MethodMirror) return false;
317 return _original == other._original;
318 }
319 int get hashCode => _original.hashCode;
320 }
321
322 class _VariableMirror extends _DeclarationMirror implements VariableMirror {
323 _VariableMirror(original) : super(original);
324 TypeMirror get type => _wrap(_original.type);
325 bool get isStatic => _original.isStatic;
326 bool get isFinal => _original.isFinal;
327 bool get isConst => _original.isConst;
328 bool operator == (other) {
329 if (other == null || other is! _VariableMirror) return false;
330 return _original == other._original;
331 }
332 int get hashCode => _original.hashCode;
333 }
334
335 class _ParameterMirror extends _VariableMirror implements ParameterMirror {
336 _ParameterMirror(original) : super(original);
337 TypeMirror get type => _wrap(_original.type);
338 bool get isOptional => _original.isOptional;
339 bool get isNamed => _original.isNamed;
340 bool get hasDefaultValue => _original.hasDefaultValue;
341 InstanceMirror get defaultValue => _original.defaultValue;
342 }
343
344 /// Wraps a map that contains mirror values
345 class _MapWrapper<K, V extends _Mirror> implements Map<K, V> {
346 Map<K, V> _original;
347 _MapWrapper(Map<K, V> original) : _original = original;
348
349 V operator [](Object key) => _wrap(_original[key]);
350
351 void operator []=(K key, V value) {
352 _original[key] = value._original;
353 }
354
355 void addAll(Map<K, V> other) {
356 other.forEach((key, value) { _original[key] = value; });
357 }
358
359 bool containsValue(Object value) =>
360 _original.containsValue(value != null && (value as dynamic)._original);
361
362 V putIfAbsent(K key, V ifAbsent()) =>
363 _original.putIfAbsent(key, () => ifAbsent()._original);
364
365 // Every other method is just delegated to the original
366
367 Iterable<V> get values => _original.values.map(_wrap);
368
369 void clear() => _original.clear();
370
371 bool containsKey(Object key) => _original.containsKey(key);
372
373 void forEach(void f(K key, V value)) {
374 _original.forEach(f);
375 }
376
377 bool get isEmpty => _original.isEmpty;
378
379 bool get isNotEmpty => _original.isNotEmpty;
380
381 Iterable<K> get keys => _original.keys;
382
383 int get length => _original.length;
384
385 V remove(Object key) => _original.remove(key);
386 }
387
388 /// Wraps a list of mirror values
389 class _ListWrapper<E extends _Mirror> extends Object with ListMixin<E> {
390 List _original;
391
392 _ListWrapper(this._original);
393
394 E operator [](int index) => _wrap(_original[index]);
395
396 void operator []=(int index, E value) {
397 _original[index] = value._original;
398 }
399
400 int get length => _original.length;
401
402 void set length(int newLength) {
403 _original.length = newLength;
404 }
405 }
OLDNEW
« no previous file with comments | « pkg/checked_mirrors/lib/src/utils.dart ('k') | pkg/checked_mirrors/lib/transformer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698