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