| 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 // 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 abstract class _LocalMirrorImpl implements Mirror { | 22 class _LocalMirrorSystemImpl implements MirrorSystem { |
| 23 // Local mirrors always return the same IsolateMirror. This field | 23 _LocalMirrorSystemImpl(this.rootLibrary, this._libraries, this.isolate) {} |
| 24 // is more interesting once we implement remote mirrors. | |
| 25 IsolateMirror get isolate() { return _Mirrors.currentIsolateMirror(); } | |
| 26 } | |
| 27 | 24 |
| 28 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl | |
| 29 implements IsolateMirror { | |
| 30 _LocalIsolateMirrorImpl(this.debugName, this.rootLibrary, this._libraries) {} | |
| 31 | |
| 32 final String debugName; | |
| 33 final LibraryMirror rootLibrary; | 25 final LibraryMirror rootLibrary; |
| 34 final Map<String, LibraryMirror> _libraries; | 26 final Map<String, LibraryMirror> _libraries; |
| 27 final IsolateMirror isolate; |
| 35 | 28 |
| 36 Map<String, LibraryMirror> libraries() { return _libraries; } | 29 Map<String, LibraryMirror> libraries() { return _libraries; } |
| 37 | 30 |
| 31 Mirror mirrorOf(Object reflectee) { |
| 32 return _Mirrors.mirrorOf(reflectee); |
| 33 } |
| 34 |
| 38 InterfaceMirror _sharedDynamic = null; | 35 InterfaceMirror _sharedDynamic = null; |
| 39 | 36 |
| 40 InterfaceMirror _dynamicMirror() { | 37 InterfaceMirror _dynamicMirror() { |
| 41 if (_sharedDynamic === null) { | 38 if (_sharedDynamic === null) { |
| 42 _sharedDynamic = | 39 _sharedDynamic = |
| 43 new _LocalInterfaceMirrorImpl( | 40 new _LocalInterfaceMirrorImpl( |
| 44 null, 'Dynamic', false, null, null, [], null, const {}); | 41 null, 'Dynamic', false, null, null, [], null, const {}); |
| 45 } | 42 } |
| 46 return _sharedDynamic; | 43 return _sharedDynamic; |
| 47 } | 44 } |
| 48 | 45 |
| 49 InterfaceMirror _sharedVoid = null; | 46 InterfaceMirror _sharedVoid = null; |
| 50 | 47 |
| 51 InterfaceMirror _voidMirror() { | 48 InterfaceMirror _voidMirror() { |
| 52 if (_sharedVoid === null) { | 49 if (_sharedVoid === null) { |
| 53 _sharedVoid = | 50 _sharedVoid = |
| 54 new _LocalInterfaceMirrorImpl( | 51 new _LocalInterfaceMirrorImpl( |
| 55 null, 'void', false, null, null, [], null, const {}); | 52 null, 'void', false, null, null, [], null, const {}); |
| 56 } | 53 } |
| 57 return _sharedVoid; | 54 return _sharedVoid; |
| 58 } | 55 } |
| 59 | 56 |
| 60 String toString() { | 57 String toString() { |
| 58 return "MirrorSystem for isolate '$debugName'"; |
| 59 } |
| 60 } |
| 61 |
| 62 abstract class _LocalMirrorImpl implements Mirror { |
| 63 // Local mirrors always return the same MirrorSystem. This field |
| 64 // is more interesting once we implement remote mirrors. |
| 65 MirrorSystem get mirrors() { return _Mirrors.currentMirrorSystem(); } |
| 66 } |
| 67 |
| 68 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl |
| 69 implements IsolateMirror { |
| 70 _LocalIsolateMirrorImpl(this.debugName) {} |
| 71 |
| 72 final String debugName; |
| 73 final bool isCurrent = true; |
| 74 |
| 75 String toString() { |
| 61 return "IsolateMirror on '$debugName'"; | 76 return "IsolateMirror on '$debugName'"; |
| 62 } | 77 } |
| 63 } | 78 } |
| 64 | 79 |
| 65 // A VMReference is used to hold a reference to a VM-internal object, | 80 // A VMReference is used to hold a reference to a VM-internal object, |
| 66 // which can include things like libraries, classes, etc. | 81 // which can include things like libraries, classes, etc. |
| 67 class VMReference extends NativeFieldWrapperClass1 { | 82 class VMReference extends NativeFieldWrapperClass1 { |
| 68 } | 83 } |
| 69 | 84 |
| 70 abstract class _LocalVMObjectMirrorImpl extends _LocalMirrorImpl { | 85 abstract class _LocalVMObjectMirrorImpl extends _LocalMirrorImpl { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl | 182 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl |
| 168 implements InstanceMirror { | 183 implements InstanceMirror { |
| 169 _LocalInstanceMirrorImpl(ref, | 184 _LocalInstanceMirrorImpl(ref, |
| 170 this._class, | 185 this._class, |
| 171 this.hasSimpleValue, | 186 this.hasSimpleValue, |
| 172 this._simpleValue) : super(ref) {} | 187 this._simpleValue) : super(ref) {} |
| 173 | 188 |
| 174 var _class; | 189 var _class; |
| 175 InterfaceMirror getClass() { | 190 InterfaceMirror getClass() { |
| 176 if (_class is _LazyInterfaceMirror) { | 191 if (_class is _LazyInterfaceMirror) { |
| 177 _class = _class.resolve(isolate); | 192 _class = _class.resolve(mirrors); |
| 178 } | 193 } |
| 179 return _class; | 194 return _class; |
| 180 } | 195 } |
| 181 | 196 |
| 182 bool hasSimpleValue; | 197 bool hasSimpleValue; |
| 183 | 198 |
| 184 var _simpleValue; | 199 var _simpleValue; |
| 185 get simpleValue() { | 200 get simpleValue() { |
| 186 if (!hasSimpleValue) { | 201 if (!hasSimpleValue) { |
| 187 throw new MirrorException( | 202 throw new MirrorException( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 200 } | 215 } |
| 201 } else { | 216 } else { |
| 202 return "InstanceMirror on instance of '${getClass().simpleName}'"; | 217 return "InstanceMirror on instance of '${getClass().simpleName}'"; |
| 203 } | 218 } |
| 204 } | 219 } |
| 205 } | 220 } |
| 206 | 221 |
| 207 class _LazyInterfaceMirror { | 222 class _LazyInterfaceMirror { |
| 208 _LazyInterfaceMirror(this.libraryName, this.interfaceName) {} | 223 _LazyInterfaceMirror(this.libraryName, this.interfaceName) {} |
| 209 | 224 |
| 210 InterfaceMirror resolve(IsolateMirror isolate) { | 225 InterfaceMirror resolve(MirrorSystem mirrors) { |
| 211 if (libraryName === null) { | 226 if (libraryName === null) { |
| 212 if (interfaceName == 'Dynamic') { | 227 if (interfaceName == 'Dynamic') { |
| 213 return isolate._dynamicMirror(); | 228 return mirrors._dynamicMirror(); |
| 214 } else if (interfaceName == 'void') { | 229 } else if (interfaceName == 'void') { |
| 215 return isolate._dynamicMirror(); | 230 return mirrors._dynamicMirror(); |
| 216 } else { | 231 } else { |
| 217 throw new NotImplementedException( | 232 throw new NotImplementedException( |
| 218 "Mirror for type '$interfaceName' not implemented"); | 233 "Mirror for type '$interfaceName' not implemented"); |
| 219 } | 234 } |
| 220 } | 235 } |
| 221 return isolate.libraries()[libraryName].members()[interfaceName]; | 236 return mirrors.libraries()[libraryName].members()[interfaceName]; |
| 222 } | 237 } |
| 223 | 238 |
| 224 final String libraryName; | 239 final String libraryName; |
| 225 final String interfaceName; | 240 final String interfaceName; |
| 226 } | 241 } |
| 227 | 242 |
| 228 class _LocalInterfaceMirrorImpl extends _LocalObjectMirrorImpl | 243 class _LocalInterfaceMirrorImpl extends _LocalObjectMirrorImpl |
| 229 implements InterfaceMirror { | 244 implements InterfaceMirror { |
| 230 _LocalInterfaceMirrorImpl(ref, | 245 _LocalInterfaceMirrorImpl(ref, |
| 231 this.simpleName, | 246 this.simpleName, |
| 232 this.isClass, | 247 this.isClass, |
| 233 this._library, | 248 this._library, |
| 234 this._superclass, | 249 this._superclass, |
| 235 this._superinterfaces, | 250 this._superinterfaces, |
| 236 this._defaultFactory, | 251 this._defaultFactory, |
| 237 this._members) : super(ref) {} | 252 this._members) : super(ref) {} |
| 238 | 253 |
| 239 final String simpleName; | 254 final String simpleName; |
| 240 final bool isClass; | 255 final bool isClass; |
| 241 | 256 |
| 242 var _library; | 257 var _library; |
| 243 LibraryMirror get library() { | 258 LibraryMirror get library() { |
| 244 if (_library is _LazyLibraryMirror) { | 259 if (_library is _LazyLibraryMirror) { |
| 245 _library = _library.resolve(isolate); | 260 _library = _library.resolve(mirrors); |
| 246 } | 261 } |
| 247 return _library; | 262 return _library; |
| 248 } | 263 } |
| 249 | 264 |
| 250 var _superclass; | 265 var _superclass; |
| 251 InterfaceMirror superclass() { | 266 InterfaceMirror superclass() { |
| 252 if (_superclass is _LazyInterfaceMirror) { | 267 if (_superclass is _LazyInterfaceMirror) { |
| 253 _superclass = _superclass.resolve(isolate); | 268 _superclass = _superclass.resolve(mirrors); |
| 254 } | 269 } |
| 255 return _superclass; | 270 return _superclass; |
| 256 } | 271 } |
| 257 | 272 |
| 258 var _superinterfaces; | 273 var _superinterfaces; |
| 259 List<InterfaceMirror> superinterfaces() { | 274 List<InterfaceMirror> superinterfaces() { |
| 260 if (_superinterfaces.length > 0 && | 275 if (_superinterfaces.length > 0 && |
| 261 _superinterfaces[0] is _LazyInterfaceMirror) { | 276 _superinterfaces[0] is _LazyInterfaceMirror) { |
| 262 List<InterfaceMirror> resolved = new List<InterfaceMirror>(); | 277 List<InterfaceMirror> resolved = new List<InterfaceMirror>(); |
| 263 for (int i = 0; i < _superinterfaces.length; i++) { | 278 for (int i = 0; i < _superinterfaces.length; i++) { |
| 264 resolved.add(_superinterfaces[i].resolve(isolate)); | 279 resolved.add(_superinterfaces[i].resolve(mirrors)); |
| 265 } | 280 } |
| 266 _superinterfaces = resolved; | 281 _superinterfaces = resolved; |
| 267 } | 282 } |
| 268 return _superinterfaces; | 283 return _superinterfaces; |
| 269 } | 284 } |
| 270 | 285 |
| 271 var _defaultFactory; | 286 var _defaultFactory; |
| 272 InterfaceMirror defaultFactory() { | 287 InterfaceMirror defaultFactory() { |
| 273 if (_defaultFactory is _LazyInterfaceMirror) { | 288 if (_defaultFactory is _LazyInterfaceMirror) { |
| 274 _defaultFactory = _defaultFactory.resolve(isolate); | 289 _defaultFactory = _defaultFactory.resolve(mirrors); |
| 275 } | 290 } |
| 276 return _defaultFactory; | 291 return _defaultFactory; |
| 277 } | 292 } |
| 278 | 293 |
| 279 Map<String, InterfaceMirror> _members; | 294 Map<String, InterfaceMirror> _members; |
| 280 Map<String, InterfaceMirror> _methods = null; | 295 Map<String, InterfaceMirror> _methods = null; |
| 281 Map<String, InterfaceMirror> _variables = null; | 296 Map<String, InterfaceMirror> _variables = null; |
| 282 | 297 |
| 283 Map<String, Mirror> members() { return _members; } | 298 Map<String, Mirror> members() { return _members; } |
| 284 | 299 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 299 } | 314 } |
| 300 | 315 |
| 301 String toString() { | 316 String toString() { |
| 302 return "InterfaceMirror on '$simpleName'"; | 317 return "InterfaceMirror on '$simpleName'"; |
| 303 } | 318 } |
| 304 } | 319 } |
| 305 | 320 |
| 306 class _LazyLibraryMirror { | 321 class _LazyLibraryMirror { |
| 307 _LazyLibraryMirror(this.libraryName) {} | 322 _LazyLibraryMirror(this.libraryName) {} |
| 308 | 323 |
| 309 LibraryMirror resolve(IsolateMirror isolate) { | 324 LibraryMirror resolve(MirrorSystem mirrors) { |
| 310 return isolate.libraries()[libraryName]; | 325 return mirrors.libraries()[libraryName]; |
| 311 } | 326 } |
| 312 | 327 |
| 313 final String libraryName; | 328 final String libraryName; |
| 314 } | 329 } |
| 315 | 330 |
| 316 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl | 331 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl |
| 317 implements LibraryMirror { | 332 implements LibraryMirror { |
| 318 _LocalLibraryMirrorImpl(ref, | 333 _LocalLibraryMirrorImpl(ref, |
| 319 this.simpleName, | 334 this.simpleName, |
| 320 this.url, | 335 this.url, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 this.isConstConstructor, | 385 this.isConstConstructor, |
| 371 this.isGenerativeConstructor, | 386 this.isGenerativeConstructor, |
| 372 this.isRedirectingConstructor, | 387 this.isRedirectingConstructor, |
| 373 this.isFactoryConstructor) {} | 388 this.isFactoryConstructor) {} |
| 374 | 389 |
| 375 final String simpleName; | 390 final String simpleName; |
| 376 | 391 |
| 377 var _owner; | 392 var _owner; |
| 378 Mirror get owner() { | 393 Mirror get owner() { |
| 379 if (_owner is! Mirror) { | 394 if (_owner is! Mirror) { |
| 380 _owner = _owner.resolve(isolate); | 395 _owner = _owner.resolve(mirrors); |
| 381 } | 396 } |
| 382 return _owner; | 397 return _owner; |
| 383 } | 398 } |
| 384 | 399 |
| 385 bool get isTopLevel() { | 400 bool get isTopLevel() { |
| 386 return owner is LibraryMirror; | 401 return owner is LibraryMirror; |
| 387 } | 402 } |
| 388 | 403 |
| 389 final bool isStatic; | 404 final bool isStatic; |
| 390 | 405 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 412 _LocalVariableMirrorImpl(this.simpleName, | 427 _LocalVariableMirrorImpl(this.simpleName, |
| 413 this._owner, | 428 this._owner, |
| 414 this.isStatic, | 429 this.isStatic, |
| 415 this.isFinal) {} | 430 this.isFinal) {} |
| 416 | 431 |
| 417 final String simpleName; | 432 final String simpleName; |
| 418 | 433 |
| 419 var _owner; | 434 var _owner; |
| 420 Mirror get owner() { | 435 Mirror get owner() { |
| 421 if (_owner is! Mirror) { | 436 if (_owner is! Mirror) { |
| 422 _owner = _owner.resolve(isolate); | 437 _owner = _owner.resolve(mirrors); |
| 423 } | 438 } |
| 424 return _owner; | 439 return _owner; |
| 425 } | 440 } |
| 426 | 441 |
| 427 bool get isTopLevel() { | 442 bool get isTopLevel() { |
| 428 return owner is LibraryMirror; | 443 return owner is LibraryMirror; |
| 429 } | 444 } |
| 430 | 445 |
| 431 final bool isStatic; | 446 final bool isStatic; |
| 432 final bool isFinal; | 447 final bool isFinal; |
| 433 | 448 |
| 434 String toString() { | 449 String toString() { |
| 435 return "VariableMirror on '$simpleName'"; | 450 return "VariableMirror on '$simpleName'"; |
| 436 } | 451 } |
| 437 } | 452 } |
| 438 | 453 |
| 439 class _Mirrors { | 454 class _Mirrors { |
| 440 // Does a port refer to our local isolate? | 455 // Does a port refer to our local isolate? |
| 441 static bool isLocalPort(SendPort port) native 'Mirrors_isLocalPort'; | 456 static bool isLocalPort(SendPort port) native 'Mirrors_isLocalPort'; |
| 442 | 457 |
| 443 static IsolateMirror _currentIsolateMirror = null; | 458 static MirrorSystem _currentMirrorSystem = null; |
| 444 | 459 |
| 445 // Creates a new local IsolateMirror. | 460 // Creates a new local MirrorSystem. |
| 446 static IsolateMirror makeLocalIsolateMirror() | 461 static MirrorSystem makeLocalMirrorSystem() |
| 447 native 'Mirrors_makeLocalIsolateMirror'; | 462 native 'Mirrors_makeLocalMirrorSystem'; |
| 448 | 463 |
| 449 // The IsolateMirror for the current isolate. | 464 // The MirrorSystem for the current isolate. |
| 450 static IsolateMirror currentIsolateMirror() { | 465 static MirrorSystem currentMirrorSystem() { |
| 451 if (_currentIsolateMirror === null) { | 466 if (_currentMirrorSystem === null) { |
| 452 _currentIsolateMirror = makeLocalIsolateMirror(); | 467 _currentMirrorSystem = makeLocalMirrorSystem(); |
| 453 } | 468 } |
| 454 return _currentIsolateMirror; | 469 return _currentMirrorSystem; |
| 455 } | 470 } |
| 456 | 471 |
| 457 static Future<IsolateMirror> isolateMirrorOf(SendPort port) { | 472 static Future<MirrorSystem> mirrorSystemOf(SendPort port) { |
| 458 Completer<IsolateMirror> completer = new Completer<IsolateMirror>(); | 473 Completer<MirrorSystem> completer = new Completer<MirrorSystem>(); |
| 459 if (isLocalPort(port)) { | 474 if (isLocalPort(port)) { |
| 460 // Make a local isolate mirror. | 475 // Make a local mirror system. |
| 461 try { | 476 try { |
| 462 completer.complete(currentIsolateMirror()); | 477 completer.complete(currentMirrorSystem()); |
| 463 } catch (var exception) { | 478 } catch (var exception) { |
| 464 completer.completeException(exception); | 479 completer.completeException(exception); |
| 465 } | 480 } |
| 466 } else { | 481 } else { |
| 467 // Make a remote isolate mirror. | 482 // Make a remote mirror system |
| 468 throw new NotImplementedException('Remote mirrors not yet implemented'); | 483 throw new NotImplementedException('Remote mirrors not yet implemented'); |
| 469 } | 484 } |
| 470 return completer.future; | 485 return completer.future; |
| 471 } | 486 } |
| 472 | 487 |
| 473 // Creates a new local InstanceMirror | 488 // Creates a new local InstanceMirror |
| 474 static InstanceMirror makeLocalInstanceMirror(Object reflectee) | 489 static InstanceMirror makeLocalInstanceMirror(Object reflectee) |
| 475 native 'Mirrors_makeLocalInstanceMirror'; | 490 native 'Mirrors_makeLocalInstanceMirror'; |
| 476 | 491 |
| 477 // The IsolateMirror for the current isolate. | 492 // Creates a new local mirror for some Object. |
| 478 static InstanceMirror mirrorOf(Object reflectee) { | 493 static InstanceMirror mirrorOf(Object reflectee) { |
| 479 return makeLocalInstanceMirror(reflectee); | 494 return makeLocalInstanceMirror(reflectee); |
| 480 } | 495 } |
| 481 } | 496 } |
| OLD | NEW |