Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: runtime/lib/mirrors_impl.dart

Issue 23604003: Support named and optional positional arguments in reflective invocation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fix wrong variable in async unwrap error message Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/lib/mirrors.cc ('k') | runtime/vm/bootstrap_natives.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 var map = new Map<Uri, LibraryMirror>(); 74 var map = new Map<Uri, LibraryMirror>();
75 list.forEach((LibraryMirror mirror) => map[mirror.uri] = mirror); 75 list.forEach((LibraryMirror mirror) => map[mirror.uri] = mirror);
76 return map; 76 return map;
77 } 77 }
78 78
79 List _metadata(reflectee) 79 List _metadata(reflectee)
80 native 'DeclarationMirror_metadata'; 80 native 'DeclarationMirror_metadata';
81 81
82 // This will verify the argument types, unwrap them, and ensure we have a fixed 82 // This will verify the argument types, unwrap them, and ensure we have a fixed
83 // array. 83 // array.
84 List _unwarpAsyncPositionals(wrappedArgs){ 84 List _unwarpAsyncPositionals(wrappedArgs) {
85 List unwrappedArgs = new List(wrappedArgs.length); 85 List unwrappedArgs = new List(wrappedArgs.length);
86 for(int i = 0; i < wrappedArgs.length; i++){ 86 for(int i = 0; i < wrappedArgs.length; i++){
87 var wrappedArg = wrappedArgs[i]; 87 var wrappedArg = wrappedArgs[i];
88 if(_isSimpleValue(wrappedArg)) { 88 if(_isSimpleValue(wrappedArg)) {
89 unwrappedArgs[i] = wrappedArg; 89 unwrappedArgs[i] = wrappedArg;
90 } else if(wrappedArg is InstanceMirror) { 90 } else if(wrappedArg is InstanceMirror) {
91 unwrappedArgs[i] = wrappedArg._reflectee; 91 unwrappedArgs[i] = wrappedArg._reflectee;
92 } else { 92 } else {
93 throw "positional argument $i ($arg) was not a simple value or InstanceMir ror"; 93 throw "positional argument $i ($wrappedArg) was "
94 "not a simple value or InstanceMirror";
94 } 95 }
95 } 96 }
96 return unwrappedArgs; 97 return unwrappedArgs;
97 } 98 }
99 Map _unwarpAsyncNamed(wrappedArgs) {
100 if (wrappedArgs==null) return null;
101 Map unwrappedArgs = new Map();
102 wrappedArgs.forEach((name, wrappedArg){
103 if(_isSimpleValue(wrappedArg)) {
104 unwrappedArgs[name] = wrappedArg;
105 } else if(wrappedArg is InstanceMirror) {
106 unwrappedArgs[name] = wrappedArg._reflectee;
107 } else {
108 throw "named argument ${_n(name)} ($wrappedArg) was "
109 "not a simple value or InstanceMirror";
110 }
111 });
112 return unwrappedArgs;
113 }
98 114
99 class _LocalMirrorSystemImpl extends MirrorSystem { 115 class _LocalMirrorSystemImpl extends MirrorSystem {
100 // Change parameter back to "this.libraries" when native code is changed. 116 // Change parameter back to "this.libraries" when native code is changed.
101 _LocalMirrorSystemImpl(List<LibraryMirror> libraries, this.isolate) 117 _LocalMirrorSystemImpl(List<LibraryMirror> libraries, this.isolate)
102 : this.libraries = _createLibrariesMap(libraries); 118 : this.libraries = _createLibrariesMap(libraries);
103 119
104 final Map<Uri, LibraryMirror> libraries; 120 final Map<Uri, LibraryMirror> libraries;
105 final IsolateMirror isolate; 121 final IsolateMirror isolate;
106 122
107 TypeMirror _dynamicType = null; 123 TypeMirror _dynamicType = null;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 164
149 abstract class _LocalObjectMirrorImpl extends _LocalMirrorImpl 165 abstract class _LocalObjectMirrorImpl extends _LocalMirrorImpl
150 implements ObjectMirror { 166 implements ObjectMirror {
151 _LocalObjectMirrorImpl(this._reflectee); 167 _LocalObjectMirrorImpl(this._reflectee);
152 168
153 final _reflectee; // May be a MirrorReference or an ordinary object. 169 final _reflectee; // May be a MirrorReference or an ordinary object.
154 170
155 InstanceMirror invoke(Symbol memberName, 171 InstanceMirror invoke(Symbol memberName,
156 List positionalArguments, 172 List positionalArguments,
157 [Map<Symbol, dynamic> namedArguments]) { 173 [Map<Symbol, dynamic> namedArguments]) {
158 if (namedArguments != null) { 174
159 throw new UnimplementedError( 175 int numPositionalArguments = positionalArguments.length;
160 'named argument support is not implemented'); 176 int numNamedArguments = namedArguments != null ? namedArguments.length : 0;
177 int numArguments = numPositionalArguments + numNamedArguments;
178 List arguments = new List(numArguments);
179 arguments.setRange(0, numPositionalArguments, positionalArguments);
180 List names = new List(numNamedArguments);
181 int argumentIndex = numPositionalArguments;
182 int nameIndex = 0;
183 if (numNamedArguments > 0) {
184 namedArguments.forEach((name, value) {
185 arguments[argumentIndex++] = value;
186 names[nameIndex++] = _n(name);
187 });
161 } 188 }
189
162 return reflect(this._invoke(_reflectee, 190 return reflect(this._invoke(_reflectee,
163 _n(memberName), 191 _n(memberName),
164 positionalArguments.toList(growable:false))); 192 arguments,
193 names));
165 } 194 }
166 195
167 InstanceMirror getField(Symbol memberName) { 196 InstanceMirror getField(Symbol memberName) {
168 return reflect(this._invokeGetter(_reflectee, 197 return reflect(this._invokeGetter(_reflectee,
169 _n(memberName))); 198 _n(memberName)));
170 } 199 }
171 200
172 InstanceMirror setField(Symbol memberName, Object value) { 201 InstanceMirror setField(Symbol memberName, Object value) {
173 this._invokeSetter(_reflectee, 202 this._invokeSetter(_reflectee,
174 _n(memberName), 203 _n(memberName),
175 value); 204 value);
176 return reflect(value); 205 return reflect(value);
177 } 206 }
178 207
179 Future<InstanceMirror> invokeAsync(Symbol memberName, 208 Future<InstanceMirror> invokeAsync(Symbol memberName,
180 List positionalArguments, 209 List positionalArguments,
181 [Map<Symbol, dynamic> namedArguments]) { 210 [Map<Symbol, dynamic> namedArguments]) {
182 if (namedArguments != null) { 211 return new Future(() {
183 throw new UnimplementedError( 212 return this.invoke(memberName,
184 'named argument support is not implemented'); 213 _unwarpAsyncPositionals(positionalArguments),
185 } 214 _unwarpAsyncNamed(namedArguments));
186 215 });
187 try {
188 var result = this._invoke(_reflectee,
189 _n(memberName),
190 _unwarpAsyncPositionals(positionalArguments));
191 return new Future.value(reflect(result));
192 } catch(e) {
193 return new Future.error(e);
194 }
195 } 216 }
196 217
197 Future<InstanceMirror> getFieldAsync(Symbol memberName) { 218 Future<InstanceMirror> getFieldAsync(Symbol memberName) {
198 try { 219 try {
199 var result = this._invokeGetter(_reflectee, 220 var result = this._invokeGetter(_reflectee,
200 _n(memberName)); 221 _n(memberName));
201 return new Future.value(reflect(result)); 222 return new Future.value(reflect(result));
202 } catch(e) { 223 } catch(e) {
203 return new Future.error(e); 224 return new Future.error(e);
204 } 225 }
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 // successive invocations. 313 // successive invocations.
293 var h = _reflectee is num ? _reflectee.hashCode : _identityHash(_reflectee); 314 var h = _reflectee is num ? _reflectee.hashCode : _identityHash(_reflectee);
294 // Avoid hash collisions with the reflectee. This constant is in Smi range 315 // Avoid hash collisions with the reflectee. This constant is in Smi range
295 // and happens to be the inner padding from RFC 2104. 316 // and happens to be the inner padding from RFC 2104.
296 return h ^ 0x36363636; 317 return h ^ 0x36363636;
297 } 318 }
298 319
299 static _identityHash(reflectee) 320 static _identityHash(reflectee)
300 native "InstanceMirror_identityHash"; 321 native "InstanceMirror_identityHash";
301 322
302 _invoke(reflectee, functionName, positionalArguments) 323 // Override to include the receiver in the arguments.
324 InstanceMirror invoke(Symbol memberName,
325 List positionalArguments,
326 [Map<Symbol, dynamic> namedArguments]) {
327
328 int numPositionalArguments = positionalArguments.length + 1; // Receiver.
329 int numNamedArguments = namedArguments != null ? namedArguments.length : 0;
330 int numArguments = numPositionalArguments + numNamedArguments;
331 List arguments = new List(numArguments);
332 arguments[0] = _reflectee; // Receiver.
333 arguments.setRange(1, numPositionalArguments, positionalArguments);
334 List names = new List(numNamedArguments);
335 int argumentIndex = numPositionalArguments;
336 int nameIndex = 0;
337 if (numNamedArguments > 0) {
338 namedArguments.forEach((name, value) {
339 arguments[argumentIndex++] = value;
340 names[nameIndex++] = _n(name);
341 });
342 }
343
344 return reflect(this._invoke(_reflectee,
345 _n(memberName),
346 arguments,
347 names));
348 }
349
350 _invoke(reflectee, functionName, arguments, argumentNames)
303 native 'InstanceMirror_invoke'; 351 native 'InstanceMirror_invoke';
304 352
305 _invokeGetter(reflectee, getterName) 353 _invokeGetter(reflectee, getterName)
306 native 'InstanceMirror_invokeGetter'; 354 native 'InstanceMirror_invokeGetter';
307 355
308 _invokeSetter(reflectee, setterName, value) 356 _invokeSetter(reflectee, setterName, value)
309 native 'InstanceMirror_invokeSetter'; 357 native 'InstanceMirror_invokeSetter';
310 358
311 static _computeType(reflectee) 359 static _computeType(reflectee)
312 native 'Object_runtimeType'; 360 native 'Object_runtimeType';
(...skipping 11 matching lines...) Expand all
324 return _function; 372 return _function;
325 } 373 }
326 374
327 String get source { 375 String get source {
328 throw new UnimplementedError( 376 throw new UnimplementedError(
329 'ClosureMirror.source is not implemented'); 377 'ClosureMirror.source is not implemented');
330 } 378 }
331 379
332 InstanceMirror apply(List<Object> positionalArguments, 380 InstanceMirror apply(List<Object> positionalArguments,
333 [Map<Symbol, Object> namedArguments]) { 381 [Map<Symbol, Object> namedArguments]) {
334 if (namedArguments != null) { 382 // TODO(iposva): When closures get an ordinary call method, this can be
335 throw new UnimplementedError( 383 // replaced with
336 'named argument support is not implemented'); 384 // return this.invoke(#call, positionalArguments, namedArguments);
385 // and the native ClosureMirror_apply can be removed.
386
387 int numPositionalArguments = positionalArguments.length + 1; // Receiver.
388 int numNamedArguments = namedArguments != null ? namedArguments.length : 0;
389 int numArguments = numPositionalArguments + numNamedArguments;
390 List arguments = new List(numArguments);
391 arguments[0] = _reflectee; // Receiver.
392 arguments.setRange(1, numPositionalArguments, positionalArguments);
393 List names = new List(numNamedArguments);
394 int argumentIndex = numPositionalArguments;
395 int nameIndex = 0;
396 if (numNamedArguments > 0) {
397 namedArguments.forEach((name, value) {
398 arguments[argumentIndex++] = value;
399 names[nameIndex++] = _n(name);
400 });
337 } 401 }
402
338 // It is tempting to implement this in terms of Function.apply, but then 403 // It is tempting to implement this in terms of Function.apply, but then
339 // lazy compilation errors would be fatal. 404 // lazy compilation errors would be fatal.
340 return reflect(_apply(_reflectee, 405 return reflect(_apply(_reflectee, arguments, names));
341 positionalArguments.toList(growable:false)));
342 } 406 }
343 407
344 Future<InstanceMirror> applyAsync(List positionalArguments, 408 Future<InstanceMirror> applyAsync(List positionalArguments,
345 [Map<Symbol, dynamic> namedArguments]) { 409 [Map<Symbol, dynamic> namedArguments]) {
346 if (namedArguments != null) { 410 return new Future(() {
347 throw new UnimplementedError( 411 return this.apply(_unwarpAsyncPositionals(positionalArguments),
348 'named argument support is not implemented'); 412 _unwarpAsyncNamed(namedArguments));
349 } 413 });
350
351 try {
352 var result = _apply(_reflectee,
353 _unwarpAsyncPositionals(positionalArguments));
354 return new Future.value(reflect(result));
355 } on MirroredError catch(e) {
356 return new Future.error(e);
357 }
358 } 414 }
359 415
360 Future<InstanceMirror> findInContext(Symbol name) { 416 Future<InstanceMirror> findInContext(Symbol name) {
361 throw new UnimplementedError( 417 throw new UnimplementedError(
362 'ClosureMirror.findInContext() is not implemented'); 418 'ClosureMirror.findInContext() is not implemented');
363 } 419 }
364 420
365 String toString() => "ClosureMirror on '${Error.safeToString(_reflectee)}'"; 421 String toString() => "ClosureMirror on '${Error.safeToString(_reflectee)}'";
366 422
367 static _apply(reflectee, positionalArguments) 423 static _apply(reflectee, arguments, argumentNames)
368 native 'ClosureMirror_apply'; 424 native 'ClosureMirror_apply';
369 425
370 static _computeFunction(reflectee) 426 static _computeFunction(reflectee)
371 native 'ClosureMirror_function'; 427 native 'ClosureMirror_function';
372 } 428 }
373 429
374 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl 430 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl
375 implements ClassMirror { 431 implements ClassMirror {
376 _LocalClassMirrorImpl(reflectee, 432 _LocalClassMirrorImpl(reflectee,
377 this._reflectedType, 433 this._reflectedType,
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 } 656 }
601 } 657 }
602 658
603 String toString() { 659 String toString() {
604 return "ClassMirror on '${_n(simpleName)}'"; 660 return "ClassMirror on '${_n(simpleName)}'";
605 } 661 }
606 662
607 InstanceMirror newInstance(Symbol constructorName, 663 InstanceMirror newInstance(Symbol constructorName,
608 List positionalArguments, 664 List positionalArguments,
609 [Map<Symbol, dynamic> namedArguments]) { 665 [Map<Symbol, dynamic> namedArguments]) {
610 if (namedArguments != null) { 666 // Native code will add the 1 or 2 implicit arguments depending on whether
611 throw new UnimplementedError( 667 // we end up invoking a factory or constructor respectively.
612 'named argument support is not implemented'); 668 int numPositionalArguments = positionalArguments.length;
669 int numNamedArguments = namedArguments != null ? namedArguments.length : 0;
670 int numArguments = numPositionalArguments + numNamedArguments;
671 List arguments = new List(numArguments);
672 arguments.setRange(0, numPositionalArguments, positionalArguments);
673 List names = new List(numNamedArguments);
674 int argumentIndex = numPositionalArguments;
675 int nameIndex = 0;
676 if (numNamedArguments > 0) {
677 namedArguments.forEach((name, value) {
678 arguments[argumentIndex++] = value;
679 names[nameIndex++] = _n(name);
680 });
613 } 681 }
682
614 return reflect(_invokeConstructor(_reflectee, 683 return reflect(_invokeConstructor(_reflectee,
615 _n(constructorName), 684 _n(constructorName),
616 positionalArguments.toList(growable:false) )); 685 arguments,
686 names));
617 } 687 }
618 688
619 Future<InstanceMirror> newInstanceAsync(Symbol constructorName, 689 Future<InstanceMirror> newInstanceAsync(Symbol constructorName,
620 List positionalArguments, 690 List positionalArguments,
621 [Map<Symbol, dynamic> namedArguments]) { 691 [Map<Symbol, dynamic> namedArguments]) {
622 if (namedArguments != null) { 692 return new Future(() {
623 throw new UnimplementedError( 693 return this.newInstance(constructorName,
624 'named argument support is not implemented'); 694 _unwarpAsyncPositionals(positionalArguments),
625 } 695 _unwarpAsyncNamed(namedArguments));
626 696 });
627 try {
628 var result = _invokeConstructor(_reflectee,
629 _n(constructorName),
630 _unwarpAsyncPositionals(positionalArgument s));
631 return new Future.value(reflect(result));
632 } catch(e) {
633 return new Future.error(e);
634 }
635 } 697 }
636 698
637 List<InstanceMirror> get metadata { 699 List<InstanceMirror> get metadata {
638 // Get the metadata objects, convert them into InstanceMirrors using 700 // Get the metadata objects, convert them into InstanceMirrors using
639 // reflect() and then make them into a Dart list. 701 // reflect() and then make them into a Dart list.
640 return _metadata(_reflectee).map(reflect).toList(growable:false); 702 return _metadata(_reflectee).map(reflect).toList(growable:false);
641 } 703 }
642 704
643 bool operator ==(other) { 705 bool operator ==(other) {
644 return this.runtimeType == other.runtimeType && 706 return this.runtimeType == other.runtimeType &&
(...skipping 17 matching lines...) Expand all
662 724
663 static _computeMixin(reflectee) 725 static _computeMixin(reflectee)
664 native "ClassMirror_mixin"; 726 native "ClassMirror_mixin";
665 727
666 _computeMembers(reflectee) 728 _computeMembers(reflectee)
667 native "ClassMirror_members"; 729 native "ClassMirror_members";
668 730
669 _computeConstructors(reflectee) 731 _computeConstructors(reflectee)
670 native "ClassMirror_constructors"; 732 native "ClassMirror_constructors";
671 733
672 _invoke(reflectee, memberName, positionalArguments) 734 _invoke(reflectee, memberName, arguments, argumentNames)
673 native 'ClassMirror_invoke'; 735 native 'ClassMirror_invoke';
674 736
675 _invokeGetter(reflectee, getterName) 737 _invokeGetter(reflectee, getterName)
676 native 'ClassMirror_invokeGetter'; 738 native 'ClassMirror_invokeGetter';
677 739
678 _invokeSetter(reflectee, setterName, value) 740 _invokeSetter(reflectee, setterName, value)
679 native 'ClassMirror_invokeSetter'; 741 native 'ClassMirror_invokeSetter';
680 742
681 static _invokeConstructor(reflectee, constructorName, positionalArguments) 743 static _invokeConstructor(reflectee, constructorName, arguments, argumentNames )
682 native 'ClassMirror_invokeConstructor'; 744 native 'ClassMirror_invokeConstructor';
683 745
684 static _ClassMirror_type_variables(reflectee) 746 static _ClassMirror_type_variables(reflectee)
685 native "ClassMirror_type_variables"; 747 native "ClassMirror_type_variables";
686 748
687 static _computeTypeArguments(reflectee) 749 static _computeTypeArguments(reflectee)
688 native "ClassMirror_type_arguments"; 750 native "ClassMirror_type_arguments";
689 } 751 }
690 752
691 class _LocalFunctionTypeMirrorImpl extends _LocalClassMirrorImpl 753 class _LocalFunctionTypeMirrorImpl extends _LocalClassMirrorImpl
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 1023
962 String toString() => "LibraryMirror on '${_n(simpleName)}'"; 1024 String toString() => "LibraryMirror on '${_n(simpleName)}'";
963 1025
964 bool operator ==(other) { 1026 bool operator ==(other) {
965 return this.runtimeType == other.runtimeType && 1027 return this.runtimeType == other.runtimeType &&
966 this._reflectee == other._reflectee; 1028 this._reflectee == other._reflectee;
967 } 1029 }
968 1030
969 int get hashCode => simpleName.hashCode; 1031 int get hashCode => simpleName.hashCode;
970 1032
971 _invoke(reflectee, memberName, positionalArguments) 1033 _invoke(reflectee, memberName, arguments, argumentNames)
972 native 'LibraryMirror_invoke'; 1034 native 'LibraryMirror_invoke';
973 1035
974 _invokeGetter(reflectee, getterName) 1036 _invokeGetter(reflectee, getterName)
975 native 'LibraryMirror_invokeGetter'; 1037 native 'LibraryMirror_invokeGetter';
976 1038
977 _invokeSetter(reflectee, setterName, value) 1039 _invokeSetter(reflectee, setterName, value)
978 native 'LibraryMirror_invokeSetter'; 1040 native 'LibraryMirror_invokeSetter';
979 1041
980 _computeMembers(reflectee) 1042 _computeMembers(reflectee)
981 native "LibraryMirror_members"; 1043 native "LibraryMirror_members";
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 if (typeMirror == null) { 1371 if (typeMirror == null) {
1310 typeMirror = makeLocalTypeMirror(key); 1372 typeMirror = makeLocalTypeMirror(key);
1311 _instanitationCache[key] = typeMirror; 1373 _instanitationCache[key] = typeMirror;
1312 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { 1374 if (typeMirror is ClassMirror && !typeMirror._isGeneric) {
1313 _declarationCache[key] = typeMirror; 1375 _declarationCache[key] = typeMirror;
1314 } 1376 }
1315 } 1377 }
1316 return typeMirror; 1378 return typeMirror;
1317 } 1379 }
1318 } 1380 }
OLDNEW
« no previous file with comments | « runtime/lib/mirrors.cc ('k') | runtime/vm/bootstrap_natives.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698