Chromium Code Reviews| 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 } |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 'positional argument $i ($arg) was not a simple value'); | 261 'positional argument $i ($arg) was not a simple value'); |
| 262 } | 262 } |
| 263 } | 263 } |
| 264 } | 264 } |
| 265 | 265 |
| 266 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl | 266 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl |
| 267 implements InstanceMirror { | 267 implements InstanceMirror { |
| 268 // TODO(ahe): This is a hack, see delegate below. | 268 // TODO(ahe): This is a hack, see delegate below. |
| 269 static Function _invokeOnClosure; | 269 static Function _invokeOnClosure; |
| 270 | 270 |
| 271 _LocalInstanceMirrorImpl(this._type, | 271 _LocalInstanceMirrorImpl(reflectee) : super(reflectee); |
| 272 reflectee) : super(reflectee) {} | |
| 273 | 272 |
| 274 var _type; | 273 ClassMirror _type; |
| 275 ClassMirror get type { | 274 ClassMirror get type { |
| 276 if (_type is! Mirror) { | 275 if (_type == null) { |
| 277 _type = _type.resolve(mirrors); | 276 // Note it not safe to use reflectee.runtimeType because runtimeType may |
| 277 // be overridden. | |
| 278 _type = reflectClass(_computeType(reflectee)); | |
| 278 } | 279 } |
| 279 return _type; | 280 return _type; |
| 280 } | 281 } |
| 281 | 282 |
| 282 // LocalInstanceMirrors always reflect local instances | 283 // LocalInstanceMirrors always reflect local instances |
| 283 bool hasReflectee = true; | 284 bool hasReflectee = true; |
| 284 | 285 |
| 285 get reflectee => _reflectee; | 286 get reflectee => _reflectee; |
| 286 | 287 |
| 287 delegate(Invocation invocation) { | 288 delegate(Invocation invocation) { |
| 288 if (_invokeOnClosure == null) { | 289 if (_invokeOnClosure == null) { |
| 289 // TODO(ahe): This is a total hack. We're using the mirror | 290 // TODO(ahe): This is a total hack. We're using the mirror |
| 290 // system to access a private field in a different library. For | 291 // system to access a private field in a different library. For |
| 291 // some reason, that works. On the other hand, calling a | 292 // some reason, that works. On the other hand, calling a |
| 292 // private method does not work. | 293 // private method does not work. |
| 293 | 294 |
| 294 _LocalInstanceMirrorImpl mirror = | 295 _LocalInstanceMirrorImpl mirror = |
| 295 _Mirrors.makeLocalInstanceMirror(invocation); | 296 reflect(invocation); |
| 296 _invokeOnClosure = | 297 _invokeOnClosure = reflectClass(invocation.runtimeType) |
| 297 reflectClass(invocation.runtimeType).getField(const Symbol('_invokeOnC losure')).reflectee; | 298 .getField(const Symbol('_invokeOnClosure')).reflectee; |
| 298 } | 299 } |
| 299 return _invokeOnClosure(reflectee, invocation); | 300 return _invokeOnClosure(reflectee, invocation); |
| 300 } | 301 } |
| 301 | 302 |
| 302 String toString() => 'InstanceMirror on ${Error.safeToString(_reflectee)}'; | 303 String toString() => 'InstanceMirror on ${Error.safeToString(_reflectee)}'; |
| 303 | 304 |
| 304 _invoke(reflectee, functionName, positionalArguments) | 305 _invoke(reflectee, functionName, positionalArguments) |
| 305 native 'InstanceMirror_invoke'; | 306 native 'InstanceMirror_invoke'; |
| 306 | 307 |
| 307 _invokeGetter(reflectee, getterName) | 308 _invokeGetter(reflectee, getterName) |
| 308 native 'InstanceMirror_invokeGetter'; | 309 native 'InstanceMirror_invokeGetter'; |
| 309 | 310 |
| 310 _invokeSetter(reflectee, setterName, value) | 311 _invokeSetter(reflectee, setterName, value) |
| 311 native 'InstanceMirror_invokeSetter'; | 312 native 'InstanceMirror_invokeSetter'; |
| 313 | |
| 314 static _computeType(reflectee) | |
| 315 native 'Object_runtimeType'; | |
|
rmacnak
2013/07/29 18:45:06
Woo! Reusing a primitive.
| |
| 312 } | 316 } |
| 313 | 317 |
| 314 class _LocalClosureMirrorImpl extends _LocalInstanceMirrorImpl | 318 class _LocalClosureMirrorImpl extends _LocalInstanceMirrorImpl |
| 315 implements ClosureMirror { | 319 implements ClosureMirror { |
| 316 _LocalClosureMirrorImpl(type, | 320 _LocalClosureMirrorImpl(reflectee) : super(reflectee); |
| 317 reflectee, | |
| 318 this.function) : super(type, reflectee) {} | |
| 319 | 321 |
| 320 final MethodMirror function; | 322 MethodMirror _function; |
| 323 MethodMirror get function { | |
| 324 if (_function == null) { | |
| 325 _function = _computeFunction(reflectee); | |
| 326 } | |
| 327 return _function; | |
| 328 } | |
| 321 | 329 |
| 322 String get source { | 330 String get source { |
| 323 throw new UnimplementedError( | 331 throw new UnimplementedError( |
| 324 'ClosureMirror.source is not implemented'); | 332 'ClosureMirror.source is not implemented'); |
| 325 } | 333 } |
| 326 | 334 |
| 327 InstanceMirror apply(List<Object> positionalArguments, | 335 InstanceMirror apply(List<Object> positionalArguments, |
| 328 [Map<Symbol, Object> namedArguments]) { | 336 [Map<Symbol, Object> namedArguments]) { |
| 329 if (namedArguments != null) { | 337 if (namedArguments != null) { |
| 330 throw new UnimplementedError( | 338 throw new UnimplementedError( |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 345 | 353 |
| 346 try { | 354 try { |
| 347 var result = _apply(_reflectee, | 355 var result = _apply(_reflectee, |
| 348 _unwarpAsyncPositionals(positionalArguments)); | 356 _unwarpAsyncPositionals(positionalArguments)); |
| 349 return new Future.value(reflect(result)); | 357 return new Future.value(reflect(result)); |
| 350 } on MirroredError catch(e) { | 358 } on MirroredError catch(e) { |
| 351 return new Future.error(e); | 359 return new Future.error(e); |
| 352 } | 360 } |
| 353 } | 361 } |
| 354 | 362 |
| 355 | |
| 356 | |
| 357 Future<InstanceMirror> findInContext(Symbol name) { | 363 Future<InstanceMirror> findInContext(Symbol name) { |
| 358 throw new UnimplementedError( | 364 throw new UnimplementedError( |
| 359 'ClosureMirror.findInContext() is not implemented'); | 365 'ClosureMirror.findInContext() is not implemented'); |
| 360 } | 366 } |
| 361 | 367 |
| 368 String toString() => "ClosureMirror on '${Error.safeToString(_reflectee)}'"; | |
| 369 | |
| 362 static _apply(reflectee, positionalArguments) | 370 static _apply(reflectee, positionalArguments) |
| 363 native 'ClosureMirror_apply'; | 371 native 'ClosureMirror_apply'; |
| 364 | 372 |
| 365 String toString() => "ClosureMirror on '${Error.safeToString(_reflectee)}'"; | 373 static _computeFunction(reflectee) |
| 374 native 'ClosureMirror_function'; | |
| 366 } | 375 } |
| 367 | 376 |
| 368 class _LazyTypeMirror { | 377 class _LazyTypeMirror { |
| 369 _LazyTypeMirror(String this.libraryUrl, String typeName) | 378 _LazyTypeMirror(String this.libraryUrl, String typeName) |
| 370 : this.typeName = _s(typeName); | 379 : this.typeName = _s(typeName); |
| 371 | 380 |
| 372 TypeMirror resolve(MirrorSystem mirrors) { | 381 TypeMirror resolve(MirrorSystem mirrors) { |
| 373 if (libraryUrl == null) { | 382 if (libraryUrl == null) { |
| 374 if (typeName == const Symbol('dynamic')) { | 383 if (typeName == const Symbol('dynamic')) { |
| 375 return mirrors.dynamicType; | 384 return mirrors.dynamicType; |
| (...skipping 834 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1210 } catch (exception) { | 1219 } catch (exception) { |
| 1211 return new Future<MirrorSystem>.error(exception); | 1220 return new Future<MirrorSystem>.error(exception); |
| 1212 } | 1221 } |
| 1213 } else { | 1222 } else { |
| 1214 // Make a remote mirror system | 1223 // Make a remote mirror system |
| 1215 throw new UnimplementedError( | 1224 throw new UnimplementedError( |
| 1216 'Remote mirror support is not implemented'); | 1225 'Remote mirror support is not implemented'); |
| 1217 } | 1226 } |
| 1218 } | 1227 } |
| 1219 | 1228 |
| 1220 // Creates a new local InstanceMirror | |
| 1221 static InstanceMirror makeLocalInstanceMirror(Object reflectee) | |
| 1222 native 'Mirrors_makeLocalInstanceMirror'; | |
| 1223 | |
| 1224 // Creates a new local mirror for some Object. | 1229 // Creates a new local mirror for some Object. |
| 1225 static InstanceMirror reflect(Object reflectee) { | 1230 static InstanceMirror reflect(Object reflectee) { |
| 1226 return makeLocalInstanceMirror(reflectee); | 1231 return reflectee is Function |
| 1232 ? new _LocalClosureMirrorImpl(reflectee) | |
| 1233 : new _LocalInstanceMirrorImpl(reflectee); | |
| 1227 } | 1234 } |
| 1228 | 1235 |
| 1229 // Creates a new local ClassMirror. | 1236 // Creates a new local ClassMirror. |
| 1230 static ClassMirror makeLocalClassMirror(Type key) | 1237 static ClassMirror makeLocalClassMirror(Type key) |
| 1231 native "Mirrors_makeLocalClassMirror"; | 1238 native "Mirrors_makeLocalClassMirror"; |
| 1232 | 1239 |
| 1233 static Expando<ClassMirror> _classMirrorCache = new Expando("ClassMirror"); | 1240 static Expando<ClassMirror> _classMirrorCache = new Expando("ClassMirror"); |
| 1234 static ClassMirror reflectClass(Type key) { | 1241 static ClassMirror reflectClass(Type key) { |
| 1235 var classMirror = _classMirrorCache[key]; | 1242 var classMirror = _classMirrorCache[key]; |
| 1236 if (classMirror == null) { | 1243 if (classMirror == null) { |
| 1237 classMirror = makeLocalClassMirror(key); | 1244 classMirror = makeLocalClassMirror(key); |
| 1238 _classMirrorCache[key] = classMirror; | 1245 _classMirrorCache[key] = classMirror; |
| 1239 } | 1246 } |
| 1240 return classMirror; | 1247 return classMirror; |
| 1241 } | 1248 } |
| 1242 } | 1249 } |
| OLD | NEW |