Chromium Code Reviews| Index: lib/src/codegen/js_codegen.dart |
| diff --git a/lib/src/codegen/js_codegen.dart b/lib/src/codegen/js_codegen.dart |
| index 6f135f0d21cad9d566835518470bb9e35b167124..a9d2f5aae71d40a584c65bfa19db4ca94ea7a835 100644 |
| --- a/lib/src/codegen/js_codegen.dart |
| +++ b/lib/src/codegen/js_codegen.dart |
| @@ -34,6 +34,11 @@ import 'code_generator.dart'; |
| // This must match the optional parameter name used in runtime.js |
| const String _jsNamedParameterName = r'opt$'; |
| +bool _isAnnotationType(Annotation m, String name) => m.name.name == name; |
| + |
| +bool _hasAnnotationType(AnnotatedNode node, String name) => |
| + node.metadata.any((annotation) => _isAnnotationType(annotation, name)); |
| + |
| class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor { |
| final LibraryInfo libraryInfo; |
| final TypeRules rules; |
| @@ -45,6 +50,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor { |
| ClassDeclaration currentClass; |
| ConstantEvaluator _constEvaluator; |
| + bool _containsJsGlobals = false; |
|
Jennifer Messerly
2015/03/17 16:39:18
should this be explicit on the library tag? e.g. l
Jacob
2015/03/17 21:22:44
Discussed offline. Switched to @JsName here and fo
|
| final _exports = <String>[]; |
| final _lazyFields = <VariableDeclaration>[]; |
| @@ -80,9 +86,11 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor { |
| } |
| var name = jsLibraryName(libraryInfo.library); |
| + var defaultValue = _containsJsGlobals ? 'window' : '{}'; |
|
Jennifer Messerly
2015/03/17 16:39:18
normally we'd do this as an interpolated expressio
Jacob
2015/03/17 21:22:44
Done.
|
| return new JS.Program([ |
| js.statement('var #;', name), |
| - js.statement("(function($_EXPORTS) { 'use strict'; #; })(# || (# = {}));", |
| + js.statement( |
| + "(function($_EXPORTS) { 'use strict'; #; })(# || (# = $defaultValue));", |
|
Jennifer Messerly
2015/03/17 16:39:18
this should be:
"(function($_EXPORTS) { 'use stric
Jacob
2015/03/17 21:22:44
Done.
|
| [body, name, name]) |
|
Jennifer Messerly
2015/03/17 16:39:18
[body, name, name, defaultValue]
Jacob
2015/03/17 21:22:44
Done.
|
| ]); |
| } |
| @@ -231,6 +239,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor { |
| @override |
| JS.Statement visitClassDeclaration(ClassDeclaration node) { |
| + if (_hasAnnotationType(node, 'JsType')) return null; |
| + |
| currentClass = node; |
| var body = <JS.Statement>[]; |
| @@ -914,6 +924,10 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor { |
| @override |
| visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { |
| + if (_hasAnnotationType(node, 'JsGlobal')) { |
| + _containsJsGlobals = true; |
|
Jennifer Messerly
2015/03/17 16:39:18
per suggestion above, this won't be needed
|
| + return null; |
| + } |
| var body = <JS.Statement>[]; |
| for (var field in node.variables.variables) { |
| @@ -960,8 +974,9 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor { |
| new JS.VariableDeclaration(last.name.name), |
| _visit(lastInitializer.target))); |
| - var result = |
| - <JS.Expression>[new JS.VariableDeclarationList('let', variables)]; |
| + var result = <JS.Expression>[ |
|
Siggi Cherem (dart-lang)
2015/03/17 01:25:36
DBC - you might need to run pub-ugprade, seems lik
|
| + new JS.VariableDeclarationList('let', variables) |
| + ]; |
| result.addAll(_visitList(lastInitializer.cascadeSections)); |
| _cascadeTarget = savedCascadeTemp; |
| return _statement(result.map((e) => new JS.ExpressionStatement(e))); |
| @@ -1016,8 +1031,9 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor { |
| void _flushLibraryProperties(List<JS.Statement> body) { |
| if (_properties.isEmpty) return; |
| - body.add(js.statement('dart.copyProperties($_EXPORTS, { # });', |
| - [_properties.map(_emitTopLevelProperty)])); |
| + body.add(js.statement('dart.copyProperties($_EXPORTS, { # });', [ |
| + _properties.map(_emitTopLevelProperty) |
| + ])); |
| _properties.clear(); |
| } |
| @@ -1609,8 +1625,9 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor { |
| @override |
| visitListLiteral(ListLiteral node) { |
| // TODO(jmesserly): make this faster. We're wasting an array. |
| - var list = js.call('new List.from(#)', |
| - [new JS.ArrayInitializer(_visitList(node.elements))]); |
| + var list = js.call('new List.from(#)', [ |
| + new JS.ArrayInitializer(_visitList(node.elements)) |
| + ]); |
| if (node.constKeyword != null) { |
| list = js.commentExpression('Unimplemented const', list); |
| } |
| @@ -1936,9 +1953,8 @@ class JSGenerator extends CodeGenerator { |
| if (options.emitSourceMaps) { |
| var outFilename = path.basename(outputPath); |
| var printer = new srcmaps.Printer(outFilename); |
| - _writeNode( |
| - new SourceMapPrintingContext(printer, path.dirname(outputPath)), |
| - jsTree); |
| + _writeNode(new SourceMapPrintingContext( |
| + printer, path.dirname(outputPath)), jsTree); |
| printer.add('//# sourceMappingURL=$outFilename.map'); |
| // Write output file and source map |
| var text = printer.text; |