| 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 buf.write(']'); | 114 buf.write(']'); |
| 115 } | 115 } |
| 116 buf.write(') -> '); | 116 buf.write(') -> '); |
| 117 buf.write(_n(returnType.qualifiedName)); | 117 buf.write(_n(returnType.qualifiedName)); |
| 118 return buf.toString(); | 118 return buf.toString(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 List _metadata(reflectee) | 121 List _metadata(reflectee) |
| 122 native 'DeclarationMirror_metadata'; | 122 native 'DeclarationMirror_metadata'; |
| 123 | 123 |
| 124 // This will verify the argument types, unwrap them, and ensure we have a fixed | |
| 125 // array. | |
| 126 List _unwrapAsyncPositionals(wrappedArgs) { | |
| 127 List unwrappedArgs = new List(wrappedArgs.length); | |
| 128 for(int i = 0; i < wrappedArgs.length; i++){ | |
| 129 var wrappedArg = wrappedArgs[i]; | |
| 130 if(_isSimpleValue(wrappedArg)) { | |
| 131 unwrappedArgs[i] = wrappedArg; | |
| 132 } else if(wrappedArg is InstanceMirror) { | |
| 133 unwrappedArgs[i] = wrappedArg._reflectee; | |
| 134 } else { | |
| 135 throw "positional argument $i ($wrappedArg) was " | |
| 136 "not a simple value or InstanceMirror"; | |
| 137 } | |
| 138 } | |
| 139 return unwrappedArgs; | |
| 140 } | |
| 141 | |
| 142 Map _unwrapAsyncNamed(wrappedArgs) { | |
| 143 if (wrappedArgs==null) return null; | |
| 144 Map unwrappedArgs = new Map(); | |
| 145 wrappedArgs.forEach((name, wrappedArg){ | |
| 146 if(_isSimpleValue(wrappedArg)) { | |
| 147 unwrappedArgs[name] = wrappedArg; | |
| 148 } else if(wrappedArg is InstanceMirror) { | |
| 149 unwrappedArgs[name] = wrappedArg._reflectee; | |
| 150 } else { | |
| 151 throw "named argument ${_n(name)} ($wrappedArg) was " | |
| 152 "not a simple value or InstanceMirror"; | |
| 153 } | |
| 154 }); | |
| 155 return unwrappedArgs; | |
| 156 } | |
| 157 | |
| 158 class _LocalMirrorSystemImpl extends MirrorSystem { | 124 class _LocalMirrorSystemImpl extends MirrorSystem { |
| 159 // Change parameter back to "this.libraries" when native code is changed. | 125 // Change parameter back to "this.libraries" when native code is changed. |
| 160 _LocalMirrorSystemImpl(List<LibraryMirror> libraries, this.isolate) | 126 _LocalMirrorSystemImpl(List<LibraryMirror> libraries, this.isolate) |
| 161 : this.libraries = new Map<Uri, LibraryMirror>.fromIterable( | 127 : this.libraries = new Map<Uri, LibraryMirror>.fromIterable( |
| 162 libraries, key: (e) => e.uri); | 128 libraries, key: (e) => e.uri); |
| 163 | 129 |
| 164 final Map<Uri, LibraryMirror> libraries; | 130 final Map<Uri, LibraryMirror> libraries; |
| 165 final IsolateMirror isolate; | 131 final IsolateMirror isolate; |
| 166 | 132 |
| 167 TypeMirror _dynamicType = null; | 133 TypeMirror _dynamicType = null; |
| 168 TypeMirror get dynamicType { | 134 TypeMirror get dynamicType { |
| 169 if (_dynamicType == null) { | 135 if (_dynamicType == null) { |
| 170 _dynamicType = new _SpecialTypeMirrorImpl('dynamic'); | 136 _dynamicType = new _SpecialTypeMirrorImpl('dynamic'); |
| 171 } | 137 } |
| 172 return _dynamicType; | 138 return _dynamicType; |
| 173 } | 139 } |
| 174 | 140 |
| 175 TypeMirror _voidType = null; | 141 TypeMirror _voidType = null; |
| 176 TypeMirror get voidType { | 142 TypeMirror get voidType { |
| 177 if (_voidType == null) { | 143 if (_voidType == null) { |
| 178 _voidType = new _SpecialTypeMirrorImpl('void'); | 144 _voidType = new _SpecialTypeMirrorImpl('void'); |
| 179 } | 145 } |
| 180 return _voidType; | 146 return _voidType; |
| 181 } | 147 } |
| 182 | 148 |
| 183 String toString() => "MirrorSystem for isolate '${isolate.debugName}'"; | 149 String toString() => "MirrorSystem for isolate '${isolate.debugName}'"; |
| 184 } | 150 } |
| 185 | 151 |
| 186 abstract class _LocalMirrorImpl implements Mirror { | 152 abstract class _LocalMirrorImpl implements Mirror {} |
| 187 // Local mirrors always return the same MirrorSystem. This field | |
| 188 // is more interesting once we implement remote mirrors. | |
| 189 MirrorSystem get mirrors => _Mirrors.currentMirrorSystem(); | |
| 190 } | |
| 191 | 153 |
| 192 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl | 154 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl |
| 193 implements IsolateMirror { | 155 implements IsolateMirror { |
| 194 _LocalIsolateMirrorImpl(this.debugName, this.rootLibrary); | 156 _LocalIsolateMirrorImpl(this.debugName, this.rootLibrary); |
| 195 | 157 |
| 196 final String debugName; | 158 final String debugName; |
| 197 final bool isCurrent = true; | 159 final bool isCurrent = true; |
| 198 final LibraryMirror rootLibrary; | 160 final LibraryMirror rootLibrary; |
| 199 | 161 |
| 200 String toString() => "IsolateMirror on '$debugName'"; | 162 String toString() => "IsolateMirror on '$debugName'"; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 _n(memberName))); | 198 _n(memberName))); |
| 237 } | 199 } |
| 238 | 200 |
| 239 InstanceMirror setField(Symbol memberName, Object value) { | 201 InstanceMirror setField(Symbol memberName, Object value) { |
| 240 this._invokeSetter(_reflectee, | 202 this._invokeSetter(_reflectee, |
| 241 _n(memberName), | 203 _n(memberName), |
| 242 value); | 204 value); |
| 243 return reflect(value); | 205 return reflect(value); |
| 244 } | 206 } |
| 245 | 207 |
| 246 Future<InstanceMirror> invokeAsync(Symbol memberName, | |
| 247 List positionalArguments, | |
| 248 [Map<Symbol, dynamic> namedArguments]) { | |
| 249 return new Future(() { | |
| 250 return this.invoke(memberName, | |
| 251 _unwrapAsyncPositionals(positionalArguments), | |
| 252 _unwrapAsyncNamed(namedArguments)); | |
| 253 }); | |
| 254 } | |
| 255 | |
| 256 Future<InstanceMirror> getFieldAsync(Symbol memberName) { | |
| 257 try { | |
| 258 var result = this._invokeGetter(_reflectee, | |
| 259 _n(memberName)); | |
| 260 return new Future.value(reflect(result)); | |
| 261 } catch(e) { | |
| 262 return new Future.error(e); | |
| 263 } | |
| 264 } | |
| 265 | |
| 266 Future<InstanceMirror> setFieldAsync(Symbol memberName, Object value) { | |
| 267 try { | |
| 268 var unwrappedValue; | |
| 269 if(_isSimpleValue(value)) { | |
| 270 unwrappedValue = value; | |
| 271 } else if(value is InstanceMirror) { | |
| 272 unwrappedValue = value._reflectee; | |
| 273 } else { | |
| 274 throw "setter argument ($value) must be" | |
| 275 "a simple value or InstanceMirror"; | |
| 276 } | |
| 277 | |
| 278 this._invokeSetter(_reflectee, | |
| 279 _n(memberName), | |
| 280 unwrappedValue); | |
| 281 return new Future.value(reflect(unwrappedValue)); | |
| 282 } catch(e) { | |
| 283 return new Future.error(e); | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 static _validateArgument(int i, Object arg) | 208 static _validateArgument(int i, Object arg) |
| 288 { | 209 { |
| 289 if (arg is Mirror) { | 210 if (arg is Mirror) { |
| 290 if (arg is! InstanceMirror) { | 211 if (arg is! InstanceMirror) { |
| 291 throw new MirrorException( | 212 throw new MirrorException( |
| 292 'positional argument $i ($arg) was not an InstanceMirror'); | 213 'positional argument $i ($arg) was not an InstanceMirror'); |
| 293 } | 214 } |
| 294 } else if (!_isSimpleValue(arg)) { | 215 } else if (!_isSimpleValue(arg)) { |
| 295 throw new MirrorException( | 216 throw new MirrorException( |
| 296 'positional argument $i ($arg) was not a simple value'); | 217 'positional argument $i ($arg) was not a simple value'); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 } | 435 } |
| 515 | 436 |
| 516 bool get isPrivate => _n(simpleName).startsWith('_'); | 437 bool get isPrivate => _n(simpleName).startsWith('_'); |
| 517 | 438 |
| 518 final bool isTopLevel = true; | 439 final bool isTopLevel = true; |
| 519 | 440 |
| 520 SourceLocation get location { | 441 SourceLocation get location { |
| 521 throw new UnimplementedError('ClassMirror.location is not implemented'); | 442 throw new UnimplementedError('ClassMirror.location is not implemented'); |
| 522 } | 443 } |
| 523 | 444 |
| 524 // TODO(rmacnak): Remove these left-overs from the days of separate interfaces | |
| 525 // once we send out a breaking change. | |
| 526 bool get isClass => true; | |
| 527 ClassMirror get defaultFactory => null; | |
| 528 | |
| 529 ClassMirror _trueSuperclassField; | 445 ClassMirror _trueSuperclassField; |
| 530 ClassMirror get _trueSuperclass { | 446 ClassMirror get _trueSuperclass { |
| 531 if (_trueSuperclassField == null) { | 447 if (_trueSuperclassField == null) { |
| 532 Type supertype = isOriginalDeclaration | 448 Type supertype = isOriginalDeclaration |
| 533 ? _supertype(_reflectedType) | 449 ? _supertype(_reflectedType) |
| 534 : _supertypeInstantiated(_reflectedType); | 450 : _supertypeInstantiated(_reflectedType); |
| 535 if (supertype == null) { | 451 if (supertype == null) { |
| 536 // Object has no superclass. | 452 // Object has no superclass. |
| 537 return null; | 453 return null; |
| 538 } | 454 } |
| (...skipping 933 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1472 native 'Mirrors_makeLocalMirrorSystem'; | 1388 native 'Mirrors_makeLocalMirrorSystem'; |
| 1473 | 1389 |
| 1474 // The MirrorSystem for the current isolate. | 1390 // The MirrorSystem for the current isolate. |
| 1475 static MirrorSystem currentMirrorSystem() { | 1391 static MirrorSystem currentMirrorSystem() { |
| 1476 if (_currentMirrorSystem == null) { | 1392 if (_currentMirrorSystem == null) { |
| 1477 _currentMirrorSystem = makeLocalMirrorSystem(); | 1393 _currentMirrorSystem = makeLocalMirrorSystem(); |
| 1478 } | 1394 } |
| 1479 return _currentMirrorSystem; | 1395 return _currentMirrorSystem; |
| 1480 } | 1396 } |
| 1481 | 1397 |
| 1482 static Future<MirrorSystem> mirrorSystemOf(SendPort port) { | |
| 1483 if (isLocalPort(port)) { | |
| 1484 // Make a local mirror system. | |
| 1485 try { | |
| 1486 return new Future<MirrorSystem>.value(currentMirrorSystem()); | |
| 1487 } catch (exception) { | |
| 1488 return new Future<MirrorSystem>.error(exception); | |
| 1489 } | |
| 1490 } else { | |
| 1491 // Make a remote mirror system | |
| 1492 throw new UnimplementedError( | |
| 1493 'Remote mirror support is not implemented'); | |
| 1494 } | |
| 1495 } | |
| 1496 | |
| 1497 // Creates a new local mirror for some Object. | 1398 // Creates a new local mirror for some Object. |
| 1498 static InstanceMirror reflect(Object reflectee) { | 1399 static InstanceMirror reflect(Object reflectee) { |
| 1499 return reflectee is Function | 1400 return reflectee is Function |
| 1500 ? new _LocalClosureMirrorImpl(reflectee) | 1401 ? new _LocalClosureMirrorImpl(reflectee) |
| 1501 : new _LocalInstanceMirrorImpl(reflectee); | 1402 : new _LocalInstanceMirrorImpl(reflectee); |
| 1502 } | 1403 } |
| 1503 | 1404 |
| 1504 static ClassMirror makeLocalClassMirror(Type key) | 1405 static ClassMirror makeLocalClassMirror(Type key) |
| 1505 native "Mirrors_makeLocalClassMirror"; | 1406 native "Mirrors_makeLocalClassMirror"; |
| 1506 static TypeMirror makeLocalTypeMirror(Type key) | 1407 static TypeMirror makeLocalTypeMirror(Type key) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1526 if (typeMirror == null) { | 1427 if (typeMirror == null) { |
| 1527 typeMirror = makeLocalTypeMirror(key); | 1428 typeMirror = makeLocalTypeMirror(key); |
| 1528 _instanitationCache[key] = typeMirror; | 1429 _instanitationCache[key] = typeMirror; |
| 1529 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1430 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
| 1530 _declarationCache[key] = typeMirror; | 1431 _declarationCache[key] = typeMirror; |
| 1531 } | 1432 } |
| 1532 } | 1433 } |
| 1533 return typeMirror; | 1434 return typeMirror; |
| 1534 } | 1435 } |
| 1535 } | 1436 } |
| OLD | NEW |