| 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 systems to generate | 6 """This module provides shared functionality for systems to generate |
| 7 Dart APIs from the IDL database.""" | 7 Dart APIs from the IDL database.""" |
| 8 | 8 |
| 9 import copy | 9 import copy |
| 10 import re | 10 import re |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 implementation. | 355 implementation. |
| 356 | 356 |
| 357 Args: | 357 Args: |
| 358 rename_type: A function that allows the types to be renamed. | 358 rename_type: A function that allows the types to be renamed. |
| 359 The function is applied to the parameter's dart_type. | 359 The function is applied to the parameter's dart_type. |
| 360 """ | 360 """ |
| 361 return self._FormatParams( | 361 return self._FormatParams( |
| 362 self.param_infos, 'null', | 362 self.param_infos, 'null', |
| 363 lambda param: TypeOrNothing(rename_type(param.dart_type))) | 363 lambda param: TypeOrNothing(rename_type(param.dart_type))) |
| 364 | 364 |
| 365 def ParametersAsArgumentList(self): | 365 def ParametersAsArgumentList(self, parameter_count = None): |
| 366 """Returns a string of the parameter names suitable for passing the | 366 """Returns a string of the parameter names suitable for passing the |
| 367 parameters as arguments. | 367 parameters as arguments. |
| 368 """ | 368 """ |
| 369 return ', '.join(map(lambda param_info: param_info.name, self.param_infos)) | 369 if parameter_count is None: |
| 370 parameter_count = len(self.param_infos) |
| 371 return ', '.join(map( |
| 372 lambda param_info: param_info.name, |
| 373 self.param_infos[:parameter_count])) |
| 370 | 374 |
| 371 def _FormatParams(self, params, default_value, type_fn): | 375 def _FormatParams(self, params, default_value, type_fn): |
| 372 def FormatParam(param): | 376 def FormatParam(param): |
| 373 """Returns a parameter declaration fragment for an ParamInfo.""" | 377 """Returns a parameter declaration fragment for an ParamInfo.""" |
| 374 type = type_fn(param) | 378 type = type_fn(param) |
| 375 if param.is_optional and default_value and default_value != 'null': | 379 if param.is_optional and default_value and default_value != 'null': |
| 376 return '%s%s = %s' % (type, param.name, default_value) | 380 return '%s%s = %s' % (type, param.name, default_value) |
| 377 return '%s%s' % (type, param.name) | 381 return '%s%s' % (type, param.name) |
| 378 | 382 |
| 379 required = [] | 383 required = [] |
| 380 optional = [] | 384 optional = [] |
| 381 for param_info in params: | 385 for param_info in params: |
| 382 if param_info.is_optional: | 386 if param_info.is_optional: |
| 383 optional.append(param_info) | 387 optional.append(param_info) |
| 384 else: | 388 else: |
| 385 if optional: | 389 if optional: |
| 386 raise Exception('Optional parameters cannot precede required ones: ' | 390 raise Exception('Optional parameters cannot precede required ones: ' |
| 387 + str(params)) | 391 + str(params)) |
| 388 required.append(param_info) | 392 required.append(param_info) |
| 389 argtexts = map(FormatParam, required) | 393 argtexts = map(FormatParam, required) |
| 390 if optional: | 394 if optional: |
| 391 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']') | 395 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']') |
| 392 return ', '.join(argtexts) | 396 return ', '.join(argtexts) |
| 393 | 397 |
| 394 def IsStatic(self): | 398 def IsStatic(self): |
| 395 is_static = self.overloads[0].is_static | 399 is_static = self.overloads[0].is_static |
| 396 assert any([is_static == o.is_static for o in self.overloads]) | 400 assert any([is_static == o.is_static for o in self.overloads]) |
| 397 return is_static | 401 return is_static |
| 398 | 402 |
| 399 def ConstructorFullName(self): | 403 def _ConstructorFullName(self): |
| 400 if self.constructor_name: | 404 if self.constructor_name: |
| 401 return self.type_name + '.' + self.constructor_name | 405 return self.type_name + '.' + self.constructor_name |
| 402 else: | 406 else: |
| 403 return self.type_name | 407 return self.type_name |
| 404 | 408 |
| 409 def ConstructorFactoryName(self, rename_type): |
| 410 return 'create' + rename_type(self._ConstructorFullName()).replace('.', '_') |
| 411 |
| 412 def GenerateFactoryInvocation(self, rename_type, emitter, factory_provider): |
| 413 has_optional = any(param_info.is_optional |
| 414 for param_info in self.param_infos) |
| 415 |
| 416 factory_name = self.ConstructorFactoryName(rename_type) |
| 417 if not has_optional: |
| 418 emitter.Emit( |
| 419 '\n' |
| 420 ' factory $CTOR($PARAMS) => ' |
| 421 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', |
| 422 CTOR=rename_type(self._ConstructorFullName()), |
| 423 PARAMS=self.ParametersInterfaceDeclaration(rename_type), |
| 424 FACTORY=factory_provider, |
| 425 CTOR_FACTORY_NAME=factory_name, |
| 426 FACTORY_PARAMS=self.ParametersAsArgumentList()) |
| 427 return |
| 428 |
| 429 dispatcher_emitter = emitter.Emit( |
| 430 '\n' |
| 431 ' factory $CTOR($PARAMS) {\n' |
| 432 '$!DISPATCHER' |
| 433 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
| 434 ' }\n', |
| 435 CTOR=rename_type(self._ConstructorFullName()), |
| 436 PARAMS=self.ParametersInterfaceDeclaration(rename_type), |
| 437 FACTORY=factory_provider, |
| 438 CTOR_FACTORY_NAME=factory_name, |
| 439 FACTORY_PARAMS=self.ParametersAsArgumentList()) |
| 440 |
| 441 # If we have optional parameters, check to see if they are set |
| 442 # and call the appropriate factory method. |
| 443 def EmitOptionalParameterInvocation(index): |
| 444 dispatcher_emitter.Emit( |
| 445 ' if (!?$OPT_PARAM_NAME) {\n' |
| 446 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
| 447 ' }\n', |
| 448 OPT_PARAM_NAME=self.param_infos[index].name, |
| 449 FACTORY=factory_provider, |
| 450 CTOR_FACTORY_NAME=factory_name, |
| 451 FACTORY_PARAMS=self.ParametersAsArgumentList(index)) |
| 452 |
| 453 for index, param_info in enumerate(self.param_infos): |
| 454 if param_info.is_optional: |
| 455 EmitOptionalParameterInvocation(index) |
| 456 |
| 457 |
| 405 def CopyAndWidenDefaultParameters(self): | 458 def CopyAndWidenDefaultParameters(self): |
| 406 """Returns equivalent OperationInfo, but default parameters are Dynamic.""" | 459 """Returns equivalent OperationInfo, but default parameters are Dynamic.""" |
| 407 info = copy.copy(self) | 460 info = copy.copy(self) |
| 408 info.param_infos = [param.Copy() for param in self.param_infos] | 461 info.param_infos = [param.Copy() for param in self.param_infos] |
| 409 for param in info.param_infos: | 462 for param in info.param_infos: |
| 410 if param.is_optional: | 463 if param.is_optional: |
| 411 param.dart_type = 'Dynamic' | 464 param.dart_type = 'Dynamic' |
| 412 return info | 465 return info |
| 413 | 466 |
| 414 | 467 |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 886 if match: | 939 if match: |
| 887 if type_name == 'DOMString[]': | 940 if type_name == 'DOMString[]': |
| 888 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt
ring')) | 941 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt
ring')) |
| 889 item_info = self.TypeInfo(match.group(1) or match.group(2)) | 942 item_info = self.TypeInfo(match.group(1) or match.group(2)) |
| 890 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) | 943 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) |
| 891 if not type_name in _idl_type_registry: | 944 if not type_name in _idl_type_registry: |
| 892 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) | 945 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) |
| 893 type_data = _idl_type_registry.get(type_name) | 946 type_data = _idl_type_registry.get(type_name) |
| 894 class_name = '%sIDLTypeInfo' % type_data.clazz | 947 class_name = '%sIDLTypeInfo' % type_data.clazz |
| 895 return globals()[class_name](type_name, type_data) | 948 return globals()[class_name](type_name, type_data) |
| OLD | NEW |