| 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 // These values are allowed to be passed directly over the wire. | 7 // These values are allowed to be passed directly over the wire. |
| 8 bool _isSimpleValue(var value) { | 8 bool _isSimpleValue(var value) { |
| 9 return (value == null || value is num || value is String || value is bool); | 9 return (value == null || value is num || value is String || value is bool); |
| 10 } | 10 } |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // For now, all VMObjects hold a VMReference. We could consider | 132 // For now, all VMObjects hold a VMReference. We could consider |
| 133 // storing the Object reference itself here if the object is a Dart | 133 // storing the Object reference itself here if the object is a Dart |
| 134 // language objects (except for objects of type VMReference, of | 134 // language objects (except for objects of type VMReference, of |
| 135 // course). | 135 // course). |
| 136 VMReference _reference; | 136 VMReference _reference; |
| 137 } | 137 } |
| 138 | 138 |
| 139 abstract class _LocalObjectMirrorImpl extends _LocalVMObjectMirrorImpl | 139 abstract class _LocalObjectMirrorImpl extends _LocalVMObjectMirrorImpl |
| 140 implements ObjectMirror { | 140 implements ObjectMirror { |
| 141 _LocalObjectMirrorImpl(ref) : super(ref) {} | 141 _LocalObjectMirrorImpl(ref) : super(ref) {} |
| 142 | 142 |
| 143 Future<InstanceMirror> invokeAsync(String memberName, | 143 InstanceMirror invoke(String memberName, |
| 144 List positionalArguments, | 144 List positionalArguments, |
| 145 [Map<String,dynamic> namedArguments]) { | 145 [Map<String,dynamic> namedArguments]) { |
| 146 if (namedArguments != null) { | 146 if (namedArguments != null) { |
| 147 throw new UnimplementedError( | 147 throw new UnimplementedError( |
| 148 'named argument support is not implemented'); | 148 'named argument support is not implemented'); |
| 149 } |
| 150 return _invoke(this, memberName, positionalArguments, false); |
| 151 } |
| 152 |
| 153 InstanceMirror getField(String fieldName) { |
| 154 return _getField(this, fieldName); |
| 155 } |
| 156 |
| 157 InstanceMirror setField(String fieldName, Object arg) { |
| 158 return _setField(this, fieldName, arg, false); |
| 159 } |
| 160 |
| 161 |
| 162 |
| 163 Future<InstanceMirror> invokeAsync(String memberName, |
| 164 List positionalArguments, |
| 165 [Map<String,dynamic> namedArguments]) { |
| 166 if (namedArguments != null) { |
| 167 throw new UnimplementedError( |
| 168 'named argument support is not implemented'); |
| 149 } | 169 } |
| 150 // Walk the arguments and make sure they are legal. | 170 // Walk the arguments and make sure they are legal. |
| 151 for (int i = 0; i < positionalArguments.length; i++) { | 171 for (int i = 0; i < positionalArguments.length; i++) { |
| 152 var arg = positionalArguments[i]; | 172 var arg = positionalArguments[i]; |
| 153 _validateArgument(i, arg); | 173 _validateArgument(i, arg); |
| 154 } | 174 } |
| 155 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); | 175 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); |
| 156 try { | 176 try { |
| 157 completer.complete( | 177 completer.complete( |
| 158 _invoke(this, memberName, positionalArguments)); | 178 _invoke(this, memberName, positionalArguments, true)); |
| 159 } catch (exception, s) { | 179 } catch (exception, s) { |
| 160 completer.completeError(exception, s); | 180 completer.completeError(exception, s); |
| 161 } | 181 } |
| 162 return completer.future; | 182 return completer.future; |
| 163 } | 183 } |
| 164 | 184 |
| 165 Future<InstanceMirror> getFieldAsync(String fieldName) { | 185 Future<InstanceMirror> getFieldAsync(String fieldName) { |
| 166 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); | 186 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); |
| 167 try { | 187 try { |
| 168 completer.complete(_getField(this, fieldName)); | 188 completer.complete(_getField(this, fieldName)); |
| 169 } catch (exception, s) { | 189 } catch (exception, s) { |
| 170 completer.completeError(exception, s); | 190 completer.completeError(exception, s); |
| 171 } | 191 } |
| 172 return completer.future; | 192 return completer.future; |
| 173 } | 193 } |
| 174 | 194 |
| 175 Future<InstanceMirror> setFieldAsync(String fieldName, Object arg) { | 195 Future<InstanceMirror> setFieldAsync(String fieldName, Object arg) { |
| 176 _validateArgument(0, arg); | 196 _validateArgument(0, arg); |
| 177 | 197 |
| 178 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); | 198 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); |
| 179 try { | 199 try { |
| 180 completer.complete(_setField(this, fieldName, arg)); | 200 completer.complete(_setField(this, fieldName, arg, true)); |
| 181 } catch (exception, s) { | 201 } catch (exception, s) { |
| 182 completer.completeError(exception, s); | 202 completer.completeError(exception, s); |
| 183 } | 203 } |
| 184 return completer.future; | 204 return completer.future; |
| 185 } | 205 } |
| 186 | 206 |
| 187 static _validateArgument(int i, Object arg) | 207 static _validateArgument(int i, Object arg) |
| 188 { | 208 { |
| 189 if (arg is Mirror) { | 209 if (arg is Mirror) { |
| 190 if (arg is! InstanceMirror) { | 210 if (arg is! InstanceMirror) { |
| 191 throw new MirrorException( | 211 throw new MirrorException( |
| 192 'positional argument $i ($arg) was not an InstanceMirror'); | 212 'positional argument $i ($arg) was not an InstanceMirror'); |
| 193 } | 213 } |
| 194 } else if (!_isSimpleValue(arg)) { | 214 } else if (!_isSimpleValue(arg)) { |
| 195 throw new MirrorException( | 215 throw new MirrorException( |
| 196 'positional argument $i ($arg) was not a simple value'); | 216 'positional argument $i ($arg) was not a simple value'); |
| 197 } | 217 } |
| 198 } | 218 } |
| 199 | 219 |
| 200 static _invoke(ref, memberName, positionalArguments) | 220 static _invoke(ref, memberName, positionalArguments, async) |
| 201 native 'LocalObjectMirrorImpl_invoke'; | 221 native 'LocalObjectMirrorImpl_invoke'; |
| 202 | 222 |
| 203 static _getField(ref, fieldName) | 223 static _getField(ref, fieldName) // same for sync and async versions |
| 204 native 'LocalObjectMirrorImpl_getField'; | 224 native 'LocalObjectMirrorImpl_getField'; |
| 205 | 225 |
| 206 static _setField(ref, fieldName, value) | 226 static _setField(ref, fieldName, value, async) |
| 207 native 'LocalObjectMirrorImpl_setField'; | 227 native 'LocalObjectMirrorImpl_setField'; |
| 208 } | 228 } |
| 209 | 229 |
| 210 // Prints a string as it might appear in dart program text. | 230 // Prints a string as it might appear in dart program text. |
| 211 // TODO(turnidge): Consider truncating. | 231 // TODO(turnidge): Consider truncating. |
| 212 String _dartEscape(String str) { | 232 String _dartEscape(String str) { |
| 213 bool isNice(int code) => (code >= 32 && code <= 126); | 233 bool isNice(int code) => (code >= 32 && code <= 126); |
| 214 | 234 |
| 215 StringBuffer buf = new StringBuffer(); | 235 StringBuffer buf = new StringBuffer(); |
| 216 for (int i = 0; i < str.length; i++) { | 236 for (int i = 0; i < str.length; i++) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 reflectee, | 316 reflectee, |
| 297 this.function) : super(ref, type, reflectee) {} | 317 this.function) : super(ref, type, reflectee) {} |
| 298 | 318 |
| 299 final MethodMirror function; | 319 final MethodMirror function; |
| 300 | 320 |
| 301 String get source { | 321 String get source { |
| 302 throw new UnimplementedError( | 322 throw new UnimplementedError( |
| 303 'ClosureMirror.source is not implemented'); | 323 'ClosureMirror.source is not implemented'); |
| 304 } | 324 } |
| 305 | 325 |
| 326 InstanceMirror apply(List<Object> positionalArguments, |
| 327 [Map<String,Object> namedArguments]) { |
| 328 if (namedArguments != null) { |
| 329 throw new UnimplementedError( |
| 330 'named argument support is not implemented'); |
| 331 } |
| 332 return _apply(this, positionalArguments, false); |
| 333 } |
| 334 |
| 306 Future<InstanceMirror> applyAsync(List<Object> positionalArguments, | 335 Future<InstanceMirror> applyAsync(List<Object> positionalArguments, |
| 307 [Map<String,Object> namedArguments]) { | 336 [Map<String,Object> namedArguments]) { |
| 308 if (namedArguments != null) { | 337 if (namedArguments != null) { |
| 309 throw new UnimplementedError( | 338 throw new UnimplementedError( |
| 310 'named argument support is not implemented'); | 339 'named argument support is not implemented'); |
| 311 } | 340 } |
| 312 // Walk the arguments and make sure they are legal. | 341 // Walk the arguments and make sure they are legal. |
| 313 for (int i = 0; i < positionalArguments.length; i++) { | 342 for (int i = 0; i < positionalArguments.length; i++) { |
| 314 var arg = positionalArguments[i]; | 343 var arg = positionalArguments[i]; |
| 315 _LocalObjectMirrorImpl._validateArgument(i, arg); | 344 _LocalObjectMirrorImpl._validateArgument(i, arg); |
| 316 } | 345 } |
| 317 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); | 346 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); |
| 318 try { | 347 try { |
| 319 completer.complete( | 348 completer.complete( |
| 320 _apply(this, positionalArguments)); | 349 _apply(this, positionalArguments, true)); |
| 321 } catch (exception) { | 350 } catch (exception) { |
| 322 completer.completeError(exception); | 351 completer.completeError(exception); |
| 323 } | 352 } |
| 324 return completer.future; | 353 return completer.future; |
| 325 } | 354 } |
| 326 | 355 |
| 327 Future<InstanceMirror> findInContext(String name) { | 356 Future<InstanceMirror> findInContext(String name) { |
| 328 throw new UnimplementedError( | 357 throw new UnimplementedError( |
| 329 'ClosureMirror.findInContext() is not implemented'); | 358 'ClosureMirror.findInContext() is not implemented'); |
| 330 } | 359 } |
| 331 | 360 |
| 332 static _apply(ref, positionalArguments) | 361 static _apply(ref, positionalArguments, async) |
| 333 native 'LocalClosureMirrorImpl_apply'; | 362 native 'LocalClosureMirrorImpl_apply'; |
| 334 } | 363 } |
| 335 | 364 |
| 336 class _LazyTypeMirror { | 365 class _LazyTypeMirror { |
| 337 _LazyTypeMirror(this.libraryName, this.typeName) {} | 366 _LazyTypeMirror(this.libraryName, this.typeName) {} |
| 338 | 367 |
| 339 TypeMirror resolve(MirrorSystem mirrors) { | 368 TypeMirror resolve(MirrorSystem mirrors) { |
| 340 if (libraryName == null) { | 369 if (libraryName == null) { |
| 341 if (typeName == 'dynamic') { | 370 if (typeName == 'dynamic') { |
| 342 return mirrors.dynamicType; | 371 return mirrors.dynamicType; |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 'ClassMirror.isOriginalDeclaration is not implemented'); | 521 'ClassMirror.isOriginalDeclaration is not implemented'); |
| 493 } | 522 } |
| 494 | 523 |
| 495 ClassMirror get genericDeclaration { | 524 ClassMirror get genericDeclaration { |
| 496 throw new UnimplementedError( | 525 throw new UnimplementedError( |
| 497 'ClassMirror.originalDeclaration is not implemented'); | 526 'ClassMirror.originalDeclaration is not implemented'); |
| 498 } | 527 } |
| 499 | 528 |
| 500 String toString() => "ClassMirror on '$simpleName'"; | 529 String toString() => "ClassMirror on '$simpleName'"; |
| 501 | 530 |
| 531 InstanceMirror apply(List<Object> positionalArguments, |
| 532 [Map<String,Object> namedArguments]) { |
| 533 if (namedArguments != null) { |
| 534 throw new UnimplementedError( |
| 535 'named argument support is not implemented'); |
| 536 } |
| 537 return _apply(this, positionalArguments); |
| 538 } |
| 539 |
| 540 InstanceMirror newInstance(String constructorName, |
| 541 List positionalArguments, |
| 542 [Map<String,dynamic> namedArguments]) { |
| 543 if (namedArguments != null) { |
| 544 throw new UnimplementedError( |
| 545 'named argument support is not implemented'); |
| 546 } |
| 547 return _invokeConstructor(this, constructorName, positionalArguments, false)
; |
| 548 } |
| 549 |
| 502 Future<InstanceMirror> newInstanceAsync(String constructorName, | 550 Future<InstanceMirror> newInstanceAsync(String constructorName, |
| 503 List positionalArguments, | 551 List positionalArguments, |
| 504 [Map<String,dynamic> namedArguments])
{ | 552 [Map<String,dynamic> namedArguments])
{ |
| 505 if (namedArguments != null) { | 553 if (namedArguments != null) { |
| 506 throw new UnimplementedError( | 554 throw new UnimplementedError( |
| 507 'named argument support is not implemented'); | 555 'named argument support is not implemented'); |
| 508 } | 556 } |
| 509 // Walk the arguments and make sure they are legal. | 557 // Walk the arguments and make sure they are legal. |
| 510 for (int i = 0; i < positionalArguments.length; i++) { | 558 for (int i = 0; i < positionalArguments.length; i++) { |
| 511 var arg = positionalArguments[i]; | 559 var arg = positionalArguments[i]; |
| 512 _LocalObjectMirrorImpl._validateArgument(i, arg); | 560 _LocalObjectMirrorImpl._validateArgument(i, arg); |
| 513 } | 561 } |
| 514 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); | 562 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); |
| 515 try { | 563 try { |
| 516 completer.complete( | 564 completer.complete( |
| 517 _invokeConstructor(this, constructorName, positionalArguments)); | 565 _invokeConstructor(this, constructorName, positionalArguments, true)); |
| 518 } catch (exception) { | 566 } catch (exception) { |
| 519 completer.completeError(exception); | 567 completer.completeError(exception); |
| 520 } | 568 } |
| 521 return completer.future; | 569 return completer.future; |
| 522 } | 570 } |
| 523 | 571 |
| 524 static _invokeConstructor(ref, constructorName, positionalArguments) | 572 static _invokeConstructor(ref, constructorName, positionalArguments, async) |
| 525 native 'LocalClassMirrorImpl_invokeConstructor'; | 573 native 'LocalClassMirrorImpl_invokeConstructor'; |
| 526 } | 574 } |
| 527 | 575 |
| 528 class _LazyFunctionTypeMirror { | 576 class _LazyFunctionTypeMirror { |
| 529 _LazyFunctionTypeMirror(this.returnType, this.parameters) {} | 577 _LazyFunctionTypeMirror(this.returnType, this.parameters) {} |
| 530 | 578 |
| 531 ClassMirror resolve(MirrorSystem mirrors) { | 579 ClassMirror resolve(MirrorSystem mirrors) { |
| 532 return mirrors._lookupFunctionTypeMirror(returnType.resolve(mirrors), | 580 return mirrors._lookupFunctionTypeMirror(returnType.resolve(mirrors), |
| 533 parameters); | 581 parameters); |
| 534 } | 582 } |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 966 | 1014 |
| 967 // Creates a new local mirror for some Object. | 1015 // Creates a new local mirror for some Object. |
| 968 static InstanceMirror reflect(Object reflectee) { | 1016 static InstanceMirror reflect(Object reflectee) { |
| 969 return makeLocalInstanceMirror(reflectee); | 1017 return makeLocalInstanceMirror(reflectee); |
| 970 } | 1018 } |
| 971 | 1019 |
| 972 static ClassMirror reflectClass(Type reflectee) { | 1020 static ClassMirror reflectClass(Type reflectee) { |
| 973 throw new UnimplementedError('reflectClass is not implemented'); | 1021 throw new UnimplementedError('reflectClass is not implemented'); |
| 974 } | 1022 } |
| 975 } | 1023 } |
| OLD | NEW |