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