Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, 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 class FrequencyBasedNamer extends Namer with _MinifiedFieldNamer, | |
| 8 _MinifiedOneShotInterceptorNamer implements jsAst.TokenFinalizer { | |
| 9 _FieldNamingRegistry fieldRegistry; | |
| 10 List<TokenName> tokens = new List<TokenName>(); | |
| 11 | |
| 12 Map<NamingScope, TokenScope> _tokenScopes = | |
| 13 new Maplet<NamingScope, TokenScope>(); | |
| 14 | |
| 15 // Some basic settings for smaller names | |
| 16 String get isolateName => 'I'; | |
| 17 String get isolatePropertiesName => 'p'; | |
| 18 bool get shouldMinify => true; | |
| 19 | |
| 20 final String getterPrefix = 'g'; | |
| 21 final String setterPrefix = 's'; | |
| 22 final String callPrefix = ''; // this will create function names $<n> | |
| 23 | |
| 24 FrequencyBasedNamer(Compiler compiler) : super(compiler) { | |
| 25 fieldRegistry = new _FieldNamingRegistry(this); | |
| 26 } | |
| 27 | |
| 28 TokenScope newScopeFor(NamingScope scope) { | |
| 29 if (scope == NamingScope.instance) { | |
| 30 Set<String> illegalNames = new Set<String>(); | |
| 31 for (String illegal in MinifyNamer._reservedNativeProperties) { | |
| 32 illegalNames.add(illegal); | |
| 33 if (MinifyNamer._hasBannedPrefix(illegal)) { | |
| 34 illegalNames.add(illegal.substring(1)); | |
| 35 } | |
| 36 } | |
| 37 return new TokenScope(illegalNames); | |
| 38 } else { | |
| 39 return new TokenScope(); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 @override | |
| 44 jsAst.Name getFreshName(NamingScope scope, String proposedName, | |
| 45 {bool sanitizeForNatives: false, | |
| 46 bool sanitizeForAnnotations: false}) { | |
| 47 // Grab the scope for this token | |
| 48 TokenScope tokenScope = _tokenScopes.putIfAbsent(scope, | |
| 49 () => newScopeFor(scope)); | |
| 50 | |
| 51 // Get the name the normal namer would use as a key. | |
| 52 String proposed = _generateFreshStringForName(proposedName, | |
| 53 getUsedNames(scope), | |
| 54 getSuggestedNames(scope), | |
| 55 sanitizeForNatives: | |
| 56 sanitizeForNatives, | |
| 57 sanitizeForAnnotations: | |
| 58 sanitizeForAnnotations); | |
| 59 | |
| 60 TokenName name = new TokenName(tokenScope, proposed); | |
| 61 tokens.add(name); | |
| 62 return name; | |
| 63 } | |
| 64 | |
| 65 @override | |
| 66 jsAst.Name instanceFieldPropertyName(Element element) { | |
| 67 jsAst.Name proposed = _minifiedInstanceFieldPropertyName(element); | |
| 68 if (proposed != null) { | |
| 69 return proposed; | |
| 70 } | |
| 71 return super.instanceFieldPropertyName(element); | |
| 72 } | |
| 73 | |
| 74 @override | |
| 75 void finalizeTokens() { | |
| 76 int compareReferenceCount(TokenName a, TokenName b) { | |
| 77 int result = b._rc - a._rc; | |
| 78 if (result == 0) result = a._proposed.compareTo(b._proposed); | |
| 79 return result; | |
| 80 } | |
| 81 | |
| 82 List<TokenName> usedNames = | |
| 83 tokens.where((TokenName a) => a._rc > 0).toList(); | |
| 84 usedNames.sort(compareReferenceCount); | |
| 85 usedNames.forEach((TokenName token) => token.finalize()); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 class TokenScope { | |
| 90 List<int> _nextName = [$a]; | |
| 91 final Set<String> illegalNames; | |
| 92 | |
| 93 TokenScope([this.illegalNames = const ImmutableEmptySet()]); | |
| 94 | |
| 95 bool _incrementPosition(int pos) { | |
|
sra1
2015/07/01 04:11:09
Add a comment to say what sequence this generates.
herhut
2015/07/02 08:54:51
Done.
| |
| 96 bool overflow = false; | |
| 97 if (pos < 0) return true; | |
| 98 int value = _nextName[pos]; | |
| 99 if (value == $_) { | |
| 100 value = $0; | |
| 101 } else if (value == $9) { | |
| 102 value = $a; | |
| 103 } else if (value == $z) { | |
| 104 value = $A; | |
| 105 } else if (value == $Z) { | |
| 106 overflow = _incrementPosition(pos - 1); | |
| 107 value = (pos > 0) ? $_ : $a; | |
| 108 } else { | |
| 109 value++; | |
| 110 } | |
| 111 _nextName[pos] = value; | |
| 112 return overflow; | |
| 113 } | |
| 114 | |
| 115 _incrementName() { | |
| 116 if (_incrementPosition(_nextName.length - 1)) { | |
| 117 _nextName.add($a); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 String getNextName() { | |
| 122 String proposal; | |
| 123 do { | |
| 124 proposal = new String.fromCharCodes(_nextName); | |
| 125 _incrementName(); | |
| 126 } while (MinifyNamer._hasBannedPrefix(proposal) || | |
| 127 illegalNames.contains(proposal)); | |
| 128 | |
| 129 return proposal; | |
| 130 } | |
| 131 } | |
| 132 | |
| OLD | NEW |