| 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 897 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 908 | 908 |
| 909 parameter_names = [param_info.name for param_info in info.param_infos] | 909 parameter_names = [param_info.name for param_info in info.param_infos] |
| 910 parameter_types = [InputType(param_info.type_id) | 910 parameter_types = [InputType(param_info.type_id) |
| 911 for param_info in info.param_infos] | 911 for param_info in info.param_infos] |
| 912 operations = info.operations | 912 operations = info.operations |
| 913 | 913 |
| 914 method_version = [0] | 914 method_version = [0] |
| 915 temp_version = [0] | 915 temp_version = [0] |
| 916 | 916 |
| 917 def GenerateCall(operation, argument_count, checks): | 917 def GenerateCall(operation, argument_count, checks): |
| 918 checks = filter(lambda e: e != 'true', checks) | |
| 919 if checks: | 918 if checks: |
| 920 (stmts_emitter, call_emitter) = body.Emit( | 919 (stmts_emitter, call_emitter) = body.Emit( |
| 921 ' if ($CHECKS) {\n$!STMTS$!CALL }\n', | 920 ' if ($CHECKS) {\n$!STMTS$!CALL }\n', |
| 922 INDENT=' ', | 921 INDENT=' ', |
| 923 CHECKS=' &&\n '.join(checks)) | 922 CHECKS=' &&\n '.join(checks)) |
| 924 else: | 923 else: |
| 925 (stmts_emitter, call_emitter) = body.Emit('$!A$!B', INDENT=' '); | 924 (stmts_emitter, call_emitter) = body.Emit('$!A$!B', INDENT=' '); |
| 926 | 925 |
| 927 method_version[0] += 1 | 926 method_version[0] += 1 |
| 928 target = '_%s_%d' % (html_name, method_version[0]); | 927 target = '_%s_%d' % (html_name, method_version[0]); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 974 call_emitter.Emit('$(INDENT)return $CALL;\n', CALL=call) | 973 call_emitter.Emit('$(INDENT)return $CALL;\n', CALL=call) |
| 975 | 974 |
| 976 self._members_emitter.Emit( | 975 self._members_emitter.Emit( |
| 977 ' $TYPE$TARGET($PARAMS) native "$NATIVE";\n', | 976 ' $TYPE$TARGET($PARAMS) native "$NATIVE";\n', |
| 978 TYPE=TypeOrNothing(native_return_type), | 977 TYPE=TypeOrNothing(native_return_type), |
| 979 TARGET=target, | 978 TARGET=target, |
| 980 PARAMS=', '.join(target_parameters), | 979 PARAMS=', '.join(target_parameters), |
| 981 NATIVE=info.declared_name) | 980 NATIVE=info.declared_name) |
| 982 | 981 |
| 983 def GenerateChecksAndCall(operation, argument_count): | 982 def GenerateChecksAndCall(operation, argument_count): |
| 984 checks = ['!?%s' % name for name in parameter_names] | 983 checks = [] |
| 985 for i in range(0, argument_count): | 984 for i in range(0, argument_count): |
| 986 argument = operation.arguments[i] | 985 argument = operation.arguments[i] |
| 987 parameter_name = parameter_names[i] | 986 parameter_name = parameter_names[i] |
| 988 test_type = self._DartType(argument.type.id) | 987 test_type = self._DartType(argument.type.id) |
| 989 if test_type in ['dynamic', 'Object']: | 988 if test_type in ['dynamic', 'Object']: |
| 990 checks[i] = '?%s' % parameter_name | 989 checks.append('?%s' % parameter_name) |
| 991 elif test_type == parameter_types[i]: | 990 elif test_type != parameter_types[i]: |
| 992 checks[i] = 'true' | 991 checks.append('(%s is %s || %s == null)' % ( |
| 993 else: | 992 parameter_name, test_type, parameter_name)) |
| 994 checks[i] = '(%s is %s || %s == null)' % ( | 993 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]]
) |
| 995 parameter_name, test_type, parameter_name) | |
| 996 # There can be multiple presence checks. We need them all since a later | 994 # There can be multiple presence checks. We need them all since a later |
| 997 # optional argument could have been passed by name, leaving 'holes'. | 995 # optional argument could have been passed by name, leaving 'holes'. |
| 998 GenerateCall(operation, argument_count, checks) | 996 GenerateCall(operation, argument_count, checks) |
| 999 | 997 |
| 1000 # TODO: Optimize the dispatch to avoid repeated checks. | 998 # TODO: Optimize the dispatch to avoid repeated checks. |
| 1001 if len(operations) > 1: | 999 if len(operations) > 1: |
| 1002 for operation in operations: | 1000 for operation in operations: |
| 1003 for position, argument in enumerate(operation.arguments): | 1001 for position, argument in enumerate(operation.arguments): |
| 1004 if self._IsOptional(operation, argument): | 1002 if self._IsOptional(operation, argument): |
| 1005 GenerateChecksAndCall(operation, position) | 1003 GenerateChecksAndCall(operation, position) |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1121 | 1119 |
| 1122 library_emitter = self._multiemitter.FileEmitter(library_file_path) | 1120 library_emitter = self._multiemitter.FileEmitter(library_file_path) |
| 1123 library_file_dir = os.path.dirname(library_file_path) | 1121 library_file_dir = os.path.dirname(library_file_path) |
| 1124 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) | 1122 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) |
| 1125 imports_emitter = library_emitter.Emit( | 1123 imports_emitter = library_emitter.Emit( |
| 1126 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) | 1124 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) |
| 1127 for path in sorted(self._path_to_emitter.keys()): | 1125 for path in sorted(self._path_to_emitter.keys()): |
| 1128 relpath = os.path.relpath(path, library_file_dir) | 1126 relpath = os.path.relpath(path, library_file_dir) |
| 1129 imports_emitter.Emit( | 1127 imports_emitter.Emit( |
| 1130 "part '$PATH';\n", PATH=massage_path(relpath)) | 1128 "part '$PATH';\n", PATH=massage_path(relpath)) |
| OLD | NEW |