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

Side by Side Diff: lib/src/codegen/module_builder.dart

Issue 1612083002: Initial --modules=es6 support (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: addressed comments (legacy, doc) + test enum utils Created 4 years, 11 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 unified diff | Download patch
« no previous file with comments | « lib/src/codegen/js_codegen.dart ('k') | lib/src/js/nodes.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library dev_compiler.src.codegen.module_builder;
6
7 import '../js/js_ast.dart' as JS;
8 import '../js/js_ast.dart' show js;
9 import '../options.dart' show ModuleFormat;
10
11 /// Helper that builds JS modules in a given [ModuleFormat].
12 abstract class ModuleBuilder {
13 final String _jsPath;
14 final String _jsModuleValue;
15 final JS.Identifier _exportsVar;
16 final List<String> _exports = <String>[];
17 final List<_ModuleImport> _imports = <_ModuleImport>[];
18
19 ModuleBuilder._(this._jsPath, this._jsModuleValue, this._exportsVar);
20
21 /// Returns a [format]-specific [ModuleBuilder].
22 /// - [jsPath] is the path of the module being built.
23 /// - [jsModuleValue] is the default value to use for the library, in case of
24 /// js interop (comes from the @js.JS(jsModuleValue) annotation on the
25 /// library directive). It is null in any other case.
26 /// - [exportsVar] is the name of the object on which items are exported. Lazy
27 /// variables and constants are assumed to be declared on this instance.
28 factory ModuleBuilder(String jsPath, String jsModuleValue,
29 JS.Identifier exportsVar, ModuleFormat format) {
30 switch (format) {
31 case ModuleFormat.legacy:
32 return new LegacyModuleBuilder(jsPath, jsModuleValue, exportsVar);
33 case ModuleFormat.es6:
34 return new ES6ModuleBuilder(jsPath, jsModuleValue, exportsVar);
35 }
36 }
37
38 /// Adds [name] to the list of names to be exported from the module.
39 void addExport(String name) {
40 _exports.add(name);
41 }
42
43 /// Adds an import from a module named [name] and locally aliased as [libVar].
44 /// When [isLazy] is true, the import should be lazy (i.e. there is some
45 /// cyclic dependency of imports).
46 void addImport(String name, JS.Identifier libVar, {bool isLazy: false}) {
47 _imports.add(new _ModuleImport(name, libVar, isLazy));
48 }
49
50 /// Builds a program out of menu items.
51 JS.Program build(List<JS.ModuleItem> moduleItems);
52 }
53
54 class _ModuleImport {
55 final String name;
56 final JS.Identifier libVar;
57 // TODO(jmesserly): Assess whether we can remove this (we shouldn't need it
58 // even in our legacy module format, but it might still be useful for Closure
59 // with ES6 modules).
60 final bool isLazy;
61 _ModuleImport(this.name, this.libVar, this.isLazy);
62 }
63
64 /// Generates modules for with DDC's `dart_library.js` loading mechanism.
65 class LegacyModuleBuilder extends ModuleBuilder {
66 LegacyModuleBuilder(jsPath, _jsModuleValue, _exportsVar)
67 : super._(jsPath, _jsModuleValue, _exportsVar);
68
69 JS.Program build(List<JS.ModuleItem> moduleItems) {
70 // TODO(jmesserly): it would be great to run the renamer on the body,
71 // then figure out if we really need each of these parameters.
72 // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34
73 var params = [_exportsVar];
74 var lazyParams = [];
75
76 var imports = <JS.Expression>[];
77 var lazyImports = <JS.Expression>[];
78 var moduleStatements = <JS.Statement>[];
79
80 for (var i in _imports) {
81 (i.isLazy ? lazyImports : imports).add(js.string(i.name, "'"));
82 (i.isLazy ? lazyParams : params).add(i.libVar);
83 }
84 params.addAll(lazyParams);
85
86 moduleStatements.addAll(_flattenBlocks(moduleItems));
87
88 if (_exports.isNotEmpty) {
89 moduleStatements.add(js.comment('Exports:'));
90 // TODO(jmesserly): make these immutable in JS?
91 for (var name in _exports) {
92 moduleStatements
93 .add(js.statement('#.# = #;', [_exportsVar, name, name]));
94 }
95 }
96
97 var module =
98 js.call("function(#) { 'use strict'; #; }", [params, moduleStatements]);
99
100 var moduleDef = js.statement("dart_library.library(#, #, #, #, #)", [
101 js.string(_jsPath, "'"),
102 _jsModuleValue ?? new JS.LiteralNull(),
103 js.commentExpression(
104 "Imports", new JS.ArrayInitializer(imports, multiline: true)),
105 js.commentExpression("Lazy imports",
106 new JS.ArrayInitializer(lazyImports, multiline: true)),
107 module
108 ]);
109 return new JS.Program(<JS.ModuleItem>[moduleDef]);
110 }
111 }
112
113 /// Generates ES6 modules.
114 // TODO(ochafik): Break strong dep cycles to accommodate the Closure Compiler.
115 class ES6ModuleBuilder extends ModuleBuilder {
116 ES6ModuleBuilder(jsPath, _jsModuleValue, _exportsVar)
117 : super._(jsPath, _jsModuleValue, _exportsVar);
118
119 JS.Program build(List<JS.ModuleItem> moduleItems) {
120 var moduleStatements = <JS.ModuleItem>[
121 // Lazy declarations may reference exports.
122 js.statement("const # = {};", [_exportsVar])
123 ];
124
125 // TODO(jmesserly): it would be great to run the renamer on the body,
126 // then figure out if we really need each of these parameters.
127 // See ES6 modules: https://github.com/dart-lang/dev_compiler/issues/34
128 for (var i in _imports) {
129 // TODO(ochafik): laziness, late binding, etc, to support Closure...
130 moduleStatements.add(new JS.ImportDeclaration(
131 defaultBinding: i.libVar, from: js.string(i.name)));
132 }
133
134 moduleStatements.addAll(_flattenBlocks(moduleItems));
135
136 if (_exports.isNotEmpty) {
137 moduleStatements.add(js.comment('Exports:'));
138 // TODO(jmesserly): make these immutable in JS?
139 for (var name in _exports) {
140 moduleStatements
141 .add(js.statement('#.# = #;', [_exportsVar, name, name]));
142 }
143 moduleStatements
144 .add(new JS.ExportDeclaration(_exportsVar, isDefault: true));
145 }
146 // TODO(ochafik): What to do of _jsModuleValue?
147 return new JS.Program(moduleStatements);
148 }
149 }
150
151 /// Flattens blocks in [stats] to a single list of module items.
152 /// Note that in general, blocks should not be flattened, because it can
153 /// mess up with block-level scoping (let, const).
154 // TODO(ochafik): Remove this / find better pattern (adding statements as they
155 // are generated from [JSCodegenVisitor], instead of composing them with
156 // [_statements]).
157 Iterable<JS.ModuleItem> _flattenBlocks(List<JS.ModuleItem> stats) =>
158 stats.expand((item) => item is JS.Block ? item.statements : [item]);
OLDNEW
« no previous file with comments | « lib/src/codegen/js_codegen.dart ('k') | lib/src/js/nodes.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698