Chromium Code Reviews| 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() { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 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 define(Element element) { | 29 String define(Element element) { |
| 30 assert(globals[element] === null); | 30 assert(globals[element] === null); |
| 31 | 31 |
| 32 String dartId = '${element.name}'; | 32 String dartId = '${element.name}'; |
|
ngeoffray
2011/12/08 16:01:40
Please add a method getName that takes an element,
floitsch
2011/12/08 16:36:00
Done.
| |
| 33 if (dartId == '') { | |
| 34 assert(element is FunctionElement); | |
|
ngeoffray
2011/12/08 16:01:40
element.kind == ElementKind.CONSTRUCTOR?
floitsch
2011/12/08 16:36:00
Done.
| |
| 35 dartId = 'default'; | |
| 36 } | |
| 33 // Prefix the dartId with '$' if the name is reserved. | 37 // Prefix the dartId with '$' if the name is reserved. |
| 34 if (jsReserved.contains(dartId)) { | 38 if (jsReserved.contains(dartId)) { |
| 35 dartId = "\$$dartId"; | 39 dartId = "\$$dartId"; |
| 36 assert(!jsReserved.contains(dartId)); | 40 assert(!jsReserved.contains(dartId)); |
| 37 } | 41 } |
| 38 | 42 |
| 39 int usedCount = usedGlobals[dartId]; | 43 int usedCount = usedGlobals[dartId]; |
| 40 if (usedCount === null) { | 44 if (usedCount === null) { |
| 41 // No element with this name has been used before. | 45 // No element with this name has been used before. |
| 42 usedGlobals[dartId] = 1; | 46 usedGlobals[dartId] = 1; |
| 43 globals[element] = dartId; | 47 globals[element] = dartId; |
| 44 return dartId; | 48 return dartId; |
| 45 } else { | 49 } else { |
| 46 // Not the first time we see an element with this name. Append a number | 50 // Not the first time we see an element with this name. Append a number |
| 47 // to make it unique. | 51 // to make it unique. |
| 48 String id; | 52 String id; |
| 49 do { | 53 do { |
| 50 usedCount++; | 54 usedCount++; |
| 51 id = '$dartId$usedCount'; | 55 id = '$dartId$usedCount'; |
| 52 } while (usedGlobals[id] !== null); | 56 } while (usedGlobals[id] !== null); |
| 53 usedGlobals[dartId] = usedCount; | 57 usedGlobals[dartId] = usedCount; |
| 54 globals[element] = id; | 58 globals[element] = id; |
| 55 return id; | 59 return id; |
| 56 } | 60 } |
| 57 } | 61 } |
| 58 | 62 |
| 59 String isolateAccess(Element element) { | 63 String isolateAccess(Element element) { |
| 60 String jsId = globals[element]; | 64 String jsId = globals[element]; |
| 61 if (jsId === null) jsId = define(element); | 65 if (jsId === null) jsId = define(element); |
| 62 return "$currentIsolate.$jsId"; | 66 return "$currentIsolate.$jsId"; |
| 63 } | 67 } |
| 64 | 68 |
| 65 String isolatePropertyAccess(Element element) { | 69 String isolatePropertyAccess(Element element) { |
| 66 String jsId = globals[element]; | 70 String jsId = globals[element]; |
| 67 if (jsId === null) jsId = define(element); | 71 if (jsId === null) jsId = define(element); |
| 68 return "$isolate.prototype.$jsId"; | 72 return "$isolate.prototype.$jsId"; |
| 69 } | 73 } |
| 70 | 74 |
| 71 String methodName(Element element) { | 75 String instanceName(Element element) { |
| 76 if (element is ConstructorBodyElement) { | |
|
ngeoffray
2011/12/08 16:01:40
element.kind == ElementKind.CONSTRUCTOR_BODY
floitsch
2011/12/08 16:36:00
Done.
| |
| 77 return constructorBodyName(element); | |
| 78 } | |
| 72 // TODO(floitsch): mangle if necessary. | 79 // TODO(floitsch): mangle if necessary. |
| 73 return '${element.name}'; | 80 return '${element.name}'; |
| 74 } | 81 } |
| 82 | |
| 83 String constructorBodyName(Element element) { | |
|
ngeoffray
2011/12/08 16:01:40
assert(element.kind == ElementKind.BODY_CONSTRUCTO
floitsch
2011/12/08 16:36:00
Actually it must be a constructor. This is necessa
| |
| 84 // TODO(floitsch): mangle if necessary. Make sure it doesn't collide with | |
| 85 // normal methods. | |
| 86 String candidate = '${element.name}'; | |
| 87 if (candidate == '') candidate = 'default'; | |
| 88 return candidate; | |
| 89 } | |
| 75 } | 90 } |
| OLD | NEW |