| 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 os | 10 import os |
| (...skipping 771 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 782 return_type = self._NarrowInputType(info.type_name) | 782 return_type = self._NarrowInputType(info.type_name) |
| 783 native_return_type = return_type | 783 native_return_type = return_type |
| 784 | 784 |
| 785 def InputType(type_name): | 785 def InputType(type_name): |
| 786 conversion = self._InputConversion(type_name, info.declared_name) | 786 conversion = self._InputConversion(type_name, info.declared_name) |
| 787 if conversion: | 787 if conversion: |
| 788 return conversion.input_type | 788 return conversion.input_type |
| 789 else: | 789 else: |
| 790 return self._NarrowInputType(type_name) if type_name else 'dynamic' | 790 return self._NarrowInputType(type_name) if type_name else 'dynamic' |
| 791 | 791 |
| 792 body = self._members_emitter.Emit( | |
| 793 '\n' | |
| 794 ' $MODIFIERS$TYPE $(HTML_NAME)($PARAMS) {\n' | |
| 795 '$!BODY' | |
| 796 ' }\n', | |
| 797 MODIFIERS='static ' if info.IsStatic() else '', | |
| 798 TYPE=return_type, | |
| 799 HTML_NAME=html_name, | |
| 800 PARAMS=info.ParametersDeclaration(InputType)) | |
| 801 | |
| 802 parameter_names = [param_info.name for param_info in info.param_infos] | 792 parameter_names = [param_info.name for param_info in info.param_infos] |
| 803 parameter_types = [InputType(param_info.type_id) | 793 parameter_types = [InputType(param_info.type_id) |
| 804 for param_info in info.param_infos] | 794 for param_info in info.param_infos] |
| 805 operations = info.operations | 795 operations = info.operations |
| 806 | 796 |
| 807 method_version = [0] | |
| 808 temp_version = [0] | 797 temp_version = [0] |
| 809 | 798 |
| 810 def GenerateCall(operation, argument_count, checks): | 799 def GenerateCall(stmts_emitter, call_emitter, version, operation, argument_c
ount): |
| 811 if checks: | 800 target = '_%s_%d' % (html_name, version); |
| 812 (stmts_emitter, call_emitter) = body.Emit( | |
| 813 ' if ($CHECKS) {\n$!STMTS$!CALL }\n', | |
| 814 INDENT=' ', | |
| 815 CHECKS=' &&\n '.join(checks)) | |
| 816 else: | |
| 817 (stmts_emitter, call_emitter) = body.Emit('$!A$!B', INDENT=' '); | |
| 818 | |
| 819 method_version[0] += 1 | |
| 820 target = '_%s_%d' % (html_name, method_version[0]); | |
| 821 arguments = [] | 801 arguments = [] |
| 822 target_parameters = [] | 802 target_parameters = [] |
| 823 for position, arg in enumerate(operation.arguments[:argument_count]): | 803 for position, arg in enumerate(operation.arguments[:argument_count]): |
| 824 conversion = self._InputConversion(arg.type.id, operation.id) | 804 conversion = self._InputConversion(arg.type.id, operation.id) |
| 825 param_name = operation.arguments[position].id | 805 param_name = operation.arguments[position].id |
| 826 if conversion: | 806 if conversion: |
| 827 temp_version[0] += 1 | 807 temp_version[0] += 1 |
| 828 temp_name = '%s_%s' % (param_name, temp_version[0]) | 808 temp_name = '%s_%s' % (param_name, temp_version[0]) |
| 829 temp_type = conversion.output_type | 809 temp_type = conversion.output_type |
| 830 stmts_emitter.Emit( | 810 stmts_emitter.Emit( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 853 '%s%s' % (TypeOrNothing(param_type), param_name)) | 833 '%s%s' % (TypeOrNothing(param_type), param_name)) |
| 854 | 834 |
| 855 argument_list = ', '.join(arguments) | 835 argument_list = ', '.join(arguments) |
| 856 # TODO(sra): If the native method has zero type checks, we can 'inline' is | 836 # TODO(sra): If the native method has zero type checks, we can 'inline' is |
| 857 # and call it directly with a JS-expression. | 837 # and call it directly with a JS-expression. |
| 858 call = '%s(%s)' % (target, argument_list) | 838 call = '%s(%s)' % (target, argument_list) |
| 859 | 839 |
| 860 if output_conversion: | 840 if output_conversion: |
| 861 call = '%s(%s)' % (output_conversion.function_name, call) | 841 call = '%s(%s)' % (output_conversion.function_name, call) |
| 862 | 842 |
| 863 if operation.type.id == 'void': | 843 call_emitter.Emit(call) |
| 864 call_emitter.Emit('$(INDENT)$CALL;\n$(INDENT)return;\n', | |
| 865 CALL=call) | |
| 866 else: | |
| 867 call_emitter.Emit('$(INDENT)return $CALL;\n', CALL=call) | |
| 868 | 844 |
| 869 self._members_emitter.Emit( | 845 self._members_emitter.Emit( |
| 870 ' $RENAME$ANNOTATIONS$MODIFIERS$TYPE$TARGET($PARAMS) native;\n', | 846 ' $RENAME$ANNOTATIONS$MODIFIERS$TYPE$TARGET($PARAMS) native;\n', |
| 871 RENAME=self._RenamingAnnotation(info.declared_name, target), | 847 RENAME=self._RenamingAnnotation(info.declared_name, target), |
| 872 ANNOTATIONS=self._Annotations(info.type_name, info.declared_name), | 848 ANNOTATIONS=self._Annotations(info.type_name, info.declared_name), |
| 873 MODIFIERS='static ' if info.IsStatic() else '', | 849 MODIFIERS='static ' if info.IsStatic() else '', |
| 874 TYPE=TypeOrNothing(native_return_type), | 850 TYPE=TypeOrNothing(native_return_type), |
| 875 TARGET=target, | 851 TARGET=target, |
| 876 PARAMS=', '.join(target_parameters)) | 852 PARAMS=', '.join(target_parameters)) |
| 877 | 853 |
| 854 declaration = '%s%s %s(%s)' % ( |
| 855 'static ' if info.IsStatic() else '', |
| 856 return_type, |
| 857 html_name, |
| 858 info.ParametersDeclaration(InputType)) |
| 878 self._GenerateDispatcherBody( | 859 self._GenerateDispatcherBody( |
| 879 body, | |
| 880 operations, | 860 operations, |
| 881 parameter_names, | 861 parameter_names, |
| 862 declaration, |
| 882 GenerateCall, | 863 GenerateCall, |
| 883 self._IsOptional, | 864 self._IsOptional, |
| 884 can_omit_type_check=lambda type, pos: type == parameter_types[pos]) | 865 can_omit_type_check=lambda type, pos: type == parameter_types[pos]) |
| 885 | 866 |
| 886 def _AddInterfaceOperation(self, info, html_name): | 867 def _AddInterfaceOperation(self, info, html_name): |
| 887 self._members_emitter.Emit( | 868 self._members_emitter.Emit( |
| 888 '\n' | 869 '\n' |
| 889 ' $TYPE $NAME($PARAMS);\n', | 870 ' $TYPE $NAME($PARAMS);\n', |
| 890 TYPE=self.SecureOutputType(info.type_name), | 871 TYPE=self.SecureOutputType(info.type_name), |
| 891 NAME=info.name, | 872 NAME=info.name, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1044 for library_name in libraries: | 1025 for library_name in libraries: |
| 1045 self._libraries[library_name] = DartLibrary( | 1026 self._libraries[library_name] = DartLibrary( |
| 1046 library_name, template_loader, library_type, output_dir) | 1027 library_name, template_loader, library_type, output_dir) |
| 1047 | 1028 |
| 1048 def AddFile(self, basename, library_name, path): | 1029 def AddFile(self, basename, library_name, path): |
| 1049 self._libraries[library_name].AddFile(path) | 1030 self._libraries[library_name].AddFile(path) |
| 1050 | 1031 |
| 1051 def Emit(self, emitter, auxiliary_dir): | 1032 def Emit(self, emitter, auxiliary_dir): |
| 1052 for lib in self._libraries.values(): | 1033 for lib in self._libraries.values(): |
| 1053 lib.Emit(emitter, auxiliary_dir) | 1034 lib.Emit(emitter, auxiliary_dir) |
| OLD | NEW |