| 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)) { |
| 13 Map new_map = new Map(); |
| 14 old_map.forEach((key, value) { |
| 15 if (filter(key, value)) { |
| 16 new_map[key] = value; |
| 17 } |
| 18 }); |
| 19 return new_map; |
| 20 } |
| 21 |
| 12 abstract class _LocalMirrorImpl implements Mirror { | 22 abstract class _LocalMirrorImpl implements Mirror { |
| 13 // Local mirrors always return the same IsolateMirror. This field | 23 // Local mirrors always return the same IsolateMirror. This field |
| 14 // is more interesting once we implement remote mirrors. | 24 // is more interesting once we implement remote mirrors. |
| 15 IsolateMirror get isolate() { return _Mirrors.currentIsolateMirror(); } | 25 IsolateMirror get isolate() { return _Mirrors.currentIsolateMirror(); } |
| 16 } | 26 } |
| 17 | 27 |
| 18 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl | 28 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl |
| 19 implements IsolateMirror { | 29 implements IsolateMirror { |
| 20 _LocalIsolateMirrorImpl(this.debugName, this.rootLibrary, this._libraries) {} | 30 _LocalIsolateMirrorImpl(this.debugName, this.rootLibrary, this._libraries) {} |
| 21 | 31 |
| 22 final String debugName; | 32 final String debugName; |
| 23 final LibraryMirror rootLibrary; | 33 final LibraryMirror rootLibrary; |
| 24 final Map<String, LibraryMirror> _libraries; | 34 final Map<String, LibraryMirror> _libraries; |
| 25 | 35 |
| 26 Map<String, LibraryMirror> libraries() { return _libraries; } | 36 Map<String, LibraryMirror> libraries() { return _libraries; } |
| 27 | 37 |
| 38 InterfaceMirror _sharedDynamic = null; |
| 39 |
| 40 InterfaceMirror _dynamicMirror() { |
| 41 if (_sharedDynamic === null) { |
| 42 _sharedDynamic = |
| 43 new _LocalInterfaceMirrorImpl( |
| 44 null, 'Dynamic', false, null, null, [], null, const {}); |
| 45 } |
| 46 return _sharedDynamic; |
| 47 } |
| 48 |
| 49 InterfaceMirror _sharedVoid = null; |
| 50 |
| 51 InterfaceMirror _voidMirror() { |
| 52 if (_sharedVoid === null) { |
| 53 _sharedVoid = |
| 54 new _LocalInterfaceMirrorImpl( |
| 55 null, 'void', false, null, null, [], null, const {}); |
| 56 } |
| 57 return _sharedVoid; |
| 58 } |
| 59 |
| 28 String toString() { | 60 String toString() { |
| 29 return "IsolateMirror on '$debugName'"; | 61 return "IsolateMirror on '$debugName'"; |
| 30 } | 62 } |
| 31 } | 63 } |
| 32 | 64 |
| 33 // A VMReference is used to hold a reference to a VM-internal object, | 65 // A VMReference is used to hold a reference to a VM-internal object, |
| 34 // which can include things like libraries, classes, etc. | 66 // which can include things like libraries, classes, etc. |
| 35 class VMReference extends NativeFieldWrapperClass1 { | 67 class VMReference extends NativeFieldWrapperClass1 { |
| 36 } | 68 } |
| 37 | 69 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // Prints a string as it might appear in dart program text. | 117 // Prints a string as it might appear in dart program text. |
| 86 // TODO(turnidge): Consider truncating. | 118 // TODO(turnidge): Consider truncating. |
| 87 String _dartEscape(String str) { | 119 String _dartEscape(String str) { |
| 88 bool isNice(int code) { | 120 bool isNice(int code) { |
| 89 return (code >= 32 && code <= 126); | 121 return (code >= 32 && code <= 126); |
| 90 } | 122 } |
| 91 | 123 |
| 92 StringBuffer buf = new StringBuffer(); | 124 StringBuffer buf = new StringBuffer(); |
| 93 for (int i = 0; i < str.length; i++) { | 125 for (int i = 0; i < str.length; i++) { |
| 94 var input = str[i]; | 126 var input = str[i]; |
| 95 String output; | 127 String output; |
| 96 switch (input) { | 128 switch (input) { |
| 97 case '\\' : | 129 case '\\' : |
| 98 output = @'\\'; | 130 output = @'\\'; |
| 99 break; | 131 break; |
| 100 case "\'" : | 132 case "\'" : |
| 101 output = @"\'"; | 133 output = @"\'"; |
| 102 break; | 134 break; |
| 103 case '\n' : | 135 case '\n' : |
| 104 output = @'\n'; | 136 output = @'\n'; |
| 105 break; | 137 break; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 } else { | 201 } else { |
| 170 return "InstanceMirror on instance of '${getClass().simpleName}'"; | 202 return "InstanceMirror on instance of '${getClass().simpleName}'"; |
| 171 } | 203 } |
| 172 } | 204 } |
| 173 } | 205 } |
| 174 | 206 |
| 175 class _LazyInterfaceMirror { | 207 class _LazyInterfaceMirror { |
| 176 _LazyInterfaceMirror(this.libraryName, this.interfaceName) {} | 208 _LazyInterfaceMirror(this.libraryName, this.interfaceName) {} |
| 177 | 209 |
| 178 InterfaceMirror resolve(IsolateMirror isolate) { | 210 InterfaceMirror resolve(IsolateMirror isolate) { |
| 211 if (libraryName === null) { |
| 212 if (interfaceName == 'Dynamic') { |
| 213 return isolate._dynamicMirror(); |
| 214 } else if (interfaceName == 'void') { |
| 215 return isolate._dynamicMirror(); |
| 216 } else { |
| 217 throw new NotImplementedException( |
| 218 "Mirror for type '$interfaceName' not implemented"); |
| 219 } |
| 220 } |
| 179 return isolate.libraries()[libraryName].members()[interfaceName]; | 221 return isolate.libraries()[libraryName].members()[interfaceName]; |
| 180 } | 222 } |
| 181 | 223 |
| 182 final String libraryName; | 224 final String libraryName; |
| 183 final String interfaceName; | 225 final String interfaceName; |
| 184 } | 226 } |
| 185 | 227 |
| 186 class _LocalInterfaceMirrorImpl extends _LocalObjectMirrorImpl | 228 class _LocalInterfaceMirrorImpl extends _LocalObjectMirrorImpl |
| 187 implements InterfaceMirror { | 229 implements InterfaceMirror { |
| 188 _LocalInterfaceMirrorImpl(ref, | 230 _LocalInterfaceMirrorImpl(ref, |
| 189 this.simpleName, | 231 this.simpleName, |
| 190 this.isClass, | 232 this.isClass, |
| 191 this._library, | 233 this._library, |
| 192 this._superclass, | 234 this._superclass, |
| 193 this._superinterfaces, | 235 this._superinterfaces, |
| 194 this._defaultFactory) : super(ref) {} | 236 this._defaultFactory, |
| 237 this._members) : super(ref) {} |
| 195 | 238 |
| 196 final String simpleName; | 239 final String simpleName; |
| 197 final bool isClass; | 240 final bool isClass; |
| 198 | 241 |
| 199 var _library; | 242 var _library; |
| 200 LibraryMirror get library() { | 243 LibraryMirror get library() { |
| 201 if (_library is _LazyLibraryMirror) { | 244 if (_library is _LazyLibraryMirror) { |
| 202 _library = _library.resolve(isolate); | 245 _library = _library.resolve(isolate); |
| 203 } | 246 } |
| 204 return _library; | 247 return _library; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 226 } | 269 } |
| 227 | 270 |
| 228 var _defaultFactory; | 271 var _defaultFactory; |
| 229 InterfaceMirror defaultFactory() { | 272 InterfaceMirror defaultFactory() { |
| 230 if (_defaultFactory is _LazyInterfaceMirror) { | 273 if (_defaultFactory is _LazyInterfaceMirror) { |
| 231 _defaultFactory = _defaultFactory.resolve(isolate); | 274 _defaultFactory = _defaultFactory.resolve(isolate); |
| 232 } | 275 } |
| 233 return _defaultFactory; | 276 return _defaultFactory; |
| 234 } | 277 } |
| 235 | 278 |
| 279 Map<String, InterfaceMirror> _members; |
| 280 Map<String, InterfaceMirror> _methods = null; |
| 281 Map<String, InterfaceMirror> _variables = null; |
| 282 |
| 283 Map<String, Mirror> members() { return _members; } |
| 284 |
| 285 Map<String, MethodMirror> methods() { |
| 286 if (_methods == null) { |
| 287 _methods = filterMap(members(), |
| 288 (key, value) => (value is MethodMirror)); |
| 289 } |
| 290 return _methods; |
| 291 } |
| 292 |
| 293 Map<String, VariableMirror> variables() { |
| 294 if (_variables == null) { |
| 295 _variables = filterMap(members(), |
| 296 (key, value) => (value is VariableMirror)); |
| 297 } |
| 298 return _variables; |
| 299 } |
| 300 |
| 236 String toString() { | 301 String toString() { |
| 237 return "InterfaceMirror on '$simpleName'"; | 302 return "InterfaceMirror on '$simpleName'"; |
| 238 } | 303 } |
| 239 } | 304 } |
| 240 | 305 |
| 241 class _LazyLibraryMirror { | 306 class _LazyLibraryMirror { |
| 242 _LazyLibraryMirror(this.libraryName) {} | 307 _LazyLibraryMirror(this.libraryName) {} |
| 243 | 308 |
| 244 LibraryMirror resolve(IsolateMirror isolate) { | 309 LibraryMirror resolve(IsolateMirror isolate) { |
| 245 return isolate.libraries()[libraryName]; | 310 return isolate.libraries()[libraryName]; |
| 246 } | 311 } |
| 247 | 312 |
| 248 final String libraryName; | 313 final String libraryName; |
| 249 } | 314 } |
| 250 | 315 |
| 251 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl | 316 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl |
| 252 implements LibraryMirror { | 317 implements LibraryMirror { |
| 253 _LocalLibraryMirrorImpl(ref, | 318 _LocalLibraryMirrorImpl(ref, |
| 254 this.simpleName, | 319 this.simpleName, |
| 255 this.url, | 320 this.url, |
| 256 this._members) : super(ref) {} | 321 this._members) : super(ref) {} |
| 257 | 322 |
| 258 final String simpleName; | 323 final String simpleName; |
| 259 final String url; | 324 final String url; |
| 260 Map<String, InterfaceMirror> _members; | 325 Map<String, InterfaceMirror> _members; |
| 326 Map<String, InterfaceMirror> _classes = null; |
| 327 Map<String, InterfaceMirror> _functions = null; |
| 328 Map<String, InterfaceMirror> _variables = null; |
| 261 | 329 |
| 262 Map<String, Mirror> members() { return _members; } | 330 Map<String, Mirror> members() { return _members; } |
| 263 | 331 |
| 332 Map<String, InterfaceMirror> classes() { |
| 333 if (_classes == null) { |
| 334 _classes = filterMap(members(), |
| 335 (key, value) => (value is InterfaceMirror)); |
| 336 } |
| 337 return _classes; |
| 338 } |
| 339 |
| 340 Map<String, MethodMirror> functions() { |
| 341 if (_functions == null) { |
| 342 _functions = filterMap(members(), |
| 343 (key, value) => (value is MethodMirror)); |
| 344 } |
| 345 return _functions; |
| 346 } |
| 347 |
| 348 Map<String, VariableMirror> variables() { |
| 349 if (_variables == null) { |
| 350 _variables = filterMap(members(), |
| 351 (key, value) => (value is VariableMirror)); |
| 352 } |
| 353 return _variables; |
| 354 } |
| 355 |
| 264 String toString() { | 356 String toString() { |
| 265 return "LibraryMirror on '$simpleName'"; | 357 return "LibraryMirror on '$simpleName'"; |
| 266 } | 358 } |
| 267 } | 359 } |
| 268 | 360 |
| 361 class _LocalMethodMirrorImpl extends _LocalMirrorImpl |
| 362 implements MethodMirror { |
| 363 _LocalMethodMirrorImpl(this.simpleName, |
| 364 this._owner, |
| 365 this.isStatic, |
| 366 this.isAbstract, |
| 367 this.isGetter, |
| 368 this.isSetter, |
| 369 this.isConstructor, |
| 370 this.isConstConstructor, |
| 371 this.isGenerativeConstructor, |
| 372 this.isRedirectingConstructor, |
| 373 this.isFactoryConstructor) {} |
| 374 |
| 375 final String simpleName; |
| 376 |
| 377 var _owner; |
| 378 Mirror get owner() { |
| 379 if (_owner is! Mirror) { |
| 380 _owner = _owner.resolve(isolate); |
| 381 } |
| 382 return _owner; |
| 383 } |
| 384 |
| 385 bool get isTopLevel() { |
| 386 return owner is LibraryMirror; |
| 387 } |
| 388 |
| 389 final bool isStatic; |
| 390 |
| 391 bool get isMethod() { |
| 392 return !isGetter && !isSetter && !isConstructor; |
| 393 } |
| 394 |
| 395 final bool isAbstract; |
| 396 final bool isGetter; |
| 397 final bool isSetter; |
| 398 final bool isConstructor; |
| 399 |
| 400 final bool isConstConstructor; |
| 401 final bool isGenerativeConstructor; |
| 402 final bool isRedirectingConstructor; |
| 403 final bool isFactoryConstructor; |
| 404 |
| 405 String toString() { |
| 406 return "MethodMirror on '$simpleName'"; |
| 407 } |
| 408 } |
| 409 |
| 410 class _LocalVariableMirrorImpl extends _LocalMirrorImpl |
| 411 implements VariableMirror { |
| 412 _LocalVariableMirrorImpl(this.simpleName, |
| 413 this._owner, |
| 414 this.isStatic, |
| 415 this.isFinal) {} |
| 416 |
| 417 final String simpleName; |
| 418 |
| 419 var _owner; |
| 420 Mirror get owner() { |
| 421 if (_owner is! Mirror) { |
| 422 _owner = _owner.resolve(isolate); |
| 423 } |
| 424 return _owner; |
| 425 } |
| 426 |
| 427 bool get isTopLevel() { |
| 428 return owner is LibraryMirror; |
| 429 } |
| 430 |
| 431 final bool isStatic; |
| 432 final bool isFinal; |
| 433 |
| 434 String toString() { |
| 435 return "VariableMirror on '$simpleName'"; |
| 436 } |
| 437 } |
| 438 |
| 269 class _Mirrors { | 439 class _Mirrors { |
| 270 // Does a port refer to our local isolate? | 440 // Does a port refer to our local isolate? |
| 271 static bool isLocalPort(SendPort port) native 'Mirrors_isLocalPort'; | 441 static bool isLocalPort(SendPort port) native 'Mirrors_isLocalPort'; |
| 272 | 442 |
| 273 static IsolateMirror _currentIsolateMirror = null; | 443 static IsolateMirror _currentIsolateMirror = null; |
| 274 | 444 |
| 275 // Creates a new local IsolateMirror. | 445 // Creates a new local IsolateMirror. |
| 276 static IsolateMirror makeLocalIsolateMirror() | 446 static IsolateMirror makeLocalIsolateMirror() |
| 277 native 'Mirrors_makeLocalIsolateMirror'; | 447 native 'Mirrors_makeLocalIsolateMirror'; |
| 278 | 448 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 302 | 472 |
| 303 // Creates a new local InstanceMirror | 473 // Creates a new local InstanceMirror |
| 304 static InstanceMirror makeLocalInstanceMirror(Object reflectee) | 474 static InstanceMirror makeLocalInstanceMirror(Object reflectee) |
| 305 native 'Mirrors_makeLocalInstanceMirror'; | 475 native 'Mirrors_makeLocalInstanceMirror'; |
| 306 | 476 |
| 307 // The IsolateMirror for the current isolate. | 477 // The IsolateMirror for the current isolate. |
| 308 static InstanceMirror mirrorOf(Object reflectee) { | 478 static InstanceMirror mirrorOf(Object reflectee) { |
| 309 return makeLocalInstanceMirror(reflectee); | 479 return makeLocalInstanceMirror(reflectee); |
| 310 } | 480 } |
| 311 } | 481 } |
| OLD | NEW |