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

Side by Side Diff: runtime/lib/mirrors_impl.dart

Issue 10916252: Implement more of the mirrors library, primarily stuff to do with types. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months 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 | « runtime/lib/mirrors.cc ('k') | runtime/tests/vm/dart/isolate_mirror_local_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // VM-specific implementation of the dart:mirrors library. 5 // VM-specific implementation of the dart:mirrors library.
6 6
7 // These values are allowed to be passed directly over the wire. 7 // These values are allowed to be passed directly over the wire.
8 bool isSimpleValue(var value) { 8 bool _isSimpleValue(var value) {
9 return (value === null || value is num || value is String || value is bool); 9 return (value == null || value is num || value is String || value is bool);
10 } 10 }
11 11
12 Map filterMap(Map old_map, bool filter(key, value)) { 12 Map _filterMap(Map old_map, bool filter(key, value)) {
13 Map new_map = new Map(); 13 Map new_map = new Map();
14 old_map.forEach((key, value) { 14 old_map.forEach((key, value) {
15 if (filter(key, value)) { 15 if (filter(key, value)) {
16 new_map[key] = value; 16 new_map[key] = value;
17 } 17 }
18 }); 18 });
19 return new_map; 19 return new_map;
20 } 20 }
21 21
22 String _makeSignatureString(TypeMirror returnType,
23 List<ParameterMirror> parameters) {
24 StringBuffer buf = new StringBuffer();
25 buf.add(returnType.qualifiedName);
26 buf.add(' (');
27 bool found_optional_param = false;
28 for (int i = 0; i < parameters.length; i++) {
29 var param = parameters[i];
30 if (param.isOptional && !found_optional_param) {
31 buf.add('[');
32 found_optional_param = true;
33 }
34 buf.add(param.type.qualifiedName);
35 if (i < (parameters.length - 1)) {
36 buf.add(', ');
37 }
38 }
39 if (found_optional_param) {
40 buf.add(']');
41 }
42 buf.add(')');
43 return buf.toString();
44 }
45
22 class _LocalMirrorSystemImpl implements MirrorSystem { 46 class _LocalMirrorSystemImpl implements MirrorSystem {
23 _LocalMirrorSystemImpl(this.libraries, this.isolate) {} 47 _LocalMirrorSystemImpl(this.libraries, this.isolate)
48 : _functionTypes = new Map<String, FunctionTypeMirror>() {}
24 49
25 final Map<String, LibraryMirror> libraries; 50 final Map<String, LibraryMirror> libraries;
26 final IsolateMirror isolate; 51 final IsolateMirror isolate;
27 52
28 TypeMirror _dynamicType = null; 53 TypeMirror _dynamicType = null;
29 54
30 TypeMirror get dynamicType() { 55 TypeMirror get dynamicType {
31 if (_dynamicType === null) { 56 if (_dynamicType == null) {
32 _dynamicType = 57 _dynamicType =
33 new _LocalClassMirrorImpl( 58 new _LocalClassMirrorImpl(
34 null, 'Dynamic', false, null, null, [], null, const {}); 59 null, 'Dynamic', false, null, null, [], null,
60 const {}, const {}, const {});
35 } 61 }
36 return _dynamicType; 62 return _dynamicType;
37 } 63 }
38 64
39 TypeMirror _voidType = null; 65 TypeMirror _voidType = null;
40 66
41 TypeMirror get voidType() { 67 TypeMirror get voidType {
42 if (_voidType === null) { 68 if (_voidType == null) {
43 _voidType = 69 _voidType =
44 new _LocalClassMirrorImpl( 70 new _LocalClassMirrorImpl(
45 null, 'void', false, null, null, [], null, const {}); 71 null, 'void', false, null, null, [], null,
72 const {}, const {}, const {});
46 } 73 }
47 return _voidType; 74 return _voidType;
48 } 75 }
49 76
77 final Map<String, FunctionTypeMirror> _functionTypes;
78 FunctionTypeMirror _lookupFunctionTypeMirror(
79 TypeMirror returnType,
80 List<ParameterMirror> parameters) {
81 var sigString = _makeSignatureString(returnType, parameters);
82 var mirror = _functionTypes[sigString];
83 if (mirror == null) {
84 mirror = new _LocalFunctionTypeMirrorImpl(null,
85 sigString,
86 returnType,
87 parameters);
88 _functionTypes[sigString] = mirror;
89 }
90 return mirror;
91 }
92
50 String toString() => "MirrorSystem for isolate '${isolate.debugName}'"; 93 String toString() => "MirrorSystem for isolate '${isolate.debugName}'";
51 } 94 }
52 95
53 abstract class _LocalMirrorImpl implements Mirror { 96 abstract class _LocalMirrorImpl implements Mirror {
54 int hashCode() { 97 int hashCode() {
55 throw new NotImplementedException('Mirror.hashCode() not yet implemented'); 98 throw new NotImplementedException('Mirror.hashCode() is not implemented');
56 } 99 }
57 100
58 // Local mirrors always return the same MirrorSystem. This field 101 // Local mirrors always return the same MirrorSystem. This field
59 // is more interesting once we implement remote mirrors. 102 // is more interesting once we implement remote mirrors.
60 MirrorSystem get mirrors() => _Mirrors.currentMirrorSystem(); 103 MirrorSystem get mirrors => _Mirrors.currentMirrorSystem();
61 } 104 }
62 105
63 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl 106 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl
64 implements IsolateMirror { 107 implements IsolateMirror {
65 _LocalIsolateMirrorImpl(this.debugName, this._rootLibrary) {} 108 _LocalIsolateMirrorImpl(this.debugName, this._rootLibrary) {}
66 109
67 final String debugName; 110 final String debugName;
68 final bool isCurrent = true; 111 final bool isCurrent = true;
69 112
70 var _rootLibrary; 113 var _rootLibrary;
71 LibraryMirror get rootLibrary() { 114 LibraryMirror get rootLibrary {
72 if (_rootLibrary is _LazyLibraryMirror) { 115 if (_rootLibrary is _LazyLibraryMirror) {
73 _rootLibrary = _rootLibrary.resolve(mirrors); 116 _rootLibrary = _rootLibrary.resolve(mirrors);
74 } 117 }
75 return _rootLibrary; 118 return _rootLibrary;
76 } 119 }
77 120
78 String toString() => "IsolateMirror on '$debugName'"; 121 String toString() => "IsolateMirror on '$debugName'";
79 } 122 }
80 123
81 // A VMReference is used to hold a reference to a VM-internal object, 124 // A VMReference is used to hold a reference to a VM-internal object,
(...skipping 11 matching lines...) Expand all
93 VMReference _reference; 136 VMReference _reference;
94 } 137 }
95 138
96 abstract class _LocalObjectMirrorImpl extends _LocalVMObjectMirrorImpl 139 abstract class _LocalObjectMirrorImpl extends _LocalVMObjectMirrorImpl
97 implements ObjectMirror { 140 implements ObjectMirror {
98 _LocalObjectMirrorImpl(ref) : super(ref) {} 141 _LocalObjectMirrorImpl(ref) : super(ref) {}
99 142
100 Future<InstanceMirror> invoke(String memberName, 143 Future<InstanceMirror> invoke(String memberName,
101 List positionalArguments, 144 List positionalArguments,
102 [Map<String,Dynamic> namedArguments]) { 145 [Map<String,Dynamic> namedArguments]) {
103 if (namedArguments !== null) { 146 if (namedArguments != null) {
104 throw new NotImplementedException('named arguments not implemented'); 147 throw new NotImplementedException(
148 'named argument support is not implemented');
105 } 149 }
106 // Walk the arguments and make sure they are legal. 150 // Walk the arguments and make sure they are legal.
107 for (int i = 0; i < positionalArguments.length; i++) { 151 for (int i = 0; i < positionalArguments.length; i++) {
108 var arg = positionalArguments[i]; 152 var arg = positionalArguments[i];
109 _validateArgument(i, arg); 153 _validateArgument(i, arg);
110 } 154 }
111 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); 155 Completer<InstanceMirror> completer = new Completer<InstanceMirror>();
112 try { 156 try {
113 completer.complete( 157 completer.complete(
114 _invoke(this, memberName, positionalArguments)); 158 _invoke(this, memberName, positionalArguments));
(...skipping 27 matching lines...) Expand all
142 return completer.future; 186 return completer.future;
143 } 187 }
144 188
145 static _validateArgument(int i, Object arg) 189 static _validateArgument(int i, Object arg)
146 { 190 {
147 if (arg is Mirror) { 191 if (arg is Mirror) {
148 if (arg is! InstanceMirror) { 192 if (arg is! InstanceMirror) {
149 throw new MirrorException( 193 throw new MirrorException(
150 'positional argument $i ($arg) was not an InstanceMirror'); 194 'positional argument $i ($arg) was not an InstanceMirror');
151 } 195 }
152 } else if (!isSimpleValue(arg)) { 196 } else if (!_isSimpleValue(arg)) {
153 throw new MirrorException( 197 throw new MirrorException(
154 'positional argument $i ($arg) was not a simple value'); 198 'positional argument $i ($arg) was not a simple value');
155 } 199 }
156 } 200 }
157 201
158 static _invoke(ref, memberName, positionalArguments) 202 static _invoke(ref, memberName, positionalArguments)
159 native 'LocalObjectMirrorImpl_invoke'; 203 native 'LocalObjectMirrorImpl_invoke';
160 204
161 static _getField(ref, fieldName) 205 static _getField(ref, fieldName)
162 native 'LocalObjectMirrorImpl_getField'; 206 native 'LocalObjectMirrorImpl_getField';
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 return buf.toString(); 257 return buf.toString();
214 } 258 }
215 259
216 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl 260 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl
217 implements InstanceMirror { 261 implements InstanceMirror {
218 _LocalInstanceMirrorImpl(ref, 262 _LocalInstanceMirrorImpl(ref,
219 this._type, 263 this._type,
220 this._reflectee) : super(ref) {} 264 this._reflectee) : super(ref) {}
221 265
222 var _type; 266 var _type;
223 ClassMirror get type() { 267 ClassMirror get type {
224 if (_type is _LazyClassMirror) { 268 if (_type is! Mirror) {
225 _type = _type.resolve(mirrors); 269 _type = _type.resolve(mirrors);
226 } 270 }
227 return _type; 271 return _type;
228 } 272 }
229 273
230 // LocalInstanceMirrors always reflect local instances 274 // LocalInstanceMirrors always reflect local instances
231 bool hasReflectee = true; 275 bool hasReflectee = true;
232 276
233 var _reflectee; 277 var _reflectee;
234 get reflectee() => _reflectee; 278 get reflectee => _reflectee;
235 279
236 String toString() { 280 String toString() {
237 if (isSimpleValue(_reflectee)) { 281 if (_isSimpleValue(_reflectee)) {
238 if (_reflectee is String) { 282 if (_reflectee is String) {
239 return "InstanceMirror on <'${_dartEscape(_reflectee)}'>"; 283 return "InstanceMirror on <'${_dartEscape(_reflectee)}'>";
240 } else { 284 } else {
241 return "InstanceMirror on <$_reflectee>"; 285 return "InstanceMirror on <$_reflectee>";
242 } 286 }
243 } else { 287 } else {
244 return "InstanceMirror on instance of '${type.simpleName}'"; 288 return "InstanceMirror on instance of '${type.simpleName}'";
245 } 289 }
246 } 290 }
247 } 291 }
248 292
249 class _LocalClosureMirrorImpl extends _LocalInstanceMirrorImpl 293 class _LocalClosureMirrorImpl extends _LocalInstanceMirrorImpl
250 implements ClosureMirror { 294 implements ClosureMirror {
251 _LocalClosureMirrorImpl(ref, 295 _LocalClosureMirrorImpl(ref,
252 klass, 296 type,
253 reflectee) : super(ref, klass, reflectee) {} 297 reflectee,
298 this.function) : super(ref, type, reflectee) {}
254 299
255 MethodMirror _function; 300 final MethodMirror function;
256 MethodMirror function() => _function;
257 301
258 String get source() { 302 String get source {
259 throw new NotImplementedException('ClosureMirror.source not implemented'); 303 throw new NotImplementedException(
304 'ClosureMirror.source is not implemented');
260 } 305 }
261 306
262 Future<ObjectMirror> apply(List<Object> positionalArguments, 307 Future<InstanceMirror> apply(List<Object> positionalArguments,
263 [Map<String,Object> namedArguments]) { 308 [Map<String,Object> namedArguments]) {
264 if (namedArguments !== null) { 309 if (namedArguments != null) {
265 throw new NotImplementedException('named arguments not implemented'); 310 throw new NotImplementedException(
311 'named argument support is not implemented');
266 } 312 }
267 // Walk the arguments and make sure they are legal. 313 // Walk the arguments and make sure they are legal.
268 for (int i = 0; i < positionalArguments.length; i++) { 314 for (int i = 0; i < positionalArguments.length; i++) {
269 var arg = positionalArguments[i]; 315 var arg = positionalArguments[i];
270 _LocalObjectMirrorImpl._validateArgument(i, arg); 316 _LocalObjectMirrorImpl._validateArgument(i, arg);
271 } 317 }
272 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); 318 Completer<InstanceMirror> completer = new Completer<InstanceMirror>();
273 try { 319 try {
274 completer.complete( 320 completer.complete(
275 _apply(this, positionalArguments)); 321 _apply(this, positionalArguments));
276 } catch (exception) { 322 } catch (exception) {
277 completer.completeException(exception); 323 completer.completeException(exception);
278 } 324 }
279 return completer.future; 325 return completer.future;
280 } 326 }
281 327
282 Future<ObjectMirror> findInContext(String name) { 328 Future<InstanceMirror> findInContext(String name) {
283 throw new NotImplementedException( 329 throw new NotImplementedException(
284 'ClosureMirror.findInContext() not implemented'); 330 'ClosureMirror.findInContext() is not implemented');
285 } 331 }
286 332
287 static _apply(ref, positionalArguments) 333 static _apply(ref, positionalArguments)
288 native 'LocalClosureMirrorImpl_apply'; 334 native 'LocalClosureMirrorImpl_apply';
289 } 335 }
290 336
291 class _LazyClassMirror { 337 class _LazyTypeMirror {
292 _LazyClassMirror(this.libraryName, this.interfaceName) {} 338 _LazyTypeMirror(this.libraryName, this.typeName) {}
293 339
294 ClassMirror resolve(MirrorSystem mirrors) { 340 TypeMirror resolve(MirrorSystem mirrors) {
295 if (libraryName === null) { 341 if (libraryName == null) {
296 if (interfaceName == 'Dynamic') { 342 if (typeName == 'Dynamic') {
297 return mirrors.dynamicType(); 343 return mirrors.dynamicType;
298 } else if (interfaceName == 'void') { 344 } else if (typeName == 'void') {
299 return mirrors.voidType(); 345 return mirrors.voidType;
300 } else { 346 } else {
301 throw new NotImplementedException( 347 throw new NotImplementedException(
302 "Mirror for type '$interfaceName' not implemented"); 348 "Mirror for type '$typeName' is not implemented");
303 } 349 }
304 } 350 }
305 return mirrors.libraries[libraryName].members[interfaceName]; 351 var resolved = mirrors.libraries[libraryName].members[typeName];
352 if (resolved == null) {
353 throw new NotImplementedException(
354 "Mirror for type '$typeName' is not implemented");
355 }
356 return resolved;
306 } 357 }
307 358
308 final String libraryName; 359 final String libraryName;
309 final String interfaceName; 360 final String typeName;
310 } 361 }
311 362
312 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl 363 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl
313 implements ClassMirror { 364 implements ClassMirror {
314 _LocalClassMirrorImpl(ref, 365 _LocalClassMirrorImpl(ref,
315 this.simpleName, 366 this.simpleName,
316 this.isClass, 367 this.isClass,
317 this._owner, 368 this._owner,
318 this._superclass, 369 this._superclass,
319 this._superinterfaces, 370 this._superinterfaces,
320 this._defaultFactory, 371 this._defaultFactory,
321 this.members) : super(ref) {} 372 this.members,
373 this.constructors,
374 this.typeVariables) : super(ref) {}
322 375
323 final String simpleName; 376 final String simpleName;
324 377
325 String _qualifiedName = null; 378 String _qualifiedName = null;
326 String get qualifiedName() { 379 String get qualifiedName {
327 if (_qualifiedName === null) { 380 if (_owner != null) {
328 _qualifiedName = '${owner.qualifiedName}.${simpleName}'; 381 if (_qualifiedName == null) {
382 _qualifiedName = '${owner.qualifiedName}.${simpleName}';
383 }
384 } else {
385 // The owner of a ClassMirror is null in certain odd cases, like
386 // 'void', 'Dynamic' and function type mirrors.
387 _qualifiedName = simpleName;
329 } 388 }
330 return _qualifiedName; 389 return _qualifiedName;
331 } 390 }
332 391
333 var _owner; 392 var _owner;
334 DeclarationMirror get owner() { 393 DeclarationMirror get owner {
335 if (_owner is! Mirror) { 394 if (_owner != null && _owner is! Mirror) {
336 _owner = _owner.resolve(mirrors); 395 _owner = _owner.resolve(mirrors);
337 } 396 }
338 return _owner; 397 return _owner;
339 } 398 }
340 399
341 bool get isPrivate() => simpleName.startsWith('_'); 400 bool get isPrivate => simpleName.startsWith('_');
342 401
343 final bool isTopLevel = true; 402 final bool isTopLevel = true;
344 403
345 SourceLocation get location() { 404 SourceLocation get location {
346 throw new NotImplementedException( 405 throw new NotImplementedException(
347 'ClassMirror.location not yet implemented'); 406 'ClassMirror.location is not implemented');
348 } 407 }
349 408
350 final bool isClass; 409 final bool isClass;
351 410
352 var _superclass; 411 var _superclass;
353 ClassMirror get superclass() { 412 ClassMirror get superclass {
354 if (_superclass is _LazyClassMirror) { 413 if (_superclass is! Mirror) {
355 _superclass = _superclass.resolve(mirrors); 414 _superclass = _superclass.resolve(mirrors);
356 } 415 }
357 return _superclass; 416 return _superclass;
358 } 417 }
359 418
360 var _superinterfaces; 419 var _superinterfaces;
361 List<ClassMirror> get superinterfaces() { 420 List<ClassMirror> get superinterfaces {
362 if (_superinterfaces.length > 0 && 421 if (_superinterfaces.length > 0 &&
363 _superinterfaces[0] is _LazyClassMirror) { 422 _superinterfaces[0] is! Mirror) {
364 List<ClassMirror> resolved = new List<ClassMirror>(); 423 List<ClassMirror> resolved = new List<ClassMirror>();
365 for (int i = 0; i < _superinterfaces.length; i++) { 424 for (int i = 0; i < _superinterfaces.length; i++) {
366 resolved.add(_superinterfaces[i].resolve(mirrors)); 425 resolved.add(_superinterfaces[i].resolve(mirrors));
367 } 426 }
368 _superinterfaces = resolved; 427 _superinterfaces = resolved;
369 } 428 }
370 return _superinterfaces; 429 return _superinterfaces;
371 } 430 }
372 431
373 var _defaultFactory; 432 var _defaultFactory;
374 ClassMirror get defaultFactory() { 433 ClassMirror get defaultFactory {
375 if (_defaultFactory is _LazyClassMirror) { 434 if (_defaultFactory != null && _defaultFactory is! Mirror) {
376 _defaultFactory = _defaultFactory.resolve(mirrors); 435 _defaultFactory = _defaultFactory.resolve(mirrors);
377 } 436 }
378 return _defaultFactory; 437 return _defaultFactory;
379 } 438 }
380 439
381 final Map<String, Mirror> members; 440 final Map<String, Mirror> members;
382 441
383 Map<String, MethodMirror> _methods = null; 442 Map<String, MethodMirror> _methods = null;
384 Map<String, MethodMirror> _constructors = null;
385 Map<String, MethodMirror> _getters = null; 443 Map<String, MethodMirror> _getters = null;
386 Map<String, MethodMirror> _setters = null; 444 Map<String, MethodMirror> _setters = null;
387 Map<String, VariableMirror> _variables = null; 445 Map<String, VariableMirror> _variables = null;
388 446
389 Map<String, MethodMirror> get methods() { 447 Map<String, MethodMirror> get methods {
390 if (_methods == null) { 448 if (_methods == null) {
391 _methods = filterMap(members, 449 _methods = _filterMap(
392 (key, value) => (value is MethodMirror)); 450 members,
451 (key, value) => (value is MethodMirror && value.isRegularMethod));
393 } 452 }
394 return _methods; 453 return _methods;
395 } 454 }
396 455
397 Map<String, MethodMirror> get constructors() { 456 Map<String, MethodMirror> get getters {
398 if (_constructors == null) {
399 _constructors = filterMap(methods,
400 (key, value) => (value.isConstructor));
401 }
402 return _constructors;
403 }
404
405 Map<String, MethodMirror> get getters() {
406 if (_getters == null) { 457 if (_getters == null) {
407 _getters = filterMap(methods, 458 _getters = _filterMap(
408 (key, value) => (value.isGetter)); 459 members,
460 (key, value) => (value is MethodMirror && value.isGetter));
409 } 461 }
410 return _getters; 462 return _getters;
411 } 463 }
412 464
413 Map<String, MethodMirror> get setters() { 465 Map<String, MethodMirror> get setters {
414 if (_setters == null) { 466 if (_setters == null) {
415 _setters = filterMap(methods, 467 _setters = _filterMap(
416 (key, value) => (value.isSetter)); 468 members,
469 (key, value) => (value is MethodMirror && value.isSetter));
417 } 470 }
418 return _setters; 471 return _setters;
419 } 472 }
420 473
421 Map<String, VariableMirror> get variables() { 474 Map<String, VariableMirror> get variables {
422 if (_variables == null) { 475 if (_variables == null) {
423 _variables = filterMap(members, 476 _variables = _filterMap(
424 (key, value) => (value is VariableMirror)); 477 members,
478 (key, value) => (value is VariableMirror));
425 } 479 }
426 return _variables; 480 return _variables;
427 } 481 }
428 482
429 List<TypeVariableMirror> get typeVariables() { 483 Map<String, MethodMirror> constructors;
484 Map<String, TypeVariableMirror> typeVariables;
485
486 Map<String, TypeMirror> get typeArguments {
430 throw new NotImplementedException( 487 throw new NotImplementedException(
431 'ClassMirror.typeVariables not yet implemented'); 488 'ClassMirror.typeArguments is not implemented');
432 } 489 }
433 490
434 List<TypeMirror> get typeArguments() { 491 bool get isOriginalDeclaration {
435 throw new NotImplementedException( 492 throw new NotImplementedException(
436 'ClassMirror.typeArguments not yet implemented'); 493 'ClassMirror.isOriginalDeclaration is not implemented');
437 } 494 }
438 495
439 bool isGenericDeclaration() { 496 ClassMirror get genericDeclaration {
440 throw new NotImplementedException( 497 throw new NotImplementedException(
441 'ClassMirror.isGenericDeclaration not yet implemented'); 498 'ClassMirror.originalDeclaration is not implemented');
442 }
443
444 ClassMirror genericDeclaration() {
445 throw new NotImplementedException(
446 'ClassMirror.genericDeclaration not yet implemented');
447 } 499 }
448 500
449 String toString() => "ClassMirror on '$simpleName'"; 501 String toString() => "ClassMirror on '$simpleName'";
450 502
451 Future<InstanceMirror> newInstance(String constructorName, 503 Future<InstanceMirror> newInstance(String constructorName,
452 List positionalArguments, 504 List positionalArguments,
453 [Map<String,Dynamic> namedArguments]) { 505 [Map<String,Dynamic> namedArguments]) {
454 if (namedArguments !== null) { 506 if (namedArguments != null) {
455 throw new NotImplementedException('named arguments not implemented'); 507 throw new NotImplementedException(
508 'named argument support is not implemented');
456 } 509 }
457 // Walk the arguments and make sure they are legal. 510 // Walk the arguments and make sure they are legal.
458 for (int i = 0; i < positionalArguments.length; i++) { 511 for (int i = 0; i < positionalArguments.length; i++) {
459 var arg = positionalArguments[i]; 512 var arg = positionalArguments[i];
460 _LocalObjectMirrorImpl._validateArgument(i, arg); 513 _LocalObjectMirrorImpl._validateArgument(i, arg);
461 } 514 }
462 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); 515 Completer<InstanceMirror> completer = new Completer<InstanceMirror>();
463 try { 516 try {
464 completer.complete( 517 completer.complete(
465 _invokeConstructor(this, constructorName, positionalArguments)); 518 _invokeConstructor(this, constructorName, positionalArguments));
466 } catch (exception) { 519 } catch (exception) {
467 completer.completeException(exception); 520 completer.completeException(exception);
468 } 521 }
469 return completer.future; 522 return completer.future;
470 } 523 }
471 524
472 static _invokeConstructor(ref, constructorName, positionalArguments) 525 static _invokeConstructor(ref, constructorName, positionalArguments)
473 native 'LocalClassMirrorImpl_invokeConstructor'; 526 native 'LocalClassMirrorImpl_invokeConstructor';
474 } 527 }
475 528
529 class _LazyFunctionTypeMirror {
530 _LazyFunctionTypeMirror(this.returnType, this.parameters) {}
531
532 ClassMirror resolve(MirrorSystem mirrors) {
533 return mirrors._lookupFunctionTypeMirror(returnType.resolve(mirrors),
534 parameters);
535 }
536
537 final returnType;
538 final List<ParameterMirror> parameters;
539 }
540
541 class _LocalFunctionTypeMirrorImpl extends _LocalClassMirrorImpl
542 implements FunctionTypeMirror {
543 _LocalFunctionTypeMirrorImpl(ref,
544 simpleName,
545 this._returnType,
546 this.parameters)
547 : super(ref,
548 simpleName,
549 true,
550 null,
551 new _LazyTypeMirror('dart:core', 'Object'),
552 [ new _LazyTypeMirror('dart:core', 'Function') ],
553 null,
554 const {},
555 const {},
556 const {});
557
558 var _returnType;
559 TypeMirror get returnType {
560 if (_returnType is! Mirror) {
561 _returnType = _returnType.resolve(mirrors);
562 }
563 return _returnType;
564 }
565
566 final List<ParameterMirror> parameters;
567
568 String toString() => "FunctionTypeMirror on '$simpleName'";
569 }
570
571
572 class _LazyTypeVariableMirror {
573 _LazyTypeVariableMirror(this._variableName, this._owner) {}
574
575 TypeVariableMirror resolve(MirrorSystem mirrors) {
576 ClassMirror owner = _owner.resolve(mirrors);
577 return owner.typeVariables[_variableName];
578 }
579
580 final String _variableName;
581 final _LazyTypeMirror _owner;
582 }
583
584 class _LocalTypeVariableMirrorImpl extends _LocalMirrorImpl
585 implements TypeVariableMirror {
586 _LocalTypeVariableMirrorImpl(this.simpleName,
587 this._owner,
588 this._upperBound) {}
589 final String simpleName;
590
591 String _qualifiedName = null;
592 String get qualifiedName {
593 if (_qualifiedName == null) {
594 _qualifiedName = '${owner.qualifiedName}.${simpleName}';
595 }
596 return _qualifiedName;
597 }
598
599 var _owner;
600 DeclarationMirror get owner {
601 if (_owner is! Mirror) {
602 _owner = _owner.resolve(mirrors);
603 }
604 return _owner;
605 }
606
607 bool get isPrivate => false;
608
609 final bool isTopLevel = false;
610
611 SourceLocation get location {
612 throw new NotImplementedException(
613 'TypeVariableMirror.location is not implemented');
614 }
615
616 var _upperBound;
617 TypeMirror get upperBound {
618 if (_upperBound is! Mirror) {
619 _upperBound = _upperBound.resolve(mirrors);
620 }
621 return _upperBound;
622 }
623
624 String toString() => "TypeVariableMirror on '$simpleName'";
625 }
626
627
628 class _LocalTypedefMirrorImpl extends _LocalMirrorImpl
629 implements TypedefMirror {
630 _LocalTypedefMirrorImpl(this.simpleName,
631 this._owner,
632 this._referent) {}
633 final String simpleName;
634
635 String _qualifiedName = null;
636 String get qualifiedName {
637 if (_qualifiedName == null) {
638 _qualifiedName = '${owner.qualifiedName}.${simpleName}';
639 }
640 return _qualifiedName;
641 }
642
643 var _owner;
644 DeclarationMirror get owner {
645 if (_owner is! Mirror) {
646 _owner = _owner.resolve(mirrors);
647 }
648 return _owner;
649 }
650
651 bool get isPrivate => false;
652
653 final bool isTopLevel = true;
654
655 SourceLocation get location {
656 throw new NotImplementedException(
657 'TypedefMirror.location is not implemented');
658 }
659
660 var _referent;
661 TypeMirror get referent {
662 if (_referent is! Mirror) {
663 _referent = _referent.resolve(mirrors);
664 }
665 return _referent;
666 }
667
668 String toString() => "TypedefMirror on '$simpleName'";
669 }
670
671
476 class _LazyLibraryMirror { 672 class _LazyLibraryMirror {
477 _LazyLibraryMirror(this.libraryName) {} 673 _LazyLibraryMirror(this.libraryName) {}
478 674
479 LibraryMirror resolve(MirrorSystem mirrors) { 675 LibraryMirror resolve(MirrorSystem mirrors) {
480 return mirrors.libraries[libraryName]; 676 return mirrors.libraries[libraryName];
481 } 677 }
482 678
483 final String libraryName; 679 final String libraryName;
484 } 680 }
485 681
486 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl 682 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl
487 implements LibraryMirror { 683 implements LibraryMirror {
488 _LocalLibraryMirrorImpl(ref, 684 _LocalLibraryMirrorImpl(ref,
489 this.simpleName, 685 this.simpleName,
490 this.url, 686 this.url,
491 this.members) : super(ref) {} 687 this.members) : super(ref) {}
492 688
493 final String simpleName; 689 final String simpleName;
494 690
495 // The simple name and the qualified name are the same for a library. 691 // The simple name and the qualified name are the same for a library.
496 String get qualifiedName() => simpleName; 692 String get qualifiedName => simpleName;
497 693
498 // Always null for libraries. 694 // Always null for libraries.
499 final DeclarationMirror owner = null; 695 final DeclarationMirror owner = null;
500 696
501 // Always false for libraries. 697 // Always false for libraries.
502 final bool isPrivate = false; 698 final bool isPrivate = false;
503 699
504 // Always false for libraries. 700 // Always false for libraries.
505 final bool isTopLevel = false; 701 final bool isTopLevel = false;
506 702
507 SourceLocation get location() { 703 SourceLocation get location {
508 throw new NotImplementedException( 704 throw new NotImplementedException(
509 'LibraryMirror.location not yet implemented'); 705 'LibraryMirror.location is not implemented');
510 } 706 }
511 707
512 final String url; 708 final String url;
513 final Map<String, Mirror> members; 709 final Map<String, Mirror> members;
514 710
515 Map<String, ClassMirror> _classes = null; 711 Map<String, ClassMirror> _classes = null;
516 Map<String, MethodMirror> _functions = null; 712 Map<String, MethodMirror> _functions = null;
517 Map<String, MethodMirror> _getters = null; 713 Map<String, MethodMirror> _getters = null;
518 Map<String, MethodMirror> _setters = null; 714 Map<String, MethodMirror> _setters = null;
519 Map<String, VariableMirror> _variables = null; 715 Map<String, VariableMirror> _variables = null;
520 716
521 Map<String, ClassMirror> get classes() { 717 Map<String, ClassMirror> get classes {
522 if (_classes == null) { 718 if (_classes == null) {
523 _classes = filterMap(members, 719 _classes = _filterMap(members,
524 (key, value) => (value is ClassMirror)); 720 (key, value) => (value is ClassMirror));
525 } 721 }
526 return _classes; 722 return _classes;
527 } 723 }
528 724
529 Map<String, MethodMirror> get functions() { 725 Map<String, MethodMirror> get functions {
530 if (_functions == null) { 726 if (_functions == null) {
531 _functions = filterMap(members, 727 _functions = _filterMap(members,
532 (key, value) => (value is MethodMirror)); 728 (key, value) => (value is MethodMirror));
533 } 729 }
534 return _functions; 730 return _functions;
535 } 731 }
536 732
537 Map<String, MethodMirror> get getters() { 733 Map<String, MethodMirror> get getters {
538 if (_getters == null) { 734 if (_getters == null) {
539 _getters = filterMap(functions, 735 _getters = _filterMap(functions,
540 (key, value) => (value.isGetter)); 736 (key, value) => (value.isGetter));
541 } 737 }
542 return _getters; 738 return _getters;
543 } 739 }
544 740
545 Map<String, MethodMirror> get setters() { 741 Map<String, MethodMirror> get setters {
546 if (_setters == null) { 742 if (_setters == null) {
547 _setters = filterMap(functions, 743 _setters = _filterMap(functions,
548 (key, value) => (value.isSetter)); 744 (key, value) => (value.isSetter));
549 } 745 }
550 return _setters; 746 return _setters;
551 } 747 }
552 748
553 Map<String, VariableMirror> get variables() { 749 Map<String, VariableMirror> get variables {
554 if (_variables == null) { 750 if (_variables == null) {
555 _variables = filterMap(members, 751 _variables = _filterMap(members,
556 (key, value) => (value is VariableMirror)); 752 (key, value) => (value is VariableMirror));
557 } 753 }
558 return _variables; 754 return _variables;
559 } 755 }
560 756
561 String toString() => "LibraryMirror on '$simpleName'"; 757 String toString() => "LibraryMirror on '$simpleName'";
562 } 758 }
563 759
564 class _LocalMethodMirrorImpl extends _LocalMirrorImpl 760 class _LocalMethodMirrorImpl extends _LocalMirrorImpl
565 implements MethodMirror { 761 implements MethodMirror {
566 _LocalMethodMirrorImpl(this.simpleName, 762 _LocalMethodMirrorImpl(this.simpleName,
567 this._owner, 763 this._owner,
568 this.parameters, 764 this.parameters,
765 this._returnType,
569 this.isStatic, 766 this.isStatic,
570 this.isAbstract, 767 this.isAbstract,
571 this.isGetter, 768 this.isGetter,
572 this.isSetter, 769 this.isSetter,
573 this.isConstructor, 770 this.isConstructor,
574 this.isConstConstructor, 771 this.isConstConstructor,
575 this.isGenerativeConstructor, 772 this.isGenerativeConstructor,
576 this.isRedirectingConstructor, 773 this.isRedirectingConstructor,
577 this.isFactoryConstructor) {} 774 this.isFactoryConstructor) {}
578 775
579 final String simpleName; 776 final String simpleName;
580 777
581 String _qualifiedName = null; 778 String _qualifiedName = null;
582 String get qualifiedName() { 779 String get qualifiedName {
583 if (_qualifiedName === null) { 780 if (_qualifiedName == null) {
584 _qualifiedName = '${owner.qualifiedName}.${simpleName}'; 781 _qualifiedName = '${owner.qualifiedName}.${simpleName}';
585 } 782 }
586 return _qualifiedName; 783 return _qualifiedName;
587 } 784 }
588 785
589 var _owner; 786 var _owner;
590 DeclarationMirror get owner() { 787 DeclarationMirror get owner {
591 if (_owner is! Mirror) { 788 if (_owner is! Mirror) {
592 _owner = _owner.resolve(mirrors); 789 _owner = _owner.resolve(mirrors);
593 } 790 }
594 return _owner; 791 return _owner;
595 } 792 }
596 793
597 bool get isPrivate() { 794 bool get isPrivate {
598 return simpleName.startsWith('_') || constructorName.startsWith('_'); 795 return simpleName.startsWith('_') || constructorName.startsWith('_');
599 } 796 }
600 797
601 bool get isTopLevel() => owner is LibraryMirror; 798 bool get isTopLevel => owner is LibraryMirror;
602 799
603 SourceLocation get location() { 800 SourceLocation get location {
604 throw new NotImplementedException( 801 throw new NotImplementedException(
605 'MethodMirror.location not yet implemented'); 802 'MethodMirror.location is not implemented');
606 } 803 }
607 804
608 TypeMirror get returnType() { 805 var _returnType;
609 throw new NotImplementedException( 806 TypeMirror get returnType {
610 'MethodMirror.returnType not yet implemented'); 807 if (_returnType is! Mirror) {
808 _returnType = _returnType.resolve(mirrors);
809 }
810 return _returnType;
611 } 811 }
612 812
613 final List<ParameterMirror> parameters; 813 final List<ParameterMirror> parameters;
614 814
615 final bool isStatic; 815 final bool isStatic;
616 final bool isAbstract; 816 final bool isAbstract;
617 817
618 bool get isRegularMethod() => !isGetter && !isSetter && !isConstructor; 818 bool get isRegularMethod => !isGetter && !isSetter && !isConstructor;
619 819
620 TypeMirror get isOperator() { 820 TypeMirror get isOperator {
621 throw new NotImplementedException( 821 throw new NotImplementedException(
622 'MethodMirror.isOperator not yet implemented'); 822 'MethodMirror.isOperator is not implemented');
623 } 823 }
624 824
625 final bool isGetter; 825 final bool isGetter;
626 final bool isSetter; 826 final bool isSetter;
627 final bool isConstructor; 827 final bool isConstructor;
628 828
629 var _constructorName = null; 829 var _constructorName = null;
630 String get constructorName() { 830 String get constructorName {
631 if (_constructorName === null) { 831 if (_constructorName == null) {
632 if (!isConstructor) { 832 if (!isConstructor) {
633 _constructorName = ''; 833 _constructorName = '';
634 } else { 834 } else {
635 var parts = simpleName.split('.'); 835 var parts = simpleName.split('.');
636 if (parts.length > 2) { 836 if (parts.length > 2) {
637 throw new MirrorException( 837 throw new MirrorException(
638 'Internal error in MethodMirror.constructorName: ' 838 'Internal error in MethodMirror.constructorName: '
639 'malformed name <$simpleName>'); 839 'malformed name <$simpleName>');
640 } else if (parts.length == 2) { 840 } else if (parts.length == 2) {
641 _constructorName = parts[1]; 841 _constructorName = parts[1];
(...skipping 10 matching lines...) Expand all
652 final bool isRedirectingConstructor; 852 final bool isRedirectingConstructor;
653 final bool isFactoryConstructor; 853 final bool isFactoryConstructor;
654 854
655 String toString() => "MethodMirror on '$simpleName'"; 855 String toString() => "MethodMirror on '$simpleName'";
656 } 856 }
657 857
658 class _LocalVariableMirrorImpl extends _LocalMirrorImpl 858 class _LocalVariableMirrorImpl extends _LocalMirrorImpl
659 implements VariableMirror { 859 implements VariableMirror {
660 _LocalVariableMirrorImpl(this.simpleName, 860 _LocalVariableMirrorImpl(this.simpleName,
661 this._owner, 861 this._owner,
862 this._type,
662 this.isStatic, 863 this.isStatic,
663 this.isFinal) {} 864 this.isFinal) {}
664 865
665 final String simpleName; 866 final String simpleName;
666 867
667 String _qualifiedName = null; 868 String _qualifiedName = null;
668 String get qualifiedName() { 869 String get qualifiedName {
669 if (_qualifiedName === null) { 870 if (_qualifiedName == null) {
670 _qualifiedName = '${owner.qualifiedName}.${simpleName}'; 871 _qualifiedName = '${owner.qualifiedName}.${simpleName}';
671 } 872 }
672 return _qualifiedName; 873 return _qualifiedName;
673 } 874 }
674 875
675 var _owner; 876 var _owner;
676 DeclarationMirror get owner() { 877 DeclarationMirror get owner {
677 if (_owner is! Mirror) { 878 if (_owner is! Mirror) {
678 _owner = _owner.resolve(mirrors); 879 _owner = _owner.resolve(mirrors);
679 } 880 }
680 return _owner; 881 return _owner;
681 } 882 }
682 883
683 bool get isPrivate() { 884 bool get isPrivate {
684 return simpleName.startsWith('_'); 885 return simpleName.startsWith('_');
685 } 886 }
686 887
687 bool get isTopLevel() { 888 bool get isTopLevel {
688 return owner is LibraryMirror; 889 return owner is LibraryMirror;
689 } 890 }
690 891
691 SourceLocation get location() { 892 SourceLocation get location {
692 throw new NotImplementedException( 893 throw new NotImplementedException(
693 'VariableMirror.location not yet implemented'); 894 'VariableMirror.location is not implemented');
694 } 895 }
695 896
696 TypeMirror get type() { 897 var _type;
697 throw new NotImplementedException( 898 TypeMirror get type {
698 'VariableMirror.type not yet implemented'); 899 if (_type is! Mirror) {
900 _type = _type.resolve(mirrors);
901 }
902 return _type;
699 } 903 }
700 904
701 final bool isStatic; 905 final bool isStatic;
702 final bool isFinal; 906 final bool isFinal;
703 907
704 String toString() => "VariableMirror on '$simpleName'"; 908 String toString() => "VariableMirror on '$simpleName'";
705 } 909 }
706 910
707 class _LocalParameterMirrorImpl extends _LocalVariableMirrorImpl 911 class _LocalParameterMirrorImpl extends _LocalVariableMirrorImpl
708 implements ParameterMirror { 912 implements ParameterMirror {
709 // TODO(rmacnak): Fill these mirrors will real information 913 _LocalParameterMirrorImpl(type, this.isOptional)
710 _LocalParameterMirrorImpl(this.isOptional) 914 : super('<TODO:unnamed>', null, type, false, false) {}
711 : super(null, null, false, false) {}
712 915
713 final bool isOptional; 916 final bool isOptional;
714 917
715 TypeMirror get type() { 918 String get defaultValue {
716 throw new NotImplementedException( 919 throw new NotImplementedException(
717 'ParameterMirror.type not yet implemented'); 920 'ParameterMirror.defaultValue is not implemented');
718 } 921 }
719 922
720 String get defaultValue() { 923 bool get hasDefaultValue {
721 throw new NotImplementedException( 924 throw new NotImplementedException(
722 'ParameterMirror.defaultValue not yet implemented'); 925 'ParameterMirror.hasDefaultValue is not implemented');
723 }
724
725 bool get hasDefaultValue() {
726 throw new NotImplementedException(
727 'ParameterMirror.hasDefaultValue not yet implemented');
728 } 926 }
729 } 927 }
730 928
731 class _Mirrors { 929 class _Mirrors {
732 // Does a port refer to our local isolate? 930 // Does a port refer to our local isolate?
733 static bool isLocalPort(SendPort port) native 'Mirrors_isLocalPort'; 931 static bool isLocalPort(SendPort port) native 'Mirrors_isLocalPort';
734 932
735 static MirrorSystem _currentMirrorSystem = null; 933 static MirrorSystem _currentMirrorSystem = null;
736 934
737 // Creates a new local MirrorSystem. 935 // Creates a new local MirrorSystem.
738 static MirrorSystem makeLocalMirrorSystem() 936 static MirrorSystem makeLocalMirrorSystem()
739 native 'Mirrors_makeLocalMirrorSystem'; 937 native 'Mirrors_makeLocalMirrorSystem';
740 938
741 // The MirrorSystem for the current isolate. 939 // The MirrorSystem for the current isolate.
742 static MirrorSystem currentMirrorSystem() { 940 static MirrorSystem currentMirrorSystem() {
743 if (_currentMirrorSystem === null) { 941 if (_currentMirrorSystem == null) {
744 _currentMirrorSystem = makeLocalMirrorSystem(); 942 _currentMirrorSystem = makeLocalMirrorSystem();
745 } 943 }
746 return _currentMirrorSystem; 944 return _currentMirrorSystem;
747 } 945 }
748 946
749 static Future<MirrorSystem> mirrorSystemOf(SendPort port) { 947 static Future<MirrorSystem> mirrorSystemOf(SendPort port) {
750 Completer<MirrorSystem> completer = new Completer<MirrorSystem>(); 948 Completer<MirrorSystem> completer = new Completer<MirrorSystem>();
751 if (isLocalPort(port)) { 949 if (isLocalPort(port)) {
752 // Make a local mirror system. 950 // Make a local mirror system.
753 try { 951 try {
754 completer.complete(currentMirrorSystem()); 952 completer.complete(currentMirrorSystem());
755 } catch (exception) { 953 } catch (exception) {
756 completer.completeException(exception); 954 completer.completeException(exception);
757 } 955 }
758 } else { 956 } else {
759 // Make a remote mirror system 957 // Make a remote mirror system
760 throw new NotImplementedException('Remote mirrors not yet implemented'); 958 throw new NotImplementedException(
959 'Remote mirror support is not implemented');
761 } 960 }
762 return completer.future; 961 return completer.future;
763 } 962 }
764 963
765 // Creates a new local InstanceMirror 964 // Creates a new local InstanceMirror
766 static InstanceMirror makeLocalInstanceMirror(Object reflectee) 965 static InstanceMirror makeLocalInstanceMirror(Object reflectee)
767 native 'Mirrors_makeLocalInstanceMirror'; 966 native 'Mirrors_makeLocalInstanceMirror';
768 967
769 // Creates a new local mirror for some Object. 968 // Creates a new local mirror for some Object.
770 static InstanceMirror reflect(Object reflectee) { 969 static InstanceMirror reflect(Object reflectee) {
771 return makeLocalInstanceMirror(reflectee); 970 return makeLocalInstanceMirror(reflectee);
772 } 971 }
773 } 972 }
OLDNEW
« no previous file with comments | « runtime/lib/mirrors.cc ('k') | runtime/tests/vm/dart/isolate_mirror_local_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698