| 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 static Set<String> _jsReserved = null; | 9 static Set<String> _jsReserved = null; |
| 10 Set<String> get jsReserved() { | 10 Set<String> get jsReserved() { |
| 11 if (_jsReserved === null) { | 11 if (_jsReserved === null) { |
| 12 _jsReserved = new Set<String>(); | 12 _jsReserved = new Set<String>(); |
| 13 _jsReserved.addAll(JsNames.javaScriptKeywords); | 13 _jsReserved.addAll(JsNames.javaScriptKeywords); |
| 14 _jsReserved.addAll(JsNames.reservedPropertySymbols); | 14 _jsReserved.addAll(JsNames.reservedPropertySymbols); |
| 15 } | 15 } |
| 16 return _jsReserved; | 16 return _jsReserved; |
| 17 } | 17 } |
| 18 | 18 |
| 19 Map<Element, String> globals; | 19 Map<Element, String> globals; |
| 20 Map<String, int> usedGlobals; | 20 Map<String, int> usedGlobals; |
| 21 | 21 |
| 22 Namer() | 22 Namer() |
| 23 : globals = new Map<Element, String>(), | 23 : globals = new Map<Element, String>(), |
| 24 usedGlobals = new Map<String, int>(); | 24 usedGlobals = new Map<String, int>(); |
| 25 | 25 |
| 26 String get currentIsolate() => "currentIsolate"; | 26 String get currentIsolate() => "currentIsolate"; |
| 27 String get isolate() => "Isolate"; | 27 String get isolate() => "Isolate"; |
| 28 | 28 |
| 29 String getName(Element element) { |
| 30 String name = '${element.name}'; |
| 31 if (name == '') { |
| 32 assert(element.kind == ElementKind.CONSTRUCTOR || |
| 33 element.kind == ElementKind.CONSTRUCTOR_BODY); |
| 34 return 'default'; |
| 35 } |
| 36 return name; |
| 37 } |
| 38 |
| 29 String define(Element element) { | 39 String define(Element element) { |
| 30 assert(globals[element] === null); | 40 assert(globals[element] === null); |
| 31 | 41 |
| 32 String dartId = '${element.name}'; | 42 String dartId = getName(element); |
| 33 // Prefix the dartId with '$' if the name is reserved. | 43 // Prefix the dartId with '$' if the name is reserved. |
| 34 if (jsReserved.contains(dartId)) { | 44 if (jsReserved.contains(dartId)) { |
| 35 dartId = "\$$dartId"; | 45 dartId = "\$$dartId"; |
| 36 assert(!jsReserved.contains(dartId)); | 46 assert(!jsReserved.contains(dartId)); |
| 37 } | 47 } |
| 38 | 48 |
| 39 int usedCount = usedGlobals[dartId]; | 49 int usedCount = usedGlobals[dartId]; |
| 40 if (usedCount === null) { | 50 if (usedCount === null) { |
| 41 // No element with this name has been used before. | 51 // No element with this name has been used before. |
| 42 usedGlobals[dartId] = 1; | 52 usedGlobals[dartId] = 1; |
| 43 globals[element] = dartId; | 53 globals[element] = dartId; |
| 44 return dartId; | 54 return dartId; |
| 45 } else { | 55 } else { |
| 46 // Not the first time we see an element with this name. Append a number | 56 // Not the first time we see an element with this name. Append a number |
| 47 // to make it unique. | 57 // to make it unique. |
| 48 String id; | 58 String id; |
| 49 do { | 59 do { |
| 50 usedCount++; | 60 usedCount++; |
| 51 id = '$dartId$usedCount'; | 61 id = '$dartId$usedCount'; |
| 52 } while (usedGlobals[id] !== null); | 62 } while (usedGlobals[id] !== null); |
| 53 usedGlobals[dartId] = usedCount; | 63 usedGlobals[dartId] = usedCount; |
| 54 globals[element] = id; | 64 globals[element] = id; |
| 55 return id; | 65 return id; |
| 56 } | 66 } |
| 57 } | 67 } |
| 58 | 68 |
| 59 String isolateAccess(Element element) { | 69 String isolateAccess(Element element) { |
| 60 String jsId = globals[element]; | 70 String jsId = globals[element]; |
| 61 if (jsId === null) jsId = define(element); | 71 if (jsId === null) jsId = define(element); |
| 62 return "$currentIsolate.$jsId"; | 72 return "$currentIsolate.$jsId"; |
| 63 } | 73 } |
| 64 | 74 |
| 65 String isolatePropertyAccess(Element element) { | 75 String isolatePropertyAccess(Element element) { |
| 66 String jsId = globals[element]; | 76 String jsId = globals[element]; |
| 67 if (jsId === null) jsId = define(element); | 77 if (jsId === null) jsId = define(element); |
| 68 return "$isolate.prototype.$jsId"; | 78 return "$isolate.prototype.$jsId"; |
| 69 } | 79 } |
| 70 | 80 |
| 71 String methodName(Element element) { | 81 String instanceName(Element element) { |
| 82 if (element.kind == ElementKind.CONSTRUCTOR_BODY) { |
| 83 ConstructorBodyElement bodyElement = element; |
| 84 return constructorBodyName(element.constructor); |
| 85 } |
| 72 // TODO(floitsch): mangle if necessary. | 86 // TODO(floitsch): mangle if necessary. |
| 73 return '${element.name}'; | 87 return '${element.name}'; |
| 74 } | 88 } |
| 89 |
| 90 /** |
| 91 * The element must be a constructor element. This way we can find the |
| 92 * body method when we only have the constructor-element (as is the case |
| 93 * when doing the super-initialization. |
| 94 */ |
| 95 String constructorBodyName(FunctionElement element) { |
| 96 assert(element.kind == ElementKind.CONSTRUCTOR); |
| 97 // TODO(floitsch): mangle if necessary. Make sure it doesn't collide with |
| 98 // normal methods. |
| 99 return getName(element); |
| 100 } |
| 75 } | 101 } |
| OLD | NEW |