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