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 { |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 h += name.codeUnitAt(i); | 202 h += name.codeUnitAt(i); |
203 h &= 0xffffffff; | 203 h &= 0xffffffff; |
204 h += h << 10; | 204 h += h << 10; |
205 h &= 0xffffffff; | 205 h &= 0xffffffff; |
206 h ^= h >> 6; | 206 h ^= h >> 6; |
207 h &= 0xffffffff; | 207 h &= 0xffffffff; |
208 } | 208 } |
209 return h; | 209 return h; |
210 } | 210 } |
211 | 211 |
| 212 /// Remember bad hashes to avoid using a the same character with long numbers |
| 213 /// for frequent hashes. For example, `closure` is a very common name. |
| 214 Map<int, int> _badNames = new Map<int, int>(); |
| 215 |
212 /// If we can't find a hash based name in the three-letter space, then base | 216 /// If we can't find a hash based name in the three-letter space, then base |
213 /// the name on a letter and a counter. | 217 /// the name on a letter and a counter. |
214 String _badName(int hash, Set<String> usedNames) { | 218 String _badName(int hash, Set<String> usedNames) { |
215 String startLetter = new String.fromCharCodes([_letterNumber(hash)]); | 219 int count = _badNames.putIfAbsent(hash, () => 0); |
| 220 String startLetter = |
| 221 new String.fromCharCodes([_letterNumber(hash + count)]); |
| 222 _badNames[hash] = count + 1; |
216 String name; | 223 String name; |
217 int i = 0; | 224 int i = 0; |
218 do { | 225 do { |
219 name = "$startLetter${i++}"; | 226 name = "$startLetter${i++}"; |
220 } while (usedNames.contains(name)); | 227 } while (usedNames.contains(name)); |
221 // We don't need to check for banned prefix because the name is in the form | 228 // We don't need to check for banned prefix because the name is in the form |
222 // xnnn, where nnn is a number. There can be no getter or setter called | 229 // xnnn, where nnn is a number. There can be no getter or setter called |
223 // gnnn since that would imply a numeric field name. | 230 // gnnn since that would imply a numeric field name. |
224 return name; | 231 return name; |
225 } | 232 } |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 _BoxFieldNamingScope(Local box, _FieldNamingRegistry registry) : | 442 _BoxFieldNamingScope(Local box, _FieldNamingRegistry registry) : |
436 super.rootScope(box, registry); | 443 super.rootScope(box, registry); |
437 | 444 |
438 bool containsField(_) => true; | 445 bool containsField(_) => true; |
439 | 446 |
440 String operator[](Element field) { | 447 String operator[](Element field) { |
441 if (!names.containsKey(field)) add(field); | 448 if (!names.containsKey(field)) add(field); |
442 return names[field]; | 449 return names[field]; |
443 } | 450 } |
444 } | 451 } |
OLD | NEW |