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 19 matching lines...) Expand all Loading... | |
670 operations = info.operations | 669 operations = info.operations |
671 | 670 |
672 method_version = [0] | 671 method_version = [0] |
673 temp_version = [0] | 672 temp_version = [0] |
674 | 673 |
675 def GenerateCall(operation, argument_count, checks): | 674 def GenerateCall(operation, argument_count, checks): |
676 if checks: | 675 if checks: |
677 (stmts_emitter, call_emitter) = body.Emit( | 676 (stmts_emitter, call_emitter) = body.Emit( |
678 ' if ($CHECKS) {\n$!STMTS$!CALL }\n', | 677 ' if ($CHECKS) {\n$!STMTS$!CALL }\n', |
679 INDENT=' ', | 678 INDENT=' ', |
680 CHECKS=' &&\n '.join(checks)) | 679 CHECKS=' && '.join(checks)) |
sra1
2012/11/09 21:34:41
Can you put this back?
blois
2012/11/09 23:20:41
Done :)
| |
681 else: | 680 else: |
682 (stmts_emitter, call_emitter) = body.Emit('$!A$!B', INDENT=' '); | 681 (stmts_emitter, call_emitter) = body.Emit('$!A$!B', INDENT=' '); |
683 | 682 |
684 method_version[0] += 1 | 683 method_version[0] += 1 |
685 target = '_%s_%d' % (html_name, method_version[0]); | 684 target = '_%s_%d' % (html_name, method_version[0]); |
686 arguments = [] | 685 arguments = [] |
687 target_parameters = [] | 686 target_parameters = [] |
688 for position, arg in enumerate(operation.arguments[:argument_count]): | 687 for position, arg in enumerate(operation.arguments[:argument_count]): |
689 conversion = self._InputConversion(arg.type.id, operation.id) | 688 conversion = self._InputConversion(arg.type.id, operation.id) |
690 param_name = operation.arguments[position].id | 689 param_name = operation.arguments[position].id |
691 if conversion: | 690 if conversion: |
692 temp_version[0] += 1 | 691 temp_version[0] += 1 |
693 temp_name = '%s_%s' % (param_name, temp_version[0]) | 692 temp_name = '%s_%s' % (param_name, temp_version[0]) |
694 temp_type = conversion.output_type | 693 temp_type = conversion.output_type |
695 stmts_emitter.Emit( | 694 stmts_emitter.Emit( |
696 '$(INDENT)$TYPE $NAME = $CONVERT($ARG);\n', | 695 '$(INDENT)$TYPE $NAME = $CONVERT($ARG);\n', |
697 TYPE=TypeOrVar(temp_type), | 696 TYPE=TypeOrVar(temp_type), |
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. |
708 verified_type = InputType(info.param_infos[position].type_id) | 707 verified_type = InputType(info.param_infos[position].type_id) |
709 | 708 |
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' | |
sra1
2012/11/09 21:34:41
What is the effect of removing this on code size i
blois
2012/11/09 23:20:41
Done.
| |
716 target_parameters.append( | 709 target_parameters.append( |
717 '%s%s' % (TypeOrNothing(param_type), param_name)) | 710 '%s%s' % (TypeOrNothing(param_type), param_name)) |
718 | 711 |
719 argument_list = ', '.join(arguments) | 712 argument_list = ', '.join(arguments) |
720 # TODO(sra): If the native method has zero type checks, we can 'inline' is | 713 # TODO(sra): If the native method has zero type checks, we can 'inline' is |
721 # and call it directly with a JS-expression. | 714 # and call it directly with a JS-expression. |
722 call = '%s(%s)' % (target, argument_list) | 715 call = '%s(%s)' % (target, argument_list) |
723 | 716 |
724 if output_conversion: | 717 if output_conversion: |
725 call = '%s(%s)' % (output_conversion.function_name, call) | 718 call = '%s(%s)' % (output_conversion.function_name, call) |
(...skipping 13 matching lines...) Expand all Loading... | |
739 | 732 |
740 def GenerateChecksAndCall(operation, argument_count): | 733 def GenerateChecksAndCall(operation, argument_count): |
741 checks = [] | 734 checks = [] |
742 for i in range(0, argument_count): | 735 for i in range(0, argument_count): |
743 argument = operation.arguments[i] | 736 argument = operation.arguments[i] |
744 parameter_name = parameter_names[i] | 737 parameter_name = parameter_names[i] |
745 test_type = self._DartType(argument.type.id) | 738 test_type = self._DartType(argument.type.id) |
746 if test_type in ['dynamic', 'Object']: | 739 if test_type in ['dynamic', 'Object']: |
747 checks.append('?%s' % parameter_name) | 740 checks.append('?%s' % parameter_name) |
748 elif test_type != parameter_types[i]: | 741 elif test_type != parameter_types[i]: |
749 checks.append('(%s is %s || %s == null)' % ( | 742 checks.append('(?%s && %s is %s)' % (parameter_name, parameter_name, t est_type)) |
sra1
2012/11/09 21:34:41
This was not a null-means-missing check.
If you ex
| |
750 parameter_name, test_type, parameter_name)) | 743 |
751 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]] ) | 744 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 | 745 # There can be multiple presence checks. We need them all since a later |
753 # optional argument could have been passed by name, leaving 'holes'. | 746 # optional argument could have been passed by name, leaving 'holes'. |
754 GenerateCall(operation, argument_count, checks) | 747 GenerateCall(operation, argument_count, checks) |
755 | 748 |
756 # TODO: Optimize the dispatch to avoid repeated checks. | 749 # TODO: Optimize the dispatch to avoid repeated checks. |
757 if len(operations) > 1: | 750 if len(operations) > 1: |
758 for operation in operations: | 751 for operation in operations: |
759 for position, argument in enumerate(operation.arguments): | 752 for position, argument in enumerate(operation.arguments): |
760 if self._IsOptional(operation, argument): | 753 if self._IsOptional(operation, argument): |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
887 | 880 |
888 library_emitter = self._multiemitter.FileEmitter(library_file_path) | 881 library_emitter = self._multiemitter.FileEmitter(library_file_path) |
889 library_file_dir = os.path.dirname(library_file_path) | 882 library_file_dir = os.path.dirname(library_file_path) |
890 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) | 883 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) |
891 imports_emitter = library_emitter.Emit( | 884 imports_emitter = library_emitter.Emit( |
892 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) | 885 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) |
893 for path in sorted(self._path_to_emitter.keys()): | 886 for path in sorted(self._path_to_emitter.keys()): |
894 relpath = os.path.relpath(path, library_file_dir) | 887 relpath = os.path.relpath(path, library_file_dir) |
895 imports_emitter.Emit( | 888 imports_emitter.Emit( |
896 "part '$PATH';\n", PATH=massage_path(relpath)) | 889 "part '$PATH';\n", PATH=massage_path(relpath)) |
OLD | NEW |