Chromium Code Reviews| 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 void _initialize() { |
| 418 : _cls = cls, | 428 var typeArgs = _getGenericArgs(_unwrap(_cls)); |
| 419 _raw = _getGenericClass(_unwrap(cls)), | |
| 420 simpleName = new Symbol(JS('String', '#.name', _unwrap(cls))) { | |
| 421 var typeArgs = _getGenericArgs(_unwrap(cls)); | |
| 422 if (typeArgs == null) { | 429 if (typeArgs == null) { |
| 423 _typeArguments = const []; | 430 _typeArguments = const []; |
| 424 } else { | 431 } else { |
| 425 _typeArguments = | 432 _typeArguments = |
| 426 new List.unmodifiable(typeArgs.map((t) => reflectType(_wrap(t)))); | 433 new List.unmodifiable(typeArgs.map((t) => reflectType(_wrap(t)))); |
| 427 } | 434 } |
| 428 } | 435 } |
| 429 | 436 |
| 437 JsClassMirror._(Type cls) | |
| 438 : _cls = cls, | |
| 439 _raw = _getGenericClass(_unwrap(cls)), | |
| 440 simpleName = new Symbol(JS('String', '#.name', _unwrap(cls))) { | |
| 441 _initialize(); | |
| 442 } | |
| 443 | |
| 444 JsClassMirror._uninstantiated(Type cls) | |
| 445 : _cls = cls, | |
| 446 _raw = null, | |
|
Bob Nystrom
2017/05/25 20:16:05
There's some duplication between these. Maybe a si
vsm
2017/05/25 20:26:52
That is cleaner. :-)
Done and re-inlined initi
| |
| 447 simpleName = new Symbol(JS('String', '#.name', _unwrap(cls))) { | |
| 448 _initialize(); | |
| 449 } | |
| 450 | |
| 430 InstanceMirror newInstance(Symbol constructorName, List args, | 451 InstanceMirror newInstance(Symbol constructorName, List args, |
| 431 [Map<Symbol, dynamic> namedArgs]) { | 452 [Map<Symbol, dynamic> namedArgs]) { |
| 432 // TODO(vsm): Support factory constructors and named arguments. | 453 // TODO(vsm): Support factory constructors and named arguments. |
| 433 var name = getName(constructorName); | 454 var name = getName(constructorName); |
| 434 assert(namedArgs == null || namedArgs.isEmpty); | 455 assert(namedArgs == null || namedArgs.isEmpty); |
| 435 var instance = (name == 'new' || name == '') | 456 var instance = (name == 'new' || name == '') |
| 436 ? JS('', 'new #(...#)', _unwrap(_cls), args) | 457 ? JS('', 'new #(...#)', _unwrap(_cls), args) |
| 437 : JS('', 'new (#.#)(...#)', _unwrap(_cls), name, args); | 458 : JS('', 'new (#.#)(...#)', _unwrap(_cls), name, args); |
| 438 return reflect(instance); | 459 return reflect(instance); |
| 439 } | 460 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 483 bool get isOriginalDeclaration => _raw == null; | 504 bool get isOriginalDeclaration => _raw == null; |
| 484 | 505 |
| 485 List<TypeMirror> get typeArguments => _typeArguments; | 506 List<TypeMirror> get typeArguments => _typeArguments; |
| 486 | 507 |
| 487 TypeMirror get originalDeclaration { | 508 TypeMirror get originalDeclaration { |
| 488 // TODO(vsm): Handle generic case. How should we represent an original | 509 // TODO(vsm): Handle generic case. How should we represent an original |
| 489 // declaration for a generic class? | 510 // declaration for a generic class? |
| 490 if (_raw == null) { | 511 if (_raw == null) { |
| 491 return this; | 512 return this; |
| 492 } | 513 } |
| 493 throw new UnimplementedError( | 514 if (_originalDeclaration != null) { |
| 494 "ClassMirror.originalDeclaration unimplemented"); | 515 return _originalDeclaration; |
| 516 } | |
| 517 _originalDeclaration = | |
| 518 new JsClassMirror._uninstantiated(_wrap(JS('', '#()', _raw))); | |
| 519 return _originalDeclaration; | |
| 495 } | 520 } |
| 496 | 521 |
| 497 ClassMirror get superclass { | 522 ClassMirror get superclass { |
| 498 if (_cls == Object) { | 523 if (_cls == Object) { |
| 499 return null; | 524 return null; |
| 500 } else { | 525 } else { |
| 501 return reflectType(_wrap(JS('Type', '#.__proto__', _unwrap(_cls)))); | 526 return reflectType(_wrap(JS('Type', '#.__proto__', _unwrap(_cls)))); |
| 502 } | 527 } |
| 503 } | 528 } |
| 504 | 529 |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 var param = | 672 var param = |
| 648 new JsParameterMirror._(new Symbol(''), _wrap(type), metadata); | 673 new JsParameterMirror._(new Symbol(''), _wrap(type), metadata); |
| 649 params[i + args.length] = param; | 674 params[i + args.length] = param; |
| 650 } | 675 } |
| 651 | 676 |
| 652 _params = new List.unmodifiable(params); | 677 _params = new List.unmodifiable(params); |
| 653 } | 678 } |
| 654 | 679 |
| 655 String toString() => "MethodMirror on '$_name'"; | 680 String toString() => "MethodMirror on '$_name'"; |
| 656 } | 681 } |
| OLD | NEW |