| 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 import "dart:collection"; | 7 import "dart:collection"; |
| 8 | 8 |
| 9 // These values are allowed to be passed directly over the wire. | 9 // These values are allowed to be passed directly over the wire. |
| 10 bool _isSimpleValue(var value) { | 10 bool _isSimpleValue(var value) { |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 } | 185 } |
| 186 | 186 |
| 187 return reflect(this._invoke(_reflectee, | 187 return reflect(this._invoke(_reflectee, |
| 188 _n(memberName), | 188 _n(memberName), |
| 189 arguments, | 189 arguments, |
| 190 names)); | 190 names)); |
| 191 } | 191 } |
| 192 | 192 |
| 193 InstanceMirror getField(Symbol memberName) { | 193 InstanceMirror getField(Symbol memberName) { |
| 194 return reflect(this._invokeGetter(_reflectee, | 194 return reflect(this._invokeGetter(_reflectee, |
| 195 _n(memberName))); | 195 _n(memberName))); |
| 196 } | 196 } |
| 197 | 197 |
| 198 InstanceMirror setField(Symbol memberName, Object value) { | 198 InstanceMirror setField(Symbol memberName, Object value) { |
| 199 this._invokeSetter(_reflectee, | 199 this._invokeSetter(_reflectee, |
| 200 _n(memberName), | 200 _n(memberName), |
| 201 value); | 201 value); |
| 202 return reflect(value); | 202 return reflect(value); |
| 203 } | 203 } |
| 204 | 204 |
| 205 Future<InstanceMirror> invokeAsync(Symbol memberName, | 205 Future<InstanceMirror> invokeAsync(Symbol memberName, |
| 206 List positionalArguments, | 206 List positionalArguments, |
| 207 [Map<Symbol, dynamic> namedArguments]) { | 207 [Map<Symbol, dynamic> namedArguments]) { |
| 208 return new Future(() { | 208 return new Future(() { |
| 209 return this.invoke(memberName, | 209 return this.invoke(memberName, |
| 210 _unwrapAsyncPositionals(positionalArguments), | 210 _unwrapAsyncPositionals(positionalArguments), |
| 211 _unwrapAsyncNamed(namedArguments)); | 211 _unwrapAsyncNamed(namedArguments)); |
| 212 }); | 212 }); |
| 213 } | 213 } |
| 214 | 214 |
| 215 Future<InstanceMirror> getFieldAsync(Symbol memberName) { | 215 Future<InstanceMirror> getFieldAsync(Symbol memberName) { |
| 216 try { | 216 try { |
| 217 var result = this._invokeGetter(_reflectee, | 217 var result = this._invokeGetter(_reflectee, |
| 218 _n(memberName)); | 218 _n(memberName)); |
| 219 return new Future.value(reflect(result)); | 219 return new Future.value(reflect(result)); |
| 220 } catch(e) { | 220 } catch(e) { |
| 221 return new Future.error(e); | 221 return new Future.error(e); |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 | 224 |
| 225 Future<InstanceMirror> setFieldAsync(Symbol memberName, Object value) { | 225 Future<InstanceMirror> setFieldAsync(Symbol memberName, Object value) { |
| 226 try { | 226 try { |
| 227 var unwrappedValue; | 227 var unwrappedValue; |
| 228 if(_isSimpleValue(value)) { | 228 if(_isSimpleValue(value)) { |
| 229 unwrappedValue = value; | 229 unwrappedValue = value; |
| 230 } else if(value is InstanceMirror) { | 230 } else if(value is InstanceMirror) { |
| 231 unwrappedValue = value._reflectee; | 231 unwrappedValue = value._reflectee; |
| 232 } else { | 232 } else { |
| 233 throw "setter argument ($value) must be" | 233 throw "setter argument ($value) must be" |
| 234 "a simple value or InstanceMirror"; | 234 "a simple value or InstanceMirror"; |
| 235 } | 235 } |
| 236 | 236 |
| 237 this._invokeSetter(_reflectee, | 237 this._invokeSetter(_reflectee, |
| 238 _n(memberName), | 238 _n(memberName), |
| 239 unwrappedValue); | 239 unwrappedValue); |
| 240 return new Future.value(reflect(unwrappedValue)); | 240 return new Future.value(reflect(unwrappedValue)); |
| 241 } catch(e) { | 241 } catch(e) { |
| 242 return new Future.error(e); | 242 return new Future.error(e); |
| 243 } | 243 } |
| 244 } | 244 } |
| 245 | 245 |
| 246 static _validateArgument(int i, Object arg) | 246 static _validateArgument(int i, Object arg) |
| 247 { | 247 { |
| 248 if (arg is Mirror) { | 248 if (arg is Mirror) { |
| 249 if (arg is! InstanceMirror) { | 249 if (arg is! InstanceMirror) { |
| 250 throw new MirrorException( | 250 throw new MirrorException( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 278 bool hasReflectee = true; | 278 bool hasReflectee = true; |
| 279 | 279 |
| 280 get reflectee => _reflectee; | 280 get reflectee => _reflectee; |
| 281 | 281 |
| 282 delegate(Invocation invocation) { | 282 delegate(Invocation invocation) { |
| 283 if (_invokeOnClosure == null) { | 283 if (_invokeOnClosure == null) { |
| 284 // TODO(ahe): This is a total hack. We're using the mirror | 284 // TODO(ahe): This is a total hack. We're using the mirror |
| 285 // system to access a private field in a different library. For | 285 // system to access a private field in a different library. For |
| 286 // some reason, that works. On the other hand, calling a | 286 // some reason, that works. On the other hand, calling a |
| 287 // private method does not work. | 287 // private method does not work. |
| 288 | 288 |
| 289 _LocalInstanceMirrorImpl mirror = | 289 _LocalInstanceMirrorImpl mirror = |
| 290 reflect(invocation); | 290 reflect(invocation); |
| 291 _invokeOnClosure = reflectClass(invocation.runtimeType) | 291 _invokeOnClosure = reflectClass(invocation.runtimeType) |
| 292 .getField(const Symbol('_invokeOnClosure')).reflectee; | 292 .getField(const Symbol('_invokeOnClosure')).reflectee; |
| 293 } | 293 } |
| 294 return _invokeOnClosure(reflectee, invocation); | 294 return _invokeOnClosure(reflectee, invocation); |
| 295 } | 295 } |
| 296 | 296 |
| 297 String toString() => 'InstanceMirror on ${Error.safeToString(_reflectee)}'; | 297 String toString() => 'InstanceMirror on ${Error.safeToString(_reflectee)}'; |
| 298 | 298 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 } | 368 } |
| 369 return _function; | 369 return _function; |
| 370 } | 370 } |
| 371 | 371 |
| 372 InstanceMirror apply(List<Object> positionalArguments, | 372 InstanceMirror apply(List<Object> positionalArguments, |
| 373 [Map<Symbol, Object> namedArguments]) { | 373 [Map<Symbol, Object> namedArguments]) { |
| 374 // TODO(iposva): When closures get an ordinary call method, this can be | 374 // TODO(iposva): When closures get an ordinary call method, this can be |
| 375 // replaced with | 375 // replaced with |
| 376 // return this.invoke(#call, positionalArguments, namedArguments); | 376 // return this.invoke(#call, positionalArguments, namedArguments); |
| 377 // and the native ClosureMirror_apply can be removed. | 377 // and the native ClosureMirror_apply can be removed. |
| 378 | 378 |
| 379 int numPositionalArguments = positionalArguments.length + 1; // Receiver. | 379 int numPositionalArguments = positionalArguments.length + 1; // Receiver. |
| 380 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; | 380 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; |
| 381 int numArguments = numPositionalArguments + numNamedArguments; | 381 int numArguments = numPositionalArguments + numNamedArguments; |
| 382 List arguments = new List(numArguments); | 382 List arguments = new List(numArguments); |
| 383 arguments[0] = _reflectee; // Receiver. | 383 arguments[0] = _reflectee; // Receiver. |
| 384 arguments.setRange(1, numPositionalArguments, positionalArguments); | 384 arguments.setRange(1, numPositionalArguments, positionalArguments); |
| 385 List names = new List(numNamedArguments); | 385 List names = new List(numNamedArguments); |
| 386 int argumentIndex = numPositionalArguments; | 386 int argumentIndex = numPositionalArguments; |
| 387 int nameIndex = 0; | 387 int nameIndex = 0; |
| 388 if (numNamedArguments > 0) { | 388 if (numNamedArguments > 0) { |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 } | 508 } |
| 509 | 509 |
| 510 get _mixinApplicationName { | 510 get _mixinApplicationName { |
| 511 var mixins = new List<ClassMirror>(); | 511 var mixins = new List<ClassMirror>(); |
| 512 var klass = this; | 512 var klass = this; |
| 513 while (_computeMixin(klass._reflectee) != null) { | 513 while (_computeMixin(klass._reflectee) != null) { |
| 514 mixins.add(klass.mixin); | 514 mixins.add(klass.mixin); |
| 515 klass = klass.superclass; | 515 klass = klass.superclass; |
| 516 } | 516 } |
| 517 return _s( | 517 return _s( |
| 518 _n(klass.qualifiedName) | 518 _n(klass.qualifiedName) |
| 519 + ' with ' | 519 + ' with ' |
| 520 + mixins.reversed.map((m)=>_n(m.qualifiedName)).join(', ')); | 520 + mixins.reversed.map((m)=>_n(m.qualifiedName)).join(', ')); |
| 521 } | 521 } |
| 522 | 522 |
| 523 var _mixin; | 523 var _mixin; |
| 524 ClassMirror get mixin { | 524 ClassMirror get mixin { |
| 525 if (_mixin == null) { | 525 if (_mixin == null) { |
| 526 if (_isMixinTypedef) { | 526 if (_isMixinTypedef) { |
| 527 _mixin = _trueSuperclass.mixin; | 527 _mixin = _trueSuperclass.mixin; |
| 528 } else { | 528 } else { |
| 529 var mixinType = _computeMixin(_reflectee); | 529 var mixinType = _computeMixin(_reflectee); |
| 530 if (mixinType == null) { | 530 if (mixinType == null) { |
| 531 // The reflectee is not a mixin application. | 531 // The reflectee is not a mixin application. |
| 532 _mixin = this; | 532 _mixin = this; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 return _variables; | 587 return _variables; |
| 588 } | 588 } |
| 589 | 589 |
| 590 Map<Symbol, MethodMirror> _constructors; | 590 Map<Symbol, MethodMirror> _constructors; |
| 591 Map<Symbol, MethodMirror> get constructors { | 591 Map<Symbol, MethodMirror> get constructors { |
| 592 if (_constructors == null) { | 592 if (_constructors == null) { |
| 593 var constructorsList = _computeConstructors(_reflectee); | 593 var constructorsList = _computeConstructors(_reflectee); |
| 594 var stringName = _n(simpleName); | 594 var stringName = _n(simpleName); |
| 595 constructorsList.forEach((c) => c._patchConstructorName(stringName)); | 595 constructorsList.forEach((c) => c._patchConstructorName(stringName)); |
| 596 _constructors = _makeMemberMap(constructorsList); | 596 _constructors = _makeMemberMap(constructorsList); |
| 597 } | 597 } |
| 598 return _constructors; | 598 return _constructors; |
| 599 } | 599 } |
| 600 | 600 |
| 601 Map<Symbol, TypeVariableMirror> _typeVariables = null; | 601 List<TypeVariableMirror> _typeVariables = null; |
| 602 Map<Symbol, TypeVariableMirror> get typeVariables { | 602 List<TypeVariableMirror> get typeVariables { |
| 603 if (_typeVariables == null) { | 603 if (_typeVariables == null) { |
| 604 List params = _ClassMirror_type_variables(_reflectee); | 604 List params = _ClassMirror_type_variables(_reflectee); |
| 605 _typeVariables = new LinkedHashMap<Symbol, TypeVariableMirror>(); | 605 _typeVariables = new List<TypeVariableMirror>(); |
| 606 var mirror; | 606 var mirror; |
| 607 for (var i = 0; i < params.length; i += 2) { | 607 for (var i = 0; i < params.length; i += 2) { |
| 608 mirror = new _LocalTypeVariableMirrorImpl( | 608 mirror = new _LocalTypeVariableMirrorImpl( |
| 609 params[i + 1], params[i], this); | 609 params[i + 1], params[i], this); |
| 610 _typeVariables[mirror.simpleName] = mirror; | 610 _typeVariables.add(mirror); |
| 611 } | 611 } |
| 612 } | 612 } |
| 613 return _typeVariables; | 613 return _typeVariables; |
| 614 } | 614 } |
| 615 | 615 |
| 616 Map<Symbol, TypeMirror> _typeArguments = null; | 616 List<TypeMirror> _typeArguments = null; |
| 617 Map<Symbol, TypeMirror> get typeArguments { | 617 List<TypeMirror> get typeArguments { |
| 618 if(_typeArguments == null) { | 618 if(_typeArguments == null) { |
| 619 if(_isGenericDeclaration) { | 619 if(_isGenericDeclaration) { |
| 620 _typeArguments = new LinkedHashMap<Symbol, TypeMirror>(); | 620 _typeArguments = new List<TypeMirror>(); |
| 621 } else { | 621 } else { |
| 622 _typeArguments = | 622 _typeArguments = |
| 623 new LinkedHashMap<Symbol, TypeMirror>.fromIterables( | 623 new List<TypeMirror>.from(_computeTypeArguments(_reflectedType)); |
| 624 typeVariables.keys, _computeTypeArguments(_reflectedType)); | |
| 625 } | 624 } |
| 626 } | 625 } |
| 627 return _typeArguments; | 626 return _typeArguments; |
| 628 } | 627 } |
| 629 | 628 |
| 630 bool get isOriginalDeclaration => !_isGeneric || _isGenericDeclaration; | 629 bool get isOriginalDeclaration => !_isGeneric || _isGenericDeclaration; |
| 631 | 630 |
| 632 ClassMirror get originalDeclaration { | 631 ClassMirror get originalDeclaration { |
| 633 if (isOriginalDeclaration) { | 632 if (isOriginalDeclaration) { |
| 634 return this; | 633 return this; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 native "ClassMirror_supertype"; | 697 native "ClassMirror_supertype"; |
| 699 | 698 |
| 700 static _interfaces(reflectee) | 699 static _interfaces(reflectee) |
| 701 native "ClassMirror_interfaces"; | 700 native "ClassMirror_interfaces"; |
| 702 | 701 |
| 703 static _computeMixin(reflectee) | 702 static _computeMixin(reflectee) |
| 704 native "ClassMirror_mixin"; | 703 native "ClassMirror_mixin"; |
| 705 | 704 |
| 706 _computeMembers(reflectee) | 705 _computeMembers(reflectee) |
| 707 native "ClassMirror_members"; | 706 native "ClassMirror_members"; |
| 708 | 707 |
| 709 _computeConstructors(reflectee) | 708 _computeConstructors(reflectee) |
| 710 native "ClassMirror_constructors"; | 709 native "ClassMirror_constructors"; |
| 711 | 710 |
| 712 _invoke(reflectee, memberName, arguments, argumentNames) | 711 _invoke(reflectee, memberName, arguments, argumentNames) |
| 713 native 'ClassMirror_invoke'; | 712 native 'ClassMirror_invoke'; |
| 714 | 713 |
| 715 _invokeGetter(reflectee, getterName) | 714 _invokeGetter(reflectee, getterName) |
| 716 native 'ClassMirror_invokeGetter'; | 715 native 'ClassMirror_invokeGetter'; |
| 717 | 716 |
| 718 _invokeSetter(reflectee, setterName, value) | 717 _invokeSetter(reflectee, setterName, value) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 } | 799 } |
| 801 | 800 |
| 802 List<InstanceMirror> get metadata { | 801 List<InstanceMirror> get metadata { |
| 803 // Get the metadata objects, convert them into InstanceMirrors using | 802 // Get the metadata objects, convert them into InstanceMirrors using |
| 804 // reflect() and then make them into a Dart list. | 803 // reflect() and then make them into a Dart list. |
| 805 return _metadata(_reflectee).map(reflect).toList(growable:false); | 804 return _metadata(_reflectee).map(reflect).toList(growable:false); |
| 806 } | 805 } |
| 807 | 806 |
| 808 bool operator ==(other) { | 807 bool operator ==(other) { |
| 809 return this.runtimeType == other.runtimeType && | 808 return this.runtimeType == other.runtimeType && |
| 810 this._reflectee == other._reflectee; | 809 this._reflectee == other._reflectee; |
| 811 } | 810 } |
| 812 | 811 |
| 813 int get hashCode => simpleName.hashCode; | 812 int get hashCode => simpleName.hashCode; |
| 814 } | 813 } |
| 815 | 814 |
| 816 class _LocalTypeVariableMirrorImpl extends _LocalDeclarationMirrorImpl | 815 class _LocalTypeVariableMirrorImpl extends _LocalDeclarationMirrorImpl |
| 817 implements TypeVariableMirror { | 816 implements TypeVariableMirror { |
| 818 _LocalTypeVariableMirrorImpl(reflectee, | 817 _LocalTypeVariableMirrorImpl(reflectee, |
| 819 String simpleName, | 818 String simpleName, |
| 820 this._owner) | 819 this._owner) |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 981 } | 980 } |
| 982 | 981 |
| 983 List<InstanceMirror> get metadata { | 982 List<InstanceMirror> get metadata { |
| 984 // Get the metadata objects, convert them into InstanceMirrors using | 983 // Get the metadata objects, convert them into InstanceMirrors using |
| 985 // reflect() and then make them into a Dart list. | 984 // reflect() and then make them into a Dart list. |
| 986 return _metadata(_reflectee).map(reflect).toList(growable:false); | 985 return _metadata(_reflectee).map(reflect).toList(growable:false); |
| 987 } | 986 } |
| 988 | 987 |
| 989 bool operator ==(other) { | 988 bool operator ==(other) { |
| 990 return this.runtimeType == other.runtimeType && | 989 return this.runtimeType == other.runtimeType && |
| 991 this._reflectee == other._reflectee; | 990 this._reflectee == other._reflectee; |
| 992 } | 991 } |
| 993 | 992 |
| 994 int get hashCode => simpleName.hashCode; | 993 int get hashCode => simpleName.hashCode; |
| 995 | 994 |
| 996 String toString() => "LibraryMirror on '${_n(simpleName)}'"; | 995 String toString() => "LibraryMirror on '${_n(simpleName)}'"; |
| 997 | 996 |
| 998 _invoke(reflectee, memberName, arguments, argumentNames) | 997 _invoke(reflectee, memberName, arguments, argumentNames) |
| 999 native 'LibraryMirror_invoke'; | 998 native 'LibraryMirror_invoke'; |
| 1000 | 999 |
| 1001 _invokeGetter(reflectee, getterName) | 1000 _invokeGetter(reflectee, getterName) |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1287 } | 1286 } |
| 1288 } else { | 1287 } else { |
| 1289 // Make a remote mirror system | 1288 // Make a remote mirror system |
| 1290 throw new UnimplementedError( | 1289 throw new UnimplementedError( |
| 1291 'Remote mirror support is not implemented'); | 1290 'Remote mirror support is not implemented'); |
| 1292 } | 1291 } |
| 1293 } | 1292 } |
| 1294 | 1293 |
| 1295 // Creates a new local mirror for some Object. | 1294 // Creates a new local mirror for some Object. |
| 1296 static InstanceMirror reflect(Object reflectee) { | 1295 static InstanceMirror reflect(Object reflectee) { |
| 1297 return reflectee is Function | 1296 return reflectee is Function |
| 1298 ? new _LocalClosureMirrorImpl(reflectee) | 1297 ? new _LocalClosureMirrorImpl(reflectee) |
| 1299 : new _LocalInstanceMirrorImpl(reflectee); | 1298 : new _LocalInstanceMirrorImpl(reflectee); |
| 1300 } | 1299 } |
| 1301 | 1300 |
| 1302 static ClassMirror makeLocalClassMirror(Type key) | 1301 static ClassMirror makeLocalClassMirror(Type key) |
| 1303 native "Mirrors_makeLocalClassMirror"; | 1302 native "Mirrors_makeLocalClassMirror"; |
| 1304 static TypeMirror makeLocalTypeMirror(Type key) | 1303 static TypeMirror makeLocalTypeMirror(Type key) |
| 1305 native "Mirrors_makeLocalTypeMirror"; | 1304 native "Mirrors_makeLocalTypeMirror"; |
| 1306 | 1305 |
| 1307 static Expando<ClassMirror> _declarationCache = new Expando("ClassMirror"); | 1306 static Expando<ClassMirror> _declarationCache = new Expando("ClassMirror"); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1324 if (typeMirror == null) { | 1323 if (typeMirror == null) { |
| 1325 typeMirror = makeLocalTypeMirror(key); | 1324 typeMirror = makeLocalTypeMirror(key); |
| 1326 _instanitationCache[key] = typeMirror; | 1325 _instanitationCache[key] = typeMirror; |
| 1327 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1326 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
| 1328 _declarationCache[key] = typeMirror; | 1327 _declarationCache[key] = typeMirror; |
| 1329 } | 1328 } |
| 1330 } | 1329 } |
| 1331 return typeMirror; | 1330 return typeMirror; |
| 1332 } | 1331 } |
| 1333 } | 1332 } |
| OLD | NEW |