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 json | 10 import json |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 is named, e.g. 'fromList' in Int8Array.fromList(list). | 407 is named, e.g. 'fromList' in Int8Array.fromList(list). |
408 type_name: A string, the name of the return type of the operation. | 408 type_name: A string, the name of the return type of the operation. |
409 param_infos: A list of ParamInfo. | 409 param_infos: A list of ParamInfo. |
410 factory_parameters: A list of parameters used for custom designed Factory | 410 factory_parameters: A list of parameters used for custom designed Factory |
411 calls. | 411 calls. |
412 """ | 412 """ |
413 | 413 |
414 def __init__(self): | 414 def __init__(self): |
415 self.factory_parameters = None | 415 self.factory_parameters = None |
416 | 416 |
417 def ParametersDeclaration(self, rename_type, force_optional=False): | 417 def ParametersAsDecVarLists(self, rename_type, force_optional=False): |
| 418 """ Returns a tuple (required, optional, named), where: |
| 419 required is a list of parameter declarations corresponding to the |
| 420 required parameters |
| 421 optional is a list of parameter declarations corresponding to the |
| 422 optional parameters |
| 423 named is a boolean which is true if the optional parameters should |
| 424 be named |
| 425 A parameter declaration is a tuple (dec, var) where var is the |
| 426 variable name, and dec is a string suitable for declaring the |
| 427 variable in a parameter list. That is, dec + var is a valid |
| 428 parameter declaration. |
| 429 """ |
418 def FormatParam(param): | 430 def FormatParam(param): |
419 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' | 431 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' |
420 return '%s%s' % (TypeOrNothing(dart_type, param.type_id), param.name) | 432 return (TypeOrNothing(dart_type, param.type_id), param.name) |
421 | |
422 required = [] | 433 required = [] |
423 optional = [] | 434 optional = [] |
424 for param_info in self.param_infos: | 435 for param_info in self.param_infos: |
425 if param_info.is_optional: | 436 if param_info.is_optional: |
426 optional.append(param_info) | 437 optional.append(FormatParam(param_info)) |
427 else: | 438 else: |
428 if optional: | 439 if optional: |
429 raise Exception('Optional parameters cannot precede required ones: ' | 440 raise Exception('Optional parameters cannot precede required ones: ' |
430 + str(params)) | 441 + str(params)) |
431 required.append(param_info) | 442 required.append(FormatParam(param_info)) |
| 443 needs_named = optional and self.requires_named_arguments and not force_optio
nal |
| 444 return (required, optional, needs_named) |
| 445 |
| 446 def ParametersAsDecStringList(self, rename_type, force_optional=False): |
| 447 """Returns a list of strings where each string corresponds to a parameter |
| 448 declaration. All of the optional/named parameters if any will appear as |
| 449 a single entry at the end of the list. |
| 450 """ |
| 451 (required, optional, needs_named) = \ |
| 452 self.ParametersAsDecVarLists(rename_type, force_optional) |
| 453 def FormatParam(dec): |
| 454 return dec[0] + dec[1] |
432 argtexts = map(FormatParam, required) | 455 argtexts = map(FormatParam, required) |
433 if optional: | 456 if optional: |
434 needs_named = self.requires_named_arguments and not force_optional | |
435 left_bracket, right_bracket = '{}' if needs_named else '[]' | 457 left_bracket, right_bracket = '{}' if needs_named else '[]' |
436 argtexts.append( | 458 argtexts.append( |
437 left_bracket + | 459 left_bracket + |
438 ', '.join(map(FormatParam, optional)) + | 460 ', '.join(map(FormatParam, optional)) + |
439 right_bracket) | 461 right_bracket) |
440 return ', '.join(argtexts) | 462 return argtexts |
| 463 |
| 464 def ParametersAsDeclaration(self, rename_type, force_optional=False): |
| 465 p_list = self.ParametersAsDecStringList(rename_type, force_optional) |
| 466 return ', '.join(p_list) |
441 | 467 |
442 def NumberOfRequiredInDart(self): | 468 def NumberOfRequiredInDart(self): |
443 """ Returns a number of required arguments in Dart declaration of | 469 """ Returns a number of required arguments in Dart declaration of |
444 the operation. | 470 the operation. |
445 """ | 471 """ |
446 return len(filter(lambda i: not i.is_optional, self.param_infos)) | 472 return len(filter(lambda i: not i.is_optional, self.param_infos)) |
447 | 473 |
448 def ParametersAsArgumentList(self, parameter_count=None): | 474 def ParametersAsArgumentList(self, parameter_count=None): |
449 """Returns a string of the parameter names suitable for passing the | 475 """Returns a string of the parameter names suitable for passing the |
450 parameters as arguments. | 476 parameters as arguments. |
451 """ | 477 """ |
452 def param_name(param_info): | 478 def param_name(param_info): |
453 if self.requires_named_arguments and param_info.is_optional: | 479 if self.requires_named_arguments and param_info.is_optional: |
454 return '%s : %s' % (param_info.name, param_info.name) | 480 return '%s : %s' % (param_info.name, param_info.name) |
455 else: | 481 else: |
456 return param_info.name | 482 return param_info.name |
457 | 483 |
458 if parameter_count is None: | 484 if parameter_count is None: |
459 parameter_count = len(self.param_infos) | 485 parameter_count = len(self.param_infos) |
460 return ', '.join(map(param_name, self.param_infos[:parameter_count])) | 486 return ', '.join(map(param_name, self.param_infos[:parameter_count])) |
461 | 487 |
| 488 def ParametersAsListOfVariables(self, parameter_count=None): |
| 489 """Returns a list of the first parameter_count parameter names |
| 490 as raw variables. |
| 491 """ |
| 492 if parameter_count is None: |
| 493 parameter_count = len(self.param_infos) |
| 494 return [p.name for p in self.param_infos[:parameter_count]] |
| 495 |
| 496 def ParametersAsStringOfVariables(self, parameter_count=None): |
| 497 """Returns a string containing the first parameter_count parameter names |
| 498 as raw variables, comma separated. |
| 499 """ |
| 500 return ', '.join(self.ParametersAsListOfVariables(parameter_count)) |
| 501 |
462 def IsStatic(self): | 502 def IsStatic(self): |
463 is_static = self.overloads[0].is_static | 503 is_static = self.overloads[0].is_static |
464 assert any([is_static == o.is_static for o in self.overloads]) | 504 assert any([is_static == o.is_static for o in self.overloads]) |
465 return is_static | 505 return is_static |
466 | 506 |
467 def _ConstructorFullName(self, rename_type): | 507 def _ConstructorFullName(self, rename_type): |
468 if self.constructor_name: | 508 if self.constructor_name: |
469 return rename_type(self.type_name) + '.' + self.constructor_name | 509 return rename_type(self.type_name) + '.' + self.constructor_name |
470 else: | 510 else: |
471 # TODO(antonm): temporary ugly hack. | 511 # TODO(antonm): temporary ugly hack. |
(...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1271 if type_data.clazz == 'BasicTypedList': | 1311 if type_data.clazz == 'BasicTypedList': |
1272 if type_name == 'ArrayBuffer': | 1312 if type_name == 'ArrayBuffer': |
1273 dart_interface_name = 'ByteBuffer' | 1313 dart_interface_name = 'ByteBuffer' |
1274 else: | 1314 else: |
1275 dart_interface_name = self._renamer.RenameInterfaceId(type_name) | 1315 dart_interface_name = self._renamer.RenameInterfaceId(type_name) |
1276 return BasicTypedListIDLTypeInfo( | 1316 return BasicTypedListIDLTypeInfo( |
1277 type_name, type_data, dart_interface_name, self) | 1317 type_name, type_data, dart_interface_name, self) |
1278 | 1318 |
1279 class_name = '%sIDLTypeInfo' % type_data.clazz | 1319 class_name = '%sIDLTypeInfo' % type_data.clazz |
1280 return globals()[class_name](type_name, type_data) | 1320 return globals()[class_name](type_name, type_data) |
OLD | NEW |