| 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 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 class OperationInfo(object): | 298 class OperationInfo(object): |
| 299 """Holder for various derived information from a set of overloaded operations. | 299 """Holder for various derived information from a set of overloaded operations. |
| 300 | 300 |
| 301 Attributes: | 301 Attributes: |
| 302 overloads: A list of IDL operation overloads with the same name. | 302 overloads: A list of IDL operation overloads with the same name. |
| 303 name: A string, the simple name of the operation. | 303 name: A string, the simple name of the operation. |
| 304 constructor_name: A string, the name of the constructor iff the constructor | 304 constructor_name: A string, the name of the constructor iff the constructor |
| 305 is named, e.g. 'fromList' in Int8Array.fromList(list). | 305 is named, e.g. 'fromList' in Int8Array.fromList(list). |
| 306 type_name: A string, the name of the return type of the operation. | 306 type_name: A string, the name of the return type of the operation. |
| 307 param_infos: A list of ParamInfo. | 307 param_infos: A list of ParamInfo. |
| 308 factory_parameters: A list of parameters used for custom designed Factory | |
| 309 calls. | |
| 310 """ | 308 """ |
| 311 | |
| 312 def __init__(self): | |
| 313 self.factory_parameters = None | |
| 314 | 309 |
| 315 def ParametersDeclaration(self, rename_type, force_optional=False): | 310 def ParametersDeclaration(self, rename_type, force_optional=False): |
| 316 def FormatParam(param): | 311 def FormatParam(param): |
| 317 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' | 312 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' |
| 318 return '%s%s' % (TypeOrNothing(dart_type, param.type_id), param.name) | 313 return '%s%s' % (TypeOrNothing(dart_type, param.type_id), param.name) |
| 319 | 314 |
| 320 required = [] | 315 required = [] |
| 321 optional = [] | 316 optional = [] |
| 322 for param_info in self.param_infos: | 317 for param_info in self.param_infos: |
| 323 if param_info.is_optional: | 318 if param_info.is_optional: |
| (...skipping 30 matching lines...) Expand all Loading... |
| 354 | 349 |
| 355 def _ConstructorFullName(self, rename_type): | 350 def _ConstructorFullName(self, rename_type): |
| 356 if self.constructor_name: | 351 if self.constructor_name: |
| 357 return rename_type(self.type_name) + '.' + self.constructor_name | 352 return rename_type(self.type_name) + '.' + self.constructor_name |
| 358 else: | 353 else: |
| 359 return rename_type(self.type_name) | 354 return rename_type(self.type_name) |
| 360 | 355 |
| 361 def ConstructorFactoryName(self, rename_type): | 356 def ConstructorFactoryName(self, rename_type): |
| 362 return 'create' + self._ConstructorFullName(rename_type).replace('.', '_') | 357 return 'create' + self._ConstructorFullName(rename_type).replace('.', '_') |
| 363 | 358 |
| 364 def GenerateFactoryInvocation(self, rename_type, emitter, factory_name, | 359 def GenerateFactoryInvocation(self, rename_type, emitter, factory_provider): |
| 365 factory_constructor_name=None, factory_parameters=None): | |
| 366 has_optional = any(param_info.is_optional | 360 has_optional = any(param_info.is_optional |
| 367 for param_info in self.param_infos) | 361 for param_info in self.param_infos) |
| 368 | 362 |
| 369 if not factory_constructor_name: | 363 factory_name = self.ConstructorFactoryName(rename_type) |
| 370 factory_constructor_name = self.ConstructorFactoryName(rename_type) | |
| 371 factory_parameters = self.ParametersAsArgumentList() | |
| 372 has_factory_provider = True | |
| 373 else: | |
| 374 factory_parameters = ', '.join(factory_parameters) | |
| 375 has_factory_provider = False | |
| 376 if not has_optional: | 364 if not has_optional: |
| 377 emitter.Emit( | 365 emitter.Emit( |
| 378 '\n' | 366 '\n' |
| 379 ' factory $CTOR($PARAMS) => ' | 367 ' factory $CTOR($PARAMS) => ' |
| 380 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', | 368 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', |
| 381 CTOR=self._ConstructorFullName(rename_type), | 369 CTOR=self._ConstructorFullName(rename_type), |
| 382 PARAMS=self.ParametersDeclaration(rename_type), | 370 PARAMS=self.ParametersDeclaration(rename_type), |
| 383 FACTORY=factory_name, | 371 FACTORY=factory_provider, |
| 384 CTOR_FACTORY_NAME=factory_constructor_name, | 372 CTOR_FACTORY_NAME=factory_name, |
| 385 FACTORY_PARAMS=factory_parameters) | 373 FACTORY_PARAMS=self.ParametersAsArgumentList()) |
| 386 return | 374 return |
| 387 if has_factory_provider: | |
| 388 self._GenerateFactoryOptParams(rename_type, emitter, factory_name) | |
| 389 else: | |
| 390 self._GenerateFactoryOptParamsWithoutFactoryProvider(rename_type, emitter, | |
| 391 factory_name, factory_constructor_name, factory_parameters) | |
| 392 | 375 |
| 393 def _GenerateFactoryOptParams(self, rename_type, emitter, factory_provider): | |
| 394 """Helper method for creating generic factory constructors with optional | |
| 395 parameters that use factory providers.""" | |
| 396 dispatcher_emitter = emitter.Emit( | 376 dispatcher_emitter = emitter.Emit( |
| 397 '\n' | 377 '\n' |
| 398 ' factory $CTOR($PARAMS) {\n' | 378 ' factory $CTOR($PARAMS) {\n' |
| 399 '$!DISPATCHER' | 379 '$!DISPATCHER' |
| 400 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | 380 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
| 401 ' }\n', | 381 ' }\n', |
| 402 CTOR=self._ConstructorFullName(rename_type), | 382 CTOR=self._ConstructorFullName(rename_type), |
| 403 PARAMS=self.ParametersDeclaration(rename_type), | 383 PARAMS=self.ParametersDeclaration(rename_type), |
| 404 FACTORY=factory_provider, | 384 FACTORY=factory_provider, |
| 405 CTOR_FACTORY_NAME=self.ConstructorFactoryName(rename_type), | 385 CTOR_FACTORY_NAME=factory_name, |
| 406 FACTORY_PARAMS=self.ParametersAsArgumentList()) | 386 FACTORY_PARAMS=self.ParametersAsArgumentList()) |
| 407 | 387 |
| 408 # If we have optional parameters, check to see if they are set | 388 # If we have optional parameters, check to see if they are set |
| 409 # and call the appropriate factory method. | 389 # and call the appropriate factory method. |
| 410 def EmitOptionalParameterInvocation(index): | 390 def EmitOptionalParameterInvocation(index): |
| 411 dispatcher_emitter.Emit( | 391 dispatcher_emitter.Emit( |
| 412 ' if (!?$OPT_PARAM_NAME) {\n' | 392 ' if (!?$OPT_PARAM_NAME) {\n' |
| 413 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | 393 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
| 414 ' }\n', | 394 ' }\n', |
| 415 OPT_PARAM_NAME=self.param_infos[index].name, | 395 OPT_PARAM_NAME=self.param_infos[index].name, |
| 416 FACTORY=factory_provider, | 396 FACTORY=factory_provider, |
| 417 CTOR_FACTORY_NAME=self.ConstructorFactoryName(rename_type), | 397 CTOR_FACTORY_NAME=factory_name, |
| 418 FACTORY_PARAMS=self.ParametersAsArgumentList(index)) | 398 FACTORY_PARAMS=self.ParametersAsArgumentList(index)) |
| 419 | 399 |
| 420 for index, param_info in enumerate(self.param_infos): | 400 for index, param_info in enumerate(self.param_infos): |
| 421 if param_info.is_optional: | 401 if param_info.is_optional: |
| 422 EmitOptionalParameterInvocation(index) | 402 EmitOptionalParameterInvocation(index) |
| 423 | |
| 424 def _GenerateFactoryOptParamsWithoutFactoryProvider(self, rename_type, | |
| 425 emitter, factory_name, factory_constructor_name, factory_parameters): | |
| 426 """Helper method for creating a factory constructor with optional | |
| 427 parameters that does not call a factory provider, it simply creates the | |
| 428 object itself. This is currently used for SVGElements and HTMLElements.""" | |
| 429 inits = emitter.Emit( | |
| 430 '\n' | |
| 431 ' factory $CONSTRUCTOR($PARAMS) {\n' | |
| 432 ' var e = $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | |
| 433 '$!INITS' | |
| 434 ' return e;\n' | |
| 435 ' }\n', | |
| 436 CONSTRUCTOR=self._ConstructorFullName(rename_type), | |
| 437 FACTORY=factory_name, | |
| 438 CTOR_FACTORY_NAME=factory_constructor_name, | |
| 439 PARAMS=self.ParametersDeclaration(rename_type, force_optional=True), | |
| 440 FACTORY_PARAMS=factory_parameters) | |
| 441 for index, param_info in enumerate(self.param_infos): | |
| 442 if param_info.is_optional: | |
| 443 inits.Emit(' if (!?$E) e.$E = $E;\n', | |
| 444 E=self.param_infos[index].name) | |
| 445 | 403 |
| 446 def ConstantOutputOrder(a, b): | 404 def ConstantOutputOrder(a, b): |
| 447 """Canonical output ordering for constants.""" | 405 """Canonical output ordering for constants.""" |
| 448 if a.id < b.id: return -1 | 406 if a.id < b.id: return -1 |
| 449 if a.id > b.id: return 1 | 407 if a.id > b.id: return 1 |
| 450 return 0 | 408 return 0 |
| 451 | 409 |
| 452 | 410 |
| 453 def _FormatNameList(names): | 411 def _FormatNameList(names): |
| 454 """Returns JavaScript array literal expression with one name per line.""" | 412 """Returns JavaScript array literal expression with one name per line.""" |
| (...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1124 else: | 1082 else: |
| 1125 dart_interface_name = type_name | 1083 dart_interface_name = type_name |
| 1126 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name, | 1084 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name, |
| 1127 self) | 1085 self) |
| 1128 | 1086 |
| 1129 if type_data.clazz == 'SVGTearOff': | 1087 if type_data.clazz == 'SVGTearOff': |
| 1130 return SVGTearOffIDLTypeInfo(type_name, type_data, self) | 1088 return SVGTearOffIDLTypeInfo(type_name, type_data, self) |
| 1131 | 1089 |
| 1132 class_name = '%sIDLTypeInfo' % type_data.clazz | 1090 class_name = '%sIDLTypeInfo' % type_data.clazz |
| 1133 return globals()[class_name](type_name, type_data) | 1091 return globals()[class_name](type_name, type_data) |
| OLD | NEW |