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

Side by Side Diff: pkg/compiler/lib/src/js_backend/minify_namer.dart

Issue 693183006: Revert "Move dart2js from sdk/lib/_internal/compiler to pkg/compiler" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of js_backend;
6
7 /**
8 * Assigns JavaScript identifiers to Dart variables, class-names and members.
9 */
10 class MinifyNamer extends Namer {
11 MinifyNamer(Compiler compiler) : super(compiler) {
12 reserveBackendNames();
13 }
14
15 String get isolateName => 'I';
16 String get isolatePropertiesName => 'p';
17 bool get shouldMinify => true;
18
19 final String getterPrefix = 'g';
20 final String setterPrefix = 's';
21 final String callPrefix = ''; // this will create function names $<n>
22
23 static const ALPHABET_CHARACTERS = 52; // a-zA-Z.
24 static const ALPHANUMERIC_CHARACTERS = 62; // a-zA-Z0-9.
25
26 // You can pass an invalid identifier to this and unlike its non-minifying
27 // counterpart it will never return the proposedName as the new fresh name.
28 String getFreshName(String proposedName,
29 Set<String> usedNames,
30 Map<String, String> suggestedNames,
31 {bool ensureSafe: true}) {
32 var freshName;
33 var suggestion = suggestedNames[proposedName];
34 if (suggestion != null && !usedNames.contains(suggestion)) {
35 freshName = suggestion;
36 } else {
37 freshName = _getUnusedName(proposedName, usedNames);
38 }
39 usedNames.add(freshName);
40 return freshName;
41 }
42
43 String getClosureVariableName(String name, int id) {
44 if (id < ALPHABET_CHARACTERS) {
45 return new String.fromCharCodes([_letterNumber(id)]);
46 }
47 return "${getMappedInstanceName('closure')}_$id";
48 }
49
50 void reserveBackendNames() {
51 // From issue 7554. These should not be used on objects (as instance
52 // variables) because they clash with names from the DOM.
53 const reservedNativeProperties = const <String>[
54 'Q', 'a', 'b', 'c', 'd', 'e', 'f', 'r', 'x', 'y', 'z',
55 // 2-letter:
56 'ch', 'cx', 'cy', 'db', 'dx', 'dy', 'fr', 'fx', 'fy', 'go', 'id', 'k1',
57 'k2', 'k3', 'k4', 'r1', 'r2', 'rx', 'ry', 'x1', 'x2', 'y1', 'y2',
58 // 3-letter:
59 'add', 'all', 'alt', 'arc', 'CCW', 'cmp', 'dir', 'end', 'get', 'in1',
60 'in2', 'INT', 'key', 'log', 'low', 'm11', 'm12', 'm13', 'm14', 'm21',
61 'm22', 'm23', 'm24', 'm31', 'm32', 'm33', 'm34', 'm41', 'm42', 'm43',
62 'm44', 'max', 'min', 'now', 'ONE', 'put', 'red', 'rel', 'rev', 'RGB',
63 'sdp', 'set', 'src', 'tag', 'top', 'uid', 'uri', 'url', 'URL',
64 // 4-letter:
65 'abbr', 'atob', 'Attr', 'axes', 'axis', 'back', 'BACK', 'beta', 'bias',
66 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE',
67 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face',
68 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head',
69 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT',
70 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line',
71 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE',
72 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX',
73 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send',
74 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text',
75 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO'];
76
77 for (var name in reservedNativeProperties) {
78 if (name.length < 2) {
79 instanceNameMap[name] = name;
80 }
81 usedInstanceNames.add(name);
82 // Getter and setter names are autogenerated by prepending 'g' and 's' to
83 // field names. Therefore there are some field names we don't want to
84 // use. It is implicit in the next line that the banned prefix is
85 // only one character.
86 if (_hasBannedPrefix(name)) usedInstanceNames.add(name.substring(1));
87 }
88
89 // These popular names are present in most programs and deserve
90 // single character minified names. We could determine the popular names
91 // individually per program, but that would mean that the output of the
92 // minifier was less stable from version to version of the program being
93 // minified.
94 _populateSuggestedNames(
95 suggestedInstanceNames,
96 usedInstanceNames,
97 const <String>[
98 r'$add', r'add$1', r'$and', r'codeUnitAt$1', r'$or',
99 r'current', r'$shr', r'$eq', r'$ne',
100 r'getPrototypeOf', r'hasOwnProperty', r'$index', r'$indexSet',
101 r'$isJavaScriptIndexingBehavior', r'$xor',
102 r'iterator', r'length', r'$lt', r'$gt', r'$le', r'$ge',
103 r'moveNext$0', r'node', r'on', r'$negate', r'push', r'self',
104 r'start', r'target', r'$shl', r'value', r'width', r'style',
105 r'noSuchMethod$1', r'$mul', r'$div', r'$sub', r'$not', r'$mod',
106 r'$tdiv']);
107
108 _populateSuggestedNames(
109 suggestedGlobalNames,
110 usedGlobalNames,
111 const <String>[
112 r'Object', 'wrapException', r'$eq', r'S', r'ioore',
113 r'UnsupportedError$', r'length', r'$sub',
114 r'getInterceptor$JSArrayJSString', r'$add',
115 r'$gt', r'$ge', r'$lt', r'$le', r'add', r'getInterceptor$JSNumber',
116 r'iterator', r'$index', r'iae', r'getInterceptor$JSArray',
117 r'ArgumentError$', r'BoundClosure', r'StateError$',
118 r'getInterceptor', r'max', r'$mul', r'List_List', r'Map_Map',
119 r'getInterceptor$JSString', r'$div', r'$indexSet',
120 r'List_List$from', r'Set_Set$from', r'toString', r'toInt', r'min',
121 r'StringBuffer_StringBuffer', r'contains1', r'WhereIterable$',
122 r'RangeError$value', r'JSString', r'JSNumber',
123 r'JSArray', r'createInvocationMirror'
124 ]);
125 }
126
127 void _populateSuggestedNames(Map<String, String> suggestionMap,
128 Set<String> used,
129 List<String> suggestions) {
130 int c = $a - 1;
131 String letter;
132 for (String name in suggestions) {
133 do {
134 assert(c != $Z);
135 c = (c == $z) ? $A : c + 1;
136 letter = new String.fromCharCodes([c]);
137 } while (used.contains(letter));
138 assert(suggestionMap[name] == null);
139 suggestionMap[name] = letter;
140 }
141 }
142
143
144 // This gets a minified name based on a hash of the proposed name. This
145 // is slightly less efficient than just getting the next name in a series,
146 // but it means that small changes in the input program will give smallish
147 // changes in the output, which can be useful for diffing etc.
148 String _getUnusedName(String proposedName, Set<String> usedNames) {
149 int hash = _calculateHash(proposedName);
150 // Avoid very small hashes that won't try many names.
151 hash = hash < 1000 ? hash * 314159 : hash; // Yes, it's prime.
152
153 // Try other n-character names based on the hash. We try one to three
154 // character identifiers. For each length we try around 10 different names
155 // in a predictable order determined by the proposed name. This is in order
156 // to make the renamer stable: small changes in the input should nornally
157 // result in relatively small changes in the output.
158 for (var n = 2; n <= 3; n++) {
159 int h = hash;
160 while (h > 10) {
161 var codes = <int>[_letterNumber(h)];
162 int h2 = h ~/ ALPHABET_CHARACTERS;
163 for (var i = 1; i < n; i++) {
164 codes.add(_alphaNumericNumber(h2));
165 h2 ~/= ALPHANUMERIC_CHARACTERS;
166 }
167 final candidate = new String.fromCharCodes(codes);
168 if (!usedNames.contains(candidate) &&
169 !jsReserved.contains(candidate) &&
170 !_hasBannedPrefix(candidate)) {
171 return candidate;
172 }
173 // Try again with a slightly different hash. After around 10 turns
174 // around this loop h is zero and we try a longer name.
175 h ~/= 7;
176 }
177 }
178
179 // If we can't find a hash based name in the three-letter space, then base
180 // the name on a letter and a counter.
181 var startLetter = new String.fromCharCodes([_letterNumber(hash)]);
182 var i = 0;
183 while (usedNames.contains("$startLetter$i")) {
184 i++;
185 }
186 // We don't need to check for banned prefix because the name is in the form
187 // xnnn, where nnn is a number. There can be no getter or setter called
188 // gnnn since that would imply a numeric field name.
189 return "$startLetter$i";
190 }
191
192 /// Instance members starting with g and s are reserved for getters and
193 /// setters.
194 bool _hasBannedPrefix(String name) {
195 int code = name.codeUnitAt(0);
196 return code == $g || code == $s;
197 }
198
199 int _calculateHash(String name) {
200 int h = 0;
201 for (int i = 0; i < name.length; i++) {
202 h += name.codeUnitAt(i);
203 h &= 0xffffffff;
204 h += h << 10;
205 h &= 0xffffffff;
206 h ^= h >> 6;
207 h &= 0xffffffff;
208 }
209 return h;
210 }
211
212 int _letterNumber(int x) {
213 if (x >= ALPHABET_CHARACTERS) x %= ALPHABET_CHARACTERS;
214 if (x < 26) return $a + x;
215 return $A + x - 26;
216 }
217
218 int _alphaNumericNumber(int x) {
219 if (x >= ALPHANUMERIC_CHARACTERS) x %= ALPHANUMERIC_CHARACTERS;
220 if (x < 26) return $a + x;
221 if (x < 52) return $A + x - 26;
222 return $0 + x - 52;
223 }
224
225 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/js_backend/js_backend.dart ('k') | pkg/compiler/lib/src/js_backend/namer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698