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

Side by Side Diff: lib/compiler/implementation/js/printer.dart

Issue 11086022: Add minifying renamer to the JS printer so locals are called z0, z1, z2, etc. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 class Printer implements NodeVisitor { 5 class Printer implements NodeVisitor {
6 final bool shouldCompressOutput; 6 final bool shouldCompressOutput;
7 leg.Compiler compiler; 7 leg.Compiler compiler;
8 leg.CodeBuffer outBuffer; 8 leg.CodeBuffer outBuffer;
9 int indentLevel = 0; 9 int indentLevel = 0;
10 bool inForInit = false; 10 bool inForInit = false;
11 bool atStatementBegin = false; 11 bool atStatementBegin = false;
12 final DanglingElseVisitor danglingElseVisitor; 12 final DanglingElseVisitor danglingElseVisitor;
13 final Namer namer;
13 14
14 Printer(leg.Compiler compiler) 15 Printer(leg.Compiler compiler)
15 : shouldCompressOutput = compiler.enableMinification, 16 : shouldCompressOutput = compiler.enableMinification,
16 this.compiler = compiler, 17 this.compiler = compiler,
17 outBuffer = new leg.CodeBuffer(), 18 outBuffer = new leg.CodeBuffer(),
18 danglingElseVisitor = new DanglingElseVisitor(compiler); 19 danglingElseVisitor = new DanglingElseVisitor(compiler),
20 namer = DetermineRenamer(compiler.enableMinification);
21
22 static Namer DetermineRenamer(bool shouldCompressOutput) {
kasperl 2012/10/10 08:46:55 This should have been determineRenamer not Determi
23 return shouldCompressOutput ? new MinifyRenamer() : new IdentityNamer();
24 }
19 25
20 void spaceOut() { 26 void spaceOut() {
21 if (!shouldCompressOutput) out(" "); 27 if (!shouldCompressOutput) out(" ");
22 } 28 }
23 void lineOut() { 29 void lineOut() {
24 if (!shouldCompressOutput) out("\n"); 30 if (!shouldCompressOutput) out("\n");
25 } 31 }
26 32
27 String lastAddedString = null; 33 String lastAddedString = null;
28 int get lastCharCode { 34 int get lastCharCode {
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 } 351 }
346 352
347 void functionOut(Fun fun, Node name) { 353 void functionOut(Fun fun, Node name) {
348 out("function"); 354 out("function");
349 if (name != null) { 355 if (name != null) {
350 out(" "); 356 out(" ");
351 // Name must be a [Decl]. Therefore only test for primary expressions. 357 // Name must be a [Decl]. Therefore only test for primary expressions.
352 visitNestedExpression(name, PRIMARY, 358 visitNestedExpression(name, PRIMARY,
353 newInForInit: false, newAtStatementBegin: false); 359 newInForInit: false, newAtStatementBegin: false);
354 } 360 }
361 namer.enterScope();
355 out("("); 362 out("(");
356 if (fun.params != null) { 363 if (fun.params != null) {
357 visitCommaSeparated(fun.params, PRIMARY, 364 visitCommaSeparated(fun.params, PRIMARY,
358 newInForInit: false, newAtStatementBegin: false); 365 newInForInit: false, newAtStatementBegin: false);
359 } 366 }
360 out(")"); 367 out(")");
361 blockBody(fun.body, needsSeparation: false, needsNewline: false); 368 blockBody(fun.body, needsSeparation: false, needsNewline: false);
369 namer.leaveScope();
362 } 370 }
363 371
364 visitFunctionDeclaration(FunctionDeclaration declaration) { 372 visitFunctionDeclaration(FunctionDeclaration declaration) {
365 indent(); 373 indent();
366 functionOut(declaration.function, declaration.name); 374 functionOut(declaration.function, declaration.name);
367 lineOut(); 375 lineOut();
368 } 376 }
369 377
370 visitNestedExpression(Expression node, int requiredPrecedence, 378 visitNestedExpression(Expression node, int requiredPrecedence,
371 [bool newInForInit, bool newAtStatementBegin]) { 379 [bool newInForInit, bool newAtStatementBegin]) {
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 } 597 }
590 598
591 visitPostfix(Postfix postfix) { 599 visitPostfix(Postfix postfix) {
592 visitNestedExpression(postfix.argument, LEFT_HAND_SIDE, 600 visitNestedExpression(postfix.argument, LEFT_HAND_SIDE,
593 newInForInit: inForInit, 601 newInForInit: inForInit,
594 newAtStatementBegin: atStatementBegin); 602 newAtStatementBegin: atStatementBegin);
595 out(postfix.op); 603 out(postfix.op);
596 } 604 }
597 605
598 visitVariableUse(VariableUse ref) { 606 visitVariableUse(VariableUse ref) {
599 out(ref.name); 607 out(namer.getName(ref.name));
600 } 608 }
601 609
602 visitThis(This node) { 610 visitThis(This node) {
603 out("this"); 611 out("this");
604 } 612 }
605 613
606 visitVariableDeclaration(VariableDeclaration decl) { 614 visitVariableDeclaration(VariableDeclaration decl) {
607 out(decl.name); 615 out(namer.declareName(decl.name));
608 } 616 }
609 617
610 visitParameter(Parameter param) { 618 visitParameter(Parameter param) {
611 out(param.name); 619 out(namer.declareName(param.name));
612 } 620 }
613 621
614 bool isDigit(int charCode) { 622 bool isDigit(int charCode) {
615 return charCodes.$0 <= charCode && charCode <= charCodes.$9; 623 return charCodes.$0 <= charCode && charCode <= charCodes.$9;
616 } 624 }
617 625
618 bool isValidJavaScriptId(String field) { 626 bool isValidJavaScriptId(String field) {
619 if (field.length < 3) return false; 627 if (field.length < 3) return false;
620 // Ignore the leading and trailing string-delimiter. 628 // Ignore the leading and trailing string-delimiter.
621 for (int i = 1; i < field.length - 1; i++) { 629 for (int i = 1; i < field.length - 1; i++) {
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
818 bool visitExpression(Expression node) => false; 826 bool visitExpression(Expression node) => false;
819 } 827 }
820 828
821 829
822 leg.CodeBuffer prettyPrint(Node node, 830 leg.CodeBuffer prettyPrint(Node node,
823 leg.Compiler compiler) { 831 leg.Compiler compiler) {
824 Printer printer = new Printer(compiler); 832 Printer printer = new Printer(compiler);
825 printer.visit(node); 833 printer.visit(node);
826 return printer.outBuffer; 834 return printer.outBuffer;
827 } 835 }
836
837
838 abstract class Namer {
839 String getName(String oldName);
840 String declareName(String oldName);
841 void enterScope();
842 void leaveScope();
843 }
844
845
846 class IdentityNamer implements Namer {
847 String getName(String oldName) => oldName;
848 String declareName(String oldName) => oldName;
849 void enterScope() {}
850 void leaveScope() {}
851 }
852
853
854 class MinifyRenamer implements Namer {
855 final List<Map<String, String>> maps;
856 final List<int> nameNumberStack;
857 int nameNumber;
858
859 MinifyRenamer() : maps = [], nameNumberStack = [], nameNumber = 0;
kasperl 2012/10/10 08:46:55 All these initialization expressions can be safely
860
861 void enterScope() {
862 maps.add(new Map<String, String>());
863 nameNumberStack.add(nameNumber);
864 }
865
866 void leaveScope() {
867 maps.removeLast();
868 nameNumber = nameNumberStack.removeLast();
869 }
870
871 String getName(String oldName) {
872 for (int i = maps.length - 1; i >= 0; i--) {
873 var map = maps[i];
874 var replacement = map[oldName];
875 if (replacement != null) return replacement;
876 }
877 return oldName;
878 }
879
880 String declareName(String oldName) {
881 if (maps.length == 0) return oldName;
kasperl 2012/10/10 08:46:55 maps.isEmpty()
882 var newName = "z${nameNumber++}";
883 maps.last()[oldName] = newName;
884 return newName;
885 }
886
887 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698