| 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" show UnmodifiableListView, UnmodifiableMapView; | 7 import "dart:collection" show UnmodifiableListView, UnmodifiableMapView; |
| 8 import "dart:async" show Future; |
| 8 | 9 |
| 10 var dirty = false; |
| 9 final emptyList = new UnmodifiableListView([]); | 11 final emptyList = new UnmodifiableListView([]); |
| 10 final emptyMap = new UnmodifiableMapView({}); | 12 final emptyMap = new UnmodifiableMapView({}); |
| 11 | 13 |
| 12 class _InternalMirrorError { | 14 class _InternalMirrorError { |
| 13 final String _msg; | 15 final String _msg; |
| 14 const _InternalMirrorError(String this._msg); | 16 const _InternalMirrorError(String this._msg); |
| 15 String toString() => _msg; | 17 String toString() => _msg; |
| 16 } | 18 } |
| 17 | 19 |
| 18 Map _makeMemberMap(List mirrors) { | 20 Map _makeMemberMap(List mirrors) { |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 | 219 |
| 218 Function operator [](String key) { | 220 Function operator [](String key) { |
| 219 var index = scanFor(key); | 221 var index = scanFor(key); |
| 220 var assoc = table[index]; | 222 var assoc = table[index]; |
| 221 if (null == assoc) return null; | 223 if (null == assoc) return null; |
| 222 assoc.usedSinceGrowth = true; | 224 assoc.usedSinceGrowth = true; |
| 223 return assoc.value; | 225 return assoc.value; |
| 224 } | 226 } |
| 225 } | 227 } |
| 226 | 228 |
| 229 class _LocalMirrorSystem extends MirrorSystem { |
| 230 final TypeMirror dynamicType = new _SpecialTypeMirror('dynamic'); |
| 231 final TypeMirror voidType = new _SpecialTypeMirror('void'); |
| 227 | 232 |
| 228 class _LocalMirrorSystem extends MirrorSystem { | 233 var _libraries; |
| 229 final Map<Uri, LibraryMirror> libraries; | 234 Map<Uri, LibraryMirror> get libraries { |
| 230 final IsolateMirror isolate; | 235 if ((_libraries == null) || dirty) { |
| 236 _libraries = new Map<Uri, LibraryMirror>.fromIterable( |
| 237 _computeLibraries(), key: (e) => e.uri); |
| 238 } |
| 239 return _libraries; |
| 240 } |
| 241 static _computeLibraries() native "MirrorSystem_libraries"; |
| 231 | 242 |
| 232 _LocalMirrorSystem(List<LibraryMirror> libraries, this.isolate) | 243 var _isolate; |
| 233 : this.libraries = new Map<Uri, LibraryMirror>.fromIterable( | 244 IsolateMirror get isolate { |
| 234 libraries, key: (e) => e.uri); | 245 if (_isolate == null) { |
| 235 | 246 _isolate = _computeIsolate(); |
| 236 TypeMirror _dynamicType = null; | |
| 237 TypeMirror get dynamicType { | |
| 238 if (_dynamicType == null) { | |
| 239 _dynamicType = new _SpecialTypeMirror('dynamic'); | |
| 240 } | 247 } |
| 241 return _dynamicType; | 248 return _isolate; |
| 242 } | 249 } |
| 243 | 250 static _computeIsolate() native "MirrorSystem_isolate"; |
| 244 TypeMirror _voidType = null; | |
| 245 TypeMirror get voidType { | |
| 246 if (_voidType == null) { | |
| 247 _voidType = new _SpecialTypeMirror('void'); | |
| 248 } | |
| 249 return _voidType; | |
| 250 } | |
| 251 | 251 |
| 252 String toString() => "MirrorSystem for isolate '${isolate.debugName}'"; | 252 String toString() => "MirrorSystem for isolate '${isolate.debugName}'"; |
| 253 } | 253 } |
| 254 | 254 |
| 255 class _SourceLocation implements SourceLocation { | 255 class _SourceLocation implements SourceLocation { |
| 256 _SourceLocation(uriString, this.line, this.column) | 256 _SourceLocation(uriString, this.line, this.column) |
| 257 : this.sourceUri = Uri.parse(uriString); | 257 : this.sourceUri = Uri.parse(uriString); |
| 258 | 258 |
| 259 // Line and column positions are 1-origin, or 0 if unknown. | 259 // Line and column positions are 1-origin, or 0 if unknown. |
| 260 final int line; | 260 final int line; |
| (...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1298 _invokeSetter(reflectee, setterName, value) | 1298 _invokeSetter(reflectee, setterName, value) |
| 1299 native 'LibraryMirror_invokeSetter'; | 1299 native 'LibraryMirror_invokeSetter'; |
| 1300 | 1300 |
| 1301 _computeMembers(reflectee) | 1301 _computeMembers(reflectee) |
| 1302 native "LibraryMirror_members"; | 1302 native "LibraryMirror_members"; |
| 1303 } | 1303 } |
| 1304 | 1304 |
| 1305 class _LocalLibraryDependencyMirror | 1305 class _LocalLibraryDependencyMirror |
| 1306 extends _LocalMirror implements LibraryDependencyMirror { | 1306 extends _LocalMirror implements LibraryDependencyMirror { |
| 1307 final LibraryMirror sourceLibrary; | 1307 final LibraryMirror sourceLibrary; |
| 1308 final LibraryMirror targetLibrary; | 1308 var _targetMirrorOrPrefix; |
| 1309 final List<CombinatorMirror> combinators; | 1309 final List<CombinatorMirror> combinators; |
| 1310 final Symbol prefix; | 1310 final Symbol prefix; |
| 1311 final bool isImport; | 1311 final bool isImport; |
| 1312 final bool isDeferred; | 1312 final bool isDeferred; |
| 1313 final List<InstanceMirror> metadata; | 1313 final List<InstanceMirror> metadata; |
| 1314 | 1314 |
| 1315 _LocalLibraryDependencyMirror(this.sourceLibrary, | 1315 _LocalLibraryDependencyMirror(this.sourceLibrary, |
| 1316 this.targetLibrary, | 1316 this._targetMirrorOrPrefix, |
| 1317 this.combinators, | 1317 this.combinators, |
| 1318 prefixString, | 1318 prefixString, |
| 1319 this.isImport, | 1319 this.isImport, |
| 1320 this.isDeferred, | 1320 this.isDeferred, |
| 1321 unwrappedMetadata) | 1321 unwrappedMetadata) |
| 1322 : prefix = _s(prefixString), | 1322 : prefix = _s(prefixString), |
| 1323 metadata = new UnmodifiableListView(unwrappedMetadata.map(reflect)); | 1323 metadata = new UnmodifiableListView(unwrappedMetadata.map(reflect)); |
| 1324 | 1324 |
| 1325 bool get isExport => !isImport; | 1325 bool get isExport => !isImport; |
| 1326 |
| 1327 LibraryMirror get targetLibrary { |
| 1328 if (_targetMirrorOrPrefix is _LocalLibraryMirror) { |
| 1329 return _targetMirrorOrPrefix; |
| 1330 } |
| 1331 var mirrorOrNull = _tryUpgradePrefix(_targetMirrorOrPrefix); |
| 1332 if (mirrorOrNull != null) { |
| 1333 _targetMirrorOrPrefix = mirrorOrNull; |
| 1334 } |
| 1335 return mirrorOrNull; |
| 1336 } |
| 1337 |
| 1338 Future<LibraryMirror> loadLibrary() { |
| 1339 if (_targetMirrorOrPrefix is _LocalLibraryMirror) { |
| 1340 return new Future.value(_targetMirrorOrPrefix); |
| 1341 } |
| 1342 var savedPrefix = _targetMirrorOrPrefix; |
| 1343 return savedPrefix.loadLibrary().then((_) { |
| 1344 return _tryUpgradePrefix(savedPrefix); |
| 1345 }); |
| 1346 } |
| 1347 |
| 1348 static _tryUpgradePrefix(libraryPrefix) native "LibraryMirror_fromPrefix"; |
| 1326 } | 1349 } |
| 1327 | 1350 |
| 1328 class _LocalCombinatorMirror extends _LocalMirror implements CombinatorMirror { | 1351 class _LocalCombinatorMirror extends _LocalMirror implements CombinatorMirror { |
| 1329 final List<Symbol> identifiers; | 1352 final List<Symbol> identifiers; |
| 1330 final bool isShow; | 1353 final bool isShow; |
| 1331 | 1354 |
| 1332 _LocalCombinatorMirror(identifierString, this.isShow) | 1355 _LocalCombinatorMirror(identifierString, this.isShow) |
| 1333 : this.identifiers = [_s(identifierString)]; | 1356 : this.identifiers = [_s(identifierString)]; |
| 1334 | 1357 |
| 1335 bool get isHide => !isShow; | 1358 bool get isHide => !isShow; |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1586 } | 1609 } |
| 1587 | 1610 |
| 1588 List<TypeVariableMirror> get typeVariables => emptyList; | 1611 List<TypeVariableMirror> get typeVariables => emptyList; |
| 1589 List<TypeMirror> get typeArguments => emptyList; | 1612 List<TypeMirror> get typeArguments => emptyList; |
| 1590 | 1613 |
| 1591 bool get isOriginalDeclaration => true; | 1614 bool get isOriginalDeclaration => true; |
| 1592 TypeMirror get originalDeclaration => this; | 1615 TypeMirror get originalDeclaration => this; |
| 1593 | 1616 |
| 1594 Symbol get qualifiedName => simpleName; | 1617 Symbol get qualifiedName => simpleName; |
| 1595 | 1618 |
| 1596 // TODO(11955): Remove once dynamicType and voidType are canonical objects in | |
| 1597 // the object store. | |
| 1598 bool operator ==(other) { | 1619 bool operator ==(other) { |
| 1599 if (other is! _SpecialTypeMirror) { | 1620 if (other is! _SpecialTypeMirror) { |
| 1600 return false; | 1621 return false; |
| 1601 } | 1622 } |
| 1602 return this.simpleName == other.simpleName; | 1623 return this.simpleName == other.simpleName; |
| 1603 } | 1624 } |
| 1604 | 1625 |
| 1605 int get hashCode => simpleName.hashCode; | 1626 int get hashCode => simpleName.hashCode; |
| 1606 | 1627 |
| 1607 String toString() => "TypeMirror on '${_n(simpleName)}'"; | 1628 String toString() => "TypeMirror on '${_n(simpleName)}'"; |
| 1608 | 1629 |
| 1609 bool isSubtypeOf(TypeMirror other) { | 1630 bool isSubtypeOf(TypeMirror other) { |
| 1610 return simpleName == #dynamic || other is _SpecialTypeMirror; | 1631 return simpleName == #dynamic || other is _SpecialTypeMirror; |
| 1611 } | 1632 } |
| 1612 | 1633 |
| 1613 bool isAssignableTo(TypeMirror other) { | 1634 bool isAssignableTo(TypeMirror other) { |
| 1614 return simpleName == #dynamic || other is _SpecialTypeMirror; | 1635 return simpleName == #dynamic || other is _SpecialTypeMirror; |
| 1615 } | 1636 } |
| 1616 } | 1637 } |
| 1617 | 1638 |
| 1618 class _Mirrors { | 1639 class _Mirrors { |
| 1619 static MirrorSystem _currentMirrorSystem = null; | 1640 static MirrorSystem _currentMirrorSystem = new _LocalMirrorSystem(); |
| 1620 | |
| 1621 // Creates a new local MirrorSystem. | |
| 1622 static MirrorSystem makeLocalMirrorSystem() | |
| 1623 native 'Mirrors_makeLocalMirrorSystem'; | |
| 1624 | |
| 1625 // The MirrorSystem for the current isolate. | |
| 1626 static MirrorSystem currentMirrorSystem() { | 1641 static MirrorSystem currentMirrorSystem() { |
| 1627 if (_currentMirrorSystem == null) { | |
| 1628 _currentMirrorSystem = makeLocalMirrorSystem(); | |
| 1629 } | |
| 1630 return _currentMirrorSystem; | 1642 return _currentMirrorSystem; |
| 1631 } | 1643 } |
| 1632 | 1644 |
| 1633 // Creates a new local mirror for some Object. | 1645 // Creates a new local mirror for some Object. |
| 1634 static InstanceMirror reflect(Object reflectee) { | 1646 static InstanceMirror reflect(Object reflectee) { |
| 1635 return reflectee is Function | 1647 return reflectee is Function |
| 1636 ? new _LocalClosureMirror(reflectee) | 1648 ? new _LocalClosureMirror(reflectee) |
| 1637 : new _LocalInstanceMirror(reflectee); | 1649 : new _LocalInstanceMirror(reflectee); |
| 1638 } | 1650 } |
| 1639 | 1651 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1662 if (typeMirror == null) { | 1674 if (typeMirror == null) { |
| 1663 typeMirror = makeLocalTypeMirror(key); | 1675 typeMirror = makeLocalTypeMirror(key); |
| 1664 _instanitationCache[key] = typeMirror; | 1676 _instanitationCache[key] = typeMirror; |
| 1665 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1677 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
| 1666 _declarationCache[key] = typeMirror; | 1678 _declarationCache[key] = typeMirror; |
| 1667 } | 1679 } |
| 1668 } | 1680 } |
| 1669 return typeMirror; | 1681 return typeMirror; |
| 1670 } | 1682 } |
| 1671 } | 1683 } |
| OLD | NEW |