| 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 part of js_backend; | 5 part of js_backend; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Assigns JavaScript identifiers to Dart variables, class-names and members. | 8 * Assigns JavaScript identifiers to Dart variables, class-names and members. |
| 9 */ | 9 */ |
| 10 class Namer implements ClosureNamer { | 10 class Namer implements ClosureNamer { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 assert(reservedGlobalObjectNames.length == 25); | 214 assert(reservedGlobalObjectNames.length == 25); |
| 215 _jsVariableReserved.addAll(reservedGlobalHelperFunctions); | 215 _jsVariableReserved.addAll(reservedGlobalHelperFunctions); |
| 216 } | 216 } |
| 217 return _jsVariableReserved; | 217 return _jsVariableReserved; |
| 218 } | 218 } |
| 219 | 219 |
| 220 final String currentIsolate = r'$'; | 220 final String currentIsolate = r'$'; |
| 221 final String getterPrefix = r'get$'; | 221 final String getterPrefix = r'get$'; |
| 222 final String setterPrefix = r'set$'; | 222 final String setterPrefix = r'set$'; |
| 223 final String metadataField = '@'; | 223 final String metadataField = '@'; |
| 224 final String callPrefix = 'call'; |
| 224 final String callCatchAllName = r'call$catchAll'; | 225 final String callCatchAllName = r'call$catchAll'; |
| 225 final String reflectableField = r'$reflectable'; | 226 final String reflectableField = r'$reflectable'; |
| 226 final String defaultValuesField = r'$defaultValues'; | 227 final String defaultValuesField = r'$defaultValues'; |
| 227 final String methodsWithOptionalArgumentsField = | 228 final String methodsWithOptionalArgumentsField = |
| 228 r'$methodsWithOptionalArguments'; | 229 r'$methodsWithOptionalArguments'; |
| 229 | 230 |
| 230 final String classDescriptorProperty = r'^'; | 231 final String classDescriptorProperty = r'^'; |
| 231 | 232 |
| 232 // Name of property in a class description for the native dispatch metadata. | 233 // Name of property in a class description for the native dispatch metadata. |
| 233 final String nativeSpecProperty = '%'; | 234 final String nativeSpecProperty = '%'; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 * [STATIC_CLOSURE_NAME_NAME]. | 284 * [STATIC_CLOSURE_NAME_NAME]. |
| 284 */ | 285 */ |
| 285 String get STATIC_CLOSURE_NAME_NAME => r'$name'; | 286 String get STATIC_CLOSURE_NAME_NAME => r'$name'; |
| 286 String get closureInvocationSelectorName => Compiler.CALL_OPERATOR_NAME; | 287 String get closureInvocationSelectorName => Compiler.CALL_OPERATOR_NAME; |
| 287 bool get shouldMinify => false; | 288 bool get shouldMinify => false; |
| 288 | 289 |
| 289 String getNameForJsGetName(Node node, String name) { | 290 String getNameForJsGetName(Node node, String name) { |
| 290 switch (name) { | 291 switch (name) { |
| 291 case 'GETTER_PREFIX': return getterPrefix; | 292 case 'GETTER_PREFIX': return getterPrefix; |
| 292 case 'SETTER_PREFIX': return setterPrefix; | 293 case 'SETTER_PREFIX': return setterPrefix; |
| 294 case 'CALL_PREFIX': return callPrefix; |
| 293 case 'CALL_CATCH_ALL': return callCatchAllName; | 295 case 'CALL_CATCH_ALL': return callCatchAllName; |
| 294 case 'REFLECTABLE': return reflectableField; | 296 case 'REFLECTABLE': return reflectableField; |
| 295 case 'CLASS_DESCRIPTOR_PROPERTY': return classDescriptorProperty; | 297 case 'CLASS_DESCRIPTOR_PROPERTY': return classDescriptorProperty; |
| 296 default: | 298 default: |
| 297 compiler.reportError( | 299 compiler.reportError( |
| 298 node, MessageKind.GENERIC, | 300 node, MessageKind.GENERIC, |
| 299 {'text': 'Error: Namer has no name for "$name".'}); | 301 {'text': 'Error: Namer has no name for "$name".'}); |
| 300 return 'BROKEN'; | 302 return 'BROKEN'; |
| 301 } | 303 } |
| 302 } | 304 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 // Selector.fromElement(element))? | 378 // Selector.fromElement(element))? |
| 377 String elementName = element.name; | 379 String elementName = element.name; |
| 378 String name = operatorNameToIdentifier(elementName); | 380 String name = operatorNameToIdentifier(elementName); |
| 379 if (name != elementName) return getMappedOperatorName(name); | 381 if (name != elementName) return getMappedOperatorName(name); |
| 380 | 382 |
| 381 LibraryElement library = element.getLibrary(); | 383 LibraryElement library = element.getLibrary(); |
| 382 if (element.isGenerativeConstructorBody()) { | 384 if (element.isGenerativeConstructorBody()) { |
| 383 name = Elements.reconstructConstructorNameSourceString(element); | 385 name = Elements.reconstructConstructorNameSourceString(element); |
| 384 } | 386 } |
| 385 FunctionSignature signature = element.functionSignature; | 387 FunctionSignature signature = element.functionSignature; |
| 386 String methodName = | 388 // We don't mangle the closure invoking function name because it |
| 387 '${privateName(library, name)}\$${signature.parameterCount}'; | 389 // is generated by string concatenation in applyFunction from |
| 390 // js_helper.dart. To keep code size down, we potentially shorten |
| 391 // the prefix though. |
| 392 String methodName; |
| 393 if (name == closureInvocationSelectorName) { |
| 394 methodName = '$callPrefix\$${signature.parameterCount}'; |
| 395 } else { |
| 396 methodName = '${privateName(library, name)}\$${signature.parameterCount}'; |
| 397 } |
| 388 if (signature.optionalParametersAreNamed && | 398 if (signature.optionalParametersAreNamed && |
| 389 !signature.optionalParameters.isEmpty) { | 399 !signature.optionalParameters.isEmpty) { |
| 390 StringBuffer buffer = new StringBuffer(); | 400 StringBuffer buffer = new StringBuffer(); |
| 391 signature.orderedOptionalParameters.forEach((Element element) { | 401 signature.orderedOptionalParameters.forEach((Element element) { |
| 392 buffer.write('\$${safeName(element.name)}'); | 402 buffer.write('\$${safeName(element.name)}'); |
| 393 }); | 403 }); |
| 394 methodName = '$methodName$buffer'; | 404 methodName = '$methodName$buffer'; |
| 395 } | 405 } |
| 396 if (name == closureInvocationSelectorName) return methodName; | 406 if (name == closureInvocationSelectorName) return methodName; |
| 397 return getMappedInstanceName(methodName); | 407 return getMappedInstanceName(methodName); |
| 398 } | 408 } |
| 399 | 409 |
| 400 String publicInstanceMethodNameByArity(String name, int arity) { | 410 String publicInstanceMethodNameByArity(String name, int arity) { |
| 401 String newName = operatorNameToIdentifier(name); | 411 String newName = operatorNameToIdentifier(name); |
| 402 if (newName != name) return getMappedOperatorName(newName); | 412 if (newName != name) return getMappedOperatorName(newName); |
| 403 assert(!isPrivateName(name)); | 413 assert(!isPrivateName(name)); |
| 404 // We don't mangle the closure invoking function name because it | 414 // We don't mangle the closure invoking function name because it |
| 405 // is generated by string concatenation in applyFunction from | 415 // is generated by string concatenation in applyFunction from |
| 406 // js_helper.dart. | 416 // js_helper.dart. To keep code size down, we potentially shorten |
| 407 String proposedName = '$name\$$arity'; | 417 // the prefix though. |
| 408 if (name == closureInvocationSelectorName) return proposedName; | 418 if (name == closureInvocationSelectorName) return '$callPrefix\$$arity'; |
| 409 return getMappedInstanceName(proposedName); | 419 |
| 420 return getMappedInstanceName('$name\$$arity'); |
| 410 } | 421 } |
| 411 | 422 |
| 412 String invocationName(Selector selector) { | 423 String invocationName(Selector selector) { |
| 413 if (selector.isGetter()) { | 424 if (selector.isGetter()) { |
| 414 String proposedName = privateName(selector.library, selector.name); | 425 String proposedName = privateName(selector.library, selector.name); |
| 415 return '$getterPrefix${getMappedInstanceName(proposedName)}'; | 426 return '$getterPrefix${getMappedInstanceName(proposedName)}'; |
| 416 } else if (selector.isSetter()) { | 427 } else if (selector.isSetter()) { |
| 417 String proposedName = privateName(selector.library, selector.name); | 428 String proposedName = privateName(selector.library, selector.name); |
| 418 return '$setterPrefix${getMappedInstanceName(proposedName)}'; | 429 return '$setterPrefix${getMappedInstanceName(proposedName)}'; |
| 419 } else { | 430 } else { |
| 420 String name = selector.name; | 431 String name = selector.name; |
| 421 if (selector.kind == SelectorKind.OPERATOR | 432 if (selector.kind == SelectorKind.OPERATOR |
| 422 || selector.kind == SelectorKind.INDEX) { | 433 || selector.kind == SelectorKind.INDEX) { |
| 423 name = operatorNameToIdentifier(name); | 434 name = operatorNameToIdentifier(name); |
| 424 assert(name != selector.name); | 435 assert(name != selector.name); |
| 425 return getMappedOperatorName(name); | 436 return getMappedOperatorName(name); |
| 426 } | 437 } |
| 427 assert(name == operatorNameToIdentifier(name)); | 438 assert(name == operatorNameToIdentifier(name)); |
| 428 StringBuffer buffer = new StringBuffer(); | 439 StringBuffer buffer = new StringBuffer(); |
| 429 for (String argumentName in selector.getOrderedNamedArguments()) { | 440 for (String argumentName in selector.getOrderedNamedArguments()) { |
| 430 buffer.write('\$${safeName(argumentName)}'); | 441 buffer.write('\$${safeName(argumentName)}'); |
| 431 } | 442 } |
| 432 String suffix = '\$${selector.argumentCount}$buffer'; | 443 String suffix = '\$${selector.argumentCount}$buffer'; |
| 433 // We don't mangle the closure invoking function name because it | 444 // We don't mangle the closure invoking function name because it |
| 434 // is generated by string concatenation in applyFunction from | 445 // is generated by string concatenation in applyFunction from |
| 435 // js_helper.dart. | 446 // js_helper.dart. We potentially shorten the prefix though. |
| 436 if (selector.isClosureCall()) { | 447 if (selector.isClosureCall()) { |
| 437 return "$name$suffix"; | 448 return "$callPrefix$suffix"; |
| 438 } else { | 449 } else { |
| 439 String proposedName = privateName(selector.library, name); | 450 String proposedName = privateName(selector.library, name); |
| 440 return getMappedInstanceName('$proposedName$suffix'); | 451 return getMappedInstanceName('$proposedName$suffix'); |
| 441 } | 452 } |
| 442 } | 453 } |
| 443 } | 454 } |
| 444 | 455 |
| 445 /** | 456 /** |
| 446 * Returns the internal name used for an invocation mirror of this selector. | 457 * Returns the internal name used for an invocation mirror of this selector. |
| 447 */ | 458 */ |
| (...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1385 if (!first) { | 1396 if (!first) { |
| 1386 sb.write('_'); | 1397 sb.write('_'); |
| 1387 } | 1398 } |
| 1388 sb.write('_'); | 1399 sb.write('_'); |
| 1389 visit(link.head); | 1400 visit(link.head); |
| 1390 first = true; | 1401 first = true; |
| 1391 } | 1402 } |
| 1392 } | 1403 } |
| 1393 } | 1404 } |
| 1394 } | 1405 } |
| OLD | NEW |