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

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

Powered by Google App Engine
This is Rietveld 408576698