| 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 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 | 302 |
| 303 events_class_name = self._event_generator.ProcessInterface( | 303 events_class_name = self._event_generator.ProcessInterface( |
| 304 self._interface, interface_name, | 304 self._interface, interface_name, |
| 305 self._backend.CustomJSMembers(), | 305 self._backend.CustomJSMembers(), |
| 306 implementation_emitter) | 306 implementation_emitter) |
| 307 if events_class_name: | 307 if events_class_name: |
| 308 self._backend.EmitEventGetter(events_class_name) | 308 self._backend.EmitEventGetter(events_class_name) |
| 309 | 309 |
| 310 merged_interface = self._interface_type_info.merged_interface() | 310 merged_interface = self._interface_type_info.merged_interface() |
| 311 if merged_interface: | 311 if merged_interface: |
| 312 self._backend.AddMembers(self._database.GetInterface(merged_interface), | 312 self._backend.AddMembers(self._database.GetInterface(merged_interface), |
| 313 not self._backend.ImplementsMergedMembers()) | 313 not self._backend.ImplementsMergedMembers()) |
| 314 | 314 |
| 315 self._backend.AddMembers(self._interface) | 315 self._backend.AddMembers(self._interface) |
| 316 self._backend.AddSecondaryMembers(self._interface) | 316 self._backend.AddSecondaryMembers(self._interface) |
| 317 self._backend.FinishInterface() | 317 self._backend.FinishInterface() |
| 318 | 318 |
| 319 def _ImplementationEmitter(self): | 319 def _ImplementationEmitter(self): |
| 320 basename = self._interface_type_info.implementation_name() | 320 basename = self._interface_type_info.implementation_name() |
| 321 if (self._interface_type_info.merged_into() and | 321 if (self._interface_type_info.merged_into() and |
| 322 self._backend.ImplementsMergedMembers()): | 322 self._backend.ImplementsMergedMembers()): |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 '\n' | 630 '\n' |
| 631 ' $MODIFIERS$TYPE $NAME($PARAMS) native;\n', | 631 ' $MODIFIERS$TYPE $NAME($PARAMS) native;\n', |
| 632 MODIFIERS='static ' if info.IsStatic() else '', | 632 MODIFIERS='static ' if info.IsStatic() else '', |
| 633 TYPE=self.SecureOutputType(info.type_name), | 633 TYPE=self.SecureOutputType(info.type_name), |
| 634 NAME=info.name, | 634 NAME=info.name, |
| 635 PARAMS=info.ParametersDeclaration(self._NarrowInputType)) | 635 PARAMS=info.ParametersDeclaration(self._NarrowInputType)) |
| 636 | 636 |
| 637 def _AddOperationWithConversions(self, info, html_name): | 637 def _AddOperationWithConversions(self, info, html_name): |
| 638 # Assert all operations have same return type. | 638 # Assert all operations have same return type. |
| 639 assert len(set([op.type.id for op in info.operations])) == 1 | 639 assert len(set([op.type.id for op in info.operations])) == 1 |
| 640 info = info.CopyAndWidenDefaultParameters() | |
| 641 output_conversion = self._OutputConversion(info.type_name, | 640 output_conversion = self._OutputConversion(info.type_name, |
| 642 info.declared_name) | 641 info.declared_name) |
| 643 if output_conversion: | 642 if output_conversion: |
| 644 return_type = output_conversion.output_type | 643 return_type = output_conversion.output_type |
| 645 native_return_type = output_conversion.input_type | 644 native_return_type = output_conversion.input_type |
| 646 else: | 645 else: |
| 647 return_type = self._NarrowInputType(info.type_name) | 646 return_type = self._NarrowInputType(info.type_name) |
| 648 native_return_type = return_type | 647 native_return_type = return_type |
| 649 | 648 |
| 650 def InputType(type_name): | 649 def InputType(type_name): |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 NAME=temp_name, | 697 NAME=temp_name, |
| 699 CONVERT=conversion.function_name, | 698 CONVERT=conversion.function_name, |
| 700 ARG=parameter_names[position]) | 699 ARG=parameter_names[position]) |
| 701 arguments.append(temp_name) | 700 arguments.append(temp_name) |
| 702 param_type = temp_type | 701 param_type = temp_type |
| 703 verified_type = temp_type # verified by assignment in checked mode. | 702 verified_type = temp_type # verified by assignment in checked mode. |
| 704 else: | 703 else: |
| 705 arguments.append(parameter_names[position]) | 704 arguments.append(parameter_names[position]) |
| 706 param_type = self._NarrowInputType(arg.type.id) | 705 param_type = self._NarrowInputType(arg.type.id) |
| 707 # Verified by argument checking on entry to the dispatcher. | 706 # Verified by argument checking on entry to the dispatcher. |
| 707 |
| 708 verified_type = InputType(info.param_infos[position].type_id) | 708 verified_type = InputType(info.param_infos[position].type_id) |
| 709 # The native method does not need an argument type if we know the type
. |
| 710 # But we do need the native methods to have correct function types, so |
| 711 # be conservative. |
| 712 if param_type == verified_type: |
| 713 if param_type in ['String', 'num', 'int', 'double', 'bool', 'Object'
]: |
| 714 param_type = 'dynamic' |
| 709 | 715 |
| 710 # The native method does not need an argument type if we know the type. | |
| 711 # But we do need the native methods to have correct function types, so | |
| 712 # be conservative. | |
| 713 if param_type == verified_type: | |
| 714 if param_type in ['String', 'num', 'int', 'double', 'bool', 'Object']: | |
| 715 param_type = 'dynamic' | |
| 716 target_parameters.append( | 716 target_parameters.append( |
| 717 '%s%s' % (TypeOrNothing(param_type), param_name)) | 717 '%s%s' % (TypeOrNothing(param_type), param_name)) |
| 718 | 718 |
| 719 argument_list = ', '.join(arguments) | 719 argument_list = ', '.join(arguments) |
| 720 # TODO(sra): If the native method has zero type checks, we can 'inline' is | 720 # TODO(sra): If the native method has zero type checks, we can 'inline' is |
| 721 # and call it directly with a JS-expression. | 721 # and call it directly with a JS-expression. |
| 722 call = '%s(%s)' % (target, argument_list) | 722 call = '%s(%s)' % (target, argument_list) |
| 723 | 723 |
| 724 if output_conversion: | 724 if output_conversion: |
| 725 call = '%s(%s)' % (output_conversion.function_name, call) | 725 call = '%s(%s)' % (output_conversion.function_name, call) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 739 | 739 |
| 740 def GenerateChecksAndCall(operation, argument_count): | 740 def GenerateChecksAndCall(operation, argument_count): |
| 741 checks = [] | 741 checks = [] |
| 742 for i in range(0, argument_count): | 742 for i in range(0, argument_count): |
| 743 argument = operation.arguments[i] | 743 argument = operation.arguments[i] |
| 744 parameter_name = parameter_names[i] | 744 parameter_name = parameter_names[i] |
| 745 test_type = self._DartType(argument.type.id) | 745 test_type = self._DartType(argument.type.id) |
| 746 if test_type in ['dynamic', 'Object']: | 746 if test_type in ['dynamic', 'Object']: |
| 747 checks.append('?%s' % parameter_name) | 747 checks.append('?%s' % parameter_name) |
| 748 elif test_type != parameter_types[i]: | 748 elif test_type != parameter_types[i]: |
| 749 checks.append('(%s is %s || %s == null)' % ( | 749 checks.append('(?%s && (%s is %s || %s == null))' % ( |
| 750 parameter_name, test_type, parameter_name)) | 750 parameter_name, parameter_name, test_type, parameter_name)) |
| 751 |
| 751 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]]
) | 752 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]]
) |
| 752 # There can be multiple presence checks. We need them all since a later | 753 # There can be multiple presence checks. We need them all since a later |
| 753 # optional argument could have been passed by name, leaving 'holes'. | 754 # optional argument could have been passed by name, leaving 'holes'. |
| 754 GenerateCall(operation, argument_count, checks) | 755 GenerateCall(operation, argument_count, checks) |
| 755 | 756 |
| 756 # TODO: Optimize the dispatch to avoid repeated checks. | 757 # TODO: Optimize the dispatch to avoid repeated checks. |
| 757 if len(operations) > 1: | 758 if len(operations) > 1: |
| 758 for operation in operations: | 759 for operation in operations: |
| 759 for position, argument in enumerate(operation.arguments): | 760 for position, argument in enumerate(operation.arguments): |
| 760 if self._IsOptional(operation, argument): | 761 if self._IsOptional(operation, argument): |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 887 | 888 |
| 888 library_emitter = self._multiemitter.FileEmitter(library_file_path) | 889 library_emitter = self._multiemitter.FileEmitter(library_file_path) |
| 889 library_file_dir = os.path.dirname(library_file_path) | 890 library_file_dir = os.path.dirname(library_file_path) |
| 890 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) | 891 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) |
| 891 imports_emitter = library_emitter.Emit( | 892 imports_emitter = library_emitter.Emit( |
| 892 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) | 893 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) |
| 893 for path in sorted(self._path_to_emitter.keys()): | 894 for path in sorted(self._path_to_emitter.keys()): |
| 894 relpath = os.path.relpath(path, library_file_dir) | 895 relpath = os.path.relpath(path, library_file_dir) |
| 895 imports_emitter.Emit( | 896 imports_emitter.Emit( |
| 896 "part '$PATH';\n", PATH=massage_path(relpath)) | 897 "part '$PATH';\n", PATH=massage_path(relpath)) |
| OLD | NEW |