| 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 |
| 11 static final CLOSURE_INVOCATION_NAME = const SourceString('\$call'); | 11 static final CLOSURE_INVOCATION_NAME = const SourceString('\$call'); |
| 12 static final OPERATOR_EQUALS = const SourceString('operator\$eq'); |
| 12 | 13 |
| 13 static Set<String> _jsReserved = null; | 14 static Set<String> _jsReserved = null; |
| 14 Set<String> get jsReserved() { | 15 Set<String> get jsReserved() { |
| 15 if (_jsReserved === null) { | 16 if (_jsReserved === null) { |
| 16 _jsReserved = new Set<String>(); | 17 _jsReserved = new Set<String>(); |
| 17 _jsReserved.addAll(JsNames.javaScriptKeywords); | 18 _jsReserved.addAll(JsNames.javaScriptKeywords); |
| 18 _jsReserved.addAll(JsNames.reservedPropertySymbols); | 19 _jsReserved.addAll(JsNames.reservedPropertySymbols); |
| 19 } | 20 } |
| 20 return _jsReserved; | 21 return _jsReserved; |
| 21 } | 22 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 return 'get\$$name'; | 54 return 'get\$$name'; |
| 54 } | 55 } |
| 55 | 56 |
| 56 /** | 57 /** |
| 57 * Returns a preferred JS-id for the given top-level or static element. | 58 * Returns a preferred JS-id for the given top-level or static element. |
| 58 * The returned id is guaranteed to be a valid JS-id. | 59 * The returned id is guaranteed to be a valid JS-id. |
| 59 */ | 60 */ |
| 60 String _computeGuess(Element element) { | 61 String _computeGuess(Element element) { |
| 61 assert(!element.isInstanceMember()); | 62 assert(!element.isInstanceMember()); |
| 62 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { | 63 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { |
| 63 SourceString name = getConstructorName(element); | |
| 64 FunctionElement functionElement = element; | 64 FunctionElement functionElement = element; |
| 65 return instanceMethodName(name, functionElement.parameterCount(compiler)); | 65 return instanceMethodName( |
| 66 element.name, functionElement.parameterCount(compiler)); |
| 66 } else { | 67 } else { |
| 67 // TODO(floitsch): deal with named constructors. | 68 // TODO(floitsch): deal with named constructors. |
| 68 String name = '${element.name}'; | 69 String name = '${element.name}'; |
| 69 if (element.kind == ElementKind.FUNCTION) { | 70 if (element.kind == ElementKind.FUNCTION) { |
| 70 FunctionElement functionElement = element; | 71 FunctionElement functionElement = element; |
| 71 name = '$name\$${functionElement.parameterCount(compiler)}'; | 72 name = '$name\$${functionElement.parameterCount(compiler)}'; |
| 72 } | 73 } |
| 73 // Prefix the name with '$' if it is reserved. | 74 // Prefix the name with '$' if it is reserved. |
| 74 if (jsReserved.contains(name)) { | 75 if (jsReserved.contains(name)) { |
| 75 name = "\$$name"; | 76 name = "\$$name"; |
| 76 assert(!jsReserved.contains(name)); | 77 assert(!jsReserved.contains(name)); |
| 77 } | 78 } |
| 78 return name; | 79 return name; |
| 79 } | 80 } |
| 80 } | 81 } |
| 81 | 82 |
| 82 String getBailoutName(Element element) { | 83 String getBailoutName(Element element) { |
| 83 return '${getName(element)}\$bailout'; | 84 return '${getName(element)}\$bailout'; |
| 84 } | 85 } |
| 85 | 86 |
| 86 SourceString getConstructorName(FunctionElement constructor) { | |
| 87 String dartName = constructor.name.stringValue; | |
| 88 return new SourceString(dartName.replaceFirst('\.', '\$')); | |
| 89 } | |
| 90 | |
| 91 /** | 87 /** |
| 92 * Returns a preferred JS-id for the given element. The returned id is | 88 * Returns a preferred JS-id for the given element. The returned id is |
| 93 * guaranteed to be a valid JS-id. Globals and static fields are furthermore | 89 * guaranteed to be a valid JS-id. Globals and static fields are furthermore |
| 94 * guaranteed to be unique. | 90 * guaranteed to be unique. |
| 95 * | 91 * |
| 96 * For accessing statics consider calling | 92 * For accessing statics consider calling |
| 97 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. | 93 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. |
| 98 */ | 94 */ |
| 99 String getName(Element element) { | 95 String getName(Element element) { |
| 100 if (element.isInstanceMember()) { | 96 if (element.isInstanceMember()) { |
| 101 SourceString name; | 97 SourceString name; |
| 102 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { | 98 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { |
| 103 ConstructorBodyElement bodyElement = element; | 99 ConstructorBodyElement bodyElement = element; |
| 104 SourceString name = getConstructorName(bodyElement.constructor); | 100 SourceString name = bodyElement.constructor.name; |
| 105 return instanceMethodName(name, bodyElement.parameterCount(compiler)); | 101 return instanceMethodName(name, bodyElement.parameterCount(compiler)); |
| 106 } else if (element.kind == ElementKind.FUNCTION) { | 102 } else if (element.kind == ElementKind.FUNCTION) { |
| 107 FunctionElement functionElement = element; | 103 FunctionElement functionElement = element; |
| 108 int parameterCount = functionElement.parameterCount(compiler); | 104 int parameterCount = functionElement.parameterCount(compiler); |
| 109 return instanceMethodName(element.name, parameterCount); | 105 return instanceMethodName(element.name, parameterCount); |
| 110 } else if (element.kind == ElementKind.GETTER) { | 106 } else if (element.kind == ElementKind.GETTER) { |
| 111 return getterName(element.name); | 107 return getterName(element.name); |
| 112 } else if (element.kind == ElementKind.SETTER) { | 108 } else if (element.kind == ElementKind.SETTER) { |
| 113 return setterName(element.name); | 109 return setterName(element.name); |
| 114 } else { | 110 } else { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 } | 166 } |
| 171 | 167 |
| 172 String isolateBailoutAccess(Element element) { | 168 String isolateBailoutAccess(Element element) { |
| 173 return '${isolateAccess(element)}\$bailout'; | 169 return '${isolateAccess(element)}\$bailout'; |
| 174 } | 170 } |
| 175 | 171 |
| 176 String operatorIs(ClassElement element) { | 172 String operatorIs(ClassElement element) { |
| 177 return 'is\$${getName(element)}'; | 173 return 'is\$${getName(element)}'; |
| 178 } | 174 } |
| 179 } | 175 } |
| OLD | NEW |