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

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

Issue 10854197: A round of edits to make mirrors.dart more like our current working (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 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
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 class _LocalMirrorSystemImpl implements MirrorSystem { 22 class _LocalMirrorSystemImpl implements MirrorSystem {
23 _LocalMirrorSystemImpl(this.rootLibrary, this._libraries, this.isolate) {} 23 _LocalMirrorSystemImpl(this.libraries, this.isolate) {}
24 24
25 final LibraryMirror rootLibrary; 25 final Map<String, LibraryMirror> libraries;
26 final Map<String, LibraryMirror> _libraries;
27 final IsolateMirror isolate; 26 final IsolateMirror isolate;
28 27
29 Map<String, LibraryMirror> libraries() { return _libraries; } 28 TypeMirror _dynamicType = null;
30 29
31 Mirror mirrorOf(Object reflectee) { 30 TypeMirror get dynamicType() {
32 return _Mirrors.mirrorOf(reflectee); 31 if (_dynamicType === null) {
32 _dynamicType =
33 new _LocalClassMirrorImpl(
34 null, 'Dynamic', false, null, null, [], null, const {});
35 }
36 return _dynamicType;
33 } 37 }
34 38
35 InterfaceMirror _sharedDynamic = null; 39 TypeMirror _voidType = null;
36 40
37 InterfaceMirror _dynamicMirror() { 41 TypeMirror get voidType() {
38 if (_sharedDynamic === null) { 42 if (_voidType === null) {
39 _sharedDynamic = 43 _voidType =
40 new _LocalInterfaceMirrorImpl( 44 new _LocalClassMirrorImpl(
41 null, 'Dynamic', false, null, null, [], null, const {});
42 }
43 return _sharedDynamic;
44 }
45
46 InterfaceMirror _sharedVoid = null;
47
48 InterfaceMirror _voidMirror() {
49 if (_sharedVoid === null) {
50 _sharedVoid =
51 new _LocalInterfaceMirrorImpl(
52 null, 'void', false, null, null, [], null, const {}); 45 null, 'void', false, null, null, [], null, const {});
53 } 46 }
54 return _sharedVoid; 47 return _voidType;
55 } 48 }
56 49
57 String toString() { 50 String toString() {
58 return "MirrorSystem for isolate '$debugName'"; 51 return "MirrorSystem for isolate '$debugName'";
59 } 52 }
60 } 53 }
61 54
62 abstract class _LocalMirrorImpl implements Mirror { 55 abstract class _LocalMirrorImpl implements Mirror {
56 int hashCode() {
57 throw new NotImplementedException('Mirror.hashCode() not yet implemented');
58 }
59
63 // Local mirrors always return the same MirrorSystem. This field 60 // Local mirrors always return the same MirrorSystem. This field
64 // is more interesting once we implement remote mirrors. 61 // is more interesting once we implement remote mirrors.
65 MirrorSystem get mirrors() { return _Mirrors.currentMirrorSystem(); } 62 MirrorSystem get mirrors() { return _Mirrors.currentMirrorSystem(); }
66 } 63 }
67 64
68 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl 65 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl
69 implements IsolateMirror { 66 implements IsolateMirror {
70 _LocalIsolateMirrorImpl(this.debugName) {} 67 _LocalIsolateMirrorImpl(this.debugName, this._rootLibrary) {}
71 68
72 final String debugName; 69 final String debugName;
73 final bool isCurrent = true; 70 final bool isCurrent = true;
74 71
72 var _rootLibrary;
73 LibraryMirror get rootLibrary() {
74 if (_rootLibrary is _LazyLibraryMirror) {
75 _rootLibrary = _rootLibrary.resolve(mirrors);
76 }
77 return _rootLibrary;
78 }
79
75 String toString() { 80 String toString() {
76 return "IsolateMirror on '$debugName'"; 81 return "IsolateMirror on '$debugName'";
77 } 82 }
78 } 83 }
79 84
80 // A VMReference is used to hold a reference to a VM-internal object, 85 // A VMReference is used to hold a reference to a VM-internal object,
81 // which can include things like libraries, classes, etc. 86 // which can include things like libraries, classes, etc.
82 class VMReference extends NativeFieldWrapperClass1 { 87 class VMReference extends NativeFieldWrapperClass1 {
83 } 88 }
84 89
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 break; 215 break;
211 } 216 }
212 buf.add(output); 217 buf.add(output);
213 } 218 }
214 return buf.toString(); 219 return buf.toString();
215 } 220 }
216 221
217 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl 222 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl
218 implements InstanceMirror { 223 implements InstanceMirror {
219 _LocalInstanceMirrorImpl(ref, 224 _LocalInstanceMirrorImpl(ref,
220 this._class, 225 this._type,
221 this._reflectee) : super(ref) {} 226 this._reflectee) : super(ref) {}
222 227
223 var _class; 228 var _type;
224 InterfaceMirror getClass() { 229 ClassMirror get type() {
225 if (_class is _LazyInterfaceMirror) { 230 if (_type is _LazyClassMirror) {
226 _class = _class.resolve(mirrors); 231 _type = _type.resolve(mirrors);
227 } 232 }
228 return _class; 233 return _type;
229 } 234 }
230 235
231 // LocalInstanceMirrors always reflect local instances 236 // LocalInstanceMirrors always reflect local instances
232 bool hasReflectee = true; 237 bool hasReflectee = true;
233 238
234 var _reflectee; 239 var _reflectee;
235 get reflectee() => _reflectee; 240 get reflectee() => _reflectee;
236 241
237 String toString() { 242 String toString() {
238 if (isSimpleValue(_reflectee)) { 243 if (isSimpleValue(_reflectee)) {
239 if (_reflectee is String) { 244 if (_reflectee is String) {
240 return "InstanceMirror on <'${_dartEscape(_reflectee)}'>"; 245 return "InstanceMirror on <'${_dartEscape(_reflectee)}'>";
241 } else { 246 } else {
242 return "InstanceMirror on <$_reflectee>"; 247 return "InstanceMirror on <$_reflectee>";
243 } 248 }
244 } else { 249 } else {
245 return "InstanceMirror on instance of '${getClass().simpleName}'"; 250 return "InstanceMirror on instance of '${type.simpleName}'";
246 } 251 }
247 } 252 }
248 } 253 }
249 254
250 class _LocalClosureMirrorImpl extends _LocalInstanceMirrorImpl 255 class _LocalClosureMirrorImpl extends _LocalInstanceMirrorImpl
251 implements ClosureMirror { 256 implements ClosureMirror {
252 _LocalClosureMirrorImpl(ref, 257 _LocalClosureMirrorImpl(ref,
253 klass, 258 klass,
254 reflectee) : super(ref, klass, reflectee) {} 259 reflectee) : super(ref, klass, reflectee) {}
255 260
(...skipping 18 matching lines...) Expand all
274 try { 279 try {
275 completer.complete( 280 completer.complete(
276 _apply(this, positionalArguments)); 281 _apply(this, positionalArguments));
277 } catch (var exception) { 282 } catch (var exception) {
278 completer.completeException(exception); 283 completer.completeException(exception);
279 } 284 }
280 return completer.future; 285 return completer.future;
281 } 286 }
282 287
283 Future<ObjectMirror> findInContext(String name) { 288 Future<ObjectMirror> findInContext(String name) {
284 throw "asYetUnimplemented"; 289 throw new NotImplementedException(
290 'ClosureMirror.findInContext() not implemented');
285 } 291 }
286 292
287 static _apply(ref, positionalArguments) 293 static _apply(ref, positionalArguments)
288 native 'LocalClosureMirrorImpl_apply'; 294 native 'LocalClosureMirrorImpl_apply';
289 } 295 }
290 296
291 class _LazyInterfaceMirror { 297 class _LazyClassMirror {
292 _LazyInterfaceMirror(this.libraryName, this.interfaceName) {} 298 _LazyClassMirror(this.libraryName, this.interfaceName) {}
293 299
294 InterfaceMirror resolve(MirrorSystem mirrors) { 300 ClassMirror resolve(MirrorSystem mirrors) {
295 if (libraryName === null) { 301 if (libraryName === null) {
296 if (interfaceName == 'Dynamic') { 302 if (interfaceName == 'Dynamic') {
297 return mirrors._dynamicMirror(); 303 return mirrors.dynamicType();
298 } else if (interfaceName == 'void') { 304 } else if (interfaceName == 'void') {
299 return mirrors._dynamicMirror(); 305 return mirrors.voidType();
300 } else { 306 } else {
301 throw new NotImplementedException( 307 throw new NotImplementedException(
302 "Mirror for type '$interfaceName' not implemented"); 308 "Mirror for type '$interfaceName' not implemented");
303 } 309 }
304 } 310 }
305 return mirrors.libraries()[libraryName].members()[interfaceName]; 311 return mirrors.libraries[libraryName].members[interfaceName];
306 } 312 }
307 313
308 final String libraryName; 314 final String libraryName;
309 final String interfaceName; 315 final String interfaceName;
310 } 316 }
311 317
312 class _LocalInterfaceMirrorImpl extends _LocalObjectMirrorImpl 318 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl
313 implements InterfaceMirror { 319 implements ClassMirror {
314 _LocalInterfaceMirrorImpl(ref, 320 _LocalClassMirrorImpl(ref,
315 this.simpleName, 321 this.simpleName,
316 this.isClass, 322 this.isClass,
317 this._library, 323 this._library,
318 this._superclass, 324 this._superclass,
319 this._superinterfaces, 325 this._superinterfaces,
320 this._defaultFactory, 326 this._defaultFactory,
321 this._members) : super(ref) {} 327 this.members) : super(ref) {}
322 328
323 final String simpleName; 329 final String simpleName;
324 final bool isClass; 330 final bool isClass;
325 331
326 var _library; 332 var _library;
327 LibraryMirror get library() { 333 LibraryMirror get library() {
328 if (_library is _LazyLibraryMirror) { 334 if (_library is _LazyLibraryMirror) {
329 _library = _library.resolve(mirrors); 335 _library = _library.resolve(mirrors);
330 } 336 }
331 return _library; 337 return _library;
332 } 338 }
333 339
334 var _superclass; 340 var _superclass;
335 InterfaceMirror superclass() { 341 ClassMirror get superclass() {
336 if (_superclass is _LazyInterfaceMirror) { 342 if (_superclass is _LazyClassMirror) {
337 _superclass = _superclass.resolve(mirrors); 343 _superclass = _superclass.resolve(mirrors);
338 } 344 }
339 return _superclass; 345 return _superclass;
340 } 346 }
341 347
342 var _superinterfaces; 348 var _superinterfaces;
343 List<InterfaceMirror> superinterfaces() { 349 List<ClassMirror> get superinterfaces() {
344 if (_superinterfaces.length > 0 && 350 if (_superinterfaces.length > 0 &&
345 _superinterfaces[0] is _LazyInterfaceMirror) { 351 _superinterfaces[0] is _LazyClassMirror) {
346 List<InterfaceMirror> resolved = new List<InterfaceMirror>(); 352 List<ClassMirror> resolved = new List<ClassMirror>();
347 for (int i = 0; i < _superinterfaces.length; i++) { 353 for (int i = 0; i < _superinterfaces.length; i++) {
348 resolved.add(_superinterfaces[i].resolve(mirrors)); 354 resolved.add(_superinterfaces[i].resolve(mirrors));
349 } 355 }
350 _superinterfaces = resolved; 356 _superinterfaces = resolved;
351 } 357 }
352 return _superinterfaces; 358 return _superinterfaces;
353 } 359 }
354 360
355 var _defaultFactory; 361 var _defaultFactory;
356 InterfaceMirror defaultFactory() { 362 ClassMirror get defaultFactory() {
357 if (_defaultFactory is _LazyInterfaceMirror) { 363 if (_defaultFactory is _LazyClassMirror) {
358 _defaultFactory = _defaultFactory.resolve(mirrors); 364 _defaultFactory = _defaultFactory.resolve(mirrors);
359 } 365 }
360 return _defaultFactory; 366 return _defaultFactory;
361 } 367 }
362 368
363 Map<String, InterfaceMirror> _members; 369 final Map<String, ClassMirror> members;
364 Map<String, InterfaceMirror> _methods = null;
365 Map<String, InterfaceMirror> _variables = null;
366 370
367 Map<String, Mirror> members() { return _members; } 371 Map<String, ClassMirror> _methods = null;
372 Map<String, ClassMirror> _variables = null;
368 373
369 Map<String, MethodMirror> methods() { 374 Map<String, MethodMirror> get methods() {
370 if (_methods == null) { 375 if (_methods == null) {
371 _methods = filterMap(members(), 376 _methods = filterMap(members,
372 (key, value) => (value is MethodMirror)); 377 (key, value) => (value is MethodMirror));
373 } 378 }
374 return _methods; 379 return _methods;
375 } 380 }
376 381
377 Map<String, VariableMirror> variables() { 382 Map<String, VariableMirror> get variables() {
378 if (_variables == null) { 383 if (_variables == null) {
379 _variables = filterMap(members(), 384 _variables = filterMap(members,
380 (key, value) => (value is VariableMirror)); 385 (key, value) => (value is VariableMirror));
381 } 386 }
382 return _variables; 387 return _variables;
383 } 388 }
384 389
385 String toString() { 390 String toString() {
386 return "InterfaceMirror on '$simpleName'"; 391 return "ClassMirror on '$simpleName'";
387 } 392 }
388 393
389 Future<InstanceMirror> newInstance(String constructorName, 394 Future<InstanceMirror> newInstance(String constructorName,
390 List positionalArguments, 395 List positionalArguments,
391 [Map<String,Dynamic> namedArguments]) { 396 [Map<String,Dynamic> namedArguments]) {
392 if (namedArguments !== null) { 397 if (namedArguments !== null) {
393 throw new NotImplementedException('named arguments not implemented'); 398 throw new NotImplementedException('named arguments not implemented');
394 } 399 }
395 // Walk the arguments and make sure they are legal. 400 // Walk the arguments and make sure they are legal.
396 for (int i = 0; i < positionalArguments.length; i++) { 401 for (int i = 0; i < positionalArguments.length; i++) {
397 var arg = positionalArguments[i]; 402 var arg = positionalArguments[i];
398 _LocalObjectMirrorImpl._validateArgument(i, arg); 403 _LocalObjectMirrorImpl._validateArgument(i, arg);
399 } 404 }
400 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); 405 Completer<InstanceMirror> completer = new Completer<InstanceMirror>();
401 try { 406 try {
402 completer.complete( 407 completer.complete(
403 _invokeConstructor(this, constructorName, positionalArguments)); 408 _invokeConstructor(this, constructorName, positionalArguments));
404 } catch (var exception) { 409 } catch (var exception) {
405 completer.completeException(exception); 410 completer.completeException(exception);
406 } 411 }
407 return completer.future; 412 return completer.future;
408 } 413 }
409 414
410 static _invokeConstructor(ref, constructorName, positionalArguments) 415 static _invokeConstructor(ref, constructorName, positionalArguments)
411 native 'LocalInterfaceMirrorImpl_invokeConstructor'; 416 native 'LocalClassMirrorImpl_invokeConstructor';
412 } 417 }
413 418
414 class _LazyLibraryMirror { 419 class _LazyLibraryMirror {
415 _LazyLibraryMirror(this.libraryName) {} 420 _LazyLibraryMirror(this.libraryName) {}
416 421
417 LibraryMirror resolve(MirrorSystem mirrors) { 422 LibraryMirror resolve(MirrorSystem mirrors) {
418 return mirrors.libraries()[libraryName]; 423 return mirrors.libraries[libraryName];
419 } 424 }
420 425
421 final String libraryName; 426 final String libraryName;
422 } 427 }
423 428
424 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl 429 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl
425 implements LibraryMirror { 430 implements LibraryMirror {
426 _LocalLibraryMirrorImpl(ref, 431 _LocalLibraryMirrorImpl(ref,
427 this.simpleName, 432 this.simpleName,
428 this.url, 433 this.url,
429 this._members) : super(ref) {} 434 this.members) : super(ref) {}
430 435
431 final String simpleName; 436 final String simpleName;
432 final String url; 437 final String url;
433 Map<String, InterfaceMirror> _members; 438 final Map<String, ClassMirror> members;
434 Map<String, InterfaceMirror> _classes = null;
435 Map<String, InterfaceMirror> _functions = null;
436 Map<String, InterfaceMirror> _variables = null;
437 439
438 Map<String, Mirror> members() { return _members; } 440 Map<String, ClassMirror> _classes = null;
441 Map<String, ClassMirror> _functions = null;
442 Map<String, ClassMirror> _variables = null;
439 443
440 Map<String, InterfaceMirror> classes() { 444 Map<String, ClassMirror> get classes() {
441 if (_classes == null) { 445 if (_classes == null) {
442 _classes = filterMap(members(), 446 _classes = filterMap(members,
443 (key, value) => (value is InterfaceMirror)); 447 (key, value) => (value is ClassMirror));
444 } 448 }
445 return _classes; 449 return _classes;
446 } 450 }
447 451
448 Map<String, MethodMirror> functions() { 452 Map<String, MethodMirror> get functions() {
449 if (_functions == null) { 453 if (_functions == null) {
450 _functions = filterMap(members(), 454 _functions = filterMap(members,
451 (key, value) => (value is MethodMirror)); 455 (key, value) => (value is MethodMirror));
452 } 456 }
453 return _functions; 457 return _functions;
454 } 458 }
455 459
456 Map<String, VariableMirror> variables() { 460 Map<String, VariableMirror> get variables() {
457 if (_variables == null) { 461 if (_variables == null) {
458 _variables = filterMap(members(), 462 _variables = filterMap(members,
459 (key, value) => (value is VariableMirror)); 463 (key, value) => (value is VariableMirror));
460 } 464 }
461 return _variables; 465 return _variables;
462 } 466 }
463 467
464 String toString() { 468 String toString() {
465 return "LibraryMirror on '$simpleName'"; 469 return "LibraryMirror on '$simpleName'";
466 } 470 }
467 } 471 }
468 472
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 List<ParameterMirror> parameters() => _parameters; 519 List<ParameterMirror> parameters() => _parameters;
516 520
517 String toString() { 521 String toString() {
518 return "MethodMirror on '$simpleName'"; 522 return "MethodMirror on '$simpleName'";
519 } 523 }
520 } 524 }
521 525
522 class _LocalParameterMirrorImpl extends _LocalVariableMirrorImpl 526 class _LocalParameterMirrorImpl extends _LocalVariableMirrorImpl
523 implements ParameterMirror { 527 implements ParameterMirror {
524 // TODO(rmacnak): Fill these mirrors will real information 528 // TODO(rmacnak): Fill these mirrors will real information
525 _LocalParameterMirrorImpl(this.isOptional) 529 _LocalParameterMirrorImpl(this.isOptional)
526 : super(null, null, false, false) {} 530 : super(null, null, false, false) {}
527 531
528 final bool isOptional; 532 final bool isOptional;
529 533
530 TypeMirror get type() => null; 534 TypeMirror get type() => null;
531 String get defaultValue() => null; 535 String get defaultValue() => null;
532 bool get hasDefaultValue() => null; 536 bool get hasDefaultValue() => null;
533 } 537 }
534 538
535 class _LocalVariableMirrorImpl extends _LocalMirrorImpl 539 class _LocalVariableMirrorImpl extends _LocalMirrorImpl
536 implements VariableMirror { 540 implements VariableMirror {
537 _LocalVariableMirrorImpl(this.simpleName, 541 _LocalVariableMirrorImpl(this.simpleName,
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 throw new NotImplementedException('Remote mirrors not yet implemented'); 597 throw new NotImplementedException('Remote mirrors not yet implemented');
594 } 598 }
595 return completer.future; 599 return completer.future;
596 } 600 }
597 601
598 // Creates a new local InstanceMirror 602 // Creates a new local InstanceMirror
599 static InstanceMirror makeLocalInstanceMirror(Object reflectee) 603 static InstanceMirror makeLocalInstanceMirror(Object reflectee)
600 native 'Mirrors_makeLocalInstanceMirror'; 604 native 'Mirrors_makeLocalInstanceMirror';
601 605
602 // Creates a new local mirror for some Object. 606 // Creates a new local mirror for some Object.
603 static InstanceMirror mirrorOf(Object reflectee) { 607 static InstanceMirror reflect(Object reflectee) {
604 return makeLocalInstanceMirror(reflectee); 608 return makeLocalInstanceMirror(reflectee);
605 } 609 }
606 } 610 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698