Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(647)

Unified Diff: lib/src/codegen/js_codegen.dart

Issue 1122133003: fixes #157, renaming local library identifiers if needed. (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: fix core.Symbol reference Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/src/codegen/js_codegen.dart
diff --git a/lib/src/codegen/js_codegen.dart b/lib/src/codegen/js_codegen.dart
index eecddeeff14005752e004750bbdda948a0ebb9da..5d25c06620f83a7455c232fd2a5f8f21c9a1aa8a 100644
--- a/lib/src/codegen/js_codegen.dart
+++ b/lib/src/codegen/js_codegen.dart
@@ -68,6 +68,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
ConstantEvaluator _constEvaluator;
+ /// Imported libraries, and the temporaries used to refer to them.
+ final _imports = new Map<LibraryElement, JS.TemporaryId>();
final _exports = new Set<String>();
final _lazyFields = <VariableDeclaration>[];
final _properties = <FunctionDeclaration>[];
@@ -77,8 +79,6 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
final _temps = new HashMap<Element, JS.TemporaryId>();
/// The name for the library's exports inside itself.
- /// This much be a constant because we interpolate it into template strings,
- /// and otherwise it would break caching for them.
/// `exports` was chosen as the most similar to ES module patterns.
final _exportsVar = new JS.TemporaryId('exports');
final _namedArgTemp = new JS.TemporaryId('opts');
@@ -141,17 +141,34 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
body.add(js.statement('#.# = #;', [_exportsVar, name, name]));
}
- var name = jsLibraryName(libraryInfo.library);
-
+ var name = jsLibraryName(currentLibrary);
var defaultValue = js.call(jsDefaultValue);
+ var imports = _imports.values.map((i) => i.name);
+ var libraries = [name]..addAll(imports);
+
+ var globals = new JS.VariableDeclarationList('var', libraries
+ .map((l) => new JS.VariableInitialization(new JS.Identifier(l), null))
+ .toList(growable: false));
+
+ // TODO(jmesserly): it would be great to run the renamer on the body,
+ // then figure out if we really need each of these parameters.
+ // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34
+ var importInit = [js.call('# || (# = #)', [name, name, defaultValue])];
+ _imports.forEach((library, temp) {
+ var name = new JS.Identifier(temp.name);
+ if (_libraryMightNotBeLoaded(library)) {
+ importInit.add(js.call('# || (# = {})', [name, name]));
+ } else {
+ importInit.add(name);
+ }
+ });
+
return new JS.Program([
- js.statement('var #;', name),
- js.statement("(function(#) { 'use strict'; #; })(# || (# = #));", [
- _exportsVar,
+ globals.toStatement(),
+ js.statement("(function(#) { 'use strict'; #; })(#);", [
+ [_exportsVar]..addAll(_imports.values),
body,
- name,
- name,
- defaultValue
+ importInit
])
]);
}
@@ -477,7 +494,10 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
bool _typeMightNotBeLoaded(DartType type) {
var library = type.element.library;
if (library == currentLibrary) return _lazyClass(type);
+ return _libraryMightNotBeLoaded(library);
+ }
+ bool _libraryMightNotBeLoaded(LibraryElement library) {
// The SDK is a special case: we optimize the order to prevent laziness.
if (library.isInSdk) {
// SDK is loaded before non-SDK libraies
@@ -1101,7 +1121,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
// library member
if (element.enclosingElement is CompilationUnitElement &&
- (element.library != libraryInfo.library ||
+ (element.library != currentLibrary ||
element is TopLevelVariableElement && !element.isConst)) {
var memberName = _emitMemberName(name, isStatic: true);
return js.call('#.#', [_libraryName(element.library), memberName]);
@@ -2243,7 +2263,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
// correctly.
var name = js.string(node.components.join('.'), "'");
var nameHint = 'symbol_' + node.components.join('_');
- return _const(node, js.call('new core.Symbol(#)', name), nameHint);
+ return _const(
+ node, new JS.New(_emitTypeName(types.symbolType), [name]), nameHint);
}
@override
@@ -2492,8 +2513,9 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
/// This never uses the library's name (the identifier in the `library`
/// declaration) as it doesn't have any meaningful rules enforced.
JS.Identifier _libraryName(LibraryElement library) {
- if (library == libraryInfo.library) return _exportsVar;
- return new JS.Identifier(jsLibraryName(library));
+ if (library == currentLibrary) return _exportsVar;
+ return _imports.putIfAbsent(
+ library, () => new JS.TemporaryId(jsLibraryName(library)));
}
DartType getStaticType(Expression e) => rules.getStaticType(e);

Powered by Google App Engine
This is Rietveld 408576698