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 String _n(Symbol symbol) => _symbol_dev.Symbol.getName(symbol); | 20 String _n(Symbol symbol) => _symbol_dev.Symbol.getName(symbol); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 213 |
212 Function operator [](String key) { | 214 Function operator [](String key) { |
213 var index = scanFor(key); | 215 var index = scanFor(key); |
214 var assoc = table[index]; | 216 var assoc = table[index]; |
215 if (null == assoc) return null; | 217 if (null == assoc) return null; |
216 assoc.usedSinceGrowth = true; | 218 assoc.usedSinceGrowth = true; |
217 return assoc.value; | 219 return assoc.value; |
218 } | 220 } |
219 } | 221 } |
220 | 222 |
| 223 class _LocalMirrorSystem extends MirrorSystem { |
| 224 final TypeMirror dynamicType = new _SpecialTypeMirror('dynamic'); |
| 225 final TypeMirror voidType = new _SpecialTypeMirror('void'); |
221 | 226 |
222 class _LocalMirrorSystem extends MirrorSystem { | 227 var _libraries; |
223 final Map<Uri, LibraryMirror> libraries; | 228 Map<Uri, LibraryMirror> get libraries { |
224 final IsolateMirror isolate; | 229 if ((_libraries == null) || dirty) { |
| 230 _libraries = new Map<Uri, LibraryMirror>.fromIterable( |
| 231 _computeLibraries(), key: (e) => e.uri); |
| 232 } |
| 233 return _libraries; |
| 234 } |
| 235 static _computeLibraries() native "MirrorSystem_libraries"; |
225 | 236 |
226 _LocalMirrorSystem(List<LibraryMirror> libraries, this.isolate) | 237 var _isolate; |
227 : this.libraries = new Map<Uri, LibraryMirror>.fromIterable( | 238 IsolateMirror get isolate { |
228 libraries, key: (e) => e.uri); | 239 if (_isolate == null) { |
229 | 240 _isolate = _computeIsolate(); |
230 TypeMirror _dynamicType = null; | |
231 TypeMirror get dynamicType { | |
232 if (_dynamicType == null) { | |
233 _dynamicType = new _SpecialTypeMirror('dynamic'); | |
234 } | 241 } |
235 return _dynamicType; | 242 return _isolate; |
236 } | 243 } |
237 | 244 static _computeIsolate() native "MirrorSystem_isolate"; |
238 TypeMirror _voidType = null; | |
239 TypeMirror get voidType { | |
240 if (_voidType == null) { | |
241 _voidType = new _SpecialTypeMirror('void'); | |
242 } | |
243 return _voidType; | |
244 } | |
245 | 245 |
246 String toString() => "MirrorSystem for isolate '${isolate.debugName}'"; | 246 String toString() => "MirrorSystem for isolate '${isolate.debugName}'"; |
247 } | 247 } |
248 | 248 |
249 class _SourceLocation implements SourceLocation { | 249 class _SourceLocation implements SourceLocation { |
250 _SourceLocation(uriString, this.line, this.column) | 250 _SourceLocation(uriString, this.line, this.column) |
251 : this.sourceUri = Uri.parse(uriString); | 251 : this.sourceUri = Uri.parse(uriString); |
252 | 252 |
253 // Line and column positions are 1-origin, or 0 if unknown. | 253 // Line and column positions are 1-origin, or 0 if unknown. |
254 final int line; | 254 final int line; |
(...skipping 1033 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1288 _invokeSetter(reflectee, setterName, value) | 1288 _invokeSetter(reflectee, setterName, value) |
1289 native 'LibraryMirror_invokeSetter'; | 1289 native 'LibraryMirror_invokeSetter'; |
1290 | 1290 |
1291 _computeMembers(reflectee) | 1291 _computeMembers(reflectee) |
1292 native "LibraryMirror_members"; | 1292 native "LibraryMirror_members"; |
1293 } | 1293 } |
1294 | 1294 |
1295 class _LocalLibraryDependencyMirror | 1295 class _LocalLibraryDependencyMirror |
1296 extends _LocalMirror implements LibraryDependencyMirror { | 1296 extends _LocalMirror implements LibraryDependencyMirror { |
1297 final LibraryMirror sourceLibrary; | 1297 final LibraryMirror sourceLibrary; |
1298 final LibraryMirror targetLibrary; | 1298 var _targetMirrorOrPrefix; |
1299 final List<CombinatorMirror> combinators; | 1299 final List<CombinatorMirror> combinators; |
1300 final Symbol prefix; | 1300 final Symbol prefix; |
1301 final bool isImport; | 1301 final bool isImport; |
1302 final bool isDeferred; | 1302 final bool isDeferred; |
1303 final List<InstanceMirror> metadata; | 1303 final List<InstanceMirror> metadata; |
1304 | 1304 |
1305 _LocalLibraryDependencyMirror(this.sourceLibrary, | 1305 _LocalLibraryDependencyMirror(this.sourceLibrary, |
1306 this.targetLibrary, | 1306 this._targetMirrorOrPrefix, |
1307 this.combinators, | 1307 this.combinators, |
1308 prefixString, | 1308 prefixString, |
1309 this.isImport, | 1309 this.isImport, |
1310 this.isDeferred, | 1310 this.isDeferred, |
1311 unwrappedMetadata) | 1311 unwrappedMetadata) |
1312 : prefix = _s(prefixString), | 1312 : prefix = _s(prefixString), |
1313 metadata = new UnmodifiableListView(unwrappedMetadata.map(reflect)); | 1313 metadata = new UnmodifiableListView(unwrappedMetadata.map(reflect)); |
1314 | 1314 |
1315 bool get isExport => !isImport; | 1315 bool get isExport => !isImport; |
| 1316 |
| 1317 LibraryMirror get targetLibrary { |
| 1318 if (_targetMirrorOrPrefix is _LocalLibraryMirror) { |
| 1319 return _targetMirrorOrPrefix; |
| 1320 } |
| 1321 var mirrorOrNull = _tryUpgradePrefix(_targetMirrorOrPrefix); |
| 1322 if (mirrorOrNull != null) { |
| 1323 _targetMirrorOrPrefix = mirrorOrNull; |
| 1324 } |
| 1325 return mirrorOrNull; |
| 1326 } |
| 1327 |
| 1328 Future<LibraryMirror> loadLibrary() { |
| 1329 if (_targetMirrorOrPrefix is _LocalLibraryMirror) { |
| 1330 return new Future.value(_targetMirrorOrPrefix); |
| 1331 } |
| 1332 var savedPrefix = _targetMirrorOrPrefix; |
| 1333 return savedPrefix.loadLibrary().then((_) { |
| 1334 return _tryUpgradePrefix(savedPrefix); |
| 1335 }); |
| 1336 } |
| 1337 |
| 1338 static _tryUpgradePrefix(libraryPrefix) native "LibraryMirror_fromPrefix"; |
1316 } | 1339 } |
1317 | 1340 |
1318 class _LocalCombinatorMirror extends _LocalMirror implements CombinatorMirror { | 1341 class _LocalCombinatorMirror extends _LocalMirror implements CombinatorMirror { |
1319 final List<Symbol> identifiers; | 1342 final List<Symbol> identifiers; |
1320 final bool isShow; | 1343 final bool isShow; |
1321 | 1344 |
1322 _LocalCombinatorMirror(identifierString, this.isShow) | 1345 _LocalCombinatorMirror(identifierString, this.isShow) |
1323 : this.identifiers = [_s(identifierString)]; | 1346 : this.identifiers = [_s(identifierString)]; |
1324 | 1347 |
1325 bool get isHide => !isShow; | 1348 bool get isHide => !isShow; |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1574 } | 1597 } |
1575 | 1598 |
1576 List<TypeVariableMirror> get typeVariables => emptyList; | 1599 List<TypeVariableMirror> get typeVariables => emptyList; |
1577 List<TypeMirror> get typeArguments => emptyList; | 1600 List<TypeMirror> get typeArguments => emptyList; |
1578 | 1601 |
1579 bool get isOriginalDeclaration => true; | 1602 bool get isOriginalDeclaration => true; |
1580 TypeMirror get originalDeclaration => this; | 1603 TypeMirror get originalDeclaration => this; |
1581 | 1604 |
1582 Symbol get qualifiedName => simpleName; | 1605 Symbol get qualifiedName => simpleName; |
1583 | 1606 |
1584 // TODO(11955): Remove once dynamicType and voidType are canonical objects in | |
1585 // the object store. | |
1586 bool operator ==(other) { | 1607 bool operator ==(other) { |
1587 if (other is! _SpecialTypeMirror) { | 1608 if (other is! _SpecialTypeMirror) { |
1588 return false; | 1609 return false; |
1589 } | 1610 } |
1590 return this.simpleName == other.simpleName; | 1611 return this.simpleName == other.simpleName; |
1591 } | 1612 } |
1592 | 1613 |
1593 int get hashCode => simpleName.hashCode; | 1614 int get hashCode => simpleName.hashCode; |
1594 | 1615 |
1595 String toString() => "TypeMirror on '${_n(simpleName)}'"; | 1616 String toString() => "TypeMirror on '${_n(simpleName)}'"; |
1596 | 1617 |
1597 bool isSubtypeOf(TypeMirror other) { | 1618 bool isSubtypeOf(TypeMirror other) { |
1598 return simpleName == #dynamic || other is _SpecialTypeMirror; | 1619 return simpleName == #dynamic || other is _SpecialTypeMirror; |
1599 } | 1620 } |
1600 | 1621 |
1601 bool isAssignableTo(TypeMirror other) { | 1622 bool isAssignableTo(TypeMirror other) { |
1602 return simpleName == #dynamic || other is _SpecialTypeMirror; | 1623 return simpleName == #dynamic || other is _SpecialTypeMirror; |
1603 } | 1624 } |
1604 } | 1625 } |
1605 | 1626 |
1606 class _Mirrors { | 1627 class _Mirrors { |
1607 static MirrorSystem _currentMirrorSystem = null; | 1628 static MirrorSystem _currentMirrorSystem = new _LocalMirrorSystem(); |
1608 | |
1609 // Creates a new local MirrorSystem. | |
1610 static MirrorSystem makeLocalMirrorSystem() | |
1611 native 'Mirrors_makeLocalMirrorSystem'; | |
1612 | |
1613 // The MirrorSystem for the current isolate. | |
1614 static MirrorSystem currentMirrorSystem() { | 1629 static MirrorSystem currentMirrorSystem() { |
1615 if (_currentMirrorSystem == null) { | |
1616 _currentMirrorSystem = makeLocalMirrorSystem(); | |
1617 } | |
1618 return _currentMirrorSystem; | 1630 return _currentMirrorSystem; |
1619 } | 1631 } |
1620 | 1632 |
1621 // Creates a new local mirror for some Object. | 1633 // Creates a new local mirror for some Object. |
1622 static InstanceMirror reflect(Object reflectee) { | 1634 static InstanceMirror reflect(Object reflectee) { |
1623 return reflectee is Function | 1635 return reflectee is Function |
1624 ? new _LocalClosureMirror(reflectee) | 1636 ? new _LocalClosureMirror(reflectee) |
1625 : new _LocalInstanceMirror(reflectee); | 1637 : new _LocalInstanceMirror(reflectee); |
1626 } | 1638 } |
1627 | 1639 |
(...skipping 22 matching lines...) Expand all Loading... |
1650 if (typeMirror == null) { | 1662 if (typeMirror == null) { |
1651 typeMirror = makeLocalTypeMirror(key); | 1663 typeMirror = makeLocalTypeMirror(key); |
1652 _instanitationCache[key] = typeMirror; | 1664 _instanitationCache[key] = typeMirror; |
1653 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1665 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
1654 _declarationCache[key] = typeMirror; | 1666 _declarationCache[key] = typeMirror; |
1655 } | 1667 } |
1656 } | 1668 } |
1657 return typeMirror; | 1669 return typeMirror; |
1658 } | 1670 } |
1659 } | 1671 } |
OLD | NEW |