| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library dart._js_mirrors; | 5 library dart._js_mirrors; |
| 6 | 6 |
| 7 import 'dart:mirrors'; | 7 import 'dart:mirrors'; |
| 8 import 'dart:_foreign_helper' show JS; | 8 import 'dart:_foreign_helper' show JS; |
| 9 import 'dart:_internal' as _internal; | 9 import 'dart:_internal' as _internal; |
| 10 | 10 |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 InstanceMirror apply(List<dynamic> args, [Map<Symbol, dynamic> namedArgs]) { | 304 InstanceMirror apply(List<dynamic> args, [Map<Symbol, dynamic> namedArgs]) { |
| 305 if (namedArgs != null) { | 305 if (namedArgs != null) { |
| 306 args = new List.from(args); | 306 args = new List.from(args); |
| 307 args.add(_toJsMap(namedArgs)); | 307 args.add(_toJsMap(namedArgs)); |
| 308 } | 308 } |
| 309 var result = _dcall(reflectee, args); | 309 var result = _dcall(reflectee, args); |
| 310 return reflect(result); | 310 return reflect(result); |
| 311 } | 311 } |
| 312 } | 312 } |
| 313 | 313 |
| 314 // For generic classes, mirrors uses the same representation, [ClassMirror], |
| 315 // for the instantiated and uninstantiated type. Somewhat awkwardly, most APIs |
| 316 // (e.g., [newInstance]) treat the uninstantiated type as if instantiated |
| 317 // with all dynamic. The representation below is correspondingly a bit wonky. |
| 318 // For an uninstantiated generic class, [_cls] is the instantiated type (with |
| 319 // dynamic) and [_raw] is null. For an instantiated generic class, [_cls] is |
| 320 // the instantiated type (with the corresponding type parameters), and [_raw] |
| 321 // is the generic factory. |
| 314 class JsClassMirror extends JsMirror implements ClassMirror { | 322 class JsClassMirror extends JsMirror implements ClassMirror { |
| 315 final Type _cls; | 323 final Type _cls; |
| 316 final Symbol simpleName; | 324 final Symbol simpleName; |
| 317 // Generic class factory | 325 // Generic class factory for instantiated types. |
| 318 final dynamic _raw; | 326 final dynamic _raw; |
| 319 | 327 |
| 328 ClassMirror _originalDeclaration; |
| 329 |
| 320 // TODO(vsm): Do this properly | 330 // TODO(vsm): Do this properly |
| 321 ClassMirror _mixin = null; | 331 ClassMirror _mixin = null; |
| 322 List<TypeMirror> _typeArguments; | 332 List<TypeMirror> _typeArguments; |
| 323 | 333 |
| 324 List<InstanceMirror> _metadata; | 334 List<InstanceMirror> _metadata; |
| 325 Map<Symbol, DeclarationMirror> _declarations; | 335 Map<Symbol, DeclarationMirror> _declarations; |
| 326 | 336 |
| 327 List<InstanceMirror> get metadata { | 337 List<InstanceMirror> get metadata { |
| 328 if (_metadata == null) { | 338 if (_metadata == null) { |
| 329 // Load metadata. | 339 // Load metadata. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 var name = getName(symbol); | 417 var name = getName(symbol); |
| 408 _declarations[symbol] = | 418 _declarations[symbol] = |
| 409 new JsMethodMirror._staticMethod(this, symbol, ft); | 419 new JsMethodMirror._staticMethod(this, symbol, ft); |
| 410 }); | 420 }); |
| 411 _declarations = | 421 _declarations = |
| 412 new Map<Symbol, DeclarationMirror>.unmodifiable(_declarations); | 422 new Map<Symbol, DeclarationMirror>.unmodifiable(_declarations); |
| 413 } | 423 } |
| 414 return _declarations; | 424 return _declarations; |
| 415 } | 425 } |
| 416 | 426 |
| 417 JsClassMirror._(Type cls) | 427 JsClassMirror._(Type cls, {bool instantiated: true}) |
| 418 : _cls = cls, | 428 : _cls = cls, |
| 419 _raw = _getGenericClass(_unwrap(cls)), | 429 _raw = instantiated ? _getGenericClass(_unwrap(cls)) : null, |
| 420 simpleName = new Symbol(JS('String', '#.name', _unwrap(cls))) { | 430 simpleName = new Symbol(JS('String', '#.name', _unwrap(cls))) { |
| 421 var typeArgs = _getGenericArgs(_unwrap(cls)); | 431 var typeArgs = _getGenericArgs(_unwrap(_cls)); |
| 422 if (typeArgs == null) { | 432 if (typeArgs == null) { |
| 423 _typeArguments = const []; | 433 _typeArguments = const []; |
| 424 } else { | 434 } else { |
| 425 _typeArguments = | 435 _typeArguments = |
| 426 new List.unmodifiable(typeArgs.map((t) => reflectType(_wrap(t)))); | 436 new List.unmodifiable(typeArgs.map((t) => reflectType(_wrap(t)))); |
| 427 } | 437 } |
| 428 } | 438 } |
| 429 | 439 |
| 430 InstanceMirror newInstance(Symbol constructorName, List args, | 440 InstanceMirror newInstance(Symbol constructorName, List args, |
| 431 [Map<Symbol, dynamic> namedArgs]) { | 441 [Map<Symbol, dynamic> namedArgs]) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 bool get hasReflectedType => true; | 488 bool get hasReflectedType => true; |
| 479 Type get reflectedType { | 489 Type get reflectedType { |
| 480 return _cls; | 490 return _cls; |
| 481 } | 491 } |
| 482 | 492 |
| 483 bool get isOriginalDeclaration => _raw == null; | 493 bool get isOriginalDeclaration => _raw == null; |
| 484 | 494 |
| 485 List<TypeMirror> get typeArguments => _typeArguments; | 495 List<TypeMirror> get typeArguments => _typeArguments; |
| 486 | 496 |
| 487 TypeMirror get originalDeclaration { | 497 TypeMirror get originalDeclaration { |
| 488 // TODO(vsm): Handle generic case. How should we represent an original | |
| 489 // declaration for a generic class? | |
| 490 if (_raw == null) { | 498 if (_raw == null) { |
| 491 return this; | 499 return this; |
| 492 } | 500 } |
| 493 throw new UnimplementedError( | 501 if (_originalDeclaration != null) { |
| 494 "ClassMirror.originalDeclaration unimplemented"); | 502 return _originalDeclaration; |
| 503 } |
| 504 _originalDeclaration = |
| 505 new JsClassMirror._(_wrap(JS('', '#()', _raw)), instantiated: false); |
| 506 return _originalDeclaration; |
| 495 } | 507 } |
| 496 | 508 |
| 497 ClassMirror get superclass { | 509 ClassMirror get superclass { |
| 498 if (_cls == Object) { | 510 if (_cls == Object) { |
| 499 return null; | 511 return null; |
| 500 } else { | 512 } else { |
| 501 return reflectType(_wrap(JS('Type', '#.__proto__', _unwrap(_cls)))); | 513 return reflectType(_wrap(JS('Type', '#.__proto__', _unwrap(_cls)))); |
| 502 } | 514 } |
| 503 } | 515 } |
| 504 | 516 |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 var param = | 659 var param = |
| 648 new JsParameterMirror._(new Symbol(''), _wrap(type), metadata); | 660 new JsParameterMirror._(new Symbol(''), _wrap(type), metadata); |
| 649 params[i + args.length] = param; | 661 params[i + args.length] = param; |
| 650 } | 662 } |
| 651 | 663 |
| 652 _params = new List.unmodifiable(params); | 664 _params = new List.unmodifiable(params); |
| 653 } | 665 } |
| 654 | 666 |
| 655 String toString() => "MethodMirror on '$_name'"; | 667 String toString() => "MethodMirror on '$_name'"; |
| 656 } | 668 } |
| OLD | NEW |