| 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 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 overloads: A list of IDL operation overloads with the same name. | 338 overloads: A list of IDL operation overloads with the same name. |
| 339 name: A string, the simple name of the operation. | 339 name: A string, the simple name of the operation. |
| 340 constructor_name: A string, the name of the constructor iff the constructor | 340 constructor_name: A string, the name of the constructor iff the constructor |
| 341 is named, e.g. 'fromList' in Int8Array.fromList(list). | 341 is named, e.g. 'fromList' in Int8Array.fromList(list). |
| 342 type_name: A string, the name of the return type of the operation. | 342 type_name: A string, the name of the return type of the operation. |
| 343 param_infos: A list of ParamInfo. | 343 param_infos: A list of ParamInfo. |
| 344 """ | 344 """ |
| 345 | 345 |
| 346 def ParametersInterfaceDeclaration(self, rename_type): | 346 def ParametersInterfaceDeclaration(self, rename_type): |
| 347 """Returns a formatted string declaring the parameters for the interface.""" | 347 """Returns a formatted string declaring the parameters for the interface.""" |
| 348 return self._FormatParams( | 348 def type_function(param): |
| 349 self.param_infos, None, | 349 # TODO(podivilov): replace param.dart_type field with param.is_optional |
| 350 lambda param: TypeOrNothing(rename_type(param.dart_type), param.type_id)
) | 350 dart_type = param.dart_type |
| 351 if dart_type != 'Dynamic': |
| 352 dart_type = rename_type(dart_type) |
| 353 return TypeOrNothing(dart_type, param.type_id) |
| 354 return self._FormatParams(self.param_infos, None, type_function) |
| 351 | 355 |
| 352 def ParametersImplementationDeclaration(self, rename_type): | 356 def ParametersImplementationDeclaration(self, rename_type): |
| 353 """Returns a formatted string declaring the parameters for the | 357 """Returns a formatted string declaring the parameters for the |
| 354 implementation. | 358 implementation. |
| 355 | 359 |
| 356 Args: | 360 Args: |
| 357 rename_type: A function that allows the types to be renamed. | 361 rename_type: A function that allows the types to be renamed. |
| 358 The function is applied to the parameter's dart_type. | 362 The function is applied to the parameter's dart_type. |
| 359 """ | 363 """ |
| 360 return self._FormatParams( | 364 def type_function(param): |
| 361 self.param_infos, 'null', | 365 # TODO(podivilov): replace param.dart_type field with param.is_optional |
| 362 lambda param: TypeOrNothing(rename_type(param.dart_type))) | 366 dart_type = param.dart_type |
| 367 if dart_type != 'Dynamic': |
| 368 dart_type = rename_type(dart_type) |
| 369 return TypeOrNothing(dart_type) |
| 370 return self._FormatParams(self.param_infos, 'null', type_function) |
| 363 | 371 |
| 364 def ParametersAsArgumentList(self, parameter_count = None): | 372 def ParametersAsArgumentList(self, parameter_count = None): |
| 365 """Returns a string of the parameter names suitable for passing the | 373 """Returns a string of the parameter names suitable for passing the |
| 366 parameters as arguments. | 374 parameters as arguments. |
| 367 """ | 375 """ |
| 368 if parameter_count is None: | 376 if parameter_count is None: |
| 369 parameter_count = len(self.param_infos) | 377 parameter_count = len(self.param_infos) |
| 370 return ', '.join(map( | 378 return ', '.join(map( |
| 371 lambda param_info: param_info.name, | 379 lambda param_info: param_info.name, |
| 372 self.param_infos[:parameter_count])) | 380 self.param_infos[:parameter_count])) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 392 argtexts = map(FormatParam, required) | 400 argtexts = map(FormatParam, required) |
| 393 if optional: | 401 if optional: |
| 394 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']') | 402 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']') |
| 395 return ', '.join(argtexts) | 403 return ', '.join(argtexts) |
| 396 | 404 |
| 397 def IsStatic(self): | 405 def IsStatic(self): |
| 398 is_static = self.overloads[0].is_static | 406 is_static = self.overloads[0].is_static |
| 399 assert any([is_static == o.is_static for o in self.overloads]) | 407 assert any([is_static == o.is_static for o in self.overloads]) |
| 400 return is_static | 408 return is_static |
| 401 | 409 |
| 402 def _ConstructorFullName(self): | 410 def _ConstructorFullName(self, rename_type): |
| 403 if self.constructor_name: | 411 if self.constructor_name: |
| 404 return self.type_name + '.' + self.constructor_name | 412 return rename_type(self.type_name) + '.' + self.constructor_name |
| 405 else: | 413 else: |
| 406 return self.type_name | 414 return rename_type(self.type_name) |
| 407 | 415 |
| 408 def ConstructorFactoryName(self, rename_type): | 416 def ConstructorFactoryName(self, rename_type): |
| 409 return 'create' + rename_type(self._ConstructorFullName()).replace('.', '_') | 417 return 'create' + self._ConstructorFullName(rename_type).replace('.', '_') |
| 410 | 418 |
| 411 def GenerateFactoryInvocation(self, rename_type, emitter, factory_provider): | 419 def GenerateFactoryInvocation(self, rename_type, emitter, factory_provider): |
| 412 has_optional = any(param_info.is_optional | 420 has_optional = any(param_info.is_optional |
| 413 for param_info in self.param_infos) | 421 for param_info in self.param_infos) |
| 414 | 422 |
| 415 factory_name = self.ConstructorFactoryName(rename_type) | 423 factory_name = self.ConstructorFactoryName(rename_type) |
| 416 if not has_optional: | 424 if not has_optional: |
| 417 emitter.Emit( | 425 emitter.Emit( |
| 418 '\n' | 426 '\n' |
| 419 ' factory $CTOR($PARAMS) => ' | 427 ' factory $CTOR($PARAMS) => ' |
| 420 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', | 428 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', |
| 421 CTOR=rename_type(self._ConstructorFullName()), | 429 CTOR=self._ConstructorFullName(rename_type), |
| 422 PARAMS=self.ParametersInterfaceDeclaration(rename_type), | 430 PARAMS=self.ParametersInterfaceDeclaration(rename_type), |
| 423 FACTORY=factory_provider, | 431 FACTORY=factory_provider, |
| 424 CTOR_FACTORY_NAME=factory_name, | 432 CTOR_FACTORY_NAME=factory_name, |
| 425 FACTORY_PARAMS=self.ParametersAsArgumentList()) | 433 FACTORY_PARAMS=self.ParametersAsArgumentList()) |
| 426 return | 434 return |
| 427 | 435 |
| 428 dispatcher_emitter = emitter.Emit( | 436 dispatcher_emitter = emitter.Emit( |
| 429 '\n' | 437 '\n' |
| 430 ' factory $CTOR($PARAMS) {\n' | 438 ' factory $CTOR($PARAMS) {\n' |
| 431 '$!DISPATCHER' | 439 '$!DISPATCHER' |
| 432 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | 440 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
| 433 ' }\n', | 441 ' }\n', |
| 434 CTOR=rename_type(self._ConstructorFullName()), | 442 CTOR=self._ConstructorFullName(rename_type), |
| 435 PARAMS=self.ParametersInterfaceDeclaration(rename_type), | 443 PARAMS=self.ParametersInterfaceDeclaration(rename_type), |
| 436 FACTORY=factory_provider, | 444 FACTORY=factory_provider, |
| 437 CTOR_FACTORY_NAME=factory_name, | 445 CTOR_FACTORY_NAME=factory_name, |
| 438 FACTORY_PARAMS=self.ParametersAsArgumentList()) | 446 FACTORY_PARAMS=self.ParametersAsArgumentList()) |
| 439 | 447 |
| 440 # If we have optional parameters, check to see if they are set | 448 # If we have optional parameters, check to see if they are set |
| 441 # and call the appropriate factory method. | 449 # and call the appropriate factory method. |
| 442 def EmitOptionalParameterInvocation(index): | 450 def EmitOptionalParameterInvocation(index): |
| 443 dispatcher_emitter.Emit( | 451 dispatcher_emitter.Emit( |
| 444 ' if (!?$OPT_PARAM_NAME) {\n' | 452 ' if (!?$OPT_PARAM_NAME) {\n' |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 return ['"Dart%s.h"' % include for include in includes] | 682 return ['"Dart%s.h"' % include for include in includes] |
| 675 | 683 |
| 676 def to_dart_conversion(self, value, interface_name=None, attributes=None): | 684 def to_dart_conversion(self, value, interface_name=None, attributes=None): |
| 677 return 'Dart%s::toDart(%s)' % (self._idl_type, value) | 685 return 'Dart%s::toDart(%s)' % (self._idl_type, value) |
| 678 | 686 |
| 679 def custom_to_dart(self): | 687 def custom_to_dart(self): |
| 680 return self._data.custom_to_dart | 688 return self._data.custom_to_dart |
| 681 | 689 |
| 682 | 690 |
| 683 class InterfaceIDLTypeInfo(IDLTypeInfo): | 691 class InterfaceIDLTypeInfo(IDLTypeInfo): |
| 684 def __init__(self, idl_type, data): | 692 def __init__(self, idl_type, data, dart_interface_name): |
| 685 super(InterfaceIDLTypeInfo, self).__init__(idl_type, data) | 693 super(InterfaceIDLTypeInfo, self).__init__(idl_type, data) |
| 694 self._dart_interface_name = dart_interface_name |
| 695 |
| 696 def dart_type(self): |
| 697 return self._data.dart_type or self._dart_interface_name |
| 686 | 698 |
| 687 | 699 |
| 688 class SequenceIDLTypeInfo(IDLTypeInfo): | 700 class SequenceIDLTypeInfo(IDLTypeInfo): |
| 689 def __init__(self, idl_type, data, item_info): | 701 def __init__(self, idl_type, data, item_info): |
| 690 super(SequenceIDLTypeInfo, self).__init__(idl_type, data) | 702 super(SequenceIDLTypeInfo, self).__init__(idl_type, data) |
| 691 self._item_info = item_info | 703 self._item_info = item_info |
| 692 | 704 |
| 693 def dart_type(self): | 705 def dart_type(self): |
| 694 return 'List<%s>' % self._item_info.dart_type() | 706 return 'List<%s>' % self._item_info.dart_type() |
| 695 | 707 |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 961 self._database = database | 973 self._database = database |
| 962 self._renamer = renamer | 974 self._renamer = renamer |
| 963 self._cache = {} | 975 self._cache = {} |
| 964 | 976 |
| 965 def TypeInfo(self, type_name): | 977 def TypeInfo(self, type_name): |
| 966 if not type_name in self._cache: | 978 if not type_name in self._cache: |
| 967 self._cache[type_name] = self._TypeInfo(type_name) | 979 self._cache[type_name] = self._TypeInfo(type_name) |
| 968 return self._cache[type_name] | 980 return self._cache[type_name] |
| 969 | 981 |
| 970 def DartType(self, type_name): | 982 def DartType(self, type_name): |
| 971 dart_type = self.TypeInfo(type_name).dart_type() | 983 return self.TypeInfo(type_name).dart_type() |
| 972 if self._database.HasInterface(dart_type): | |
| 973 interface = self._database.GetInterface(dart_type) | |
| 974 if self._renamer: | |
| 975 return self._renamer.RenameInterface(interface) | |
| 976 else: | |
| 977 return interface.ext_attrs.get('InterfaceName', interface.id) | |
| 978 return dart_type | |
| 979 | 984 |
| 980 def _TypeInfo(self, type_name): | 985 def _TypeInfo(self, type_name): |
| 981 match = re.match(r'(?:sequence<(\w+)>|(\w+)\[\])$', type_name) | 986 match = re.match(r'(?:sequence<(\w+)>|(\w+)\[\])$', type_name) |
| 982 if match: | 987 if match: |
| 983 if type_name == 'DOMString[]': | 988 if type_name == 'DOMString[]': |
| 984 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt
ring')) | 989 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt
ring')) |
| 985 item_info = self.TypeInfo(match.group(1) or match.group(2)) | 990 item_info = self.TypeInfo(match.group(1) or match.group(2)) |
| 986 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) | 991 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) |
| 992 |
| 987 if not type_name in _idl_type_registry: | 993 if not type_name in _idl_type_registry: |
| 988 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) | 994 interface = self._database.GetInterface(type_name) |
| 995 return InterfaceIDLTypeInfo( |
| 996 type_name, |
| 997 TypeData('Interface'), |
| 998 self._renamer.RenameInterface(interface)) |
| 999 |
| 989 type_data = _idl_type_registry.get(type_name) | 1000 type_data = _idl_type_registry.get(type_name) |
| 1001 if type_data.clazz == 'Interface': |
| 1002 if self._database.HasInterface(type_name): |
| 1003 dart_interface_name = self._renamer.RenameInterface( |
| 1004 self._database.GetInterface(type_name)) |
| 1005 else: |
| 1006 dart_interface_name = type_name |
| 1007 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name) |
| 1008 |
| 990 class_name = '%sIDLTypeInfo' % type_data.clazz | 1009 class_name = '%sIDLTypeInfo' % type_data.clazz |
| 991 return globals()[class_name](type_name, type_data) | 1010 return globals()[class_name](type_name, type_data) |
| OLD | NEW |