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 | |
143 InstanceMirror invoke(String memberName, | |
144 List positionalArguments, | |
145 [Map<String,dynamic> namedArguments]) { | |
146 if (namedArguments != null) { | |
147 throw new UnimplementedError( | |
148 'named argument support is not implemented'); | |
149 } | |
150 return _invoke(this, memberName, positionalArguments); // needs to be distin ct for sync calls | |
Ivan Posva
2013/04/12 21:15:26
Long lines.
| |
151 } | |
152 | |
153 InstanceMirror getField(String fieldName) { | |
154 return _getField(this, fieldName); // needs to be distinct for sync calls | |
155 } | |
156 | |
157 InstanceMirror setField(String fieldName, Object arg) { | |
158 return _setField(this, fieldName, arg); // needs to be distinct for sync cal ls | |
159 } | |
160 | |
161 | |
142 | 162 |
143 Future<InstanceMirror> invokeAsync(String memberName, | 163 Future<InstanceMirror> invokeAsync(String memberName, |
Ivan Posva
2013/04/12 21:15:26
Please indent this appropriately.
| |
144 List positionalArguments, | 164 List positionalArguments, |
145 [Map<String,dynamic> namedArguments]) { | 165 [Map<String,dynamic> namedArguments]) { |
146 if (namedArguments != null) { | 166 if (namedArguments != null) { |
147 throw new UnimplementedError( | 167 throw new UnimplementedError( |
148 'named argument support is not implemented'); | 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 _invokeAsync(this, memberName, positionalArguments)); |
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(_setFieldAsync(this, fieldName, arg)); |
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) |
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) |
207 native 'LocalObjectMirrorImpl_setField'; | 227 native 'LocalObjectMirrorImpl_setField'; |
228 | |
229 static _invokeAsync(ref, memberName, positionalArguments) | |
230 native 'LocalObjectMirrorImpl_invokeAsync'; | |
231 | |
232 static _setFieldAsync(ref, fieldName, value) | |
233 native 'LocalObjectMirrorImpl_setFieldAsync'; | |
208 } | 234 } |
209 | 235 |
210 // Prints a string as it might appear in dart program text. | 236 // Prints a string as it might appear in dart program text. |
211 // TODO(turnidge): Consider truncating. | 237 // TODO(turnidge): Consider truncating. |
212 String _dartEscape(String str) { | 238 String _dartEscape(String str) { |
213 bool isNice(int code) => (code >= 32 && code <= 126); | 239 bool isNice(int code) => (code >= 32 && code <= 126); |
214 | 240 |
215 StringBuffer buf = new StringBuffer(); | 241 StringBuffer buf = new StringBuffer(); |
216 for (int i = 0; i < str.length; i++) { | 242 for (int i = 0; i < str.length; i++) { |
217 var input = str[i]; | 243 var input = str[i]; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 reflectee, | 322 reflectee, |
297 this.function) : super(ref, type, reflectee) {} | 323 this.function) : super(ref, type, reflectee) {} |
298 | 324 |
299 final MethodMirror function; | 325 final MethodMirror function; |
300 | 326 |
301 String get source { | 327 String get source { |
302 throw new UnimplementedError( | 328 throw new UnimplementedError( |
303 'ClosureMirror.source is not implemented'); | 329 'ClosureMirror.source is not implemented'); |
304 } | 330 } |
305 | 331 |
332 InstanceMirror apply(List<Object> positionalArguments, | |
333 [Map<String,Object> namedArguments]) { | |
334 if (namedArguments != null) { | |
335 throw new UnimplementedError( | |
336 'named argument support is not implemented'); | |
337 } | |
338 return _apply(this, positionalArguments); | |
339 } | |
340 | |
306 Future<InstanceMirror> applyAsync(List<Object> positionalArguments, | 341 Future<InstanceMirror> applyAsync(List<Object> positionalArguments, |
307 [Map<String,Object> namedArguments]) { | 342 [Map<String,Object> namedArguments]) { |
308 if (namedArguments != null) { | 343 if (namedArguments != null) { |
309 throw new UnimplementedError( | 344 throw new UnimplementedError( |
310 'named argument support is not implemented'); | 345 'named argument support is not implemented'); |
311 } | 346 } |
312 // Walk the arguments and make sure they are legal. | 347 // Walk the arguments and make sure they are legal. |
313 for (int i = 0; i < positionalArguments.length; i++) { | 348 for (int i = 0; i < positionalArguments.length; i++) { |
314 var arg = positionalArguments[i]; | 349 var arg = positionalArguments[i]; |
315 _LocalObjectMirrorImpl._validateArgument(i, arg); | 350 _LocalObjectMirrorImpl._validateArgument(i, arg); |
316 } | 351 } |
317 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); | 352 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); |
318 try { | 353 try { |
319 completer.complete( | 354 completer.complete( |
320 _apply(this, positionalArguments)); | 355 _applyAsync(this, positionalArguments)); |
321 } catch (exception) { | 356 } catch (exception) { |
322 completer.completeError(exception); | 357 completer.completeError(exception); |
323 } | 358 } |
324 return completer.future; | 359 return completer.future; |
325 } | 360 } |
326 | 361 |
327 Future<InstanceMirror> findInContext(String name) { | 362 Future<InstanceMirror> findInContext(String name) { |
328 throw new UnimplementedError( | 363 throw new UnimplementedError( |
329 'ClosureMirror.findInContext() is not implemented'); | 364 'ClosureMirror.findInContext() is not implemented'); |
330 } | 365 } |
331 | 366 |
332 static _apply(ref, positionalArguments) | 367 static _apply(ref, positionalArguments) |
333 native 'LocalClosureMirrorImpl_apply'; | 368 native 'LocalClosureMirrorImpl_apply'; |
369 | |
370 static _applyAsync(ref, positionalArguments) | |
371 native 'LocalClosureMirrorImpl_applyAsync'; | |
334 } | 372 } |
335 | 373 |
336 class _LazyTypeMirror { | 374 class _LazyTypeMirror { |
337 _LazyTypeMirror(this.libraryName, this.typeName) {} | 375 _LazyTypeMirror(this.libraryName, this.typeName) {} |
338 | 376 |
339 TypeMirror resolve(MirrorSystem mirrors) { | 377 TypeMirror resolve(MirrorSystem mirrors) { |
340 if (libraryName == null) { | 378 if (libraryName == null) { |
341 if (typeName == 'dynamic') { | 379 if (typeName == 'dynamic') { |
342 return mirrors.dynamicType; | 380 return mirrors.dynamicType; |
343 } else if (typeName == 'void') { | 381 } else if (typeName == 'void') { |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 'ClassMirror.isOriginalDeclaration is not implemented'); | 530 'ClassMirror.isOriginalDeclaration is not implemented'); |
493 } | 531 } |
494 | 532 |
495 ClassMirror get genericDeclaration { | 533 ClassMirror get genericDeclaration { |
496 throw new UnimplementedError( | 534 throw new UnimplementedError( |
497 'ClassMirror.originalDeclaration is not implemented'); | 535 'ClassMirror.originalDeclaration is not implemented'); |
498 } | 536 } |
499 | 537 |
500 String toString() => "ClassMirror on '$simpleName'"; | 538 String toString() => "ClassMirror on '$simpleName'"; |
501 | 539 |
540 InstanceMirror apply(List<Object> positionalArguments, | |
541 [Map<String,Object> namedArguments]) { | |
542 if (namedArguments != null) { | |
543 throw new UnimplementedError( | |
544 'named argument support is not implemented'); | |
545 } | |
546 return _apply(this, positionalArguments); | |
547 } | |
548 | |
549 InstanceMirror newInstance(String constructorName, | |
550 List positionalArguments, | |
551 [Map<String,dynamic> namedArguments]) { | |
552 if (namedArguments != null) { | |
553 throw new UnimplementedError( | |
554 'named argument support is not implemented'); | |
555 } | |
556 return _invokeConstructor(this, constructorName, positionalArguments); | |
557 } | |
558 | |
502 Future<InstanceMirror> newInstanceAsync(String constructorName, | 559 Future<InstanceMirror> newInstanceAsync(String constructorName, |
503 List positionalArguments, | 560 List positionalArguments, |
504 [Map<String,dynamic> namedArguments]) { | 561 [Map<String,dynamic> namedArguments]) { |
505 if (namedArguments != null) { | 562 if (namedArguments != null) { |
506 throw new UnimplementedError( | 563 throw new UnimplementedError( |
507 'named argument support is not implemented'); | 564 'named argument support is not implemented'); |
508 } | 565 } |
509 // Walk the arguments and make sure they are legal. | 566 // Walk the arguments and make sure they are legal. |
510 for (int i = 0; i < positionalArguments.length; i++) { | 567 for (int i = 0; i < positionalArguments.length; i++) { |
511 var arg = positionalArguments[i]; | 568 var arg = positionalArguments[i]; |
512 _LocalObjectMirrorImpl._validateArgument(i, arg); | 569 _LocalObjectMirrorImpl._validateArgument(i, arg); |
513 } | 570 } |
514 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); | 571 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); |
515 try { | 572 try { |
516 completer.complete( | 573 completer.complete( |
517 _invokeConstructor(this, constructorName, positionalArguments)); | 574 _invokeConstructorAsync(this, constructorName, positionalArguments)); |
518 } catch (exception) { | 575 } catch (exception) { |
519 completer.completeError(exception); | 576 completer.completeError(exception); |
520 } | 577 } |
521 return completer.future; | 578 return completer.future; |
522 } | 579 } |
523 | 580 |
524 static _invokeConstructor(ref, constructorName, positionalArguments) | 581 static _invokeConstructor(ref, constructorName, positionalArguments) |
525 native 'LocalClassMirrorImpl_invokeConstructor'; | 582 native 'LocalClassMirrorImpl_invokeConstructor'; |
583 | |
584 static _invokeConstructorAsync(ref, constructorName, positionalArguments) | |
585 native 'LocalClassMirrorImpl_invokeConstructorAsync'; | |
526 } | 586 } |
527 | 587 |
528 class _LazyFunctionTypeMirror { | 588 class _LazyFunctionTypeMirror { |
529 _LazyFunctionTypeMirror(this.returnType, this.parameters) {} | 589 _LazyFunctionTypeMirror(this.returnType, this.parameters) {} |
530 | 590 |
531 ClassMirror resolve(MirrorSystem mirrors) { | 591 ClassMirror resolve(MirrorSystem mirrors) { |
532 return mirrors._lookupFunctionTypeMirror(returnType.resolve(mirrors), | 592 return mirrors._lookupFunctionTypeMirror(returnType.resolve(mirrors), |
533 parameters); | 593 parameters); |
534 } | 594 } |
535 | 595 |
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
966 | 1026 |
967 // Creates a new local mirror for some Object. | 1027 // Creates a new local mirror for some Object. |
968 static InstanceMirror reflect(Object reflectee) { | 1028 static InstanceMirror reflect(Object reflectee) { |
969 return makeLocalInstanceMirror(reflectee); | 1029 return makeLocalInstanceMirror(reflectee); |
970 } | 1030 } |
971 | 1031 |
972 static ClassMirror reflectClass(Type reflectee) { | 1032 static ClassMirror reflectClass(Type reflectee) { |
973 throw new UnimplementedError('reflectClass is not implemented'); | 1033 throw new UnimplementedError('reflectClass is not implemented'); |
974 } | 1034 } |
975 } | 1035 } |
OLD | NEW |