| 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 from generator import AnalyzeOperation, ConstantOutputOrder, \ | 9 from generator import AnalyzeOperation, ConstantOutputOrder, \ |
| 10 DartDomNameOfAttribute, FindMatchingAttribute, IsDartCollectionType, \ | 10 DartDomNameOfAttribute, FindMatchingAttribute, IsDartCollectionType, \ |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 self.EmitOperation(info, '_item') | 169 self.EmitOperation(info, '_item') |
| 170 return | 170 return |
| 171 | 171 |
| 172 if declare_only: | 172 if declare_only: |
| 173 self.DeclareOperation(info, | 173 self.DeclareOperation(info, |
| 174 self.SecureOutputType(info.type_name), method_name) | 174 self.SecureOutputType(info.type_name), method_name) |
| 175 else: | 175 else: |
| 176 self.EmitOperation(info, method_name) | 176 self.EmitOperation(info, method_name) |
| 177 | 177 |
| 178 def _GenerateDispatcherBody(self, | 178 def _GenerateDispatcherBody(self, |
| 179 emitter, | |
| 180 operations, | 179 operations, |
| 181 parameter_names, | 180 parameter_names, |
| 181 declaration, |
| 182 generate_call, | 182 generate_call, |
| 183 is_optional, | 183 is_optional, |
| 184 can_omit_type_check=lambda type, pos: False): | 184 can_omit_type_check=lambda type, pos: False): |
| 185 | 185 |
| 186 body_emitter = self._members_emitter.Emit( |
| 187 '\n' |
| 188 ' $DECLARATION {\n' |
| 189 '$!BODY' |
| 190 ' }\n', |
| 191 DECLARATION=declaration) |
| 192 |
| 193 version = [0] |
| 194 def GenerateCall(operation, argument_count, checks): |
| 195 if checks: |
| 196 (stmts_emitter, call_emitter) = body_emitter.Emit( |
| 197 ' if ($CHECKS) {\n$!STMTS$!CALL }\n', |
| 198 INDENT=' ', |
| 199 CHECKS=' && '.join(checks)) |
| 200 else: |
| 201 (stmts_emitter, call_emitter) = body_emitter.Emit( |
| 202 '$!STMTS$!CALL', |
| 203 INDENT=' '); |
| 204 |
| 205 if operation.type.id == 'void': |
| 206 call_emitter = call_emitter.Emit('$(INDENT)$!CALL;\n$(INDENT)return;\n') |
| 207 else: |
| 208 call_emitter = call_emitter.Emit('$(INDENT)return $!CALL;\n') |
| 209 |
| 210 version[0] += 1 |
| 211 generate_call( |
| 212 stmts_emitter, call_emitter, version[0], operation, argument_count) |
| 213 |
| 186 def GenerateChecksAndCall(operation, argument_count): | 214 def GenerateChecksAndCall(operation, argument_count): |
| 187 checks = [] | 215 checks = [] |
| 188 for i in range(0, argument_count): | 216 for i in range(0, argument_count): |
| 189 argument = operation.arguments[i] | 217 argument = operation.arguments[i] |
| 190 parameter_name = parameter_names[i] | 218 parameter_name = parameter_names[i] |
| 191 test_type = self._DartType(argument.type.id) | 219 test_type = self._DartType(argument.type.id) |
| 192 if test_type in ['dynamic', 'Object']: | 220 if test_type in ['dynamic', 'Object']: |
| 193 checks.append('?%s' % parameter_name) | 221 checks.append('?%s' % parameter_name) |
| 194 elif not can_omit_type_check(test_type, i): | 222 elif not can_omit_type_check(test_type, i): |
| 195 checks.append('(%s is %s || %s == null)' % ( | 223 checks.append('(%s is %s || %s == null)' % ( |
| 196 parameter_name, test_type, parameter_name)) | 224 parameter_name, test_type, parameter_name)) |
| 197 # There can be multiple presence checks. We need them all since a later | 225 # There can be multiple presence checks. We need them all since a later |
| 198 # optional argument could have been passed by name, leaving 'holes'. | 226 # optional argument could have been passed by name, leaving 'holes'. |
| 199 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]]
) | 227 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]]
) |
| 200 | 228 |
| 201 generate_call(operation, argument_count, checks) | 229 GenerateCall(operation, argument_count, checks) |
| 202 | 230 |
| 203 # TODO: Optimize the dispatch to avoid repeated checks. | 231 # TODO: Optimize the dispatch to avoid repeated checks. |
| 204 if len(operations) > 1: | 232 if len(operations) > 1: |
| 205 for operation in operations: | 233 for operation in operations: |
| 206 for position, argument in enumerate(operation.arguments): | 234 for position, argument in enumerate(operation.arguments): |
| 207 if is_optional(operation, argument): | 235 if is_optional(operation, argument): |
| 208 GenerateChecksAndCall(operation, position) | 236 GenerateChecksAndCall(operation, position) |
| 209 GenerateChecksAndCall(operation, len(operation.arguments)) | 237 GenerateChecksAndCall(operation, len(operation.arguments)) |
| 210 emitter.Emit( | 238 body_emitter.Emit( |
| 211 ' throw new ArgumentError("Incorrect number or type of arguments");
' | 239 ' throw new ArgumentError("Incorrect number or type of arguments");
' |
| 212 '\n'); | 240 '\n'); |
| 213 else: | 241 else: |
| 214 operation = operations[0] | 242 operation = operations[0] |
| 215 argument_count = len(operation.arguments) | 243 argument_count = len(operation.arguments) |
| 216 for position, argument in list(enumerate(operation.arguments))[::-1]: | 244 for position, argument in list(enumerate(operation.arguments))[::-1]: |
| 217 if is_optional(operation, argument): | 245 if is_optional(operation, argument): |
| 218 check = '?%s' % parameter_names[position] | 246 check = '?%s' % parameter_names[position] |
| 219 # argument_count instead of position + 1 is used here to cover one | 247 # argument_count instead of position + 1 is used here to cover one |
| 220 # complicated case with the effectively optional argument in the middl
e. | 248 # complicated case with the effectively optional argument in the middl
e. |
| 221 # Consider foo(x, [Optional] y, [Optional=DefaultIsNullString] z) | 249 # Consider foo(x, [Optional] y, [Optional=DefaultIsNullString] z) |
| 222 # (as of now it's modelled after HTMLMediaElement.webkitAddKey). | 250 # (as of now it's modelled after HTMLMediaElement.webkitAddKey). |
| 223 # y is optional in WebCore, while z is not. | 251 # y is optional in WebCore, while z is not. |
| 224 # In this case, if y was actually passed, we'd like to emit foo(x, y,
z) invocation, | 252 # In this case, if y was actually passed, we'd like to emit foo(x, y,
z) invocation, |
| 225 # not foo(x, y). | 253 # not foo(x, y). |
| 226 generate_call(operation, argument_count, [check]) | 254 GenerateCall(operation, argument_count, [check]) |
| 227 argument_count = position | 255 argument_count = position |
| 228 generate_call(operation, argument_count, []) | 256 GenerateCall(operation, argument_count, []) |
| 229 | 257 |
| 230 def AdditionalImplementedInterfaces(self): | 258 def AdditionalImplementedInterfaces(self): |
| 231 # TODO: Include all implemented interfaces, including other Lists. | 259 # TODO: Include all implemented interfaces, including other Lists. |
| 232 implements = [] | 260 implements = [] |
| 233 if self._interface_type_info.is_typed_array(): | 261 if self._interface_type_info.is_typed_array(): |
| 234 element_type = self._interface_type_info.list_item_type() | 262 element_type = self._interface_type_info.list_item_type() |
| 235 implements.append('List<%s>' % self._DartType(element_type)) | 263 implements.append('List<%s>' % self._DartType(element_type)) |
| 236 if self._interface_type_info.list_item_type(): | 264 if self._interface_type_info.list_item_type(): |
| 237 item_type_info = self._type_registry.TypeInfo( | 265 item_type_info = self._type_registry.TypeInfo( |
| 238 self._interface_type_info.list_item_type()) | 266 self._interface_type_info.list_item_type()) |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 walk(interface.parents) | 472 walk(interface.parents) |
| 445 else: | 473 else: |
| 446 walk(interface.parents[1:]) | 474 walk(interface.parents[1:]) |
| 447 return result | 475 return result |
| 448 | 476 |
| 449 def _DartType(self, type_name): | 477 def _DartType(self, type_name): |
| 450 return self._type_registry.DartType(type_name) | 478 return self._type_registry.DartType(type_name) |
| 451 | 479 |
| 452 def _IsPrivate(self, name): | 480 def _IsPrivate(self, name): |
| 453 return name.startswith('_') | 481 return name.startswith('_') |
| OLD | NEW |