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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 final _lazyFields = <VariableDeclaration>[]; | 77 final _lazyFields = <VariableDeclaration>[]; |
78 final _properties = <FunctionDeclaration>[]; | 78 final _properties = <FunctionDeclaration>[]; |
79 final _privateNames = new HashMap<String, JS.TemporaryId>(); | 79 final _privateNames = new HashMap<String, JS.TemporaryId>(); |
80 final _moduleItems = <JS.Statement>[]; | 80 final _moduleItems = <JS.Statement>[]; |
81 final _temps = new HashMap<Element, JS.TemporaryId>(); | 81 final _temps = new HashMap<Element, JS.TemporaryId>(); |
82 final _qualifiedIds = new HashMap<Element, JS.MaybeQualifiedId>(); | 82 final _qualifiedIds = new HashMap<Element, JS.MaybeQualifiedId>(); |
83 final _qualifiedGenericIds = new HashMap<Element, JS.MaybeQualifiedId>(); | 83 final _qualifiedGenericIds = new HashMap<Element, JS.MaybeQualifiedId>(); |
84 | 84 |
85 /// The name for the library's exports inside itself. | 85 /// The name for the library's exports inside itself. |
86 /// `exports` was chosen as the most similar to ES module patterns. | 86 /// `exports` was chosen as the most similar to ES module patterns. |
| 87 final _dartxVar = new JS.Identifier('dartx'); |
87 final _exportsVar = new JS.TemporaryId('exports'); | 88 final _exportsVar = new JS.TemporaryId('exports'); |
| 89 final _runtimeLibVar = new JS.Identifier('dart'); |
88 final _namedArgTemp = new JS.TemporaryId('opts'); | 90 final _namedArgTemp = new JS.TemporaryId('opts'); |
89 | 91 |
90 ConstFieldVisitor _constField; | 92 ConstFieldVisitor _constField; |
91 | 93 |
92 ModuleItemLoadOrder _loader; | 94 ModuleItemLoadOrder _loader; |
93 | 95 |
94 /// _interceptors.JSArray<E>, used for List literals. | 96 /// _interceptors.JSArray<E>, used for List literals. |
95 ClassElement _jsArray; | 97 ClassElement _jsArray; |
96 | 98 |
97 Map<String, DartType> _objectMembers; | 99 Map<String, DartType> _objectMembers; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 // TODO(jmesserly): make these immutable in JS? | 176 // TODO(jmesserly): make these immutable in JS? |
175 for (var name in _exports) { | 177 for (var name in _exports) { |
176 _moduleItems.add(js.statement('#.# = #;', [_exportsVar, name, name])); | 178 _moduleItems.add(js.statement('#.# = #;', [_exportsVar, name, name])); |
177 } | 179 } |
178 | 180 |
179 var jsPath = js.string(jsOutputBase(currentLibrary, root), "'"); | 181 var jsPath = js.string(jsOutputBase(currentLibrary, root), "'"); |
180 | 182 |
181 // TODO(jmesserly): it would be great to run the renamer on the body, | 183 // TODO(jmesserly): it would be great to run the renamer on the body, |
182 // then figure out if we really need each of these parameters. | 184 // then figure out if we really need each of these parameters. |
183 // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34 | 185 // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34 |
184 var params = [_exportsVar]; | 186 var params = [_exportsVar, _runtimeLibVar, _dartxVar]; |
185 var processImport = (LibraryElement library, JS.TemporaryId temp, | 187 var processImport = (LibraryElement library, JS.TemporaryId temp, |
186 List list) { | 188 List list) { |
187 params.add(temp); | 189 params.add(temp); |
188 list.add(js.string(jsOutputBase(library, root), "'")); | 190 list.add(js.string(jsOutputBase(library, root), "'")); |
189 }; | 191 }; |
190 | 192 |
191 var imports = []; | 193 var imports = [js.string('dart/dart_runtime'), js.string('dart/dartx')]; |
192 _imports.forEach((library, temp) { | 194 _imports.forEach((library, temp) { |
193 if (_loader.libraryIsLoaded(library)) { | 195 if (_loader.libraryIsLoaded(library)) { |
194 processImport(library, temp, imports); | 196 processImport(library, temp, imports); |
195 } | 197 } |
196 }); | 198 }); |
197 | 199 |
198 var lazyImports = []; | 200 var lazyImports = []; |
199 _imports.forEach((library, temp) { | 201 _imports.forEach((library, temp) { |
200 if (!_loader.libraryIsLoaded(library)) { | 202 if (!_loader.libraryIsLoaded(library)) { |
201 processImport(library, temp, lazyImports); | 203 processImport(library, temp, lazyImports); |
202 } | 204 } |
203 }); | 205 }); |
204 | 206 |
205 var module = | 207 var module = |
206 js.call("function(#) { 'use strict'; #; }", [params, _moduleItems]); | 208 js.call("function(#) { 'use strict'; #; }", [params, _moduleItems]); |
207 | 209 |
208 var program = [ | 210 var program = [ |
209 js.statement("dart.library(#, #, #, #, #)", [ | 211 js.statement("loader.library(#, #, #, #, #)", [ |
210 jsPath, | 212 jsPath, |
211 jsDefaultValue != null ? jsDefaultValue : new JS.LiteralNull(), | 213 jsDefaultValue != null ? jsDefaultValue : new JS.LiteralNull(), |
212 js.commentExpression( | 214 js.commentExpression( |
213 "Imports", new JS.ArrayInitializer(imports, multiline: true)), | 215 "Imports", new JS.ArrayInitializer(imports, multiline: true)), |
214 js.commentExpression("Lazy imports", | 216 js.commentExpression("Lazy imports", |
215 new JS.ArrayInitializer(lazyImports, multiline: true)), | 217 new JS.ArrayInitializer(lazyImports, multiline: true)), |
216 module | 218 module |
217 ]) | 219 ]) |
218 ]; | 220 ]; |
219 | 221 |
(...skipping 2566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2786 | 2788 |
2787 /// A special kind of element created by the compiler, signifying a temporary | 2789 /// A special kind of element created by the compiler, signifying a temporary |
2788 /// variable. These objects use instance equality, and should be shared | 2790 /// variable. These objects use instance equality, and should be shared |
2789 /// everywhere in the tree where they are treated as the same variable. | 2791 /// everywhere in the tree where they are treated as the same variable. |
2790 class TemporaryVariableElement extends LocalVariableElementImpl { | 2792 class TemporaryVariableElement extends LocalVariableElementImpl { |
2791 TemporaryVariableElement.forNode(Identifier name) : super.forNode(name); | 2793 TemporaryVariableElement.forNode(Identifier name) : super.forNode(name); |
2792 | 2794 |
2793 int get hashCode => identityHashCode(this); | 2795 int get hashCode => identityHashCode(this); |
2794 bool operator ==(Object other) => identical(this, other); | 2796 bool operator ==(Object other) => identical(this, other); |
2795 } | 2797 } |
OLD | NEW |