OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Patch library for dart:mirrors. | 5 // Patch library for dart:mirrors. |
6 | 6 |
7 import 'dart:_foreign_helper' show JS; | 7 import 'dart:_foreign_helper' show JS; |
8 import 'dart:_collection-dev' as _symbol_dev; | 8 import 'dart:_collection-dev' as _symbol_dev; |
9 import 'dart:_js_helper' show createInvocationMirror; | 9 import 'dart:_js_helper' show createInvocationMirror; |
10 import 'dart:_interceptors' show getInterceptor; | 10 import 'dart:_interceptors' show Interceptor; |
11 | 11 |
12 patch class MirrorSystem { | 12 patch class MirrorSystem { |
13 patch static String getName(Symbol symbol) => _n(symbol); | 13 patch static String getName(Symbol symbol) => _n(symbol); |
14 } | 14 } |
15 | 15 |
16 class _MirrorSystem implements MirrorSystem { | 16 class _MirrorSystem implements MirrorSystem { |
17 TypeMirror get dynamicType => _dynamicType; | 17 TypeMirror get dynamicType => _dynamicType; |
18 TypeMirror get voidType => _voidType; | 18 TypeMirror get voidType => _voidType; |
19 | 19 |
20 final static TypeMirror _dynamicType = | 20 final static TypeMirror _dynamicType = |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 final Uri uri; | 54 final Uri uri; |
55 final List<String> _classes; | 55 final List<String> _classes; |
56 final List<String> _functions; | 56 final List<String> _functions; |
57 | 57 |
58 _LibraryMirror(this._name, this.uri, this._classes, this._functions); | 58 _LibraryMirror(this._name, this.uri, this._classes, this._functions); |
59 | 59 |
60 Map<Symbol, ClassMirror> get classes { | 60 Map<Symbol, ClassMirror> get classes { |
61 var result = new Map<Symbol, ClassMirror>(); | 61 var result = new Map<Symbol, ClassMirror>(); |
62 for (int i = 0; i < _classes.length; i += 2) { | 62 for (int i = 0; i < _classes.length; i += 2) { |
63 Symbol symbol = _s(_classes[i]); | 63 Symbol symbol = _s(_classes[i]); |
64 result[symbol] = _reflectClass(symbol, _classes[i + 1]); | 64 _ClassMirror cls = _reflectClass(symbol, _classes[i + 1]); |
| 65 result[symbol] = cls; |
| 66 cls._owner = this; |
65 } | 67 } |
66 return result; | 68 return result; |
67 } | 69 } |
68 | 70 |
69 InstanceMirror setField(Symbol fieldName, Object arg) { | 71 InstanceMirror setField(Symbol fieldName, Object arg) { |
70 // TODO(ahe): This is extremely dangerous!!! | 72 // TODO(ahe): This is extremely dangerous!!! |
71 JS('void', r'$[#] = #', _n(fieldName), arg); | 73 JS('void', r'$[#] = #', _n(fieldName), arg); |
72 return _reflect(arg); | 74 return _reflect(arg); |
73 } | 75 } |
74 | 76 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 return JSInvocationMirror.invokeFromMirror(invocation, reflectee); | 200 return JSInvocationMirror.invokeFromMirror(invocation, reflectee); |
199 } | 201 } |
200 | 202 |
201 String toString() => 'InstanceMirror($reflectee)'; | 203 String toString() => 'InstanceMirror($reflectee)'; |
202 } | 204 } |
203 | 205 |
204 class _ClassMirror extends _ObjectMirror implements ClassMirror { | 206 class _ClassMirror extends _ObjectMirror implements ClassMirror { |
205 final Symbol simpleName; | 207 final Symbol simpleName; |
206 final _jsConstructor; | 208 final _jsConstructor; |
207 final String _fields; | 209 final String _fields; |
| 210 // Set as side-effect of accessing _LibraryMirror.classes. |
| 211 _LibraryMirror _owner; |
208 | 212 |
209 _ClassMirror(this.simpleName, this._jsConstructor, this._fields); | 213 _ClassMirror(this.simpleName, this._jsConstructor, this._fields); |
210 | 214 |
211 Map<Symbol, Mirror> get members { | 215 Map<Symbol, Mirror> get members { |
212 var result = new Map<Symbol, Mirror>(); | 216 var result = new Map<Symbol, Mirror>(); |
213 var s = _fields.split(";"); | 217 var s = _fields.split(";"); |
214 var fields = s[1] == "" ? [] : s[1].split(","); | 218 var fields = s[1] == "" ? [] : s[1].split(","); |
215 for (String field in fields) { | 219 for (String field in fields) { |
216 _VariableMirror mirror = new _VariableMirror.from(field); | 220 _VariableMirror mirror = new _VariableMirror.from(field); |
217 result[mirror.simpleName] = mirror; | 221 result[mirror.simpleName] = mirror; |
(...skipping 29 matching lines...) Expand all Loading... |
247 List positionalArguments, | 251 List positionalArguments, |
248 [Map<Symbol, dynamic> namedArguments]) { | 252 [Map<Symbol, dynamic> namedArguments]) { |
249 if (namedArguments != null && !namedArguments.isEmpty) { | 253 if (namedArguments != null && !namedArguments.isEmpty) { |
250 throw new UnsupportedError('Named arguments are not implemented'); | 254 throw new UnsupportedError('Named arguments are not implemented'); |
251 } | 255 } |
252 return new Future<InstanceMirror>( | 256 return new Future<InstanceMirror>( |
253 () => newInstance( | 257 () => newInstance( |
254 constructorName, positionalArguments, namedArguments)); | 258 constructorName, positionalArguments, namedArguments)); |
255 } | 259 } |
256 | 260 |
| 261 DeclarationMirror get owner { |
| 262 if (_owner == null) { |
| 263 if (_jsConstructor is Interceptor) { |
| 264 _owner = __reflectClass(Object).owner; |
| 265 } else { |
| 266 for (var list in _MirrorSystem.librariesByName.values) { |
| 267 for (_LibraryMirror library in list) { |
| 268 // This will set _owner field on all clasess as a side |
| 269 // effect. This gives us a fast path to reflect on a |
| 270 // class without parsing reflection data. |
| 271 library.classes; |
| 272 } |
| 273 } |
| 274 } |
| 275 if (_owner == null) { |
| 276 throw new StateError('Class "${_n(simpleName)}" has no owner'); |
| 277 } |
| 278 } |
| 279 return _owner; |
| 280 } |
257 | 281 |
258 String toString() => 'ClassMirror(${_n(simpleName)})'; | 282 String toString() => 'ClassMirror(${_n(simpleName)})'; |
259 } | 283 } |
260 | 284 |
261 class _VariableMirror implements VariableMirror { | 285 class _VariableMirror implements VariableMirror { |
262 // TODO(ahe): The values in these fields are virtually untested. | 286 // TODO(ahe): The values in these fields are virtually untested. |
263 final Symbol simpleName; | 287 final Symbol simpleName; |
264 final String _jsName; | 288 final String _jsName; |
265 final bool _readOnly; | 289 final bool _readOnly; |
266 | 290 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 final _jsFunction; | 358 final _jsFunction; |
335 final int _parameterCount; | 359 final int _parameterCount; |
336 | 360 |
337 _MethodMirror(this._jsFunction, this._parameterCount); | 361 _MethodMirror(this._jsFunction, this._parameterCount); |
338 | 362 |
339 List<ParameterMirror> get parameters { | 363 List<ParameterMirror> get parameters { |
340 // TODO(ahe): Fill the list with parameter mirrors. | 364 // TODO(ahe): Fill the list with parameter mirrors. |
341 return new List<ParameterMirror>(_parameterCount); | 365 return new List<ParameterMirror>(_parameterCount); |
342 } | 366 } |
343 } | 367 } |
OLD | NEW |