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 import 'dart:collection' show HashMap, HashSet; | 5 import 'dart:collection' show HashMap, HashSet; |
6 import 'dart:math' show min, max; | 6 import 'dart:math' show min, max; |
7 | 7 |
8 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 8 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
10 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; | 10 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; |
(...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
694 if (findAnnotation(classElem, isPublicJSAnnotation) != null) return null; | 694 if (findAnnotation(classElem, isPublicJSAnnotation) != null) return null; |
695 | 695 |
696 // If this is a JavaScript type, emit it now and then exit. | 696 // If this is a JavaScript type, emit it now and then exit. |
697 var jsTypeDef = _emitJsType(classElem); | 697 var jsTypeDef = _emitJsType(classElem); |
698 if (jsTypeDef != null) return jsTypeDef; | 698 if (jsTypeDef != null) return jsTypeDef; |
699 | 699 |
700 var ctors = <ConstructorDeclaration>[]; | 700 var ctors = <ConstructorDeclaration>[]; |
701 var fields = <FieldDeclaration>[]; | 701 var fields = <FieldDeclaration>[]; |
702 var staticFields = <FieldDeclaration>[]; | 702 var staticFields = <FieldDeclaration>[]; |
703 var methods = <MethodDeclaration>[]; | 703 var methods = <MethodDeclaration>[]; |
704 bool isCallable = false; | |
704 for (var member in node.members) { | 705 for (var member in node.members) { |
705 if (member is ConstructorDeclaration) { | 706 if (member is ConstructorDeclaration) { |
706 ctors.add(member); | 707 ctors.add(member); |
707 } else if (member is FieldDeclaration) { | 708 } else if (member is FieldDeclaration) { |
708 (member.isStatic ? staticFields : fields).add(member); | 709 (member.isStatic ? staticFields : fields).add(member); |
709 } else if (member is MethodDeclaration) { | 710 } else if (member is MethodDeclaration) { |
710 methods.add(member); | 711 methods.add(member); |
712 if (member.name.name == 'call' && | |
713 !member.isGetter && | |
714 !member.isSetter) { | |
vsm
2016/06/15 15:23:25
Per spec / VM, 'call' could be a getter or field.
Jennifer Messerly
2016/06/15 16:50:32
Oh crazy... it felt like some of the tests seem to
| |
715 isCallable = true; | |
716 } | |
711 } | 717 } |
712 } | 718 } |
713 | 719 |
714 JS.Expression className; | 720 JS.Expression className; |
715 if (classElem.typeParameters.isNotEmpty) { | 721 if (classElem.typeParameters.isNotEmpty) { |
716 // Generic classes will be defined inside a function that closes over the | 722 // Generic classes will be defined inside a function that closes over the |
717 // type parameter. So we can use their local variable name directly. | 723 // type parameter. So we can use their local variable name directly. |
718 className = new JS.Identifier(classElem.name); | 724 className = new JS.Identifier(classElem.name); |
719 } else { | 725 } else { |
720 className = _emitTopLevelName(classElem); | 726 className = _emitTopLevelName(classElem); |
(...skipping 10 matching lines...) Expand all Loading... | |
731 var classExpr = _emitClassExpression(classElem, | 737 var classExpr = _emitClassExpression(classElem, |
732 _emitClassMethods(node, ctors, fields, superclasses, virtualFields), | 738 _emitClassMethods(node, ctors, fields, superclasses, virtualFields), |
733 fields: allFields); | 739 fields: allFields); |
734 | 740 |
735 var body = <JS.Statement>[]; | 741 var body = <JS.Statement>[]; |
736 var extensions = _extensionsToImplement(classElem); | 742 var extensions = _extensionsToImplement(classElem); |
737 _initExtensionSymbols(classElem, methods, fields, body); | 743 _initExtensionSymbols(classElem, methods, fields, body); |
738 _emitSuperHelperSymbols(_superHelperSymbols, body); | 744 _emitSuperHelperSymbols(_superHelperSymbols, body); |
739 | 745 |
740 // Emit the class, e.g. `core.Object = class Object { ... }` | 746 // Emit the class, e.g. `core.Object = class Object { ... }` |
741 _defineClass(classElem, className, classExpr, body); | 747 _defineClass(classElem, className, classExpr, isCallable, body); |
742 | 748 |
743 // Emit things that come after the ES6 `class ... { ... }`. | 749 // Emit things that come after the ES6 `class ... { ... }`. |
744 var jsPeerName = _getJSPeerName(classElem); | 750 var jsPeerName = _getJSPeerName(classElem); |
745 _setBaseClass(classElem, className, jsPeerName, body); | 751 _setBaseClass(classElem, className, jsPeerName, body); |
746 | 752 |
747 _emitClassTypeTests(classElem, className, body); | 753 _emitClassTypeTests(classElem, className, body); |
748 | 754 |
749 _defineNamedConstructors(ctors, body, className); | 755 _defineNamedConstructors(ctors, body, className, isCallable); |
750 _emitVirtualFieldSymbols(virtualFieldSymbols, body); | 756 _emitVirtualFieldSymbols(virtualFieldSymbols, body); |
751 _emitClassSignature(methods, classElem, ctors, extensions, className, body); | 757 _emitClassSignature(methods, classElem, ctors, extensions, className, body); |
752 _defineExtensionMembers(extensions, className, body); | 758 _defineExtensionMembers(extensions, className, body); |
753 _emitClassMetadata(node.metadata, className, body); | 759 _emitClassMetadata(node.metadata, className, body); |
754 | 760 |
755 JS.Statement classDef = _statement(body); | 761 JS.Statement classDef = _statement(body); |
756 var typeFormals = classElem.typeParameters; | 762 var typeFormals = classElem.typeParameters; |
757 if (typeFormals.isNotEmpty) { | 763 if (typeFormals.isNotEmpty) { |
758 classDef = _defineClassTypeArguments(classElem, typeFormals, classDef); | 764 classDef = _defineClassTypeArguments(classElem, typeFormals, classDef); |
759 } | 765 } |
760 | 766 |
761 body = <JS.Statement>[classDef]; | 767 body = <JS.Statement>[classDef]; |
762 _emitStaticFields(staticFields, staticFieldOverrides, classElem, body); | 768 _emitStaticFields(staticFields, staticFieldOverrides, classElem, body); |
763 _registerExtensionType(classElem, jsPeerName, body); | 769 _registerExtensionType(classElem, jsPeerName, body); |
764 return _statement(body); | 770 return _statement(body); |
765 } | 771 } |
766 | 772 |
773 /// Emits code to support a class with a "call" method and an unnamed | |
774 /// constructor. | |
775 /// | |
776 /// This ensures instances created by the unnamed constructor are functions. | |
777 /// Named constructors are handled elsewhere, see [_defineNamedConstructors]. | |
778 JS.Expression _emitCallableClass(JS.ClassExpression classExpr) { | |
779 var newClassExpr = js.call(r'''function (...args) { | |
780 let call = this.call.bind(this); | |
781 call.__proto__ = this.__proto__; | |
782 call.new.apply(call, args); | |
783 return call; | |
784 }'''); | |
785 // Name the constructor function the same as the class. | |
786 return js.call('dart.callableClass(#, #)', | |
787 [new JS.NamedFunction(classExpr.name, newClassExpr), classExpr]); | |
788 } | |
789 | |
767 void _emitClassTypeTests(ClassElement classElem, JS.Expression className, | 790 void _emitClassTypeTests(ClassElement classElem, JS.Expression className, |
768 List<JS.Statement> body) { | 791 List<JS.Statement> body) { |
769 if (classElem == objectClass) { | 792 if (classElem == objectClass) { |
770 // We rely on ES6 static inheritance. All types that are represented by | 793 // We rely on ES6 static inheritance. All types that are represented by |
771 // class constructor functions will see these definitions, with [this] | 794 // class constructor functions will see these definitions, with [this] |
772 // being bound to the class constructor. | 795 // being bound to the class constructor. |
773 | 796 |
774 // The 'instanceof' checks don't work for primitive types (which have fast | 797 // The 'instanceof' checks don't work for primitive types (which have fast |
775 // definitions below) and don't work for native types. In those cases we | 798 // definitions below) and don't work for native types. In those cases we |
776 // fall through to the general purpose checking code. | 799 // fall through to the general purpose checking code. |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
972 virtualFieldSymbols.add(js.statement( | 995 virtualFieldSymbols.add(js.statement( |
973 'const # = Symbol(#.name + "." + #.toString());', | 996 'const # = Symbol(#.name + "." + #.toString());', |
974 [virtualField, className, fieldName])); | 997 [virtualField, className, fieldName])); |
975 } | 998 } |
976 } | 999 } |
977 } | 1000 } |
978 } | 1001 } |
979 } | 1002 } |
980 | 1003 |
981 void _defineClass(ClassElement classElem, JS.Expression className, | 1004 void _defineClass(ClassElement classElem, JS.Expression className, |
982 JS.ClassExpression classExpr, List<JS.Statement> body) { | 1005 JS.ClassExpression classExpr, bool isCallable, List<JS.Statement> body) { |
1006 bool isCallableUnnamed = isCallable && classElem.unnamedConstructor != null; | |
1007 | |
983 if (classElem.typeParameters.isNotEmpty) { | 1008 if (classElem.typeParameters.isNotEmpty) { |
984 body.add(new JS.ClassDeclaration(classExpr)); | 1009 if (isCallableUnnamed) { |
1010 body.add(js.statement( | |
1011 'const # = #;', [classExpr.name, _emitCallableClass(classExpr)])); | |
1012 } else { | |
1013 body.add(new JS.ClassDeclaration(classExpr)); | |
1014 } | |
985 } else { | 1015 } else { |
986 body.add(js.statement('# = #;', [className, classExpr])); | 1016 var expr = isCallableUnnamed ? _emitCallableClass(classExpr) : classExpr; |
1017 body.add(js.statement('# = #;', [className, expr])); | |
987 } | 1018 } |
988 } | 1019 } |
989 | 1020 |
990 void _emitVirtualFieldSymbols( | 1021 void _emitVirtualFieldSymbols( |
991 List<JS.Statement> virtualFields, List<JS.Statement> body) { | 1022 List<JS.Statement> virtualFields, List<JS.Statement> body) { |
992 body.addAll(virtualFields); | 1023 body.addAll(virtualFields); |
993 } | 1024 } |
994 | 1025 |
995 List<JS.Identifier> _emitTypeFormals(List<TypeParameterElement> typeFormals) { | 1026 List<JS.Identifier> _emitTypeFormals(List<TypeParameterElement> typeFormals) { |
996 return typeFormals | 1027 return typeFormals |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1394 'dart.setExtensionBaseClass(#, #);', [className, newBaseClass])); | 1425 'dart.setExtensionBaseClass(#, #);', [className, newBaseClass])); |
1395 } else if (_hasDeferredSupertype.contains(classElem)) { | 1426 } else if (_hasDeferredSupertype.contains(classElem)) { |
1396 var newBaseClass = _emitType(classElem.type.superclass, | 1427 var newBaseClass = _emitType(classElem.type.superclass, |
1397 nameType: false, subClass: classElem, className: className); | 1428 nameType: false, subClass: classElem, className: className); |
1398 body.add( | 1429 body.add( |
1399 js.statement('dart.setBaseClass(#, #);', [className, newBaseClass])); | 1430 js.statement('dart.setBaseClass(#, #);', [className, newBaseClass])); |
1400 } | 1431 } |
1401 } | 1432 } |
1402 | 1433 |
1403 void _defineNamedConstructors(List<ConstructorDeclaration> ctors, | 1434 void _defineNamedConstructors(List<ConstructorDeclaration> ctors, |
1404 List<JS.Statement> body, JS.Expression className) { | 1435 List<JS.Statement> body, JS.Expression className, bool isCallable) { |
1436 var runtimeHelper = isCallable | |
1437 ? 'defineNamedConstructorCallable' | |
1438 : 'defineNamedConstructor'; | |
1439 | |
1405 for (ConstructorDeclaration member in ctors) { | 1440 for (ConstructorDeclaration member in ctors) { |
1406 if (member.name != null && member.factoryKeyword == null) { | 1441 if (member.name != null && member.factoryKeyword == null) { |
1407 body.add(js.statement('dart.defineNamedConstructor(#, #);', | 1442 body.add(js.statement('dart.#(#, #);', |
1408 [className, _constructorName(member.element)])); | 1443 [runtimeHelper, className, _constructorName(member.element)])); |
1409 } | 1444 } |
1410 } | 1445 } |
1411 } | 1446 } |
1412 | 1447 |
1413 /// Emits static fields for a class, and initialize them eagerly if possible, | 1448 /// Emits static fields for a class, and initialize them eagerly if possible, |
1414 /// otherwise define them as lazy properties. | 1449 /// otherwise define them as lazy properties. |
1415 void _emitStaticFields( | 1450 void _emitStaticFields( |
1416 List<FieldDeclaration> staticFields, | 1451 List<FieldDeclaration> staticFields, |
1417 Set<FieldElement> staticFieldOverrides, | 1452 Set<FieldElement> staticFieldOverrides, |
1418 ClassElement classElem, | 1453 ClassElement classElem, |
(...skipping 3714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5133 } | 5168 } |
5134 | 5169 |
5135 bool isLibraryPrefix(Expression node) => | 5170 bool isLibraryPrefix(Expression node) => |
5136 node is SimpleIdentifier && node.staticElement is PrefixElement; | 5171 node is SimpleIdentifier && node.staticElement is PrefixElement; |
5137 | 5172 |
5138 LibraryElement _getLibrary(AnalysisContext c, String uri) => | 5173 LibraryElement _getLibrary(AnalysisContext c, String uri) => |
5139 c.computeLibraryElement(c.sourceFactory.forUri(uri)); | 5174 c.computeLibraryElement(c.sourceFactory.forUri(uri)); |
5140 | 5175 |
5141 bool _isDartRuntime(LibraryElement l) => | 5176 bool _isDartRuntime(LibraryElement l) => |
5142 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; | 5177 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; |
OLD | NEW |