| 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 | 10 |
| (...skipping 1058 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1069 else: | 1069 else: |
| 1070 return self._NarrowInputType(type_name) | 1070 return self._NarrowInputType(type_name) |
| 1071 | 1071 |
| 1072 body = self._members_emitter.Emit( | 1072 body = self._members_emitter.Emit( |
| 1073 '\n' | 1073 '\n' |
| 1074 ' $TYPE $(HTML_NAME)($PARAMS) {\n' | 1074 ' $TYPE $(HTML_NAME)($PARAMS) {\n' |
| 1075 '$!BODY' | 1075 '$!BODY' |
| 1076 ' }\n', | 1076 ' }\n', |
| 1077 TYPE=return_type, | 1077 TYPE=return_type, |
| 1078 HTML_NAME=html_name, | 1078 HTML_NAME=html_name, |
| 1079 PARAMS=info.ParametersImplementationDeclaration(InputType, '_default')) | 1079 PARAMS=info.ParametersImplementationDeclaration(InputType)) |
| 1080 | 1080 |
| 1081 parameter_names = [param_info.name for param_info in info.param_infos] | 1081 parameter_names = [param_info.name for param_info in info.param_infos] |
| 1082 parameter_types = [InputType(param_info.dart_type) | 1082 parameter_types = [InputType(param_info.dart_type) |
| 1083 for param_info in info.param_infos] | 1083 for param_info in info.param_infos] |
| 1084 operations = info.operations | 1084 operations = info.operations |
| 1085 | 1085 |
| 1086 method_version = [0] | 1086 method_version = [0] |
| 1087 temp_version = [0] | 1087 temp_version = [0] |
| 1088 | 1088 |
| 1089 def GenerateCall(operation, argument_count, checks): | 1089 def GenerateCall(operation, argument_count, checks): |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1146 call_emitter.Emit('$(INDENT)return $CALL;\n', CALL=call) | 1146 call_emitter.Emit('$(INDENT)return $CALL;\n', CALL=call) |
| 1147 | 1147 |
| 1148 self._members_emitter.Emit( | 1148 self._members_emitter.Emit( |
| 1149 ' $TYPE$TARGET($PARAMS) native "$NATIVE";\n', | 1149 ' $TYPE$TARGET($PARAMS) native "$NATIVE";\n', |
| 1150 TYPE=TypeOrNothing(native_return_type), | 1150 TYPE=TypeOrNothing(native_return_type), |
| 1151 TARGET=target, | 1151 TARGET=target, |
| 1152 PARAMS=', '.join(target_parameters), | 1152 PARAMS=', '.join(target_parameters), |
| 1153 NATIVE=info.declared_name) | 1153 NATIVE=info.declared_name) |
| 1154 | 1154 |
| 1155 def GenerateChecksAndCall(operation, argument_count): | 1155 def GenerateChecksAndCall(operation, argument_count): |
| 1156 checks = ['_default == %s' % name for name in parameter_names] | 1156 checks = ['!?%s' % name for name in parameter_names] |
| 1157 for i in range(0, argument_count): | 1157 for i in range(0, argument_count): |
| 1158 argument = operation.arguments[i] | 1158 argument = operation.arguments[i] |
| 1159 parameter_name = parameter_names[i] | 1159 parameter_name = parameter_names[i] |
| 1160 test_type = self._DartType(argument.type.id) | 1160 test_type = self._DartType(argument.type.id) |
| 1161 if test_type in ['Dynamic', 'Object']: | 1161 if test_type in ['Dynamic', 'Object']: |
| 1162 checks[i] = '_default != %s' % parameter_name | 1162 checks[i] = '?%s' % parameter_name |
| 1163 elif test_type == parameter_types[i]: | 1163 elif test_type == parameter_types[i]: |
| 1164 checks[i] = 'true' | 1164 checks[i] = 'true' |
| 1165 else: | 1165 else: |
| 1166 checks[i] = '(%s is %s || %s == null)' % ( | 1166 checks[i] = '(%s is %s || %s === null)' % ( |
| 1167 parameter_name, test_type, parameter_name) | 1167 parameter_name, test_type, parameter_name) |
| 1168 # There can be multiple _default checks. We need them all since a later | 1168 # There can be multiple presence checks. We need them all since a later |
| 1169 # optional argument could have been passed by name, leaving 'holes'. | 1169 # optional argument could have been passed by name, leaving 'holes'. |
| 1170 GenerateCall(operation, argument_count, checks) | 1170 GenerateCall(operation, argument_count, checks) |
| 1171 | 1171 |
| 1172 # TODO: Optimize the dispatch to avoid repeated checks. | 1172 # TODO: Optimize the dispatch to avoid repeated checks. |
| 1173 if len(operations) > 1: | 1173 if len(operations) > 1: |
| 1174 for operation in operations: | 1174 for operation in operations: |
| 1175 for position, argument in enumerate(operation.arguments): | 1175 for position, argument in enumerate(operation.arguments): |
| 1176 if self._IsOptional(operation, argument): | 1176 if self._IsOptional(operation, argument): |
| 1177 GenerateChecksAndCall(operation, position) | 1177 GenerateChecksAndCall(operation, position) |
| 1178 GenerateChecksAndCall(operation, len(operation.arguments)) | 1178 GenerateChecksAndCall(operation, len(operation.arguments)) |
| 1179 body.Emit( | 1179 body.Emit( |
| 1180 ' throw const Exception("Incorrect number or type of arguments");' | 1180 ' throw const Exception("Incorrect number or type of arguments");' |
| 1181 '\n'); | 1181 '\n'); |
| 1182 else: | 1182 else: |
| 1183 operation = operations[0] | 1183 operation = operations[0] |
| 1184 argument_count = len(operation.arguments) | 1184 argument_count = len(operation.arguments) |
| 1185 for position, argument in list(enumerate(operation.arguments))[::-1]: | 1185 for position, argument in list(enumerate(operation.arguments))[::-1]: |
| 1186 if self._IsOptional(operation, argument): | 1186 if self._IsOptional(operation, argument): |
| 1187 check = '_default != %s' % parameter_names[position] | 1187 check = '?%s' % parameter_names[position] |
| 1188 GenerateCall(operation, position + 1, [check]) | 1188 GenerateCall(operation, position + 1, [check]) |
| 1189 argument_count = position | 1189 argument_count = position |
| 1190 GenerateCall(operation, argument_count, []) | 1190 GenerateCall(operation, argument_count, []) |
| 1191 | 1191 |
| 1192 | 1192 |
| 1193 def _IsOptional(self, operation, argument): | 1193 def _IsOptional(self, operation, argument): |
| 1194 return IsOptional(argument) | 1194 return IsOptional(argument) |
| 1195 | 1195 |
| 1196 | 1196 |
| 1197 def _OperationRequiresConversions(self, operation): | 1197 def _OperationRequiresConversions(self, operation): |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1233 return HtmlDart2JSClassGenerator(self, interface) | 1233 return HtmlDart2JSClassGenerator(self, interface) |
| 1234 | 1234 |
| 1235 def GenerateLibraries(self, dart_files): | 1235 def GenerateLibraries(self, dart_files): |
| 1236 self._GenerateLibFile( | 1236 self._GenerateLibFile( |
| 1237 'html_dart2js.darttemplate', | 1237 'html_dart2js.darttemplate', |
| 1238 os.path.join(self._output_dir, 'html_dart2js.dart'), | 1238 os.path.join(self._output_dir, 'html_dart2js.dart'), |
| 1239 dart_files) | 1239 dart_files) |
| 1240 | 1240 |
| 1241 def Finish(self): | 1241 def Finish(self): |
| 1242 pass | 1242 pass |
| OLD | NEW |