| 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 dev_compiler.src.codegen.js_codegen; | 5 library dev_compiler.src.codegen.js_codegen; |
| 6 | 6 |
| 7 import 'dart:collection' show HashSet, HashMap, SplayTreeSet; | 7 import 'dart:collection' show HashSet, HashMap, SplayTreeSet; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
| 10 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; | 10 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 final _exportsVar = new JS.TemporaryId('exports'); | 87 final _exportsVar = new JS.TemporaryId('exports'); |
| 88 final _namedArgTemp = new JS.TemporaryId('opts'); | 88 final _namedArgTemp = new JS.TemporaryId('opts'); |
| 89 | 89 |
| 90 ConstFieldVisitor _constField; | 90 ConstFieldVisitor _constField; |
| 91 | 91 |
| 92 ModuleItemLoadOrder _loader; | 92 ModuleItemLoadOrder _loader; |
| 93 | 93 |
| 94 /// _interceptors.JSArray<E>, used for List literals. | 94 /// _interceptors.JSArray<E>, used for List literals. |
| 95 ClassElement _jsArray; | 95 ClassElement _jsArray; |
| 96 | 96 |
| 97 Map<String, DartType> _objectMembers; |
| 98 |
| 97 JSCodegenVisitor(AbstractCompiler compiler, this.libraryInfo, | 99 JSCodegenVisitor(AbstractCompiler compiler, this.libraryInfo, |
| 98 this._extensionTypes, this._fieldsNeedingStorage) | 100 this._extensionTypes, this._fieldsNeedingStorage) |
| 99 : compiler = compiler, | 101 : compiler = compiler, |
| 100 options = compiler.options, | 102 options = compiler.options, |
| 101 rules = compiler.rules, | 103 rules = compiler.rules, |
| 102 root = compiler.entryPointUri { | 104 root = compiler.entryPointUri { |
| 103 _loader = new ModuleItemLoadOrder(_emitModuleItem); | 105 _loader = new ModuleItemLoadOrder(_emitModuleItem); |
| 104 | 106 |
| 105 var context = compiler.context; | 107 var context = compiler.context; |
| 106 var src = context.sourceFactory.forUri('dart:_interceptors'); | 108 var src = context.sourceFactory.forUri('dart:_interceptors'); |
| 107 var interceptors = context.computeLibraryElement(src); | 109 var interceptors = context.computeLibraryElement(src); |
| 108 _jsArray = interceptors.getType('JSArray'); | 110 _jsArray = interceptors.getType('JSArray'); |
| 111 |
| 112 _objectMembers = getObjectMemberMap(types); |
| 109 } | 113 } |
| 110 | 114 |
| 111 LibraryElement get currentLibrary => libraryInfo.library; | 115 LibraryElement get currentLibrary => libraryInfo.library; |
| 112 TypeProvider get types => rules.provider; | 116 TypeProvider get types => rules.provider; |
| 113 | 117 |
| 114 JS.Program emitLibrary(LibraryUnit library) { | 118 JS.Program emitLibrary(LibraryUnit library) { |
| 115 String jsDefaultValue = null; | 119 String jsDefaultValue = null; |
| 116 | 120 |
| 117 // Modify the AST to make coercions explicit. | 121 // Modify the AST to make coercions explicit. |
| 118 new CoercionReifier(library, compiler).reify(); | 122 new CoercionReifier(library, compiler).reify(); |
| (...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 | 575 |
| 572 if (_extensionTypes.contains(classElem)) { | 576 if (_extensionTypes.contains(classElem)) { |
| 573 var dartxNames = []; | 577 var dartxNames = []; |
| 574 for (var m in methods) { | 578 for (var m in methods) { |
| 575 if (!m.isAbstract && !m.isStatic && m.element.isPublic) { | 579 if (!m.isAbstract && !m.isStatic && m.element.isPublic) { |
| 576 dartxNames.add(_elementMemberName(m.element, allowExtensions: false)); | 580 dartxNames.add(_elementMemberName(m.element, allowExtensions: false)); |
| 577 } | 581 } |
| 578 } | 582 } |
| 579 if (dartxNames.isNotEmpty) { | 583 if (dartxNames.isNotEmpty) { |
| 580 body.add(js.statement('dart.defineExtensionNames(#)', | 584 body.add(js.statement('dart.defineExtensionNames(#)', |
| 581 [new JS.ArrayInitializer(dartxNames, multiline: true)])); | 585 [new JS.ArrayInitializer(dartxNames, multiline: true)])); |
| 582 } | 586 } |
| 583 } | 587 } |
| 584 | 588 |
| 585 body.add(new JS.ClassDeclaration(cls)); | 589 body.add(new JS.ClassDeclaration(cls)); |
| 586 | 590 |
| 587 // TODO(jmesserly): we should really just extend native Array. | 591 // TODO(jmesserly): we should really just extend native Array. |
| 588 if (jsPeerName != null && classElem.typeParameters.isNotEmpty) { | 592 if (jsPeerName != null && classElem.typeParameters.isNotEmpty) { |
| 589 body.add(js.statement('dart.setBaseClass(#, dart.global.#);', [ | 593 body.add(js.statement('dart.setBaseClass(#, dart.global.#);', [ |
| 590 classElem.name, | 594 classElem.name, |
| 591 _propertyName(jsPeerName) | 595 _propertyName(jsPeerName) |
| (...skipping 2078 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2670 | 2674 |
| 2671 if (name == '[]') { | 2675 if (name == '[]') { |
| 2672 name = 'get'; | 2676 name = 'get'; |
| 2673 } else if (name == '[]=') { | 2677 } else if (name == '[]=') { |
| 2674 name = 'set'; | 2678 name = 'set'; |
| 2675 } else if (name == '-' && unary) { | 2679 } else if (name == '-' && unary) { |
| 2676 name = 'unary-'; | 2680 name = 'unary-'; |
| 2677 } | 2681 } |
| 2678 | 2682 |
| 2679 // Dart "extension" methods. Used for JS Array, Boolean, Number, String. | 2683 // Dart "extension" methods. Used for JS Array, Boolean, Number, String. |
| 2680 if (allowExtensions && _extensionTypes.contains(type.element)) { | 2684 if (allowExtensions && |
| 2685 _extensionTypes.contains(type.element) && |
| 2686 !_objectMembers.containsKey(name)) { |
| 2681 return js.call('dartx.#', _propertyName(name)); | 2687 return js.call('dartx.#', _propertyName(name)); |
| 2682 } | 2688 } |
| 2683 | 2689 |
| 2684 return _propertyName(name); | 2690 return _propertyName(name); |
| 2685 } | 2691 } |
| 2686 | 2692 |
| 2687 bool _externalOrNative(node) => | 2693 bool _externalOrNative(node) => |
| 2688 node.externalKeyword != null || _functionBody(node) is NativeFunctionBody; | 2694 node.externalKeyword != null || _functionBody(node) is NativeFunctionBody; |
| 2689 | 2695 |
| 2690 FunctionBody _functionBody(node) => | 2696 FunctionBody _functionBody(node) => |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2780 | 2786 |
| 2781 /// A special kind of element created by the compiler, signifying a temporary | 2787 /// A special kind of element created by the compiler, signifying a temporary |
| 2782 /// variable. These objects use instance equality, and should be shared | 2788 /// variable. These objects use instance equality, and should be shared |
| 2783 /// everywhere in the tree where they are treated as the same variable. | 2789 /// everywhere in the tree where they are treated as the same variable. |
| 2784 class TemporaryVariableElement extends LocalVariableElementImpl { | 2790 class TemporaryVariableElement extends LocalVariableElementImpl { |
| 2785 TemporaryVariableElement.forNode(Identifier name) : super.forNode(name); | 2791 TemporaryVariableElement.forNode(Identifier name) : super.forNode(name); |
| 2786 | 2792 |
| 2787 int get hashCode => identityHashCode(this); | 2793 int get hashCode => identityHashCode(this); |
| 2788 bool operator ==(Object other) => identical(this, other); | 2794 bool operator ==(Object other) => identical(this, other); |
| 2789 } | 2795 } |
| OLD | NEW |