| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library native; | 5 library native; |
| 6 | 6 |
| 7 import 'dart:uri'; | 7 import 'dart:uri'; |
| 8 import 'dart2jslib.dart' hide SourceString; | 8 import 'dart2jslib.dart' hide SourceString; |
| 9 import 'elements/elements.dart'; | 9 import 'elements/elements.dart'; |
| 10 import 'js_backend/js_backend.dart'; | 10 import 'js_backend/js_backend.dart'; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 } | 28 } |
| 29 | 29 |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * This could be an abstract class but we use it as a stub for the dart_backend. | 32 * This could be an abstract class but we use it as a stub for the dart_backend. |
| 33 */ | 33 */ |
| 34 class NativeEnqueuer { | 34 class NativeEnqueuer { |
| 35 /// Initial entry point to native enqueuer. | 35 /// Initial entry point to native enqueuer. |
| 36 void processNativeClasses(Collection<LibraryElement> libraries) {} | 36 void processNativeClasses(Collection<LibraryElement> libraries) {} |
| 37 | 37 |
| 38 /// Notification of a main Enqueuer worklist element. For methods, adds |
| 39 /// information from metadata attributes, and computes types instantiated due |
| 40 /// to calling the method. |
| 38 void registerElement(Element element) {} | 41 void registerElement(Element element) {} |
| 39 | 42 |
| 40 /// Method is a member of a native class. | 43 /// Notification of native field. Adds information from metadata attributes. |
| 41 void registerMethod(Element method) {} | 44 void registerField(Element field) {} |
| 42 | 45 |
| 43 /// Compute types instantiated due to getting a native field. | 46 /// Computes types instantiated due to getting a native field. |
| 44 void registerFieldLoad(Element field) {} | 47 void registerFieldLoad(Element field) {} |
| 45 | 48 |
| 46 /// Compute types instantiated due to setting a native field. | 49 /// Computes types instantiated due to setting a native field. |
| 47 void registerFieldStore(Element field) {} | 50 void registerFieldStore(Element field) {} |
| 48 | 51 |
| 49 /** | 52 /** |
| 50 * Handles JS-calls, which can be an instantiation point for types. | 53 * Handles JS-calls, which can be an instantiation point for types. |
| 51 * | 54 * |
| 52 * For example, the following code instantiates and returns native classes | 55 * For example, the following code instantiates and returns native classes |
| 53 * that are `_DOMWindowImpl` or a subtype. | 56 * that are `_DOMWindowImpl` or a subtype. |
| 54 * | 57 * |
| 55 * JS('_DOMWindowImpl', 'window') | 58 * JS('_DOMWindowImpl', 'window') |
| 56 * | 59 * |
| (...skipping 29 matching lines...) Expand all Loading... |
| 86 final queue = new Queue(); | 89 final queue = new Queue(); |
| 87 bool flushing = false; | 90 bool flushing = false; |
| 88 | 91 |
| 89 | 92 |
| 90 final Enqueuer world; | 93 final Enqueuer world; |
| 91 final Compiler compiler; | 94 final Compiler compiler; |
| 92 final bool enableLiveTypeAnalysis; | 95 final bool enableLiveTypeAnalysis; |
| 93 | 96 |
| 94 ClassElement _annotationCreatesClass; | 97 ClassElement _annotationCreatesClass; |
| 95 ClassElement _annotationReturnsClass; | 98 ClassElement _annotationReturnsClass; |
| 99 ClassElement _annotationJsNameClass; |
| 96 | 100 |
| 97 /// Subclasses of [NativeEnqueuerBase] are constructed by the backend. | 101 /// Subclasses of [NativeEnqueuerBase] are constructed by the backend. |
| 98 NativeEnqueuerBase(this.world, this.compiler, this.enableLiveTypeAnalysis); | 102 NativeEnqueuerBase(this.world, this.compiler, this.enableLiveTypeAnalysis); |
| 99 | 103 |
| 100 void processNativeClasses(Collection<LibraryElement> libraries) { | 104 void processNativeClasses(Collection<LibraryElement> libraries) { |
| 101 libraries.forEach(processNativeClassesInLibrary); | 105 libraries.forEach(processNativeClassesInLibrary); |
| 102 if (!enableLiveTypeAnalysis) { | 106 if (!enableLiveTypeAnalysis) { |
| 103 nativeClasses.forEach((c) => enqueueClass(c, 'forced')); | 107 nativeClasses.forEach((c) => enqueueClass(c, 'forced')); |
| 104 flushQueue(); | 108 flushQueue(); |
| 105 } | 109 } |
| 106 } | 110 } |
| 107 | 111 |
| 108 void processNativeClassesInLibrary(LibraryElement library) { | 112 void processNativeClassesInLibrary(LibraryElement library) { |
| 109 // Use implementation to ensure the inclusion of injected members. | 113 // Use implementation to ensure the inclusion of injected members. |
| 110 library.implementation.forEachLocalMember((Element element) { | 114 library.implementation.forEachLocalMember((Element element) { |
| 111 if (element.kind == ElementKind.CLASS) { | 115 if (element.kind == ElementKind.CLASS) { |
| 112 ClassElement classElement = element; | 116 ClassElement classElement = element; |
| 113 if (classElement.isNative()) { | 117 if (classElement.isNative()) { |
| 114 nativeClasses.add(classElement); | 118 nativeClasses.add(classElement); |
| 115 unusedClasses.add(classElement); | 119 unusedClasses.add(classElement); |
| 116 | 120 |
| 117 // Resolve class to ensure the class has valid inheritance info. | 121 // Resolve class to ensure the class has valid inheritance info. |
| 118 classElement.ensureResolved(compiler); | 122 classElement.ensureResolved(compiler); |
| 119 } | 123 } |
| 120 } | 124 } |
| 121 }); | 125 }); |
| 122 } | 126 } |
| 123 | 127 |
| 124 ClassElement get annotationCreatesClass { | 128 ClassElement get annotationCreatesClass { |
| 125 if (_annotationCreatesClass == null) findAnnotationClasses(); | 129 findAnnotationClasses(); |
| 126 return _annotationCreatesClass; | 130 return _annotationCreatesClass; |
| 127 } | 131 } |
| 128 | 132 |
| 129 ClassElement get annotationReturnsClass { | 133 ClassElement get annotationReturnsClass { |
| 130 if (_annotationReturnsClass == null) findAnnotationClasses(); | 134 findAnnotationClasses(); |
| 131 return _annotationReturnsClass; | 135 return _annotationReturnsClass; |
| 132 } | 136 } |
| 133 | 137 |
| 138 ClassElement get annotationJsNameClass { |
| 139 findAnnotationClasses(); |
| 140 return _annotationJsNameClass; |
| 141 } |
| 142 |
| 134 void findAnnotationClasses() { | 143 void findAnnotationClasses() { |
| 144 if (_annotationCreatesClass != null) return; |
| 135 ClassElement find(name) { | 145 ClassElement find(name) { |
| 136 Element e = compiler.findHelper(name); | 146 Element e = compiler.findHelper(name); |
| 137 if (e == null || e is! ClassElement) { | 147 if (e == null || e is! ClassElement) { |
| 138 compiler.cancel("Could not find implementation class '${name}'"); | 148 compiler.cancel("Could not find implementation class '${name}'"); |
| 139 } | 149 } |
| 140 return e; | 150 return e; |
| 141 } | 151 } |
| 142 _annotationCreatesClass = find(const SourceString('Creates')); | 152 _annotationCreatesClass = find(const SourceString('Creates')); |
| 143 _annotationReturnsClass = find(const SourceString('Returns')); | 153 _annotationReturnsClass = find(const SourceString('Returns')); |
| 154 _annotationJsNameClass = find(const SourceString('JSName')); |
| 144 } | 155 } |
| 145 | 156 |
| 157 /// Returns the JSName annotation string or `null` if no JSName annotation is |
| 158 /// present. |
| 159 String findJsNameFromAnnotation(Element element) { |
| 160 String name = null; |
| 161 ClassElement annotationClass = annotationJsNameClass; |
| 162 for (Link<MetadataAnnotation> link = element.metadata; |
| 163 !link.isEmpty; |
| 164 link = link.tail) { |
| 165 MetadataAnnotation annotation = link.head.ensureResolved(compiler); |
| 166 var value = annotation.value; |
| 167 if (value is! ConstructedConstant) continue; |
| 168 if (value.type is! InterfaceType) continue; |
| 169 if (!identical(value.type.element, annotationClass)) continue; |
| 170 |
| 171 var fields = value.fields; |
| 172 // TODO(sra): Better validation of the constant. |
| 173 if (fields.length != 1 || fields[0] is! StringConstant) { |
| 174 PartialMetadataAnnotation partial = annotation; |
| 175 compiler.cancel( |
| 176 'Annotations needs one string: ${partial.parseNode(compiler)}'); |
| 177 } |
| 178 String specString = fields[0].toDartString().slowToString(); |
| 179 if (name == null) { |
| 180 name = specString; |
| 181 } else { |
| 182 PartialMetadataAnnotation partial = annotation; |
| 183 compiler.cancel( |
| 184 'Too many JSName annotations: ${partial.parseNode(compiler)}'); |
| 185 } |
| 186 } |
| 187 return name; |
| 188 } |
| 146 | 189 |
| 147 enqueueClass(ClassElement classElement, cause) { | 190 enqueueClass(ClassElement classElement, cause) { |
| 148 assert(unusedClasses.contains(classElement)); | 191 assert(unusedClasses.contains(classElement)); |
| 149 unusedClasses.remove(classElement); | 192 unusedClasses.remove(classElement); |
| 150 pendingClasses.add(classElement); | 193 pendingClasses.add(classElement); |
| 151 queue.add(() { processClass(classElement, cause); }); | 194 queue.add(() { processClass(classElement, cause); }); |
| 152 } | 195 } |
| 153 | 196 |
| 154 void flushQueue() { | 197 void flushQueue() { |
| 155 if (flushing) return; | 198 if (flushing) return; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 175 | 218 |
| 176 if (firstTime) { | 219 if (firstTime) { |
| 177 queue.add(onFirstNativeClass); | 220 queue.add(onFirstNativeClass); |
| 178 } | 221 } |
| 179 } | 222 } |
| 180 | 223 |
| 181 registerElement(Element element) { | 224 registerElement(Element element) { |
| 182 if (element.isFunction()) return registerMethod(element); | 225 if (element.isFunction()) return registerMethod(element); |
| 183 } | 226 } |
| 184 | 227 |
| 228 registerField(Element element) { |
| 229 if (element.enclosingElement.isNative()) { |
| 230 setNativeName(element); |
| 231 } |
| 232 } |
| 233 |
| 185 registerMethod(Element method) { | 234 registerMethod(Element method) { |
| 186 if (isNativeMethod(method)) { | 235 if (isNativeMethod(method)) { |
| 236 setNativeName(method); |
| 187 processNativeBehavior( | 237 processNativeBehavior( |
| 188 NativeBehavior.ofMethod(method, compiler), | 238 NativeBehavior.ofMethod(method, compiler), |
| 189 method); | 239 method); |
| 190 flushQueue(); | 240 flushQueue(); |
| 191 } | 241 } |
| 192 } | 242 } |
| 193 | 243 |
| 244 /// Sets the native name of [element], either from an annotation, or |
| 245 /// defaulting to the Dart name. |
| 246 void setNativeName(Element element) { |
| 247 String name = findJsNameFromAnnotation(element); |
| 248 if (name == null) name = element.name.slowToString(); |
| 249 element.setNative(name); |
| 250 } |
| 251 |
| 194 bool isNativeMethod(Element element) { | 252 bool isNativeMethod(Element element) { |
| 195 if (!element.getLibrary().canUseNative) return false; | 253 if (!element.getLibrary().canUseNative) return false; |
| 196 // Native method? | 254 // Native method? |
| 197 return compiler.withCurrentElement(element, () { | 255 return compiler.withCurrentElement(element, () { |
| 198 Node node = element.parseNode(compiler); | 256 Node node = element.parseNode(compiler); |
| 199 if (node is! FunctionExpression) return false; | 257 if (node is! FunctionExpression) return false; |
| 200 node = node.body; | 258 node = node.body; |
| 201 Token token = node.getBeginToken(); | 259 Token token = node.getBeginToken(); |
| 202 if (identical(token.stringValue, 'native')) return true; | 260 if (identical(token.stringValue, 'native')) return true; |
| 203 return false; | 261 return false; |
| (...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 listener.endLiteralString(0); | 717 listener.endLiteralString(0); |
| 660 token = token.next; | 718 token = token.next; |
| 661 } | 719 } |
| 662 listener.endReturnStatement(hasExpression, begin, token); | 720 listener.endReturnStatement(hasExpression, begin, token); |
| 663 // TODO(ngeoffray): expect a ';'. | 721 // TODO(ngeoffray): expect a ';'. |
| 664 // Currently there are method with both native marker and Dart body. | 722 // Currently there are method with both native marker and Dart body. |
| 665 return token.next; | 723 return token.next; |
| 666 } | 724 } |
| 667 | 725 |
| 668 SourceString checkForNativeClass(ElementListener listener) { | 726 SourceString checkForNativeClass(ElementListener listener) { |
| 669 SourceString nativeName; | 727 SourceString nativeTagInfo; |
| 670 Node node = listener.nodes.head; | 728 Node node = listener.nodes.head; |
| 671 if (node != null | 729 if (node != null |
| 672 && node.asIdentifier() != null | 730 && node.asIdentifier() != null |
| 673 && node.asIdentifier().source.stringValue == 'native') { | 731 && node.asIdentifier().source.stringValue == 'native') { |
| 674 nativeName = node.asIdentifier().token.next.value; | 732 nativeTagInfo = node.asIdentifier().token.next.value; |
| 675 listener.popNode(); | 733 listener.popNode(); |
| 676 } | 734 } |
| 677 return nativeName; | 735 return nativeTagInfo; |
| 678 } | 736 } |
| 679 | 737 |
| 680 bool isOverriddenMethod(FunctionElement element, | 738 bool isOverriddenMethod(FunctionElement element, |
| 681 ClassElement cls, | 739 ClassElement cls, |
| 682 NativeEmitter nativeEmitter) { | 740 NativeEmitter nativeEmitter) { |
| 683 List<ClassElement> subtypes = nativeEmitter.subtypes[cls]; | 741 List<ClassElement> subtypes = nativeEmitter.subtypes[cls]; |
| 684 if (subtypes == null) return false; | 742 if (subtypes == null) return false; |
| 685 for (ClassElement subtype in subtypes) { | 743 for (ClassElement subtype in subtypes) { |
| 686 if (subtype.lookupLocalMember(element.name) != null) return true; | 744 if (subtype.lookupLocalMember(element.name) != null) return true; |
| 687 } | 745 } |
| 688 return false; | 746 return false; |
| 689 } | 747 } |
| 690 | 748 |
| 691 final RegExp nativeRedirectionRegExp = new RegExp(r'^[a-zA-Z][a-zA-Z_$0-9]*$'); | 749 final RegExp nativeRedirectionRegExp = new RegExp(r'^[a-zA-Z][a-zA-Z_$0-9]*$'); |
| 692 | 750 |
| 693 void handleSsaNative(SsaBuilder builder, Expression nativeBody) { | 751 void handleSsaNative(SsaBuilder builder, Expression nativeBody) { |
| 694 Compiler compiler = builder.compiler; | 752 Compiler compiler = builder.compiler; |
| 695 FunctionElement element = builder.work.element; | 753 FunctionElement element = builder.work.element; |
| 696 element.setNative(); | |
| 697 NativeEmitter nativeEmitter = builder.emitter.nativeEmitter; | 754 NativeEmitter nativeEmitter = builder.emitter.nativeEmitter; |
| 698 // If what we're compiling is a getter named 'typeName' and the native | 755 // If what we're compiling is a getter named 'typeName' and the native |
| 699 // class is named 'DOMType', we generate a call to the typeNameOf | 756 // class is named 'DOMType', we generate a call to the typeNameOf |
| 700 // function attached on the isolate. | 757 // function attached on the isolate. |
| 701 // The DOM classes assume that their 'typeName' property, which is | 758 // The DOM classes assume that their 'typeName' property, which is |
| 702 // not a JS property on the DOM types, returns the type name. | 759 // not a JS property on the DOM types, returns the type name. |
| 703 if (element.name == const SourceString('typeName') | 760 if (element.name == const SourceString('typeName') |
| 704 && element.isGetter() | 761 && element.isGetter() |
| 705 && nativeEmitter.toNativeName(element.getEnclosingClass()) == 'DOMType') { | 762 && nativeEmitter.toNativeName(element.getEnclosingClass()) == 'DOMType') { |
| 706 Element helper = | 763 Element helper = |
| 707 compiler.findHelper(const SourceString('getTypeNameOf')); | 764 compiler.findHelper(const SourceString('getTypeNameOf')); |
| 708 builder.pushInvokeHelper1(helper, builder.localsHandler.readThis()); | 765 builder.pushInvokeHelper1(helper, builder.localsHandler.readThis()); |
| 709 builder.close(new HReturn(builder.pop())).addSuccessor(builder.graph.exit); | 766 builder.close(new HReturn(builder.pop())).addSuccessor(builder.graph.exit); |
| 710 } | 767 } |
| 711 | 768 |
| 712 HInstruction convertDartClosure(Element parameter, FunctionType type) { | 769 HInstruction convertDartClosure(Element parameter, FunctionType type) { |
| 713 HInstruction local = builder.localsHandler.readLocal(parameter); | 770 HInstruction local = builder.localsHandler.readLocal(parameter); |
| 714 Constant arityConstant = | 771 Constant arityConstant = |
| 715 builder.constantSystem.createInt(type.computeArity()); | 772 builder.constantSystem.createInt(type.computeArity()); |
| 716 HInstruction arity = builder.graph.addConstant(arityConstant); | 773 HInstruction arity = builder.graph.addConstant(arityConstant); |
| 717 // TODO(ngeoffray): For static methods, we could pass a method with a | 774 // TODO(ngeoffray): For static methods, we could pass a method with a |
| 718 // defined arity. | 775 // defined arity. |
| 719 Element helper = builder.interceptors.getClosureConverter(); | 776 Element helper = builder.interceptors.getClosureConverter(); |
| 720 builder.pushInvokeHelper2(helper, local, arity); | 777 builder.pushInvokeHelper2(helper, local, arity); |
| 721 HInstruction closure = builder.pop(); | 778 HInstruction closure = builder.pop(); |
| 722 return closure; | 779 return closure; |
| 723 } | 780 } |
| 724 | 781 |
| 725 // Check which pattern this native method follows: | 782 // Check which pattern this native method follows: |
| 726 // 1) foo() native; hasBody = false, isRedirecting = false | 783 // 1) foo() native; |
| 727 // 2) foo() native "bar"; hasBody = false, isRedirecting = true | 784 // hasBody = false, isRedirecting = false |
| 728 // 3) foo() native "return 42"; hasBody = true, isRedirecting = false | 785 // 2) foo() native "bar"; |
| 786 // hasBody = false, isRedirecting = true, no longer supported. |
| 787 // 3) foo() native "return 42"; |
| 788 // hasBody = true, isRedirecting = false |
| 729 bool hasBody = false; | 789 bool hasBody = false; |
| 730 bool isRedirecting = false; | 790 String nativeMethodName = element.nativeName(); |
| 731 String nativeMethodName = element.name.slowToString(); | |
| 732 if (nativeBody != null) { | 791 if (nativeBody != null) { |
| 733 LiteralString jsCode = nativeBody.asLiteralString(); | 792 LiteralString jsCode = nativeBody.asLiteralString(); |
| 734 String str = jsCode.dartString.slowToString(); | 793 String str = jsCode.dartString.slowToString(); |
| 735 if (nativeRedirectionRegExp.hasMatch(str)) { | 794 if (nativeRedirectionRegExp.hasMatch(str)) { |
| 736 nativeMethodName = str; | 795 compiler.cancel("Deprecated syntax, use @JSName('name') instead.", |
| 737 isRedirecting = true; | 796 node: nativeBody); |
| 738 nativeEmitter.addRedirectingMethod(element, nativeMethodName); | |
| 739 } else { | |
| 740 hasBody = true; | |
| 741 } | 797 } |
| 798 hasBody = true; |
| 742 } | 799 } |
| 743 | 800 |
| 744 if (!hasBody) { | 801 if (!hasBody) { |
| 745 nativeEmitter.nativeMethods.add(element); | 802 nativeEmitter.nativeMethods.add(element); |
| 746 } | 803 } |
| 747 | 804 |
| 748 FunctionSignature parameters = element.computeSignature(builder.compiler); | 805 FunctionSignature parameters = element.computeSignature(builder.compiler); |
| 749 if (!hasBody) { | 806 if (!hasBody) { |
| 750 List<String> arguments = <String>[]; | 807 List<String> arguments = <String>[]; |
| 751 List<HInstruction> inputs = <HInstruction>[]; | 808 List<HInstruction> inputs = <HInstruction>[]; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 845 String parameters) { | 902 String parameters) { |
| 846 buffer.add(" if (Object.getPrototypeOf(this).hasOwnProperty"); | 903 buffer.add(" if (Object.getPrototypeOf(this).hasOwnProperty"); |
| 847 buffer.add("('$methodName')) {\n"); | 904 buffer.add("('$methodName')) {\n"); |
| 848 buffer.add(" $code"); | 905 buffer.add(" $code"); |
| 849 buffer.add(" } else {\n"); | 906 buffer.add(" } else {\n"); |
| 850 buffer.add(" return Object.prototype.$methodName.call(this"); | 907 buffer.add(" return Object.prototype.$methodName.call(this"); |
| 851 buffer.add(parameters == '' ? '' : ', $parameters'); | 908 buffer.add(parameters == '' ? '' : ', $parameters'); |
| 852 buffer.add(");\n"); | 909 buffer.add(");\n"); |
| 853 buffer.add(" }\n"); | 910 buffer.add(" }\n"); |
| 854 } | 911 } |
| OLD | NEW |