| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
| 4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 """This module provides shared functionality for the system to generate | 6 """This module provides shared functionality for the system to generate |
| 7 Dart:html APIs from the IDL database.""" | 7 Dart:html APIs from the IDL database.""" |
| 8 | 8 |
| 9 import emitter | 9 import emitter |
| 10 import logging |
| 10 import monitored | 11 import monitored |
| 11 import os | 12 import os |
| 13 import re |
| 12 from generator import * | 14 from generator import * |
| 13 from htmldartgenerator import * | 15 from htmldartgenerator import * |
| 14 | 16 |
| 17 _logger = logging.getLogger('systemhtml') |
| 18 |
| 15 HTML_LIBRARY_NAMES = ['chrome', 'html', 'indexed_db', 'svg', | 19 HTML_LIBRARY_NAMES = ['chrome', 'html', 'indexed_db', 'svg', |
| 16 'web_audio', 'web_gl', 'web_sql'] | 20 'web_audio', 'web_gl', 'web_sql'] |
| 17 | 21 |
| 18 _js_custom_members = monitored.Set('systemhtml._js_custom_members', [ | 22 _js_custom_members = monitored.Set('systemhtml._js_custom_members', [ |
| 19 'AudioBufferSourceNode.start', | 23 'AudioBufferSourceNode.start', |
| 20 'AudioBufferSourceNode.stop', | 24 'AudioBufferSourceNode.stop', |
| 21 'AudioContext.createGain', | 25 'AudioContext.createGain', |
| 22 'AudioContext.createScriptProcessor', | 26 'AudioContext.createScriptProcessor', |
| 23 'CanvasRenderingContext2D.drawImage', | 27 'CanvasRenderingContext2D.drawImage', |
| 24 'CanvasRenderingContext2D.lineDashOffset', | 28 'CanvasRenderingContext2D.lineDashOffset', |
| (...skipping 762 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 | 791 |
| 788 # If the type has a conversion we need a getter or setter to contain the | 792 # If the type has a conversion we need a getter or setter to contain the |
| 789 # conversion code. | 793 # conversion code. |
| 790 if (self._OutputConversion(attribute.type.id, attribute.id) or | 794 if (self._OutputConversion(attribute.type.id, attribute.id) or |
| 791 self._InputConversion(attribute.type.id, attribute.id)): | 795 self._InputConversion(attribute.type.id, attribute.id)): |
| 792 self._AddAttributeUsingProperties(attribute, html_name, read_only) | 796 self._AddAttributeUsingProperties(attribute, html_name, read_only) |
| 793 return | 797 return |
| 794 | 798 |
| 795 output_type = self.SecureOutputType(attribute.type.id) | 799 output_type = self.SecureOutputType(attribute.type.id) |
| 796 input_type = self._NarrowInputType(attribute.type.id) | 800 input_type = self._NarrowInputType(attribute.type.id) |
| 797 metadata = self._Metadata(attribute.type.id, attribute.id) | 801 metadata = self._Metadata(attribute.type.id, attribute.id, output_type) |
| 798 rename = self._RenamingAnnotation(attribute.id, html_name) | 802 rename = self._RenamingAnnotation(attribute.id, html_name) |
| 799 if not read_only: | 803 if not read_only: |
| 800 self._members_emitter.Emit( | 804 self._members_emitter.Emit( |
| 801 '\n $RENAME$METADATA$TYPE $NAME;' | 805 '\n $RENAME$METADATA$TYPE $NAME;' |
| 802 '\n', | 806 '\n', |
| 803 RENAME=rename, | 807 RENAME=rename, |
| 804 METADATA=metadata, | 808 METADATA=metadata, |
| 805 NAME=html_name, | 809 NAME=html_name, |
| 806 TYPE=output_type) | 810 TYPE=output_type) |
| 807 else: | 811 else: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 860 HTML_NAME=html_name, | 864 HTML_NAME=html_name, |
| 861 NAME=attr.id, | 865 NAME=attr.id, |
| 862 TYPE=self._NarrowInputType(attr.type.id)) | 866 TYPE=self._NarrowInputType(attr.type.id)) |
| 863 | 867 |
| 864 def _AddConvertingGetter(self, attr, html_name, conversion): | 868 def _AddConvertingGetter(self, attr, html_name, conversion): |
| 865 self._members_emitter.Emit( | 869 self._members_emitter.Emit( |
| 866 '\n $RETURN_TYPE get $HTML_NAME => $CONVERT(this._get_$(HTML_NAME));' | 870 '\n $RETURN_TYPE get $HTML_NAME => $CONVERT(this._get_$(HTML_NAME));' |
| 867 "\n @JSName('$NAME')" | 871 "\n @JSName('$NAME')" |
| 868 '\n $(METADATA)final $NATIVE_TYPE _get_$HTML_NAME;' | 872 '\n $(METADATA)final $NATIVE_TYPE _get_$HTML_NAME;' |
| 869 '\n', | 873 '\n', |
| 870 METADATA=self._Metadata(attr.type.id, html_name), | 874 METADATA=self._Metadata(attr.type.id, html_name, conversion.input_type), |
| 871 CONVERT=conversion.function_name, | 875 CONVERT=conversion.function_name, |
| 872 HTML_NAME=html_name, | 876 HTML_NAME=html_name, |
| 873 NAME=attr.id, | 877 NAME=attr.id, |
| 874 RETURN_TYPE=conversion.output_type, | 878 RETURN_TYPE=conversion.output_type, |
| 875 NATIVE_TYPE=conversion.input_type) | 879 NATIVE_TYPE=conversion.input_type) |
| 876 | 880 |
| 877 def _AddConvertingSetter(self, attr, html_name, conversion): | 881 def _AddConvertingSetter(self, attr, html_name, conversion): |
| 878 self._members_emitter.Emit( | 882 self._members_emitter.Emit( |
| 879 # TODO(sra): Use metadata to provide native name. | 883 # TODO(sra): Use metadata to provide native name. |
| 880 '\n void set $HTML_NAME($INPUT_TYPE value) {' | 884 '\n void set $HTML_NAME($INPUT_TYPE value) {' |
| (...skipping 28 matching lines...) Expand all Loading... |
| 909 # Any conversions needed? | 913 # Any conversions needed? |
| 910 self._AddOperationWithConversions(info, html_name) | 914 self._AddOperationWithConversions(info, html_name) |
| 911 else: | 915 else: |
| 912 self._AddDirectNativeOperation(info, html_name) | 916 self._AddDirectNativeOperation(info, html_name) |
| 913 | 917 |
| 914 def _AddDirectNativeOperation(self, info, html_name): | 918 def _AddDirectNativeOperation(self, info, html_name): |
| 915 self._members_emitter.Emit( | 919 self._members_emitter.Emit( |
| 916 '\n' | 920 '\n' |
| 917 ' $RENAME$METADATA$MODIFIERS$TYPE $NAME($PARAMS) native;\n', | 921 ' $RENAME$METADATA$MODIFIERS$TYPE $NAME($PARAMS) native;\n', |
| 918 RENAME=self._RenamingAnnotation(info.declared_name, html_name), | 922 RENAME=self._RenamingAnnotation(info.declared_name, html_name), |
| 919 METADATA=self._Metadata(info.type_name, info.declared_name), | 923 METADATA=self._Metadata(info.type_name, info.declared_name, |
| 924 self.SecureOutputType(info.type_name)), |
| 920 MODIFIERS='static ' if info.IsStatic() else '', | 925 MODIFIERS='static ' if info.IsStatic() else '', |
| 921 TYPE=self.SecureOutputType(info.type_name), | 926 TYPE=self.SecureOutputType(info.type_name), |
| 922 NAME=html_name, | 927 NAME=html_name, |
| 923 PARAMS=info.ParametersDeclaration(self._NarrowInputType)) | 928 PARAMS=info.ParametersDeclaration(self._NarrowInputType)) |
| 924 | 929 |
| 925 def _AddOperationWithConversions(self, info, html_name): | 930 def _AddOperationWithConversions(self, info, html_name): |
| 926 # Assert all operations have same return type. | 931 # Assert all operations have same return type. |
| 927 assert len(set([op.type.id for op in info.operations])) == 1 | 932 assert len(set([op.type.id for op in info.operations])) == 1 |
| 928 output_conversion = self._OutputConversion(info.type_name, | 933 output_conversion = self._OutputConversion(info.type_name, |
| 929 info.declared_name) | 934 info.declared_name) |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 991 call = '%s(%s)' % (target, argument_list) | 996 call = '%s(%s)' % (target, argument_list) |
| 992 | 997 |
| 993 if output_conversion: | 998 if output_conversion: |
| 994 call = '%s(%s)' % (output_conversion.function_name, call) | 999 call = '%s(%s)' % (output_conversion.function_name, call) |
| 995 | 1000 |
| 996 call_emitter.Emit(call) | 1001 call_emitter.Emit(call) |
| 997 | 1002 |
| 998 self._members_emitter.Emit( | 1003 self._members_emitter.Emit( |
| 999 ' $RENAME$METADATA$MODIFIERS$TYPE$TARGET($PARAMS) native;\n', | 1004 ' $RENAME$METADATA$MODIFIERS$TYPE$TARGET($PARAMS) native;\n', |
| 1000 RENAME=self._RenamingAnnotation(info.declared_name, target), | 1005 RENAME=self._RenamingAnnotation(info.declared_name, target), |
| 1001 METADATA=self._Metadata(info.type_name, info.declared_name), | 1006 METADATA=self._Metadata(info.type_name, info.declared_name, None), |
| 1002 MODIFIERS='static ' if info.IsStatic() else '', | 1007 MODIFIERS='static ' if info.IsStatic() else '', |
| 1003 TYPE=TypeOrNothing(native_return_type), | 1008 TYPE=TypeOrNothing(native_return_type), |
| 1004 TARGET=target, | 1009 TARGET=target, |
| 1005 PARAMS=', '.join(target_parameters)) | 1010 PARAMS=', '.join(target_parameters)) |
| 1006 | 1011 |
| 1007 declaration = '%s%s%s %s(%s)' % ( | 1012 declaration = '%s%s%s %s(%s)' % ( |
| 1008 self._Metadata(info.type_name, info.declared_name), | 1013 self._Metadata(info.type_name, info.declared_name, return_type), |
| 1009 'static ' if info.IsStatic() else '', | 1014 'static ' if info.IsStatic() else '', |
| 1010 return_type, | 1015 return_type, |
| 1011 html_name, | 1016 html_name, |
| 1012 info.ParametersDeclaration(InputType)) | 1017 info.ParametersDeclaration(InputType)) |
| 1013 self._GenerateDispatcherBody( | 1018 self._GenerateDispatcherBody( |
| 1014 info, | 1019 info, |
| 1015 operations, | 1020 operations, |
| 1016 declaration, | 1021 declaration, |
| 1017 GenerateCall, | 1022 GenerateCall, |
| 1018 lambda _, argument: IsOptional(argument), | 1023 lambda _, argument: IsOptional(argument), |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1046 | 1051 |
| 1047 def _HasCustomImplementation(self, member_name): | 1052 def _HasCustomImplementation(self, member_name): |
| 1048 member_name = '%s.%s' % (self._interface.doc_js_name, member_name) | 1053 member_name = '%s.%s' % (self._interface.doc_js_name, member_name) |
| 1049 return member_name in _js_custom_members | 1054 return member_name in _js_custom_members |
| 1050 | 1055 |
| 1051 def _RenamingAnnotation(self, idl_name, member_name): | 1056 def _RenamingAnnotation(self, idl_name, member_name): |
| 1052 if member_name != idl_name: | 1057 if member_name != idl_name: |
| 1053 return "@JSName('%s')\n " % idl_name | 1058 return "@JSName('%s')\n " % idl_name |
| 1054 return '' | 1059 return '' |
| 1055 | 1060 |
| 1056 def _Metadata(self, idl_type, idl_member_name, indent=' '): | 1061 def _Metadata(self, idl_type, idl_member_name, dart_type, indent=' '): |
| 1057 anns = self._metadata.GetDart2JSMetadata( | 1062 anns = self._metadata.GetDart2JSMetadata( |
| 1058 idl_type, self._library_name, self._interface, idl_member_name) | 1063 idl_type, self._library_name, self._interface, idl_member_name) |
| 1059 | 1064 |
| 1060 if not self._metadata.AnyConversionAnnotations( | 1065 if not self._metadata.AnyConversionAnnotations( |
| 1061 idl_type, self._interface.id, idl_member_name): | 1066 idl_type, self._interface.id, idl_member_name): |
| 1062 return_type = self.SecureOutputType(idl_type) | 1067 return_type = self.SecureOutputType(idl_type) |
| 1063 native_type = self._NarrowToImplementationType(idl_type) | 1068 native_type = self._NarrowToImplementationType(idl_type) |
| 1064 if native_type != return_type: | 1069 if native_type != return_type: |
| 1065 anns = anns + [ | 1070 anns = anns + [ |
| 1066 "@Returns('%s')" % native_type, | 1071 "@Returns('%s')" % native_type, |
| 1067 "@Creates('%s')" % native_type, | 1072 "@Creates('%s')" % native_type, |
| 1068 ] | 1073 ] |
| 1074 if dart_type == 'dynamic' or dart_type == 'Object': |
| 1075 def js_type_annotation(ann): |
| 1076 return re.search('^@.*Returns', ann) or re.search('^@.*Creates', ann) |
| 1077 if not filter(js_type_annotation, anns): |
| 1078 _logger.warn('Member with wildcard native type: %s.%s' % |
| 1079 (self._interface.id, idl_member_name)) |
| 1080 |
| 1069 return self._metadata.FormatMetadata(anns, indent); | 1081 return self._metadata.FormatMetadata(anns, indent); |
| 1070 | 1082 |
| 1071 def CustomJSMembers(self): | 1083 def CustomJSMembers(self): |
| 1072 return _js_custom_members | 1084 return _js_custom_members |
| 1073 | 1085 |
| 1074 def _NarrowToImplementationType(self, type_name): | 1086 def _NarrowToImplementationType(self, type_name): |
| 1075 return self._type_registry.TypeInfo(type_name).narrow_dart_type() | 1087 return self._type_registry.TypeInfo(type_name).narrow_dart_type() |
| 1076 | 1088 |
| 1077 def _NarrowInputType(self, type_name): | 1089 def _NarrowInputType(self, type_name): |
| 1078 return self._NarrowToImplementationType(type_name) | 1090 return self._NarrowToImplementationType(type_name) |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1176 for library_name in libraries: | 1188 for library_name in libraries: |
| 1177 self._libraries[library_name] = DartLibrary( | 1189 self._libraries[library_name] = DartLibrary( |
| 1178 library_name, template_loader, library_type, output_dir) | 1190 library_name, template_loader, library_type, output_dir) |
| 1179 | 1191 |
| 1180 def AddFile(self, basename, library_name, path): | 1192 def AddFile(self, basename, library_name, path): |
| 1181 self._libraries[library_name].AddFile(path) | 1193 self._libraries[library_name].AddFile(path) |
| 1182 | 1194 |
| 1183 def Emit(self, emitter, auxiliary_dir): | 1195 def Emit(self, emitter, auxiliary_dir): |
| 1184 for lib in self._libraries.values(): | 1196 for lib in self._libraries.values(): |
| 1185 lib.Emit(emitter, auxiliary_dir) | 1197 lib.Emit(emitter, auxiliary_dir) |
| OLD | NEW |