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

Unified Diff: pkg/compiler/lib/src/js/js.dart

Issue 1212613009: dart2js: Implement frequency based naming. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Reserve reserved names :) Created 5 years, 6 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
« no previous file with comments | « pkg/compiler/lib/src/dart2js.dart ('k') | pkg/compiler/lib/src/js_backend/backend.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/js/js.dart
diff --git a/pkg/compiler/lib/src/js/js.dart b/pkg/compiler/lib/src/js/js.dart
index 2d208a6940a14714429d5c817bc0c1c5c5d0731e..e65fa7aa9e6a9a89a4eb6e468c073a634eb149bf 100644
--- a/pkg/compiler/lib/src/js/js.dart
+++ b/pkg/compiler/lib/src/js/js.dart
@@ -16,11 +16,14 @@ import '../dump_info.dart' show DumpInfoTask;
CodeBuffer prettyPrint(Node node, leg.Compiler compiler,
{DumpInfoTask monitor,
- bool allowVariableMinification: true}) {
+ bool allowVariableMinification: true,
+ Renamer renamerForNames:
+ JavaScriptPrintingOptions.identityRenamer}) {
JavaScriptPrintingOptions options = new JavaScriptPrintingOptions(
shouldCompressOutput: compiler.enableMinification,
minifyLocalVariables: allowVariableMinification,
- preferSemicolonToNewlineInMinifiedOutput: USE_NEW_EMITTER);
+ preferSemicolonToNewlineInMinifiedOutput: USE_NEW_EMITTER,
+ renamerForNames: renamerForNames);
Dart2JSJavaScriptPrintingContext context =
new Dart2JSJavaScriptPrintingContext(compiler, monitor);
Printer printer = new Printer(options, context);
@@ -85,3 +88,83 @@ class Dart2JSJavaScriptPrintingContext implements JavaScriptPrintingContext {
}
}
}
+
+/// Interface for ast nodes that encapsulate an ast that needs to be
+/// traversed when counting tokens.
+abstract class AstContainer implements Node {
+ Iterable<Node> get containedNodes;
+}
+
+/// Interface for tasks in the compiler that need to finalize tokens after
+/// counting them.
+abstract class TokenFinalizer {
+ void finalizeTokens();
+}
+
+/// Implements reference counting for instances of [ReferenceCountedAstNode]
+class TokenCounter extends BaseVisitor {
+ @override
+ visitNode(Node node) {
+ if (node is AstContainer) {
+ for (Node element in node.containedNodes) {
+ element.accept(this);
+ }
+ } else if (node is ReferenceCountedAstNode) {
+ node.markSeen(this);
+ } else {
+ super.visitNode(node);
+ }
+ }
+
+ void countTokens(Node node) => node.accept(this);
+}
+
+abstract class ReferenceCountedAstNode implements Node {
+ markSeen(TokenCounter visitor);
+}
+
+/// Represents the LiteralString resulting from unparsing [expression]. The
+/// actual unparsing is done on demand when requesting the [value] of this
+/// node.
+///
+/// This is used when generated code needs to be represented as a string,
+/// for example by the lazy emitter or when generating code generators.
+class UnparsedNode extends DeferredString
+ implements AstContainer {
+ @override
+ final Node tree;
+ final leg.Compiler _compiler;
+ final bool _protectForEval;
+ LiteralString _cachedLiteral;
+
+ Iterable<Node> get containedNodes => [tree];
+
+ /// A [js.Literal] that represents the string result of unparsing [ast].
+ ///
+ /// When its string [value] is requested, the node pretty-prints the given
+ /// [ast] and, if [protectForEval] is true, wraps the resulting
+ /// string in parenthesis. The result is also escaped.
+ UnparsedNode(this.tree, this._compiler, this._protectForEval);
+
+ LiteralString get _literal {
+ if (_cachedLiteral == null) {
+ String text = prettyPrint(tree, _compiler).getText();
+ if (_protectForEval) {
+ if (tree is Fun) text = '($text)';
+ if (tree is LiteralExpression) {
+ LiteralExpression literalExpression = tree;
+ String template = literalExpression.template;
+ if (template.startsWith("function ") ||
+ template.startsWith("{")) {
+ text = '($text)';
+ }
+ }
+ }
+ _cachedLiteral = js.escapedString(text);
+ }
+ return _cachedLiteral;
+ }
+
+ @override
+ String get value => _literal.value;
+}
« no previous file with comments | « pkg/compiler/lib/src/dart2js.dart ('k') | pkg/compiler/lib/src/js_backend/backend.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698