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 dev_compiler.src.codegen.js_codegen; | 5 library dev_compiler.src.codegen.js_codegen; |
| 6 | 6 |
| 7 import 'dart:collection' show HashSet, HashMap; | 7 import 'dart:collection' show HashSet, HashMap; |
| 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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 new CoercionReifier(library, compiler).reify(); | 107 new CoercionReifier(library, compiler).reify(); |
| 108 | 108 |
| 109 var unit = library.library; | 109 var unit = library.library; |
| 110 if (unit.directives.isNotEmpty) { | 110 if (unit.directives.isNotEmpty) { |
| 111 var libraryDir = unit.directives.first; | 111 var libraryDir = unit.directives.first; |
| 112 if (libraryDir is LibraryDirective) { | 112 if (libraryDir is LibraryDirective) { |
| 113 var jsName = getAnnotationValue(libraryDir, _isJsNameAnnotation); | 113 var jsName = getAnnotationValue(libraryDir, _isJsNameAnnotation); |
| 114 jsDefaultValue = getConstantField(jsName, 'name', types.stringType); | 114 jsDefaultValue = getConstantField(jsName, 'name', types.stringType); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 if (jsDefaultValue == null) jsDefaultValue = '{}'; | |
| 118 | 117 |
| 119 // TODO(jmesserly): visit scriptTag, directives? | 118 // TODO(jmesserly): visit scriptTag, directives? |
| 120 | 119 |
| 121 _loader.collectElements(currentLibrary, library.partsThenLibrary); | 120 _loader.collectElements(currentLibrary, library.partsThenLibrary); |
| 122 | 121 |
| 123 for (var unit in library.partsThenLibrary) { | 122 for (var unit in library.partsThenLibrary) { |
| 124 _constField = new ConstFieldVisitor(types, unit); | 123 _constField = new ConstFieldVisitor(types, unit); |
| 125 | 124 |
| 126 for (var decl in unit.declarations) { | 125 for (var decl in unit.declarations) { |
| 127 if (decl is TopLevelVariableDeclaration) { | 126 if (decl is TopLevelVariableDeclaration) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 155 _qualifiedIds.forEach(unqualifyIfNeeded); | 154 _qualifiedIds.forEach(unqualifyIfNeeded); |
| 156 _qualifiedGenericIds.forEach(unqualifyIfNeeded); | 155 _qualifiedGenericIds.forEach(unqualifyIfNeeded); |
| 157 | 156 |
| 158 if (_exports.isNotEmpty) _moduleItems.add(js.comment('Exports:')); | 157 if (_exports.isNotEmpty) _moduleItems.add(js.comment('Exports:')); |
| 159 | 158 |
| 160 // TODO(jmesserly): make these immutable in JS? | 159 // TODO(jmesserly): make these immutable in JS? |
| 161 for (var name in _exports) { | 160 for (var name in _exports) { |
| 162 _moduleItems.add(js.statement('#.# = #;', [_exportsVar, name, name])); | 161 _moduleItems.add(js.statement('#.# = #;', [_exportsVar, name, name])); |
| 163 } | 162 } |
| 164 | 163 |
| 165 var name = new JS.Identifier(jsLibraryName(currentLibrary)); | 164 var name = new JS.Identifier(jsLibraryName(currentLibrary)); |
|
Jennifer Messerly
2015/06/05 16:22:27
suggestion, maybe build the Identifier later, and
vsm
2015/06/05 17:01:04
Done.
| |
| 165 var jsName = js.string(name.name, "'"); | |
| 166 | 166 |
| 167 // TODO(jmesserly): it would be great to run the renamer on the body, | 167 // TODO(jmesserly): it would be great to run the renamer on the body, |
| 168 // then figure out if we really need each of these parameters. | 168 // then figure out if we really need each of these parameters. |
| 169 // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34 | 169 // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34 |
| 170 var program = [ | 170 var program = [ |
| 171 js.statement('var # = dart.defineLibrary(#, #);', [ | 171 jsDefaultValue != null |
| 172 name, | 172 ? js.statement( |
| 173 name, | 173 'dart.defineLibrary(#, #);', [jsName, js.call(jsDefaultValue)]) |
| 174 js.call(jsDefaultValue) | 174 : js.statement('dart.defineLibrary(#);', [jsName]) |
| 175 ]) | |
| 176 ]; | 175 ]; |
| 177 | 176 |
| 178 var params = [_exportsVar]; | 177 var params = [_exportsVar]; |
| 179 var args = [name]; | 178 var args = [name]; |
| 180 _imports.forEach((library, temp) { | 179 _imports.forEach((library, temp) { |
| 181 var name = new JS.Identifier(temp.name); | 180 var name = new JS.Identifier(temp.name); |
| 182 params.add(temp); | 181 params.add(temp); |
| 183 args.add(name); | 182 args.add(name); |
| 184 var helper = _loader.libraryIsLoaded(library) ? 'import' : 'lazyImport'; | 183 var helper = _loader.libraryIsLoaded(library) ? 'import' : 'lazyImport'; |
| 185 program.add(js.statement('var # = dart.#(#);', [name, helper, name])); | 184 program |
| 185 .add(js.statement('dart.#(#);', [helper, js.string(name.name, "'")])); | |
| 186 }); | 186 }); |
| 187 | 187 |
| 188 program.add(js.statement("(function(#) { 'use strict'; #; })(#);", [ | 188 program.add(js.statement("(function(#) { 'use strict'; #; })(#);", [ |
| 189 params, | 189 params, |
| 190 _moduleItems, | 190 _moduleItems, |
| 191 args | 191 args |
| 192 ])); | 192 ])); |
| 193 | 193 |
| 194 return new JS.Program(program); | 194 return new JS.Program(program); |
| 195 } | 195 } |
| (...skipping 2454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2650 | 2650 |
| 2651 /// A special kind of element created by the compiler, signifying a temporary | 2651 /// A special kind of element created by the compiler, signifying a temporary |
| 2652 /// variable. These objects use instance equality, and should be shared | 2652 /// variable. These objects use instance equality, and should be shared |
| 2653 /// everywhere in the tree where they are treated as the same variable. | 2653 /// everywhere in the tree where they are treated as the same variable. |
| 2654 class TemporaryVariableElement extends LocalVariableElementImpl { | 2654 class TemporaryVariableElement extends LocalVariableElementImpl { |
| 2655 TemporaryVariableElement.forNode(Identifier name) : super.forNode(name); | 2655 TemporaryVariableElement.forNode(Identifier name) : super.forNode(name); |
| 2656 | 2656 |
| 2657 int get hashCode => identityHashCode(this); | 2657 int get hashCode => identityHashCode(this); |
| 2658 bool operator ==(Object other) => identical(this, other); | 2658 bool operator ==(Object other) => identical(this, other); |
| 2659 } | 2659 } |
| OLD | NEW |