Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 part of js_backend; | 5 part of js_backend; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Assigns JavaScript identifiers to Dart variables, class-names and members. | 8 * Assigns JavaScript identifiers to Dart variables, class-names and members. |
| 9 */ | 9 */ |
| 10 class MinifyNamer extends Namer { | 10 class MinifyNamer extends Namer { |
| 11 MinifyNamer(Compiler compiler) : super(compiler); | 11 MinifyNamer(Compiler compiler) : super(compiler) { |
| 12 reserveBackendNames(); | |
| 13 } | |
| 12 | 14 |
| 13 String get isolateName => 'I'; | 15 String get isolateName => 'I'; |
| 14 String get isolatePropertiesName => 'p'; | 16 String get isolatePropertiesName => 'p'; |
| 15 bool get shouldMinify => true; | 17 bool get shouldMinify => true; |
| 16 | 18 |
| 17 const ALPHABET_CHARACTERS = 52; // a-zA-Z. | 19 const ALPHABET_CHARACTERS = 52; // a-zA-Z. |
| 18 const ALPHANUMERIC_CHARACTERS = 62; // a-zA-Z0-9. | 20 const ALPHANUMERIC_CHARACTERS = 62; // a-zA-Z0-9. |
| 19 | 21 |
| 20 // You can pass an invalid identifier to this and unlike its non-minifying | 22 // You can pass an invalid identifier to this and unlike its non-minifying |
| 21 // counterpart it will never return the proposedName as the new fresh name. | 23 // counterpart it will never return the proposedName as the new fresh name. |
| 22 String getFreshName(String proposedName, Set<String> usedNames) { | 24 String getFreshName(String proposedName, Set<String> usedNames) { |
| 23 var freshName = _getUnusedName(proposedName, usedNames); | 25 var freshName = _getUnusedName(proposedName, usedNames); |
| 24 usedNames.add(freshName); | 26 usedNames.add(freshName); |
| 25 return freshName; | 27 return freshName; |
| 26 } | 28 } |
| 27 | 29 |
| 28 SourceString getClosureVariableName(SourceString name, int id) { | 30 SourceString getClosureVariableName(SourceString name, int id) { |
| 29 if (id < ALPHABET_CHARACTERS) { | 31 if (id < ALPHABET_CHARACTERS) { |
| 30 return new SourceString(new String.fromCharCodes([_letterNumber(id)])); | 32 return new SourceString(new String.fromCharCodes([_letterNumber(id)])); |
| 31 } | 33 } |
| 32 return new SourceString("${getMappedInstanceName('closure')}_$id"); | 34 return new SourceString("${getMappedInstanceName('closure')}_$id"); |
| 33 } | 35 } |
| 34 | 36 |
| 37 void reserveBackendNames() { | |
| 38 for (var name in JsNames.reservedNativeProperties) { | |
| 39 if (name.length < 3) { | |
|
ngeoffray
2012/12/18 12:34:03
What does this mean? Why 3?
| |
| 40 instanceNameMap[name] = name; | |
| 41 } | |
| 42 usedInstanceNames.add(name); | |
| 43 } | |
| 44 } | |
| 45 | |
| 35 // This gets a minified name based on a hash of the proposed name. This | 46 // This gets a minified name based on a hash of the proposed name. This |
| 36 // is slightly less efficient than just getting the next name in a series, | 47 // is slightly less efficient than just getting the next name in a series, |
| 37 // but it means that small changes in the input program will give smallish | 48 // but it means that small changes in the input program will give smallish |
| 38 // changes in the output, which can be useful for diffing etc. | 49 // changes in the output, which can be useful for diffing etc. |
| 39 String _getUnusedName(String proposedName, Set<String> usedNames) { | 50 String _getUnusedName(String proposedName, Set<String> usedNames) { |
| 40 // Try single-character names with characters that occur in the | 51 // Try single-character names with characters that occur in the |
| 41 // input. | 52 // input. |
| 42 for (int i = 0; i < proposedName.length; i++) { | 53 for (int i = 0; i < proposedName.length; i++) { |
| 43 String candidate = proposedName[i]; | 54 String candidate = proposedName[i]; |
| 44 int code = candidate.charCodeAt(0); | 55 int code = candidate.charCodeAt(0); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 } | 117 } |
| 107 | 118 |
| 108 int _alphaNumericNumber(int x) { | 119 int _alphaNumericNumber(int x) { |
| 109 if (x >= ALPHANUMERIC_CHARACTERS) x %= ALPHANUMERIC_CHARACTERS; | 120 if (x >= ALPHANUMERIC_CHARACTERS) x %= ALPHANUMERIC_CHARACTERS; |
| 110 if (x < 26) return $a + x; | 121 if (x < 26) return $a + x; |
| 111 if (x < 52) return $A + x - 26; | 122 if (x < 52) return $A + x - 26; |
| 112 return $0 + x - 52; | 123 return $0 + x - 52; |
| 113 } | 124 } |
| 114 | 125 |
| 115 } | 126 } |
| OLD | NEW |