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 /** | 5 /** |
6 * Assigns JavaScript identifiers to Dart variables, class-names and members. | 6 * Assigns JavaScript identifiers to Dart variables, class-names and members. |
7 */ | 7 */ |
8 class Namer { | 8 class Namer { |
9 final Compiler compiler; | 9 final Compiler compiler; |
10 | 10 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 } | 99 } |
100 | 100 |
101 String getterName(LibraryElement lib, SourceString name) { | 101 String getterName(LibraryElement lib, SourceString name) { |
102 // We dynamically create getters from the field-name. The getter name must | 102 // We dynamically create getters from the field-name. The getter name must |
103 // therefore be derived from the instance field-name. | 103 // therefore be derived from the instance field-name. |
104 String fieldName = safeName(privateName(lib, name)); | 104 String fieldName = safeName(privateName(lib, name)); |
105 return 'get\$$fieldName'; | 105 return 'get\$$fieldName'; |
106 } | 106 } |
107 | 107 |
108 String getFreshGlobalName(String proposedName) { | 108 String getFreshGlobalName(String proposedName) { |
109 int usedCount = usedGlobals[proposedName]; | 109 String name = proposedName; |
110 if (usedCount === null) { | 110 int count = usedGlobals[name]; |
111 // No element with this name has been used before. | 111 if (count !== null) { |
112 usedGlobals[proposedName] = 1; | |
113 return proposedName; | |
114 } else { | |
115 // Not the first time we see this name. Append a number to make it unique. | 112 // Not the first time we see this name. Append a number to make it unique. |
116 String name; | |
117 do { | 113 do { |
118 usedCount++; | 114 name = '$proposedName${count++}'; |
119 name = '$proposedName$usedCount'; | |
120 } while (usedGlobals[name] !== null); | 115 } while (usedGlobals[name] !== null); |
121 usedGlobals[proposedName] = usedCount; | 116 // Record the count in case we see this name later. We |
122 return name; | 117 // frequently see names multiple times, as all our closures use |
| 118 // the same name for their class. |
| 119 usedGlobals[proposedName] = count; |
123 } | 120 } |
| 121 usedGlobals[name] = 0; |
| 122 return name; |
124 } | 123 } |
125 | 124 |
126 static final String LIBRARY_PREFIX = "lib"; | 125 static final String LIBRARY_PREFIX = "lib"; |
127 | 126 |
128 /** | 127 /** |
129 * Returns a preferred JS-id for the given top-level or static element. | 128 * Returns a preferred JS-id for the given top-level or static element. |
130 * The returned id is guaranteed to be a valid JS-id. | 129 * The returned id is guaranteed to be a valid JS-id. |
131 */ | 130 */ |
132 String _computeGuess(Element element) { | 131 String _computeGuess(Element element) { |
133 assert(!element.isInstanceMember()); | 132 assert(!element.isInstanceMember()); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 } | 239 } |
241 | 240 |
242 String safeName(String name) { | 241 String safeName(String name) { |
243 if (jsReserved.contains(name) || name.startsWith('\$')) { | 242 if (jsReserved.contains(name) || name.startsWith('\$')) { |
244 name = "\$$name"; | 243 name = "\$$name"; |
245 assert(!jsReserved.contains(name)); | 244 assert(!jsReserved.contains(name)); |
246 } | 245 } |
247 return name; | 246 return name; |
248 } | 247 } |
249 } | 248 } |
OLD | NEW |