| 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 * Names are generated through three stages: | 10 * Names are generated through three stages: |
| (...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 } | 627 } |
| 628 | 628 |
| 629 /// Annotated name for [method] encoding arity and named parameters. | 629 /// Annotated name for [method] encoding arity and named parameters. |
| 630 jsAst.Name instanceMethodName(FunctionElement method) { | 630 jsAst.Name instanceMethodName(FunctionElement method) { |
| 631 if (method.isGenerativeConstructorBody) { | 631 if (method.isGenerativeConstructorBody) { |
| 632 return constructorBodyName(method); | 632 return constructorBodyName(method); |
| 633 } | 633 } |
| 634 return invocationName(new Selector.fromElement(method)); | 634 return invocationName(new Selector.fromElement(method)); |
| 635 } | 635 } |
| 636 | 636 |
| 637 String _jsNameHelper(Element e) { |
| 638 if (e.jsInteropName != null && e.jsInteropName.isNotEmpty) |
| 639 return e.jsInteropName; |
| 640 return e.isLibrary ? 'self' : e.name; |
| 641 } |
| 642 |
| 643 /// Returns a JavaScript path specifying the context in which |
| 644 /// [element.fixedBackendName] should be evaluated. Only applicable for |
| 645 /// elements using typed JavaScript interop. |
| 646 /// For example: fixedBackendPath for the static method createMap in the |
| 647 /// Map class of the goog.map JavaScript library would have path |
| 648 /// "goog.maps.Map". |
| 649 String fixedBackendPath(Element element) { |
| 650 if (!element.isJsInterop) return null; |
| 651 if (element.isInstanceMember) return 'this'; |
| 652 if (element.isConstructor) return fixedBackendPath(element.enclosingClass); |
| 653 if (element.isLibrary) return 'self'; |
| 654 var sb = new StringBuffer(); |
| 655 sb..write(_jsNameHelper(element.library)); |
| 656 |
| 657 if (element.enclosingClass != null && element.enclosingClass != element) { |
| 658 sb..write('.')..write(_jsNameHelper(element.enclosingClass)); |
| 659 } |
| 660 return sb.toString(); |
| 661 } |
| 662 |
| 637 /// Returns the annotated name for a variant of `call`. | 663 /// Returns the annotated name for a variant of `call`. |
| 638 /// The result has the form: | 664 /// The result has the form: |
| 639 /// | 665 /// |
| 640 /// call$<N>$namedParam1...$namedParam<M> | 666 /// call$<N>$namedParam1...$namedParam<M> |
| 641 /// | 667 /// |
| 642 /// This name cannot be minified because it is generated by string | 668 /// This name cannot be minified because it is generated by string |
| 643 /// concatenation at runtime, by applyFunction in js_helper.dart. | 669 /// concatenation at runtime, by applyFunction in js_helper.dart. |
| 644 jsAst.Name deriveCallMethodName(List<String> suffix) { | 670 jsAst.Name deriveCallMethodName(List<String> suffix) { |
| 645 // TODO(asgerf): Avoid clashes when named parameters contain $ symbols. | 671 // TODO(asgerf): Avoid clashes when named parameters contain $ symbols. |
| 646 return new StringBackedName('$callPrefix\$${suffix.join(r'$')}'); | 672 return new StringBackedName('$callPrefix\$${suffix.join(r'$')}'); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 return _disambiguateGlobal(element); | 782 return _disambiguateGlobal(element); |
| 757 } | 783 } |
| 758 | 784 |
| 759 /** | 785 /** |
| 760 * Returns the JavaScript property name used to store an instance field. | 786 * Returns the JavaScript property name used to store an instance field. |
| 761 */ | 787 */ |
| 762 jsAst.Name instanceFieldPropertyName(FieldElement element) { | 788 jsAst.Name instanceFieldPropertyName(FieldElement element) { |
| 763 ClassElement enclosingClass = element.enclosingClass; | 789 ClassElement enclosingClass = element.enclosingClass; |
| 764 | 790 |
| 765 if (element.hasFixedBackendName) { | 791 if (element.hasFixedBackendName) { |
| 766 // Certain native fields must be given a specific name. Native names must | |
| 767 // not contain '$'. We rely on this to avoid clashes. | |
| 768 assert(enclosingClass.isNative && | |
| 769 !element.fixedBackendName.contains(r'$')); | |
| 770 | |
| 771 return new StringBackedName(element.fixedBackendName); | 792 return new StringBackedName(element.fixedBackendName); |
| 772 } | 793 } |
| 773 | 794 |
| 774 // Instances of BoxFieldElement are special. They are already created with | 795 // Instances of BoxFieldElement are special. They are already created with |
| 775 // a unique and safe name. However, as boxes are not really instances of | 796 // a unique and safe name. However, as boxes are not really instances of |
| 776 // classes, the usual naming scheme that tries to avoid name clashes with | 797 // classes, the usual naming scheme that tries to avoid name clashes with |
| 777 // super classes does not apply. We still do not mark the name as a | 798 // super classes does not apply. We still do not mark the name as a |
| 778 // fixedBackendName, as we want to allow other namers to do something more | 799 // fixedBackendName, as we want to allow other namers to do something more |
| 779 // clever with them. | 800 // clever with them. |
| 780 if (element is BoxFieldElement) { | 801 if (element is BoxFieldElement) { |
| (...skipping 1212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1993 } | 2014 } |
| 1994 } | 2015 } |
| 1995 } | 2016 } |
| 1996 } | 2017 } |
| 1997 | 2018 |
| 1998 enum NamingScope { | 2019 enum NamingScope { |
| 1999 global, | 2020 global, |
| 2000 instance, | 2021 instance, |
| 2001 constant | 2022 constant |
| 2002 } | 2023 } |
| OLD | NEW |