OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 * Support for interoperating with JavaScript. | 6 * Support for interoperating with JavaScript. |
7 * | 7 * |
8 * This library provides access to JavaScript objects from Dart, allowing | 8 * This library provides access to JavaScript objects from Dart, allowing |
9 * Dart code to get and set properties, and call methods of JavaScript objects | 9 * Dart code to get and set properties, and call methods of JavaScript objects |
10 * and invoke JavaScript functions. The library takes care of converting | 10 * and invoke JavaScript functions. The library takes care of converting |
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 _isExternal(declaration)) { | 635 _isExternal(declaration)) { |
636 addMemberHelper(declaration, jsLibraryName, sb); | 636 addMemberHelper(declaration, jsLibraryName, sb); |
637 } | 637 } |
638 } else if (declaration is mirrors.ClassMirror) { | 638 } else if (declaration is mirrors.ClassMirror) { |
639 mirrors.ClassMirror clazz = declaration; | 639 mirrors.ClassMirror clazz = declaration; |
640 var isDom = dartLibrary ? hasDomName(clazz) : false; | 640 var isDom = dartLibrary ? hasDomName(clazz) : false; |
641 var isJsInterop = _hasJsName(clazz); | 641 var isJsInterop = _hasJsName(clazz); |
642 if (isDom || isJsInterop) { | 642 if (isDom || isJsInterop) { |
643 // TODO(jacobr): verify class implements JavaScriptObject. | 643 // TODO(jacobr): verify class implements JavaScriptObject. |
644 var className = mirrors.MirrorSystem.getName(clazz.simpleName); | 644 var className = mirrors.MirrorSystem.getName(clazz.simpleName); |
645 bool isPrivate = className.startsWith('_'); | 645 bool isPrivateUserDefinedClass = |
646 className.startsWith('_') && !dartLibrary; | |
646 var classNameImpl = '${className}Impl'; | 647 var classNameImpl = '${className}Impl'; |
647 var sbPatch = new StringBuffer(); | 648 var sbPatch = new StringBuffer(); |
648 if (isJsInterop) { | 649 if (isJsInterop) { |
649 String jsClassName = _getJsMemberName(clazz); | 650 String jsClassName = _getJsMemberName(clazz); |
650 | 651 |
651 jsInterfaceTypes.add(clazz); | 652 jsInterfaceTypes.add(clazz); |
652 clazz.declarations.forEach((name, declaration) { | 653 clazz.declarations.forEach((name, declaration) { |
653 if (declaration is! mirrors.MethodMirror || | 654 if (declaration is! mirrors.MethodMirror || |
654 !_isExternal(declaration)) return; | 655 !_isExternal(declaration)) return; |
655 if (declaration.isFactoryConstructor && _isAnonymousClass(clazz)) { | 656 if (declaration.isFactoryConstructor && _isAnonymousClass(clazz)) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
708 : jsClassName, | 709 : jsClassName, |
709 sbPatch, | 710 sbPatch, |
710 isStatic: true); | 711 isStatic: true); |
711 } | 712 } |
712 }); | 713 }); |
713 } | 714 } |
714 if (isDom) { | 715 if (isDom) { |
715 sbPatch.write( | 716 sbPatch.write( |
716 " static Type get instanceRuntimeType => ${classNameImpl};\n"); | 717 " static Type get instanceRuntimeType => ${classNameImpl};\n"); |
717 } | 718 } |
718 if (isPrivate) { | 719 if (isPrivateUserDefinedClass) { |
terry
2016/08/04 13:50:33
I'm assuming this should not happen? Are there cl
terry
2016/08/04 13:58:21
Looks like there are classes in dart: with undersc
| |
719 sb.write(""" | 720 sb.write(""" |
720 class ${escapePrivateClassPrefix}${className} implements $className {} | 721 class ${escapePrivateClassPrefix}${className} implements $className {} |
721 """); | 722 """); |
722 } | 723 } |
723 | 724 |
724 if (sbPatch.isNotEmpty) { | 725 if (sbPatch.isNotEmpty) { |
725 var typeVariablesClause = ''; | 726 var typeVariablesClause = ''; |
726 if (!clazz.typeVariables.isEmpty) { | 727 if (!clazz.typeVariables.isEmpty) { |
727 typeVariablesClause = | 728 typeVariablesClause = |
728 '<${clazz.typeVariables.map((m) => mirrors.MirrorSystem.getName( m.simpleName)).join(',')}>'; | 729 '<${clazz.typeVariables.map((m) => mirrors.MirrorSystem.getName( m.simpleName)).join(',')}>'; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
803 | 804 |
804 var implements = <String>[]; | 805 var implements = <String>[]; |
805 var implementsArray = <String>[]; | 806 var implementsArray = <String>[]; |
806 var implementsDom = <String>[]; | 807 var implementsDom = <String>[]; |
807 var listMirror = mirrors.reflectType(List); | 808 var listMirror = mirrors.reflectType(List); |
808 var functionMirror = mirrors.reflectType(Function); | 809 var functionMirror = mirrors.reflectType(Function); |
809 var jsObjectMirror = mirrors.reflectType(JSObject); | 810 var jsObjectMirror = mirrors.reflectType(JSObject); |
810 | 811 |
811 for (var typeMirror in jsInterfaceTypes) { | 812 for (var typeMirror in jsInterfaceTypes) { |
812 mirrors.LibraryMirror libraryMirror = typeMirror.owner; | 813 mirrors.LibraryMirror libraryMirror = typeMirror.owner; |
814 var location = libraryMirror.location; | |
815 var dartLibrary = location != null && location.sourceUri.scheme == 'dart'; | |
816 | |
813 var prefixName; | 817 var prefixName; |
814 if (libraryPrefixes.containsKey(libraryMirror)) { | 818 if (libraryPrefixes.containsKey(libraryMirror)) { |
815 prefixName = libraryPrefixes[libraryMirror]; | 819 prefixName = libraryPrefixes[libraryMirror]; |
816 } else { | 820 } else { |
817 var basePrefixName = | 821 var basePrefixName = |
818 mirrors.MirrorSystem.getName(libraryMirror.simpleName); | 822 mirrors.MirrorSystem.getName(libraryMirror.simpleName); |
819 basePrefixName = basePrefixName.replaceAll('.', '_'); | 823 basePrefixName = basePrefixName.replaceAll('.', '_'); |
820 if (basePrefixName.isEmpty) basePrefixName = "lib"; | 824 if (basePrefixName.isEmpty) basePrefixName = "lib"; |
821 prefixName = basePrefixName; | 825 prefixName = basePrefixName; |
822 var i = 1; | 826 var i = 1; |
823 while (prefixNames.contains(prefixName)) { | 827 while (prefixNames.contains(prefixName)) { |
824 prefixName = '$basePrefixName$i'; | 828 prefixName = '$basePrefixName$i'; |
825 i++; | 829 i++; |
826 } | 830 } |
827 prefixNames.add(prefixName); | 831 prefixNames.add(prefixName); |
828 libraryPrefixes[libraryMirror] = prefixName; | 832 libraryPrefixes[libraryMirror] = prefixName; |
829 } | 833 } |
830 var isArray = typeMirror.isSubtypeOf(listMirror); | 834 var isArray = typeMirror.isSubtypeOf(listMirror); |
831 var isFunction = typeMirror.isSubtypeOf(functionMirror); | 835 var isFunction = typeMirror.isSubtypeOf(functionMirror); |
832 var isJSObject = typeMirror.isSubtypeOf(jsObjectMirror); | 836 var isJSObject = typeMirror.isSubtypeOf(jsObjectMirror); |
833 var className = mirrors.MirrorSystem.getName(typeMirror.simpleName); | 837 var className = mirrors.MirrorSystem.getName(typeMirror.simpleName); |
834 var isPrivate = className.startsWith('_'); | 838 var isPrivateUserDefinedClass = className.startsWith('_') && !dartLibrary; |
835 if (isPrivate) className = '${escapePrivateClassPrefix}${className}'; | 839 if (isPrivateUserDefinedClass) |
840 className = '${escapePrivateClassPrefix}${className}'; | |
836 var fullName = '${prefixName}.${className}'; | 841 var fullName = '${prefixName}.${className}'; |
837 (isArray ? implementsArray : implements).add(fullName); | 842 (isArray ? implementsArray : implements).add(fullName); |
838 if (!isArray && !isFunction && !isJSObject) { | 843 if (!isArray && !isFunction && !isJSObject) { |
839 // For DOM classes we need to be a bit more conservative at tagging them | 844 // For DOM classes we need to be a bit more conservative at tagging them |
840 // as implementing JS inteorp classes risks strange unintended | 845 // as implementing JS inteorp classes risks strange unintended |
841 // consequences as unrleated code may have instanceof checks. Checking | 846 // consequences as unrleated code may have instanceof checks. Checking |
842 // for isJSObject ensures we do not accidentally pull in existing | 847 // for isJSObject ensures we do not accidentally pull in existing |
843 // dart:html classes as they all have JSObject as a base class. | 848 // dart:html classes as they all have JSObject as a base class. |
844 // Note that methods from these classes can still be called on a | 849 // Note that methods from these classes can still be called on a |
845 // dart:html instance but checked mode type checks will fail. This is | 850 // dart:html instance but checked mode type checks will fail. This is |
(...skipping 843 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1689 } else { | 1694 } else { |
1690 var ret = _interopCaptureThisExpando[f]; | 1695 var ret = _interopCaptureThisExpando[f]; |
1691 if (ret == null) { | 1696 if (ret == null) { |
1692 // TODO(jacobr): we could optimize this. | 1697 // TODO(jacobr): we could optimize this. |
1693 ret = JSFunction._createWithThis(f); | 1698 ret = JSFunction._createWithThis(f); |
1694 _interopCaptureThisExpando[f] = ret; | 1699 _interopCaptureThisExpando[f] = ret; |
1695 } | 1700 } |
1696 return ret; | 1701 return ret; |
1697 } | 1702 } |
1698 } | 1703 } |
OLD | NEW |