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 final emptyList = new UnmodifiableListView([]); | 9 final emptyList = new UnmodifiableListView([]); |
10 final emptyMap = new _UnmodifiableMapView({}); | 10 final emptyMap = new _UnmodifiableMapView({}); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 implements IsolateMirror { | 193 implements IsolateMirror { |
194 _LocalIsolateMirrorImpl(this.debugName, this.rootLibrary); | 194 _LocalIsolateMirrorImpl(this.debugName, this.rootLibrary); |
195 | 195 |
196 final String debugName; | 196 final String debugName; |
197 final bool isCurrent = true; | 197 final bool isCurrent = true; |
198 final LibraryMirror rootLibrary; | 198 final LibraryMirror rootLibrary; |
199 | 199 |
200 String toString() => "IsolateMirror on '$debugName'"; | 200 String toString() => "IsolateMirror on '$debugName'"; |
201 } | 201 } |
202 | 202 |
| 203 class _InvocationTrampoline implements Function { |
| 204 ObjectMirror _receiver; |
| 205 Symbol _selector; |
| 206 _InvocationTrampoline(this._receiver, this._selector); |
| 207 noSuchMethod(Invocation msg) { |
| 208 if (msg.memberName != #call) return super.noSuchMethod(msg); |
| 209 return _receiver.invoke(_selector, |
| 210 msg.positionalArguments, |
| 211 msg.namedArguments); |
| 212 } |
| 213 } |
| 214 |
203 abstract class _LocalObjectMirrorImpl extends _LocalMirrorImpl | 215 abstract class _LocalObjectMirrorImpl extends _LocalMirrorImpl |
204 implements ObjectMirror { | 216 implements ObjectMirror { |
205 _LocalObjectMirrorImpl(this._reflectee); | 217 _LocalObjectMirrorImpl(this._reflectee); |
206 | 218 |
207 final _reflectee; // May be a MirrorReference or an ordinary object. | 219 final _reflectee; // May be a MirrorReference or an ordinary object. |
208 | 220 |
209 InstanceMirror invoke(Symbol memberName, | 221 InstanceMirror invoke(Symbol memberName, |
210 List positionalArguments, | 222 List positionalArguments, |
211 [Map<Symbol, dynamic> namedArguments]) { | 223 [Map<Symbol, dynamic> namedArguments]) { |
212 | 224 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 return other is _LocalInstanceMirrorImpl && | 353 return other is _LocalInstanceMirrorImpl && |
342 identical(_reflectee, other._reflectee); | 354 identical(_reflectee, other._reflectee); |
343 } | 355 } |
344 | 356 |
345 int get hashCode { | 357 int get hashCode { |
346 // Avoid hash collisions with the reflectee. This constant is in Smi range | 358 // Avoid hash collisions with the reflectee. This constant is in Smi range |
347 // and happens to be the inner padding from RFC 2104. | 359 // and happens to be the inner padding from RFC 2104. |
348 return identityHashCode(_reflectee) ^ 0x36363636; | 360 return identityHashCode(_reflectee) ^ 0x36363636; |
349 } | 361 } |
350 | 362 |
| 363 Function operator [](Symbol selector) { |
| 364 bool found = false; |
| 365 for (ClassMirror c = type; c != null; c = c.superclass) { |
| 366 var target = c.methods[selector]; |
| 367 if (target != null && !target.isStatic && target.isRegularMethod) { |
| 368 found = true; |
| 369 break; |
| 370 } |
| 371 } |
| 372 if (!found) { |
| 373 throw new ArgumentError( |
| 374 "${MirrorSystem.getName(type.simpleName)} has no instance method " |
| 375 "${MirrorSystem.getName(selector)}"); |
| 376 } |
| 377 return new _InvocationTrampoline(this, selector); |
| 378 } |
| 379 |
351 // Override to include the receiver in the arguments. | 380 // Override to include the receiver in the arguments. |
352 InstanceMirror invoke(Symbol memberName, | 381 InstanceMirror invoke(Symbol memberName, |
353 List positionalArguments, | 382 List positionalArguments, |
354 [Map<Symbol, dynamic> namedArguments]) { | 383 [Map<Symbol, dynamic> namedArguments]) { |
355 int numPositionalArguments = positionalArguments.length + 1; // Receiver. | 384 int numPositionalArguments = positionalArguments.length + 1; // Receiver. |
356 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; | 385 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; |
357 int numArguments = numPositionalArguments + numNamedArguments; | 386 int numArguments = numPositionalArguments + numNamedArguments; |
358 List arguments = new List(numArguments); | 387 List arguments = new List(numArguments); |
359 arguments[0] = _reflectee; // Receiver. | 388 arguments[0] = _reflectee; // Receiver. |
360 arguments.setRange(1, numPositionalArguments, positionalArguments); | 389 arguments.setRange(1, numPositionalArguments, positionalArguments); |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 ClassMirror get originalDeclaration { | 733 ClassMirror get originalDeclaration { |
705 if (isOriginalDeclaration) { | 734 if (isOriginalDeclaration) { |
706 return this; | 735 return this; |
707 } else { | 736 } else { |
708 return reflectClass(_reflectedType); | 737 return reflectClass(_reflectedType); |
709 } | 738 } |
710 } | 739 } |
711 | 740 |
712 String toString() => "ClassMirror on '${MirrorSystem.getName(simpleName)}'"; | 741 String toString() => "ClassMirror on '${MirrorSystem.getName(simpleName)}'"; |
713 | 742 |
| 743 Function operator [](Symbol selector) { |
| 744 var target = methods[selector]; |
| 745 if (target == null || !target.isStatic || !target.isRegularMethod) { |
| 746 throw new ArgumentError( |
| 747 "${MirrorSystem.getName(simpleName)} has no static method " |
| 748 "${MirrorSystem.getName(selector)}"); |
| 749 } |
| 750 return new _InvocationTrampoline(this, selector); |
| 751 } |
| 752 |
714 InstanceMirror newInstance(Symbol constructorName, | 753 InstanceMirror newInstance(Symbol constructorName, |
715 List positionalArguments, | 754 List positionalArguments, |
716 [Map<Symbol, dynamic> namedArguments]) { | 755 [Map<Symbol, dynamic> namedArguments]) { |
717 // Native code will add the 1 or 2 implicit arguments depending on whether | 756 // Native code will add the 1 or 2 implicit arguments depending on whether |
718 // we end up invoking a factory or constructor respectively. | 757 // we end up invoking a factory or constructor respectively. |
719 int numPositionalArguments = positionalArguments.length; | 758 int numPositionalArguments = positionalArguments.length; |
720 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; | 759 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; |
721 int numArguments = numPositionalArguments + numNamedArguments; | 760 int numArguments = numPositionalArguments + numNamedArguments; |
722 List arguments = new List(numArguments); | 761 List arguments = new List(numArguments); |
723 arguments.setRange(0, numPositionalArguments, positionalArguments); | 762 arguments.setRange(0, numPositionalArguments, positionalArguments); |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1181 | 1220 |
1182 bool operator ==(other) { | 1221 bool operator ==(other) { |
1183 return this.runtimeType == other.runtimeType && | 1222 return this.runtimeType == other.runtimeType && |
1184 this._reflectee == other._reflectee; | 1223 this._reflectee == other._reflectee; |
1185 } | 1224 } |
1186 | 1225 |
1187 int get hashCode => simpleName.hashCode; | 1226 int get hashCode => simpleName.hashCode; |
1188 | 1227 |
1189 String toString() => "LibraryMirror on '${_n(simpleName)}'"; | 1228 String toString() => "LibraryMirror on '${_n(simpleName)}'"; |
1190 | 1229 |
| 1230 Function operator [](Symbol selector) { |
| 1231 var target = functions[selector]; |
| 1232 if (target == null || !target.isRegularMethod) { |
| 1233 throw new ArgumentError( |
| 1234 "${MirrorSystem.getName(simpleName)} has no top-level method " |
| 1235 "${MirrorSystem.getName(selector)}"); |
| 1236 } |
| 1237 return new _InvocationTrampoline(this, selector); |
| 1238 } |
| 1239 |
1191 _invoke(reflectee, memberName, arguments, argumentNames) | 1240 _invoke(reflectee, memberName, arguments, argumentNames) |
1192 native 'LibraryMirror_invoke'; | 1241 native 'LibraryMirror_invoke'; |
1193 | 1242 |
1194 _invokeGetter(reflectee, getterName) | 1243 _invokeGetter(reflectee, getterName) |
1195 native 'LibraryMirror_invokeGetter'; | 1244 native 'LibraryMirror_invokeGetter'; |
1196 | 1245 |
1197 _invokeSetter(reflectee, setterName, value) | 1246 _invokeSetter(reflectee, setterName, value) |
1198 native 'LibraryMirror_invokeSetter'; | 1247 native 'LibraryMirror_invokeSetter'; |
1199 | 1248 |
1200 _computeMembers(reflectee) | 1249 _computeMembers(reflectee) |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1526 if (typeMirror == null) { | 1575 if (typeMirror == null) { |
1527 typeMirror = makeLocalTypeMirror(key); | 1576 typeMirror = makeLocalTypeMirror(key); |
1528 _instanitationCache[key] = typeMirror; | 1577 _instanitationCache[key] = typeMirror; |
1529 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1578 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
1530 _declarationCache[key] = typeMirror; | 1579 _declarationCache[key] = typeMirror; |
1531 } | 1580 } |
1532 } | 1581 } |
1533 return typeMirror; | 1582 return typeMirror; |
1534 } | 1583 } |
1535 } | 1584 } |
OLD | NEW |