| 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 '../common.dart'; | 8 import '../common.dart'; |
| 9 import '../constants/values.dart' | 9 import '../constants/values.dart' |
| 10 show ConstantValue, ConstructedConstantValue, StringConstantValue; | 10 show ConstantValue, ConstructedConstantValue, StringConstantValue; |
| 11 import '../elements/resolution_types.dart' | 11 import '../elements/resolution_types.dart' |
| 12 show ResolutionDartType, ResolutionDynamicType, ResolutionFunctionType; | 12 show ResolutionDartType, ResolutionDynamicType, ResolutionFunctionType; |
| 13 import '../diagnostics/messages.dart' show MessageKind; | 13 import '../diagnostics/messages.dart' show MessageKind; |
| 14 import '../elements/elements.dart' | 14 import '../elements/elements.dart' |
| 15 show | 15 show |
| 16 ClassElement, | 16 ClassElement, |
| 17 Element, | 17 Element, |
| 18 FieldElement, | 18 FieldElement, |
| 19 LibraryElement, | 19 LibraryElement, |
| 20 ParameterElement, | 20 ParameterElement, |
| 21 MemberElement, | 21 MemberElement, |
| 22 MethodElement, | 22 MethodElement, |
| 23 MetadataAnnotation; | 23 MetadataAnnotation; |
| 24 import '../js/js.dart' as jsAst; | 24 import '../js/js.dart' as jsAst; |
| 25 import '../js/js.dart' show js; | 25 import '../js/js.dart' show js; |
| 26 import '../universe/selector.dart' show Selector; | 26 import '../universe/selector.dart' show Selector; |
| 27 import '../universe/world_builder.dart' show SelectorConstraints; | 27 import '../universe/world_builder.dart' show SelectorConstraints; |
| 28 import 'backend_helpers.dart' show BackendHelpers; | |
| 29 import 'js_backend.dart' show JavaScriptBackend; | 28 import 'js_backend.dart' show JavaScriptBackend; |
| 30 | 29 |
| 31 class JsInteropAnalysis { | 30 class JsInteropAnalysis { |
| 32 final JavaScriptBackend backend; | 31 final JavaScriptBackend backend; |
| 33 | 32 |
| 34 /// The resolved [FieldElement] for `Js.name`. | 33 /// The resolved [FieldElement] for `Js.name`. |
| 35 FieldElement nameField; | 34 FieldElement nameField; |
| 36 bool enabledJsInterop = false; | 35 bool enabledJsInterop = false; |
| 37 | 36 |
| 38 /// Whether the backend is currently processing the codegen queue. | 37 /// Whether the backend is currently processing the codegen queue. |
| 39 bool _inCodegen = false; | 38 bool _inCodegen = false; |
| 40 | 39 |
| 41 JsInteropAnalysis(this.backend); | 40 JsInteropAnalysis(this.backend); |
| 42 | 41 |
| 43 BackendHelpers get helpers => backend.helpers; | |
| 44 | |
| 45 void onQueueClosed() { | 42 void onQueueClosed() { |
| 46 if (_inCodegen) return; | 43 if (_inCodegen) return; |
| 47 | 44 |
| 48 if (helpers.jsAnnotationClass != null) { | 45 if (backend.compiler.commonElements.jsAnnotationClass != null) { |
| 49 ClassElement cls = helpers.jsAnnotationClass; | 46 ClassElement cls = backend.compiler.commonElements.jsAnnotationClass; |
| 50 nameField = cls.lookupMember('name'); | 47 nameField = cls.lookupMember('name'); |
| 51 backend.compiler.libraryLoader.libraries | 48 backend.compiler.libraryLoader.libraries |
| 52 .forEach(processJsInteropAnnotationsInLibrary); | 49 .forEach(processJsInteropAnnotationsInLibrary); |
| 53 } | 50 } |
| 54 } | 51 } |
| 55 | 52 |
| 56 void onCodegenStart() { | 53 void onCodegenStart() { |
| 57 _inCodegen = true; | 54 _inCodegen = true; |
| 58 } | 55 } |
| 59 | 56 |
| 60 /// Resolves the metadata of [element] and returns the name of the `JS(...)` | 57 /// Resolves the metadata of [element] and returns the name of the `JS(...)` |
| 61 /// annotation for js interop, if found. | 58 /// annotation for js interop, if found. |
| 62 String processJsInteropAnnotation(Element element) { | 59 String processJsInteropAnnotation(Element element) { |
| 63 for (MetadataAnnotation annotation in element.implementation.metadata) { | 60 for (MetadataAnnotation annotation in element.implementation.metadata) { |
| 64 // TODO(johnniwinther): Avoid processing unresolved elements. | 61 // TODO(johnniwinther): Avoid processing unresolved elements. |
| 65 if (annotation.constant == null) continue; | 62 if (annotation.constant == null) continue; |
| 66 ConstantValue constant = | 63 ConstantValue constant = |
| 67 backend.compiler.constants.getConstantValue(annotation.constant); | 64 backend.compiler.constants.getConstantValue(annotation.constant); |
| 68 if (constant == null || constant is! ConstructedConstantValue) continue; | 65 if (constant == null || constant is! ConstructedConstantValue) continue; |
| 69 ConstructedConstantValue constructedConstant = constant; | 66 ConstructedConstantValue constructedConstant = constant; |
| 70 if (constructedConstant.type.element == helpers.jsAnnotationClass) { | 67 if (constructedConstant.type.element == |
| 68 backend.compiler.commonElements.jsAnnotationClass) { |
| 71 ConstantValue value = constructedConstant.fields[nameField]; | 69 ConstantValue value = constructedConstant.fields[nameField]; |
| 72 String name; | 70 String name; |
| 73 if (value.isString) { | 71 if (value.isString) { |
| 74 StringConstantValue stringValue = value; | 72 StringConstantValue stringValue = value; |
| 75 name = stringValue.primitiveValue.slowToString(); | 73 name = stringValue.primitiveValue.slowToString(); |
| 76 } else { | 74 } else { |
| 77 // TODO(jacobr): report a warning if the value is not a String. | 75 // TODO(jacobr): report a warning if the value is not a String. |
| 78 name = ''; | 76 name = ''; |
| 79 } | 77 } |
| 80 enabledJsInterop = true; | 78 enabledJsInterop = true; |
| 81 return name; | 79 return name; |
| 82 } | 80 } |
| 83 } | 81 } |
| 84 return null; | 82 return null; |
| 85 } | 83 } |
| 86 | 84 |
| 87 bool hasAnonymousAnnotation(Element element) { | 85 bool hasAnonymousAnnotation(Element element) { |
| 88 if (backend.helpers.jsAnonymousClass == null) return false; | 86 if (backend.compiler.commonElements.jsAnonymousClass == null) return false; |
| 89 return element.metadata.any((MetadataAnnotation annotation) { | 87 return element.metadata.any((MetadataAnnotation annotation) { |
| 90 ConstantValue constant = | 88 ConstantValue constant = |
| 91 backend.compiler.constants.getConstantValue(annotation.constant); | 89 backend.compiler.constants.getConstantValue(annotation.constant); |
| 92 if (constant == null || constant is! ConstructedConstantValue) | 90 if (constant == null || constant is! ConstructedConstantValue) |
| 93 return false; | 91 return false; |
| 94 ConstructedConstantValue constructedConstant = constant; | 92 ConstructedConstantValue constructedConstant = constant; |
| 95 return constructedConstant.type.element == | 93 return constructedConstant.type.element == |
| 96 backend.helpers.jsAnonymousClass; | 94 backend.compiler.commonElements.jsAnonymousClass; |
| 97 }); | 95 }); |
| 98 } | 96 } |
| 99 | 97 |
| 100 void _checkFunctionParameters(MethodElement fn) { | 98 void _checkFunctionParameters(MethodElement fn) { |
| 101 if (fn.hasFunctionSignature && | 99 if (fn.hasFunctionSignature && |
| 102 fn.functionSignature.optionalParametersAreNamed) { | 100 fn.functionSignature.optionalParametersAreNamed) { |
| 103 backend.reporter.reportErrorMessage( | 101 backend.reporter.reportErrorMessage( |
| 104 fn, | 102 fn, |
| 105 MessageKind.JS_INTEROP_METHOD_WITH_NAMED_ARGUMENTS, | 103 MessageKind.JS_INTEROP_METHOD_WITH_NAMED_ARGUMENTS, |
| 106 {'method': fn.name}); | 104 {'method': fn.name}); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 135 } | 133 } |
| 136 if (!backend.nativeData.isJsInteropClass(classElement)) return; | 134 if (!backend.nativeData.isJsInteropClass(classElement)) return; |
| 137 | 135 |
| 138 // Skip classes that are completely unreachable. This should only happen | 136 // Skip classes that are completely unreachable. This should only happen |
| 139 // when all of jsinterop types are unreachable from main. | 137 // when all of jsinterop types are unreachable from main. |
| 140 if (!backend.compiler.resolutionWorldBuilder | 138 if (!backend.compiler.resolutionWorldBuilder |
| 141 .isImplemented(classElement)) { | 139 .isImplemented(classElement)) { |
| 142 return; | 140 return; |
| 143 } | 141 } |
| 144 | 142 |
| 145 if (!classElement.implementsInterface(helpers.jsJavaScriptObjectClass)) { | 143 if (!classElement.implementsInterface( |
| 144 backend.compiler.commonElements.jsJavaScriptObjectClass)) { |
| 146 backend.reporter.reportErrorMessage(classElement, | 145 backend.reporter.reportErrorMessage(classElement, |
| 147 MessageKind.JS_INTEROP_CLASS_CANNOT_EXTEND_DART_CLASS, { | 146 MessageKind.JS_INTEROP_CLASS_CANNOT_EXTEND_DART_CLASS, { |
| 148 'cls': classElement.name, | 147 'cls': classElement.name, |
| 149 'superclass': classElement.superclass.name | 148 'superclass': classElement.superclass.name |
| 150 }); | 149 }); |
| 151 } | 150 } |
| 152 | 151 |
| 153 classElement | 152 classElement |
| 154 .forEachMember((ClassElement classElement, MemberElement member) { | 153 .forEachMember((ClassElement classElement, MemberElement member) { |
| 155 String memberName = processJsInteropAnnotation(member); | 154 String memberName = processJsInteropAnnotation(member); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 ResolutionFunctionType buildJsFunctionType() { | 217 ResolutionFunctionType buildJsFunctionType() { |
| 219 // TODO(jacobr): consider using codegenWorldBuilder.isChecks to determine th
e | 218 // TODO(jacobr): consider using codegenWorldBuilder.isChecks to determine th
e |
| 220 // range of positional arguments that need to be supported by JavaScript | 219 // range of positional arguments that need to be supported by JavaScript |
| 221 // function types. | 220 // function types. |
| 222 return new ResolutionFunctionType.synthesized( | 221 return new ResolutionFunctionType.synthesized( |
| 223 const ResolutionDynamicType(), | 222 const ResolutionDynamicType(), |
| 224 [], | 223 [], |
| 225 new List<ResolutionDartType>.filled(16, const ResolutionDynamicType())); | 224 new List<ResolutionDartType>.filled(16, const ResolutionDynamicType())); |
| 226 } | 225 } |
| 227 } | 226 } |
| OLD | NEW |