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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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"; |
| 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 Loading... |
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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 | 297 |
279 String toString() => 'InstanceMirror on ${Error.safeToString(_reflectee)}'; | 298 String toString() => 'InstanceMirror on ${Error.safeToString(_reflectee)}'; |
280 | 299 |
281 bool operator ==(other) { | 300 bool operator ==(other) { |
282 return other is _LocalInstanceMirrorImpl && | 301 return other is _LocalInstanceMirrorImpl && |
283 identical(_reflectee, other._reflectee); | 302 identical(_reflectee, other._reflectee); |
284 } | 303 } |
285 | 304 |
286 int get hashCode => _reflectee.hashCode; | 305 int get hashCode => _reflectee.hashCode; |
287 | 306 |
288 _invoke(reflectee, functionName, positionalArguments) | 307 // Override to include the receiver in the arguments. |
| 308 InstanceMirror invoke(Symbol memberName, |
| 309 List positionalArguments, |
| 310 [Map<Symbol, dynamic> namedArguments]) { |
| 311 |
| 312 int numPositionalArguments = positionalArguments.length + 1; // Receiver. |
| 313 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; |
| 314 int numArguments = numPositionalArguments + numNamedArguments; |
| 315 List arguments = new List(numArguments); |
| 316 arguments[0] = _reflectee; // Receiver. |
| 317 arguments.setRange(1, numPositionalArguments, positionalArguments); |
| 318 List names = new List(numNamedArguments); |
| 319 int argumentIndex = numPositionalArguments; |
| 320 int nameIndex = 0; |
| 321 if (numNamedArguments > 0) { |
| 322 namedArguments.forEach((name, value) { |
| 323 arguments[argumentIndex++] = value; |
| 324 names[nameIndex++] = _n(name); |
| 325 }); |
| 326 } |
| 327 |
| 328 return reflect(this._invoke(_reflectee, |
| 329 _n(memberName), |
| 330 arguments, |
| 331 names)); |
| 332 } |
| 333 |
| 334 _invoke(reflectee, functionName, arguments, argumentNames) |
289 native 'InstanceMirror_invoke'; | 335 native 'InstanceMirror_invoke'; |
290 | 336 |
291 _invokeGetter(reflectee, getterName) | 337 _invokeGetter(reflectee, getterName) |
292 native 'InstanceMirror_invokeGetter'; | 338 native 'InstanceMirror_invokeGetter'; |
293 | 339 |
294 _invokeSetter(reflectee, setterName, value) | 340 _invokeSetter(reflectee, setterName, value) |
295 native 'InstanceMirror_invokeSetter'; | 341 native 'InstanceMirror_invokeSetter'; |
296 | 342 |
297 static _computeType(reflectee) | 343 static _computeType(reflectee) |
298 native 'Object_runtimeType'; | 344 native 'Object_runtimeType'; |
(...skipping 11 matching lines...) Expand all Loading... |
310 return _function; | 356 return _function; |
311 } | 357 } |
312 | 358 |
313 String get source { | 359 String get source { |
314 throw new UnimplementedError( | 360 throw new UnimplementedError( |
315 'ClosureMirror.source is not implemented'); | 361 'ClosureMirror.source is not implemented'); |
316 } | 362 } |
317 | 363 |
318 InstanceMirror apply(List<Object> positionalArguments, | 364 InstanceMirror apply(List<Object> positionalArguments, |
319 [Map<Symbol, Object> namedArguments]) { | 365 [Map<Symbol, Object> namedArguments]) { |
320 if (namedArguments != null) { | 366 int numPositionalArguments = positionalArguments.length + 1; // Receiver. |
321 throw new UnimplementedError( | 367 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; |
322 'named argument support is not implemented'); | 368 int numArguments = numPositionalArguments + numNamedArguments; |
| 369 List arguments = new List(numArguments); |
| 370 arguments[0] = _reflectee; // Receiver. |
| 371 arguments.setRange(1, numPositionalArguments, positionalArguments); |
| 372 List names = new List(numNamedArguments); |
| 373 int argumentIndex = numPositionalArguments; |
| 374 int nameIndex = 0; |
| 375 if (numNamedArguments > 0) { |
| 376 namedArguments.forEach((name, value) { |
| 377 arguments[argumentIndex++] = value; |
| 378 names[nameIndex++] = _n(name); |
| 379 }); |
323 } | 380 } |
| 381 |
324 // It is tempting to implement this in terms of Function.apply, but then | 382 // It is tempting to implement this in terms of Function.apply, but then |
325 // lazy compilation errors would be fatal. | 383 // lazy compilation errors would be fatal. |
326 return reflect(_apply(_reflectee, | 384 return reflect(_apply(_reflectee, arguments, names)); |
327 positionalArguments.toList(growable:false))); | |
328 } | 385 } |
329 | 386 |
330 Future<InstanceMirror> applyAsync(List positionalArguments, | 387 Future<InstanceMirror> applyAsync(List positionalArguments, |
331 [Map<Symbol, dynamic> namedArguments]) { | 388 [Map<Symbol, dynamic> namedArguments]) { |
332 if (namedArguments != null) { | 389 return new Future(() { |
333 throw new UnimplementedError( | 390 return this.apply(_unwarpAsyncPositionals(positionalArguments), |
334 'named argument support is not implemented'); | 391 _unwarpAsyncNamed(namedArguments)); |
335 } | 392 }); |
336 | |
337 try { | |
338 var result = _apply(_reflectee, | |
339 _unwarpAsyncPositionals(positionalArguments)); | |
340 return new Future.value(reflect(result)); | |
341 } on MirroredError catch(e) { | |
342 return new Future.error(e); | |
343 } | |
344 } | 393 } |
345 | 394 |
346 Future<InstanceMirror> findInContext(Symbol name) { | 395 Future<InstanceMirror> findInContext(Symbol name) { |
347 throw new UnimplementedError( | 396 throw new UnimplementedError( |
348 'ClosureMirror.findInContext() is not implemented'); | 397 'ClosureMirror.findInContext() is not implemented'); |
349 } | 398 } |
350 | 399 |
351 String toString() => "ClosureMirror on '${Error.safeToString(_reflectee)}'"; | 400 String toString() => "ClosureMirror on '${Error.safeToString(_reflectee)}'"; |
352 | 401 |
353 static _apply(reflectee, positionalArguments) | 402 static _apply(reflectee, arguments, argumentNames) |
354 native 'ClosureMirror_apply'; | 403 native 'ClosureMirror_apply'; |
355 | 404 |
356 static _computeFunction(reflectee) | 405 static _computeFunction(reflectee) |
357 native 'ClosureMirror_function'; | 406 native 'ClosureMirror_function'; |
358 } | 407 } |
359 | 408 |
360 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl | 409 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl |
361 implements ClassMirror { | 410 implements ClassMirror { |
362 _LocalClassMirrorImpl(reflectee, | 411 _LocalClassMirrorImpl(reflectee, |
363 this._reflectedType, | 412 this._reflectedType, |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 } | 590 } |
542 } | 591 } |
543 | 592 |
544 String toString() { | 593 String toString() { |
545 return "ClassMirror on '${_n(simpleName)}'"; | 594 return "ClassMirror on '${_n(simpleName)}'"; |
546 } | 595 } |
547 | 596 |
548 InstanceMirror newInstance(Symbol constructorName, | 597 InstanceMirror newInstance(Symbol constructorName, |
549 List positionalArguments, | 598 List positionalArguments, |
550 [Map<Symbol, dynamic> namedArguments]) { | 599 [Map<Symbol, dynamic> namedArguments]) { |
551 if (namedArguments != null) { | 600 // Native code will add the 1 or 2 implicit arguments depending on whether |
552 throw new UnimplementedError( | 601 // we end up invoking a factory or constructor respectively. |
553 'named argument support is not implemented'); | 602 int numPositionalArguments = positionalArguments.length; |
| 603 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; |
| 604 int numArguments = numPositionalArguments + numNamedArguments; |
| 605 List arguments = new List(numArguments); |
| 606 arguments.setRange(0, numPositionalArguments, positionalArguments); |
| 607 List names = new List(numNamedArguments); |
| 608 int argumentIndex = numPositionalArguments; |
| 609 int nameIndex = 0; |
| 610 if (numNamedArguments > 0) { |
| 611 namedArguments.forEach((name, value) { |
| 612 arguments[argumentIndex++] = value; |
| 613 names[nameIndex++] = _n(name); |
| 614 }); |
554 } | 615 } |
| 616 |
555 return reflect(_invokeConstructor(_reflectee, | 617 return reflect(_invokeConstructor(_reflectee, |
556 _n(constructorName), | 618 _n(constructorName), |
557 positionalArguments.toList(growable:false)
)); | 619 arguments, |
| 620 names)); |
558 } | 621 } |
559 | 622 |
560 Future<InstanceMirror> newInstanceAsync(Symbol constructorName, | 623 Future<InstanceMirror> newInstanceAsync(Symbol constructorName, |
561 List positionalArguments, | 624 List positionalArguments, |
562 [Map<Symbol, dynamic> namedArguments])
{ | 625 [Map<Symbol, dynamic> namedArguments])
{ |
563 if (namedArguments != null) { | 626 return new Future(() { |
564 throw new UnimplementedError( | 627 return this.newInstance(constructorName, |
565 'named argument support is not implemented'); | 628 _unwarpAsyncPositionals(positionalArguments), |
566 } | 629 _unwarpAsyncNamed(namedArguments)); |
567 | 630 }); |
568 try { | |
569 var result = _invokeConstructor(_reflectee, | |
570 _n(constructorName), | |
571 _unwarpAsyncPositionals(positionalArgument
s)); | |
572 return new Future.value(reflect(result)); | |
573 } catch(e) { | |
574 return new Future.error(e); | |
575 } | |
576 } | 631 } |
577 | 632 |
578 List<InstanceMirror> get metadata { | 633 List<InstanceMirror> get metadata { |
579 // Get the metadata objects, convert them into InstanceMirrors using | 634 // Get the metadata objects, convert them into InstanceMirrors using |
580 // reflect() and then make them into a Dart list. | 635 // reflect() and then make them into a Dart list. |
581 return _metadata(_reflectee).map(reflect).toList(growable:false); | 636 return _metadata(_reflectee).map(reflect).toList(growable:false); |
582 } | 637 } |
583 | 638 |
584 bool operator ==(other) { | 639 bool operator ==(other) { |
585 return this.runtimeType == other.runtimeType && | 640 return this.runtimeType == other.runtimeType && |
(...skipping 15 matching lines...) Expand all Loading... |
601 | 656 |
602 static _interfaces(reflectee) | 657 static _interfaces(reflectee) |
603 native "ClassMirror_interfaces"; | 658 native "ClassMirror_interfaces"; |
604 | 659 |
605 _computeMembers(reflectee) | 660 _computeMembers(reflectee) |
606 native "ClassMirror_members"; | 661 native "ClassMirror_members"; |
607 | 662 |
608 _computeConstructors(reflectee) | 663 _computeConstructors(reflectee) |
609 native "ClassMirror_constructors"; | 664 native "ClassMirror_constructors"; |
610 | 665 |
611 _invoke(reflectee, memberName, positionalArguments) | 666 _invoke(reflectee, memberName, arguments, argumentNames) |
612 native 'ClassMirror_invoke'; | 667 native 'ClassMirror_invoke'; |
613 | 668 |
614 _invokeGetter(reflectee, getterName) | 669 _invokeGetter(reflectee, getterName) |
615 native 'ClassMirror_invokeGetter'; | 670 native 'ClassMirror_invokeGetter'; |
616 | 671 |
617 _invokeSetter(reflectee, setterName, value) | 672 _invokeSetter(reflectee, setterName, value) |
618 native 'ClassMirror_invokeSetter'; | 673 native 'ClassMirror_invokeSetter'; |
619 | 674 |
620 static _invokeConstructor(reflectee, constructorName, positionalArguments) | 675 static _invokeConstructor(reflectee, constructorName, arguments, argumentNames
) |
621 native 'ClassMirror_invokeConstructor'; | 676 native 'ClassMirror_invokeConstructor'; |
622 | 677 |
623 static _ClassMirror_type_variables(reflectee) | 678 static _ClassMirror_type_variables(reflectee) |
624 native "ClassMirror_type_variables"; | 679 native "ClassMirror_type_variables"; |
625 | 680 |
626 static _computeTypeArguments(reflectee) | 681 static _computeTypeArguments(reflectee) |
627 native "ClassMirror_type_arguments"; | 682 native "ClassMirror_type_arguments"; |
628 } | 683 } |
629 | 684 |
630 class _LocalFunctionTypeMirrorImpl extends _LocalClassMirrorImpl | 685 class _LocalFunctionTypeMirrorImpl extends _LocalClassMirrorImpl |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 | 954 |
900 String toString() => "LibraryMirror on '${_n(simpleName)}'"; | 955 String toString() => "LibraryMirror on '${_n(simpleName)}'"; |
901 | 956 |
902 bool operator ==(other) { | 957 bool operator ==(other) { |
903 return this.runtimeType == other.runtimeType && | 958 return this.runtimeType == other.runtimeType && |
904 this._reflectee == other._reflectee; | 959 this._reflectee == other._reflectee; |
905 } | 960 } |
906 | 961 |
907 int get hashCode => simpleName.hashCode; | 962 int get hashCode => simpleName.hashCode; |
908 | 963 |
909 _invoke(reflectee, memberName, positionalArguments) | 964 _invoke(reflectee, memberName, arguments, argumentNames) |
910 native 'LibraryMirror_invoke'; | 965 native 'LibraryMirror_invoke'; |
911 | 966 |
912 _invokeGetter(reflectee, getterName) | 967 _invokeGetter(reflectee, getterName) |
913 native 'LibraryMirror_invokeGetter'; | 968 native 'LibraryMirror_invokeGetter'; |
914 | 969 |
915 _invokeSetter(reflectee, setterName, value) | 970 _invokeSetter(reflectee, setterName, value) |
916 native 'LibraryMirror_invokeSetter'; | 971 native 'LibraryMirror_invokeSetter'; |
917 | 972 |
918 _computeMembers(reflectee) | 973 _computeMembers(reflectee) |
919 native "LibraryMirror_members"; | 974 native "LibraryMirror_members"; |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1237 if (typeMirror == null) { | 1292 if (typeMirror == null) { |
1238 typeMirror = makeLocalTypeMirror(key); | 1293 typeMirror = makeLocalTypeMirror(key); |
1239 _instanitationCache[key] = typeMirror; | 1294 _instanitationCache[key] = typeMirror; |
1240 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1295 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
1241 _declarationCache[key] = typeMirror; | 1296 _declarationCache[key] = typeMirror; |
1242 } | 1297 } |
1243 } | 1298 } |
1244 return typeMirror; | 1299 return typeMirror; |
1245 } | 1300 } |
1246 } | 1301 } |
OLD | NEW |