Chromium Code Reviews| 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) => $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARA MS);\n', | |
|
vsm
2012/09/18 15:57:03
nit: line length
blois
2012/09/18 18:12:26
Done.
| |
| 421 CTOR=rename_type(self._ConstructorFullName()), | |
| 422 PARAMS=self.ParametersInterfaceDeclaration(rename_type), | |
| 423 FACTORY=factory_provider, | |
| 424 CTOR_FACTORY_NAME=factory_name, | |
| 425 FACTORY_PARAMS=self.ParametersAsArgumentList()) | |
| 426 return | |
| 427 | |
| 428 # If we have optional parameters, check to see if they are set | |
| 429 # and call the appropriate factory method. | |
| 430 def EmitOptionalParameterInvocation(index, emitter): | |
|
Anton Muhin
2012/09/18 07:45:34
up to you, another option would be to put definiti
blois
2012/09/18 18:12:26
Done.
| |
| 431 emitter.Emit( | |
| 432 ' if (!?$OPT_PARAM_NAME) {\n' | |
| 433 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | |
| 434 ' }\n', | |
| 435 OPT_PARAM_NAME=self.param_infos[index].name, | |
| 436 FACTORY=factory_provider, | |
| 437 CTOR_FACTORY_NAME=factory_name, | |
| 438 FACTORY_PARAMS=self.ParametersAsArgumentList(index)) | |
| 439 | |
| 440 dispatcher_emitter = emitter.Emit( | |
| 441 '\n' | |
| 442 ' factory $CTOR($PARAMS) {\n' | |
| 443 '$!DISPATCHER' | |
| 444 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | |
| 445 ' }\n', | |
| 446 CTOR=rename_type(self._ConstructorFullName()), | |
| 447 PARAMS=self.ParametersInterfaceDeclaration(rename_type), | |
| 448 FACTORY=factory_provider, | |
| 449 CTOR_FACTORY_NAME=factory_name, | |
| 450 FACTORY_PARAMS=self.ParametersAsArgumentList()) | |
| 451 | |
| 452 for index, param_info in enumerate(self.param_infos): | |
| 453 if param_info.is_optional: | |
| 454 EmitOptionalParameterInvocation(index, dispatcher_emitter) | |
| 455 | |
| 456 | |
| 405 def CopyAndWidenDefaultParameters(self): | 457 def CopyAndWidenDefaultParameters(self): |
| 406 """Returns equivalent OperationInfo, but default parameters are Dynamic.""" | 458 """Returns equivalent OperationInfo, but default parameters are Dynamic.""" |
| 407 info = copy.copy(self) | 459 info = copy.copy(self) |
| 408 info.param_infos = [param.Copy() for param in self.param_infos] | 460 info.param_infos = [param.Copy() for param in self.param_infos] |
| 409 for param in info.param_infos: | 461 for param in info.param_infos: |
| 410 if param.is_optional: | 462 if param.is_optional: |
| 411 param.dart_type = 'Dynamic' | 463 param.dart_type = 'Dynamic' |
| 412 return info | 464 return info |
| 413 | 465 |
| 414 | 466 |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 886 if match: | 938 if match: |
| 887 if type_name == 'DOMString[]': | 939 if type_name == 'DOMString[]': |
| 888 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt ring')) | 940 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt ring')) |
| 889 item_info = self.TypeInfo(match.group(1) or match.group(2)) | 941 item_info = self.TypeInfo(match.group(1) or match.group(2)) |
| 890 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) | 942 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) |
| 891 if not type_name in _idl_type_registry: | 943 if not type_name in _idl_type_registry: |
| 892 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) | 944 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) |
| 893 type_data = _idl_type_registry.get(type_name) | 945 type_data = _idl_type_registry.get(type_name) |
| 894 class_name = '%sIDLTypeInfo' % type_data.clazz | 946 class_name = '%sIDLTypeInfo' % type_data.clazz |
| 895 return globals()[class_name](type_name, type_data) | 947 return globals()[class_name](type_name, type_data) |
| OLD | NEW |