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

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 Renamer _renamer;
floitsch 2012/10/09 13:45:43 no need to make it private.
erikcorry 2012/10/09 20:15:51 Done.
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 _renamer = shouldCompressOutput ? new MinifyRenamer() : new DummyRenamer();
21 }
19 22
20 void spaceOut() { 23 void spaceOut() {
21 if (!shouldCompressOutput) out(" "); 24 if (!shouldCompressOutput) out(" ");
22 } 25 }
23 void lineOut() { 26 void lineOut() {
24 if (!shouldCompressOutput) out("\n"); 27 if (!shouldCompressOutput) out("\n");
25 } 28 }
26 29
27 String lastAddedString = null; 30 String lastAddedString = null;
28 int get lastCharCode { 31 int get lastCharCode {
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 } 348 }
346 349
347 void functionOut(Fun fun, Node name) { 350 void functionOut(Fun fun, Node name) {
348 out("function"); 351 out("function");
349 if (name != null) { 352 if (name != null) {
350 out(" "); 353 out(" ");
351 // Name must be a [Decl]. Therefore only test for primary expressions. 354 // Name must be a [Decl]. Therefore only test for primary expressions.
352 visitNestedExpression(name, PRIMARY, 355 visitNestedExpression(name, PRIMARY,
353 newInForInit: false, newAtStatementBegin: false); 356 newInForInit: false, newAtStatementBegin: false);
354 } 357 }
358 _renamer.enterScope();
355 out("("); 359 out("(");
356 if (fun.params != null) { 360 if (fun.params != null) {
357 visitCommaSeparated(fun.params, PRIMARY, 361 visitCommaSeparated(fun.params, PRIMARY,
358 newInForInit: false, newAtStatementBegin: false); 362 newInForInit: false, newAtStatementBegin: false);
359 } 363 }
360 out(")"); 364 out(")");
361 blockBody(fun.body, needsSeparation: false, needsNewline: false); 365 blockBody(fun.body, needsSeparation: false, needsNewline: false);
366 _renamer.leaveScope();
362 } 367 }
363 368
364 visitFunctionDeclaration(FunctionDeclaration declaration) { 369 visitFunctionDeclaration(FunctionDeclaration declaration) {
365 indent(); 370 indent();
366 functionOut(declaration.function, declaration.name); 371 functionOut(declaration.function, declaration.name);
367 lineOut(); 372 lineOut();
368 } 373 }
369 374
370 visitNestedExpression(Expression node, int requiredPrecedence, 375 visitNestedExpression(Expression node, int requiredPrecedence,
371 [bool newInForInit, bool newAtStatementBegin]) { 376 [bool newInForInit, bool newAtStatementBegin]) {
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 } 594 }
590 595
591 visitPostfix(Postfix postfix) { 596 visitPostfix(Postfix postfix) {
592 visitNestedExpression(postfix.argument, LEFT_HAND_SIDE, 597 visitNestedExpression(postfix.argument, LEFT_HAND_SIDE,
593 newInForInit: inForInit, 598 newInForInit: inForInit,
594 newAtStatementBegin: atStatementBegin); 599 newAtStatementBegin: atStatementBegin);
595 out(postfix.op); 600 out(postfix.op);
596 } 601 }
597 602
598 visitVariableUse(VariableUse ref) { 603 visitVariableUse(VariableUse ref) {
599 out(ref.name); 604 var newName = _renamer.getName(ref.name);
floitsch 2012/10/09 13:45:43 FYI: I think it can currently happen that the name
erikcorry 2012/10/09 20:15:51 ok
605 out(newName);
floitsch 2012/10/09 13:45:43 out(renamer.getName(ref.name)) ?
erikcorry 2012/10/09 20:15:51 Done.
600 } 606 }
601 607
602 visitThis(This node) { 608 visitThis(This node) {
603 out("this"); 609 out("this");
604 } 610 }
605 611
606 visitVariableDeclaration(VariableDeclaration decl) { 612 visitVariableDeclaration(VariableDeclaration decl) {
607 out(decl.name); 613 var newName = _renamer.declareName(decl.name);
614 out(newName);
608 } 615 }
609 616
610 visitParameter(Parameter param) { 617 visitParameter(Parameter param) {
611 out(param.name); 618 var newName = _renamer.declareName(param.name);
619 out(newName);
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 Renamer {
floitsch 2012/10/09 13:45:43 Missing enterScope, leaveScope.
floitsch 2012/10/09 13:45:43 I would call this class 'Namer'. Otherwise it impl
erikcorry 2012/10/09 20:15:51 Done.
erikcorry 2012/10/09 20:15:51 Done and the member variable was also renamed to m
839 String getName(String oldName);
840 String declareName(String oldName);
841 }
842
843
844 class DummyRenamer implements Renamer {
floitsch 2012/10/09 13:45:43 Don't like "Dummy". Maybe "PassThroughNamer", "Ori
ngeoffray 2012/10/09 13:53:42 IdentityNamer?
floitsch 2012/10/09 13:55:35 yeah. That's actually the one I was looking for. t
erikcorry 2012/10/09 20:15:51 Done.
845 String getName(String oldName) => oldName;
846 String declareName(String oldName) => oldName;
847 void enterScope() {}
848 void leaveScope() {}
849 }
850
851
852 class MinifyRenamer implements Renamer {
853 MinifyRenamer() : _maps = [], _name_number_stack = [], _name_number = 0;
ngeoffray 2012/10/09 13:53:42 _name_number -> _nameNumber _name_number_stack ->
ngeoffray 2012/10/09 13:53:42 You could type these lists: _maps = <Map<String, S
erikcorry 2012/10/09 20:15:51 Done.
erikcorry 2012/10/09 20:15:51 I'd rather not.
854
855 void enterScope() {
856 _maps.add(new Map<String, String>());
857 _name_number_stack.add(_name_number);
floitsch 2012/10/09 13:45:43 _nameNumberStack
erikcorry 2012/10/09 20:15:51 Done.
858 }
859
860 void leaveScope() {
861 _maps.removeLast();
862 _name_number = _name_number_stack.removeLast();
863 }
864
865 String getName(String oldName) {
866 for (int i = _maps.length - 1; i >= 0; i--) {
867 var map = _maps[i];
floitsch 2012/10/09 13:45:43 Personally I would prefer having a type here, but
erikcorry 2012/10/09 20:15:51 I'd rather not.
868 if (map.containsKey(oldName)) return map[oldName];
floitsch 2012/10/09 13:45:43 String replacement = map[oldName]; if (replacement
erikcorry 2012/10/09 20:15:51 Done.
869 }
870 return oldName;
871 }
872
873 Strint declareName(String oldName) {
874 if (_maps.length == 0) return oldName;
875 var newName = "z${_name_number++}";
876 _maps.last()[oldName] = newName;
877 return newName;
878 }
879
880 List<Map<String, String>> _maps;
floitsch 2012/10/09 13:45:43 We usually have locals on the top of the class.
floitsch 2012/10/09 13:45:43 No need to make the fields private.
floitsch 2012/10/09 13:45:43 make the maps field final.
erikcorry 2012/10/09 20:15:51 Done.
erikcorry 2012/10/09 20:15:51 Done.
erikcorry 2012/10/09 20:15:51 Done.
881 List<int> _name_number_stack;
floitsch 2012/10/09 13:45:43 make nameNumberStack final.
erikcorry 2012/10/09 20:15:51 Done.
882 int _name_number;
883 }
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