| 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 buf.write(']'); | 69 buf.write(']'); |
| 70 } | 70 } |
| 71 buf.write(') -> '); | 71 buf.write(') -> '); |
| 72 buf.write(_n(returnType.qualifiedName)); | 72 buf.write(_n(returnType.qualifiedName)); |
| 73 return buf.toString(); | 73 return buf.toString(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 List _metadata(reflectee) | 76 List _metadata(reflectee) |
| 77 native 'DeclarationMirror_metadata'; | 77 native 'DeclarationMirror_metadata'; |
| 78 | 78 |
| 79 // This will verify the argument types, unwrap them, and ensure we have a fixed | |
| 80 // array. | |
| 81 List _unwrapAsyncPositionals(wrappedArgs) { | |
| 82 List unwrappedArgs = new List(wrappedArgs.length); | |
| 83 for(int i = 0; i < wrappedArgs.length; i++){ | |
| 84 var wrappedArg = wrappedArgs[i]; | |
| 85 if(_isSimpleValue(wrappedArg)) { | |
| 86 unwrappedArgs[i] = wrappedArg; | |
| 87 } else if(wrappedArg is InstanceMirror) { | |
| 88 unwrappedArgs[i] = wrappedArg._reflectee; | |
| 89 } else { | |
| 90 throw "positional argument $i ($wrappedArg) was " | |
| 91 "not a simple value or InstanceMirror"; | |
| 92 } | |
| 93 } | |
| 94 return unwrappedArgs; | |
| 95 } | |
| 96 | |
| 97 Map _unwrapAsyncNamed(wrappedArgs) { | |
| 98 if (wrappedArgs==null) return null; | |
| 99 Map unwrappedArgs = new Map(); | |
| 100 wrappedArgs.forEach((name, wrappedArg){ | |
| 101 if(_isSimpleValue(wrappedArg)) { | |
| 102 unwrappedArgs[name] = wrappedArg; | |
| 103 } else if(wrappedArg is InstanceMirror) { | |
| 104 unwrappedArgs[name] = wrappedArg._reflectee; | |
| 105 } else { | |
| 106 throw "named argument ${_n(name)} ($wrappedArg) was " | |
| 107 "not a simple value or InstanceMirror"; | |
| 108 } | |
| 109 }); | |
| 110 return unwrappedArgs; | |
| 111 } | |
| 112 | |
| 113 class _LocalMirrorSystemImpl extends MirrorSystem { | 79 class _LocalMirrorSystemImpl extends MirrorSystem { |
| 114 // Change parameter back to "this.libraries" when native code is changed. | 80 // Change parameter back to "this.libraries" when native code is changed. |
| 115 _LocalMirrorSystemImpl(List<LibraryMirror> libraries, this.isolate) | 81 _LocalMirrorSystemImpl(List<LibraryMirror> libraries, this.isolate) |
| 116 : this.libraries = new Map<Uri, LibraryMirror>.fromIterable( | 82 : this.libraries = new Map<Uri, LibraryMirror>.fromIterable( |
| 117 libraries, key: (e) => e.uri); | 83 libraries, key: (e) => e.uri); |
| 118 | 84 |
| 119 final Map<Uri, LibraryMirror> libraries; | 85 final Map<Uri, LibraryMirror> libraries; |
| 120 final IsolateMirror isolate; | 86 final IsolateMirror isolate; |
| 121 | 87 |
| 122 TypeMirror _dynamicType = null; | 88 TypeMirror _dynamicType = null; |
| 123 TypeMirror get dynamicType { | 89 TypeMirror get dynamicType { |
| 124 if (_dynamicType == null) { | 90 if (_dynamicType == null) { |
| 125 _dynamicType = new _SpecialTypeMirrorImpl('dynamic'); | 91 _dynamicType = new _SpecialTypeMirrorImpl('dynamic'); |
| 126 } | 92 } |
| 127 return _dynamicType; | 93 return _dynamicType; |
| 128 } | 94 } |
| 129 | 95 |
| 130 TypeMirror _voidType = null; | 96 TypeMirror _voidType = null; |
| 131 TypeMirror get voidType { | 97 TypeMirror get voidType { |
| 132 if (_voidType == null) { | 98 if (_voidType == null) { |
| 133 _voidType = new _SpecialTypeMirrorImpl('void'); | 99 _voidType = new _SpecialTypeMirrorImpl('void'); |
| 134 } | 100 } |
| 135 return _voidType; | 101 return _voidType; |
| 136 } | 102 } |
| 137 | 103 |
| 138 String toString() => "MirrorSystem for isolate '${isolate.debugName}'"; | 104 String toString() => "MirrorSystem for isolate '${isolate.debugName}'"; |
| 139 } | 105 } |
| 140 | 106 |
| 141 abstract class _LocalMirrorImpl implements Mirror { | 107 abstract class _LocalMirrorImpl implements Mirror { |
| 142 int get hashCode { | |
| 143 throw new UnimplementedError('Mirror.hashCode is not implemented'); | |
| 144 } | |
| 145 | |
| 146 // Local mirrors always return the same MirrorSystem. This field | |
| 147 // is more interesting once we implement remote mirrors. | |
| 148 MirrorSystem get mirrors => _Mirrors.currentMirrorSystem(); | |
| 149 } | 108 } |
| 150 | 109 |
| 151 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl | 110 class _LocalIsolateMirrorImpl extends _LocalMirrorImpl |
| 152 implements IsolateMirror { | 111 implements IsolateMirror { |
| 153 _LocalIsolateMirrorImpl(this.debugName, this.rootLibrary); | 112 _LocalIsolateMirrorImpl(this.debugName, this.rootLibrary); |
| 154 | 113 |
| 155 final String debugName; | 114 final String debugName; |
| 156 final bool isCurrent = true; | 115 final bool isCurrent = true; |
| 157 final LibraryMirror rootLibrary; | 116 final LibraryMirror rootLibrary; |
| 158 | 117 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 _n(memberName))); | 154 _n(memberName))); |
| 196 } | 155 } |
| 197 | 156 |
| 198 InstanceMirror setField(Symbol memberName, Object value) { | 157 InstanceMirror setField(Symbol memberName, Object value) { |
| 199 this._invokeSetter(_reflectee, | 158 this._invokeSetter(_reflectee, |
| 200 _n(memberName), | 159 _n(memberName), |
| 201 value); | 160 value); |
| 202 return reflect(value); | 161 return reflect(value); |
| 203 } | 162 } |
| 204 | 163 |
| 205 Future<InstanceMirror> invokeAsync(Symbol memberName, | |
| 206 List positionalArguments, | |
| 207 [Map<Symbol, dynamic> namedArguments]) { | |
| 208 return new Future(() { | |
| 209 return this.invoke(memberName, | |
| 210 _unwrapAsyncPositionals(positionalArguments), | |
| 211 _unwrapAsyncNamed(namedArguments)); | |
| 212 }); | |
| 213 } | |
| 214 | |
| 215 Future<InstanceMirror> getFieldAsync(Symbol memberName) { | |
| 216 try { | |
| 217 var result = this._invokeGetter(_reflectee, | |
| 218 _n(memberName)); | |
| 219 return new Future.value(reflect(result)); | |
| 220 } catch(e) { | |
| 221 return new Future.error(e); | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 Future<InstanceMirror> setFieldAsync(Symbol memberName, Object value) { | |
| 226 try { | |
| 227 var unwrappedValue; | |
| 228 if(_isSimpleValue(value)) { | |
| 229 unwrappedValue = value; | |
| 230 } else if(value is InstanceMirror) { | |
| 231 unwrappedValue = value._reflectee; | |
| 232 } else { | |
| 233 throw "setter argument ($value) must be" | |
| 234 "a simple value or InstanceMirror"; | |
| 235 } | |
| 236 | |
| 237 this._invokeSetter(_reflectee, | |
| 238 _n(memberName), | |
| 239 unwrappedValue); | |
| 240 return new Future.value(reflect(unwrappedValue)); | |
| 241 } catch(e) { | |
| 242 return new Future.error(e); | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 static _validateArgument(int i, Object arg) | 164 static _validateArgument(int i, Object arg) |
| 247 { | 165 { |
| 248 if (arg is Mirror) { | 166 if (arg is Mirror) { |
| 249 if (arg is! InstanceMirror) { | 167 if (arg is! InstanceMirror) { |
| 250 throw new MirrorException( | 168 throw new MirrorException( |
| 251 'positional argument $i ($arg) was not an InstanceMirror'); | 169 'positional argument $i ($arg) was not an InstanceMirror'); |
| 252 } | 170 } |
| 253 } else if (!_isSimpleValue(arg)) { | 171 } else if (!_isSimpleValue(arg)) { |
| 254 throw new MirrorException( | 172 throw new MirrorException( |
| 255 'positional argument $i ($arg) was not a simple value'); | 173 'positional argument $i ($arg) was not a simple value'); |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 } | 388 } |
| 471 | 389 |
| 472 bool get isPrivate => _n(simpleName).startsWith('_'); | 390 bool get isPrivate => _n(simpleName).startsWith('_'); |
| 473 | 391 |
| 474 final bool isTopLevel = true; | 392 final bool isTopLevel = true; |
| 475 | 393 |
| 476 SourceLocation get location { | 394 SourceLocation get location { |
| 477 throw new UnimplementedError('ClassMirror.location is not implemented'); | 395 throw new UnimplementedError('ClassMirror.location is not implemented'); |
| 478 } | 396 } |
| 479 | 397 |
| 480 // TODO(rmacnak): Remove these left-overs from the days of separate interfaces | |
| 481 // once we send out a breaking change. | |
| 482 bool get isClass => true; | |
| 483 ClassMirror get defaultFactory => null; | |
| 484 | |
| 485 ClassMirror _trueSuperclassField; | 398 ClassMirror _trueSuperclassField; |
| 486 ClassMirror get _trueSuperclass { | 399 ClassMirror get _trueSuperclass { |
| 487 if (_trueSuperclassField == null) { | 400 if (_trueSuperclassField == null) { |
| 488 Type supertype = _supertype(_reflectee); | 401 Type supertype = _supertype(_reflectee); |
| 489 if (supertype == null) { | 402 if (supertype == null) { |
| 490 // Object has no superclass. | 403 // Object has no superclass. |
| 491 return null; | 404 return null; |
| 492 } | 405 } |
| 493 _trueSuperclassField = _Mirrors._reflectType(supertype); | 406 _trueSuperclassField = _Mirrors._reflectType(supertype); |
| 494 } | 407 } |
| (...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1270 native 'Mirrors_makeLocalMirrorSystem'; | 1183 native 'Mirrors_makeLocalMirrorSystem'; |
| 1271 | 1184 |
| 1272 // The MirrorSystem for the current isolate. | 1185 // The MirrorSystem for the current isolate. |
| 1273 static MirrorSystem currentMirrorSystem() { | 1186 static MirrorSystem currentMirrorSystem() { |
| 1274 if (_currentMirrorSystem == null) { | 1187 if (_currentMirrorSystem == null) { |
| 1275 _currentMirrorSystem = makeLocalMirrorSystem(); | 1188 _currentMirrorSystem = makeLocalMirrorSystem(); |
| 1276 } | 1189 } |
| 1277 return _currentMirrorSystem; | 1190 return _currentMirrorSystem; |
| 1278 } | 1191 } |
| 1279 | 1192 |
| 1280 static Future<MirrorSystem> mirrorSystemOf(SendPort port) { | |
| 1281 if (isLocalPort(port)) { | |
| 1282 // Make a local mirror system. | |
| 1283 try { | |
| 1284 return new Future<MirrorSystem>.value(currentMirrorSystem()); | |
| 1285 } catch (exception) { | |
| 1286 return new Future<MirrorSystem>.error(exception); | |
| 1287 } | |
| 1288 } else { | |
| 1289 // Make a remote mirror system | |
| 1290 throw new UnimplementedError( | |
| 1291 'Remote mirror support is not implemented'); | |
| 1292 } | |
| 1293 } | |
| 1294 | |
| 1295 // Creates a new local mirror for some Object. | 1193 // Creates a new local mirror for some Object. |
| 1296 static InstanceMirror reflect(Object reflectee) { | 1194 static InstanceMirror reflect(Object reflectee) { |
| 1297 return reflectee is Function | 1195 return reflectee is Function |
| 1298 ? new _LocalClosureMirrorImpl(reflectee) | 1196 ? new _LocalClosureMirrorImpl(reflectee) |
| 1299 : new _LocalInstanceMirrorImpl(reflectee); | 1197 : new _LocalInstanceMirrorImpl(reflectee); |
| 1300 } | 1198 } |
| 1301 | 1199 |
| 1302 static ClassMirror makeLocalClassMirror(Type key) | 1200 static ClassMirror makeLocalClassMirror(Type key) |
| 1303 native "Mirrors_makeLocalClassMirror"; | 1201 native "Mirrors_makeLocalClassMirror"; |
| 1304 static TypeMirror makeLocalTypeMirror(Type key) | 1202 static TypeMirror makeLocalTypeMirror(Type key) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1324 if (typeMirror == null) { | 1222 if (typeMirror == null) { |
| 1325 typeMirror = makeLocalTypeMirror(key); | 1223 typeMirror = makeLocalTypeMirror(key); |
| 1326 _instanitationCache[key] = typeMirror; | 1224 _instanitationCache[key] = typeMirror; |
| 1327 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1225 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
| 1328 _declarationCache[key] = typeMirror; | 1226 _declarationCache[key] = typeMirror; |
| 1329 } | 1227 } |
| 1330 } | 1228 } |
| 1331 return typeMirror; | 1229 return typeMirror; |
| 1332 } | 1230 } |
| 1333 } | 1231 } |
| OLD | NEW |