| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Analysis to determine how to generate code for typed JavaScript interop. | 5 /// Analysis to determine how to generate code for typed JavaScript interop. |
| 6 library compiler.src.js_backend.js_interop_analysis; | 6 library compiler.src.js_backend.js_interop_analysis; |
| 7 | 7 |
| 8 import '../diagnostics/messages.dart' show MessageKind; | 8 import '../diagnostics/messages.dart' show MessageKind; |
| 9 import '../constants/values.dart' | 9 import '../constants/values.dart' |
| 10 show | 10 show |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 .forEach(processJsInteropAnnotationsInLibrary); | 55 .forEach(processJsInteropAnnotationsInLibrary); |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 void onCodegenStart() { | 59 void onCodegenStart() { |
| 60 _inCodegen = true; | 60 _inCodegen = true; |
| 61 } | 61 } |
| 62 | 62 |
| 63 void processJsInteropAnnotation(Element e) { | 63 void processJsInteropAnnotation(Element e) { |
| 64 for (MetadataAnnotation annotation in e.implementation.metadata) { | 64 for (MetadataAnnotation annotation in e.implementation.metadata) { |
| 65 ConstantValue constant = backend.compiler.constants.getConstantValue( | 65 ConstantValue constant = |
| 66 annotation.constant); | 66 backend.compiler.constants.getConstantValue(annotation.constant); |
| 67 if (constant == null || constant is! ConstructedConstantValue) continue; | 67 if (constant == null || constant is! ConstructedConstantValue) continue; |
| 68 ConstructedConstantValue constructedConstant = constant; | 68 ConstructedConstantValue constructedConstant = constant; |
| 69 if (constructedConstant.type.element == helpers.jsAnnotationClass) { | 69 if (constructedConstant.type.element == helpers.jsAnnotationClass) { |
| 70 ConstantValue value = constructedConstant.fields[nameField]; | 70 ConstantValue value = constructedConstant.fields[nameField]; |
| 71 if (value.isString) { | 71 if (value.isString) { |
| 72 StringConstantValue stringValue = value; | 72 StringConstantValue stringValue = value; |
| 73 backend.nativeData.setJsInteropName( | 73 backend.nativeData |
| 74 e, stringValue.primitiveValue.slowToString()); | 74 .setJsInteropName(e, stringValue.primitiveValue.slowToString()); |
| 75 } else { | 75 } else { |
| 76 // TODO(jacobr): report a warning if the value is not a String. | 76 // TODO(jacobr): report a warning if the value is not a String. |
| 77 backend.nativeData.setJsInteropName(e, ''); | 77 backend.nativeData.setJsInteropName(e, ''); |
| 78 } | 78 } |
| 79 enabledJsInterop = true; | 79 enabledJsInterop = true; |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 bool hasAnonymousAnnotation(Element element) { | 85 bool hasAnonymousAnnotation(Element element) { |
| 86 if (backend.helpers.jsAnonymousClass == null) return false; | 86 if (backend.helpers.jsAnonymousClass == null) return false; |
| 87 return element.metadata.any((MetadataAnnotation annotation) { | 87 return element.metadata.any((MetadataAnnotation annotation) { |
| 88 ConstantValue constant = backend.compiler.constants.getConstantValue( | 88 ConstantValue constant = |
| 89 annotation.constant); | 89 backend.compiler.constants.getConstantValue(annotation.constant); |
| 90 if (constant == null || | 90 if (constant == null || constant is! ConstructedConstantValue) |
| 91 constant is! ConstructedConstantValue) return false; | 91 return false; |
| 92 ConstructedConstantValue constructedConstant = constant; | 92 ConstructedConstantValue constructedConstant = constant; |
| 93 return constructedConstant.type.element == | 93 return constructedConstant.type.element == |
| 94 backend.helpers.jsAnonymousClass; | 94 backend.helpers.jsAnonymousClass; |
| 95 }); | 95 }); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void _checkFunctionParameters(FunctionElement fn) { | 98 void _checkFunctionParameters(FunctionElement fn) { |
| 99 if (fn.hasFunctionSignature && | 99 if (fn.hasFunctionSignature && |
| 100 fn.functionSignature.optionalParametersAreNamed) { | 100 fn.functionSignature.optionalParametersAreNamed) { |
| 101 backend.reporter.reportErrorMessage(fn, | 101 backend.reporter.reportErrorMessage( |
| 102 MessageKind.JS_INTEROP_METHOD_WITH_NAMED_ARGUMENTS, { | 102 fn, |
| 103 'method': fn.name | 103 MessageKind.JS_INTEROP_METHOD_WITH_NAMED_ARGUMENTS, |
| 104 }); | 104 {'method': fn.name}); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 void processJsInteropAnnotationsInLibrary(LibraryElement library) { | 108 void processJsInteropAnnotationsInLibrary(LibraryElement library) { |
| 109 processJsInteropAnnotation(library); | 109 processJsInteropAnnotation(library); |
| 110 library.implementation.forEachLocalMember((Element element) { | 110 library.implementation.forEachLocalMember((Element element) { |
| 111 processJsInteropAnnotation(element); | 111 processJsInteropAnnotation(element); |
| 112 if (!backend.isJsInterop(element)) return; | 112 if (!backend.isJsInterop(element)) return; |
| 113 if (element is FunctionElement) { | 113 if (element is FunctionElement) { |
| 114 _checkFunctionParameters(element); | 114 _checkFunctionParameters(element); |
| 115 } | 115 } |
| 116 | 116 |
| 117 if (!element.isClass) return; | 117 if (!element.isClass) return; |
| 118 | 118 |
| 119 ClassElement classElement = element; | 119 ClassElement classElement = element; |
| 120 | 120 |
| 121 // Skip classes that are completely unreachable. This should only happen | 121 // Skip classes that are completely unreachable. This should only happen |
| 122 // when all of jsinterop types are unreachable from main. | 122 // when all of jsinterop types are unreachable from main. |
| 123 if (!backend.compiler.world.isImplemented(classElement)) return; | 123 if (!backend.compiler.world.isImplemented(classElement)) return; |
| 124 | 124 |
| 125 if (!classElement | 125 if (!classElement.implementsInterface(helpers.jsJavaScriptObjectClass)) { |
| 126 .implementsInterface(helpers.jsJavaScriptObjectClass)) { | |
| 127 backend.reporter.reportErrorMessage(classElement, | 126 backend.reporter.reportErrorMessage(classElement, |
| 128 MessageKind.JS_INTEROP_CLASS_CANNOT_EXTEND_DART_CLASS, { | 127 MessageKind.JS_INTEROP_CLASS_CANNOT_EXTEND_DART_CLASS, { |
| 129 'cls': classElement.name, | 128 'cls': classElement.name, |
| 130 'superclass': classElement.superclass.name | 129 'superclass': classElement.superclass.name |
| 131 }); | 130 }); |
| 132 } | 131 } |
| 133 | 132 |
| 134 classElement.forEachMember( | 133 classElement.forEachMember((ClassElement classElement, Element member) { |
| 135 (ClassElement classElement, Element member) { | |
| 136 processJsInteropAnnotation(member); | 134 processJsInteropAnnotation(member); |
| 137 | 135 |
| 138 if (!member.isSynthesized && | 136 if (!member.isSynthesized && |
| 139 backend.isJsInterop(classElement) && | 137 backend.isJsInterop(classElement) && |
| 140 member is FunctionElement) { | 138 member is FunctionElement) { |
| 141 FunctionElement fn = member; | 139 FunctionElement fn = member; |
| 142 if (!fn.isExternal && !fn.isAbstract && !fn.isConstructor && | 140 if (!fn.isExternal && |
| 141 !fn.isAbstract && |
| 142 !fn.isConstructor && |
| 143 !fn.isStatic) { | 143 !fn.isStatic) { |
| 144 backend.reporter.reportErrorMessage( | 144 backend.reporter.reportErrorMessage( |
| 145 fn, | 145 fn, |
| 146 MessageKind.JS_INTEROP_CLASS_NON_EXTERNAL_MEMBER, | 146 MessageKind.JS_INTEROP_CLASS_NON_EXTERNAL_MEMBER, |
| 147 {'cls': classElement.name, 'member': member.name}); | 147 {'cls': classElement.name, 'member': member.name}); |
| 148 } | 148 } |
| 149 | 149 |
| 150 if (fn.isFactoryConstructor && hasAnonymousAnnotation(classElement)) { | 150 if (fn.isFactoryConstructor && hasAnonymousAnnotation(classElement)) { |
| 151 fn.functionSignature.orderedForEachParameter( | 151 fn.functionSignature |
| 152 (ParameterElement parameter) { | 152 .orderedForEachParameter((ParameterElement parameter) { |
| 153 if (!parameter.isNamed) { | 153 if (!parameter.isNamed) { |
| 154 backend.reporter.reportErrorMessage(parameter, | 154 backend.reporter.reportErrorMessage( |
| 155 parameter, |
| 155 MessageKind | 156 MessageKind |
| 156 .JS_OBJECT_LITERAL_CONSTRUCTOR_WITH_POSITIONAL_ARGUMENTS
, | 157 .JS_OBJECT_LITERAL_CONSTRUCTOR_WITH_POSITIONAL_ARGUMENTS
, |
| 157 { | 158 {'parameter': parameter.name, 'cls': classElement.name}); |
| 158 'parameter': parameter.name, | |
| 159 'cls': classElement.name | |
| 160 }); | |
| 161 } | 159 } |
| 162 }); | 160 }); |
| 163 } else { | 161 } else { |
| 164 _checkFunctionParameters(fn); | 162 _checkFunctionParameters(fn); |
| 165 } | 163 } |
| 166 } | 164 } |
| 167 }); | 165 }); |
| 168 }); | 166 }); |
| 169 } | 167 } |
| 170 | 168 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 186 var name = backend.namer.invocationName(selector); | 184 var name = backend.namer.invocationName(selector); |
| 187 statements.add(js.statement( | 185 statements.add(js.statement( |
| 188 'Function.prototype.# = function(#) { return this(#) }', | 186 'Function.prototype.# = function(#) { return this(#) }', |
| 189 [name, parameters, parameters])); | 187 [name, parameters, parameters])); |
| 190 } | 188 } |
| 191 }); | 189 }); |
| 192 }); | 190 }); |
| 193 return new jsAst.Block(statements); | 191 return new jsAst.Block(statements); |
| 194 } | 192 } |
| 195 } | 193 } |
| OLD | NEW |