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 838 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 def GenerateChecksAndCall(operation, argument_count): |
859 checks = [] | 859 GenerateCall(operation, argument_count, |
860 for i in range(0, argument_count): | 860 self._OverloadChecks( |
861 argument = operation.arguments[i] | 861 operation, |
862 parameter_name = parameter_names[i] | 862 parameter_names, |
863 test_type = self._DartType(argument.type.id) | 863 argument_count, |
864 if test_type in ['dynamic', 'Object']: | 864 can_omit_type_check=lambda type, pos: type == parameter_types[pos])) |
865 checks.append('?%s' % parameter_name) | |
866 elif test_type != parameter_types[i]: | |
867 checks.append('(?%s && (%s is %s || %s == null))' % ( | |
868 parameter_name, parameter_name, test_type, parameter_name)) | |
869 | |
870 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]]
) | |
871 # There can be multiple presence checks. We need them all since a later | |
872 # optional argument could have been passed by name, leaving 'holes'. | |
873 GenerateCall(operation, argument_count, checks) | |
874 | 865 |
875 # TODO: Optimize the dispatch to avoid repeated checks. | 866 # TODO: Optimize the dispatch to avoid repeated checks. |
876 if len(operations) > 1: | 867 if len(operations) > 1: |
877 for operation in operations: | 868 for operation in operations: |
878 for position, argument in enumerate(operation.arguments): | 869 for position, argument in enumerate(operation.arguments): |
879 if self._IsOptional(operation, argument): | 870 if self._IsOptional(operation, argument): |
880 GenerateChecksAndCall(operation, position) | 871 GenerateChecksAndCall(operation, position) |
881 GenerateChecksAndCall(operation, len(operation.arguments)) | 872 GenerateChecksAndCall(operation, len(operation.arguments)) |
882 body.Emit( | 873 body.Emit( |
883 ' throw new ArgumentError("Incorrect number or type of arguments");
' | 874 ' throw new ArgumentError("Incorrect number or type of arguments");
' |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1050 for library_name in libraries: | 1041 for library_name in libraries: |
1051 self._libraries[library_name] = DartLibrary( | 1042 self._libraries[library_name] = DartLibrary( |
1052 library_name, template_loader, library_type, output_dir) | 1043 library_name, template_loader, library_type, output_dir) |
1053 | 1044 |
1054 def AddFile(self, basename, library_name, path): | 1045 def AddFile(self, basename, library_name, path): |
1055 self._libraries[library_name].AddFile(path) | 1046 self._libraries[library_name].AddFile(path) |
1056 | 1047 |
1057 def Emit(self, emitter, auxiliary_dir): | 1048 def Emit(self, emitter, auxiliary_dir): |
1058 for lib in self._libraries.values(): | 1049 for lib in self._libraries.values(): |
1059 lib.Emit(emitter, auxiliary_dir) | 1050 lib.Emit(emitter, auxiliary_dir) |
OLD | NEW |