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 837 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
848 | 848 |
849 self._members_emitter.Emit( | 849 self._members_emitter.Emit( |
850 ' $RENAME$ANNOTATIONS$MODIFIERS$TYPE$TARGET($PARAMS) native;\n', | 850 ' $RENAME$ANNOTATIONS$MODIFIERS$TYPE$TARGET($PARAMS) native;\n', |
851 RENAME=self._RenamingAnnotation(info.declared_name, target), | 851 RENAME=self._RenamingAnnotation(info.declared_name, target), |
852 ANNOTATIONS=self._Annotations(info.type_name, info.declared_name), | 852 ANNOTATIONS=self._Annotations(info.type_name, info.declared_name), |
853 MODIFIERS='static ' if info.IsStatic() else '', | 853 MODIFIERS='static ' if info.IsStatic() else '', |
854 TYPE=TypeOrNothing(native_return_type), | 854 TYPE=TypeOrNothing(native_return_type), |
855 TARGET=target, | 855 TARGET=target, |
856 PARAMS=', '.join(target_parameters)) | 856 PARAMS=', '.join(target_parameters)) |
857 | 857 |
858 def GenerateChecksAndCall(operation, argument_count): | 858 self._GenerateDispatcherBody( |
859 GenerateCall(operation, argument_count, | 859 body, |
860 self._OverloadChecks( | 860 operations, |
861 operation, | 861 parameter_names, |
862 parameter_names, | 862 GenerateCall, |
863 argument_count, | 863 self._IsOptional, |
864 can_omit_type_check=lambda type, pos: type == parameter_types[pos])) | 864 can_omit_type_check=lambda type, pos: type == parameter_types[pos]) |
865 | |
866 # TODO: Optimize the dispatch to avoid repeated checks. | |
867 if len(operations) > 1: | |
868 for operation in operations: | |
869 for position, argument in enumerate(operation.arguments): | |
870 if self._IsOptional(operation, argument): | |
871 GenerateChecksAndCall(operation, position) | |
872 GenerateChecksAndCall(operation, len(operation.arguments)) | |
873 body.Emit( | |
874 ' throw new ArgumentError("Incorrect number or type of arguments");
' | |
875 '\n'); | |
876 else: | |
877 operation = operations[0] | |
878 argument_count = len(operation.arguments) | |
879 for position, argument in list(enumerate(operation.arguments))[::-1]: | |
880 if self._IsOptional(operation, argument): | |
881 check = '?%s' % parameter_names[position] | |
882 GenerateCall(operation, position + 1, [check]) | |
883 argument_count = position | |
884 GenerateCall(operation, argument_count, []) | |
885 | 865 |
886 def _AddInterfaceOperation(self, info, html_name): | 866 def _AddInterfaceOperation(self, info, html_name): |
887 self._members_emitter.Emit( | 867 self._members_emitter.Emit( |
888 '\n' | 868 '\n' |
889 ' $TYPE $NAME($PARAMS);\n', | 869 ' $TYPE $NAME($PARAMS);\n', |
890 TYPE=self.SecureOutputType(info.type_name), | 870 TYPE=self.SecureOutputType(info.type_name), |
891 NAME=info.name, | 871 NAME=info.name, |
892 PARAMS=info.ParametersDeclaration(self._NarrowInputType)) | 872 PARAMS=info.ParametersDeclaration(self._NarrowInputType)) |
893 | 873 |
894 def _IsOptional(self, operation, argument): | 874 def _IsOptional(self, operation, argument): |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1041 for library_name in libraries: | 1021 for library_name in libraries: |
1042 self._libraries[library_name] = DartLibrary( | 1022 self._libraries[library_name] = DartLibrary( |
1043 library_name, template_loader, library_type, output_dir) | 1023 library_name, template_loader, library_type, output_dir) |
1044 | 1024 |
1045 def AddFile(self, basename, library_name, path): | 1025 def AddFile(self, basename, library_name, path): |
1046 self._libraries[library_name].AddFile(path) | 1026 self._libraries[library_name].AddFile(path) |
1047 | 1027 |
1048 def Emit(self, emitter, auxiliary_dir): | 1028 def Emit(self, emitter, auxiliary_dir): |
1049 for lib in self._libraries.values(): | 1029 for lib in self._libraries.values(): |
1050 lib.Emit(emitter, auxiliary_dir) | 1030 lib.Emit(emitter, auxiliary_dir) |
OLD | NEW |