| 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 from generator import AnalyzeOperation, ConstantOutputOrder, \ | 10 from generator import AnalyzeOperation, ConstantOutputOrder, \ |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 self.EmitOperation(info, '_item') | 191 self.EmitOperation(info, '_item') |
| 192 return | 192 return |
| 193 | 193 |
| 194 if declare_only: | 194 if declare_only: |
| 195 self.DeclareOperation(info, | 195 self.DeclareOperation(info, |
| 196 self.SecureOutputType(info.type_name), method_name) | 196 self.SecureOutputType(info.type_name), method_name) |
| 197 else: | 197 else: |
| 198 self.EmitOperation(info, method_name) | 198 self.EmitOperation(info, method_name) |
| 199 | 199 |
| 200 def _GenerateOverloadDispatcher(self, | 200 def _GenerateOverloadDispatcher(self, |
| 201 info, |
| 201 signatures, | 202 signatures, |
| 202 is_void, | 203 is_void, |
| 203 parameter_names, | |
| 204 declaration, | 204 declaration, |
| 205 generate_call, | 205 generate_call, |
| 206 is_optional, | 206 is_optional, |
| 207 can_omit_type_check=lambda type, pos: False): | 207 can_omit_type_check=lambda type, pos: False): |
| 208 | 208 |
| 209 parameter_names = [p.name for p in info.param_infos] |
| 210 number_of_required_in_dart = info.NumberOfRequiredInDart() |
| 211 |
| 209 body_emitter = self._members_emitter.Emit( | 212 body_emitter = self._members_emitter.Emit( |
| 210 '\n' | 213 '\n' |
| 211 ' $DECLARATION {\n' | 214 ' $DECLARATION {\n' |
| 212 '$!BODY' | 215 '$!BODY' |
| 213 ' }\n', | 216 ' }\n', |
| 214 DECLARATION=declaration) | 217 DECLARATION=declaration) |
| 215 | 218 |
| 216 version = [0] | 219 version = [0] |
| 217 def GenerateCall(signature_index, argument_count, checks): | 220 def GenerateCall(signature_index, argument_count, checks): |
| 218 if checks: | 221 if checks: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 239 for i in range(0, argument_count): | 242 for i in range(0, argument_count): |
| 240 argument = signatures[signature_index][i] | 243 argument = signatures[signature_index][i] |
| 241 parameter_name = parameter_names[i] | 244 parameter_name = parameter_names[i] |
| 242 test_type = self._DartType(argument.type.id) | 245 test_type = self._DartType(argument.type.id) |
| 243 | 246 |
| 244 if test_type in ['dynamic', 'Object']: | 247 if test_type in ['dynamic', 'Object']: |
| 245 checks.append('?%s' % parameter_name) | 248 checks.append('?%s' % parameter_name) |
| 246 elif not can_omit_type_check(test_type, i): | 249 elif not can_omit_type_check(test_type, i): |
| 247 checks.append('(%s is %s || %s == null)' % ( | 250 checks.append('(%s is %s || %s == null)' % ( |
| 248 parameter_name, test_type, parameter_name)) | 251 parameter_name, test_type, parameter_name)) |
| 249 else: | 252 elif i >= number_of_required_in_dart: |
| 250 for signature in signatures: | 253 checks.append('?%s' % parameter_name) |
| 251 if (len(signature) <= i or signature[i].id not in | |
| 252 parameter_name.split('_OR_')): | |
| 253 | |
| 254 checks.append('?%s' % parameter_name) | |
| 255 break | |
| 256 | 254 |
| 257 # There can be multiple presence checks. We need them all since a later | 255 # There can be multiple presence checks. We need them all since a later |
| 258 # optional argument could have been passed by name, leaving 'holes'. | 256 # optional argument could have been passed by name, leaving 'holes'. |
| 259 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]]
) | 257 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]]
) |
| 260 | 258 |
| 261 GenerateCall(signature_index, argument_count, checks) | 259 GenerateCall(signature_index, argument_count, checks) |
| 262 | 260 |
| 263 # TODO: Optimize the dispatch to avoid repeated checks. | 261 # TODO: Optimize the dispatch to avoid repeated checks. |
| 264 if len(signatures) > 1: | 262 if len(signatures) > 1: |
| 265 index_swaps = {} | 263 index_swaps = {} |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 # Consider foo(x, optional y, [Default=NullString] optional z) | 298 # Consider foo(x, optional y, [Default=NullString] optional z) |
| 301 # (as of now it's modelled after HTMLMediaElement.webkitAddKey). | 299 # (as of now it's modelled after HTMLMediaElement.webkitAddKey). |
| 302 # y is optional in WebCore, while z is not. | 300 # y is optional in WebCore, while z is not. |
| 303 # In this case, if y was actually passed, we'd like to emit foo(x, y,
z) invocation, | 301 # In this case, if y was actually passed, we'd like to emit foo(x, y,
z) invocation, |
| 304 # not foo(x, y). | 302 # not foo(x, y). |
| 305 GenerateCall(0, argument_count, [check]) | 303 GenerateCall(0, argument_count, [check]) |
| 306 argument_count = argument_position | 304 argument_count = argument_position |
| 307 GenerateCall(0, argument_count, []) | 305 GenerateCall(0, argument_count, []) |
| 308 | 306 |
| 309 def _GenerateDispatcherBody(self, | 307 def _GenerateDispatcherBody(self, |
| 308 info, |
| 310 operations, | 309 operations, |
| 311 parameter_names, | |
| 312 declaration, | 310 declaration, |
| 313 generate_call, | 311 generate_call, |
| 314 is_optional, | 312 is_optional, |
| 315 can_omit_type_check=lambda type, pos: False): | 313 can_omit_type_check=lambda type, pos: False): |
| 316 | 314 |
| 317 def GenerateCall( | 315 def GenerateCall( |
| 318 stmts_emitter, call_emitter, version, signature_index, argument_count): | 316 stmts_emitter, call_emitter, version, signature_index, argument_count): |
| 319 generate_call( | 317 generate_call( |
| 320 stmts_emitter, call_emitter, | 318 stmts_emitter, call_emitter, |
| 321 version, operations[signature_index], argument_count) | 319 version, operations[signature_index], argument_count) |
| 322 | 320 |
| 323 def IsOptional(signature_index, argument): | 321 def IsOptional(signature_index, argument): |
| 324 return is_optional(operations[signature_index], argument) | 322 return is_optional(operations[signature_index], argument) |
| 325 | 323 |
| 326 self._GenerateOverloadDispatcher( | 324 self._GenerateOverloadDispatcher( |
| 325 info, |
| 327 [operation.arguments for operation in operations], | 326 [operation.arguments for operation in operations], |
| 328 operations[0].type.id == 'void', | 327 operations[0].type.id == 'void', |
| 329 parameter_names, | |
| 330 declaration, | 328 declaration, |
| 331 GenerateCall, | 329 GenerateCall, |
| 332 IsOptional, | 330 IsOptional, |
| 333 can_omit_type_check) | 331 can_omit_type_check) |
| 334 | 332 |
| 335 def AdditionalImplementedInterfaces(self): | 333 def AdditionalImplementedInterfaces(self): |
| 336 # TODO: Include all implemented interfaces, including other Lists. | 334 # TODO: Include all implemented interfaces, including other Lists. |
| 337 implements = [] | 335 implements = [] |
| 338 if self._interface_type_info.list_item_type(): | 336 if self._interface_type_info.list_item_type(): |
| 339 item_type_info = self._type_registry.TypeInfo( | 337 item_type_info = self._type_registry.TypeInfo( |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 constructor_info, name, | 428 constructor_info, name, |
| 431 constructor_info.idl_args[signature_index][:argument_count]) | 429 constructor_info.idl_args[signature_index][:argument_count]) |
| 432 | 430 |
| 433 def IsOptional(signature_index, argument): | 431 def IsOptional(signature_index, argument): |
| 434 return self.IsConstructorArgumentOptional(argument) | 432 return self.IsConstructorArgumentOptional(argument) |
| 435 | 433 |
| 436 custom_factory_ctr = self._interface.id in _custom_factories | 434 custom_factory_ctr = self._interface.id in _custom_factories |
| 437 constructor_full_name = constructor_info._ConstructorFullName( | 435 constructor_full_name = constructor_info._ConstructorFullName( |
| 438 self._DartType) | 436 self._DartType) |
| 439 self._GenerateOverloadDispatcher( | 437 self._GenerateOverloadDispatcher( |
| 438 constructor_info, |
| 440 constructor_info.idl_args, | 439 constructor_info.idl_args, |
| 441 False, | 440 False, |
| 442 [info.name for info in constructor_info.param_infos], | |
| 443 emitter.Format('$(METADATA)$FACTORY_KEYWORD $CTOR($PARAMS)', | 441 emitter.Format('$(METADATA)$FACTORY_KEYWORD $CTOR($PARAMS)', |
| 444 FACTORY_KEYWORD=('factory' if not custom_factory_ctr else | 442 FACTORY_KEYWORD=('factory' if not custom_factory_ctr else |
| 445 'static %s' % constructor_full_name), | 443 'static %s' % constructor_full_name), |
| 446 CTOR=(('' if not custom_factory_ctr else '_factory') | 444 CTOR=(('' if not custom_factory_ctr else '_factory') |
| 447 + constructor_full_name), | 445 + constructor_full_name), |
| 448 METADATA=metadata, | 446 METADATA=metadata, |
| 449 PARAMS=constructor_info.ParametersDeclaration(self._DartType)), | 447 PARAMS=constructor_info.ParametersDeclaration(self._DartType)), |
| 450 GenerateCall, | 448 GenerateCall, |
| 451 IsOptional) | 449 IsOptional) |
| 452 | 450 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 if interface.parents: | 587 if interface.parents: |
| 590 parent = interface.parents[0] | 588 parent = interface.parents[0] |
| 591 if IsPureInterface(parent.type.id): | 589 if IsPureInterface(parent.type.id): |
| 592 walk(interface.parents) | 590 walk(interface.parents) |
| 593 else: | 591 else: |
| 594 walk(interface.parents[1:]) | 592 walk(interface.parents[1:]) |
| 595 return result | 593 return result |
| 596 | 594 |
| 597 def _DartType(self, type_name): | 595 def _DartType(self, type_name): |
| 598 return self._type_registry.DartType(type_name) | 596 return self._type_registry.DartType(type_name) |
| OLD | NEW |