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. |
308 """ | 310 """ |
| 311 |
| 312 def __init__(self): |
| 313 self.factory_parameters = None |
309 | 314 |
310 def ParametersDeclaration(self, rename_type, force_optional=False): | 315 def ParametersDeclaration(self, rename_type, force_optional=False): |
311 def FormatParam(param): | 316 def FormatParam(param): |
312 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' | 317 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' |
313 return '%s%s' % (TypeOrNothing(dart_type, param.type_id), param.name) | 318 return '%s%s' % (TypeOrNothing(dart_type, param.type_id), param.name) |
314 | 319 |
315 required = [] | 320 required = [] |
316 optional = [] | 321 optional = [] |
317 for param_info in self.param_infos: | 322 for param_info in self.param_infos: |
318 if param_info.is_optional: | 323 if param_info.is_optional: |
(...skipping 30 matching lines...) Expand all Loading... |
349 | 354 |
350 def _ConstructorFullName(self, rename_type): | 355 def _ConstructorFullName(self, rename_type): |
351 if self.constructor_name: | 356 if self.constructor_name: |
352 return rename_type(self.type_name) + '.' + self.constructor_name | 357 return rename_type(self.type_name) + '.' + self.constructor_name |
353 else: | 358 else: |
354 return rename_type(self.type_name) | 359 return rename_type(self.type_name) |
355 | 360 |
356 def ConstructorFactoryName(self, rename_type): | 361 def ConstructorFactoryName(self, rename_type): |
357 return 'create' + self._ConstructorFullName(rename_type).replace('.', '_') | 362 return 'create' + self._ConstructorFullName(rename_type).replace('.', '_') |
358 | 363 |
359 def GenerateFactoryInvocation(self, rename_type, emitter, factory_provider): | 364 def GenerateFactoryInvocation(self, rename_type, emitter, factory_name, |
| 365 factory_constructor_name=None, factory_parameters=None): |
360 has_optional = any(param_info.is_optional | 366 has_optional = any(param_info.is_optional |
361 for param_info in self.param_infos) | 367 for param_info in self.param_infos) |
362 | 368 |
363 factory_name = self.ConstructorFactoryName(rename_type) | 369 if not factory_constructor_name: |
| 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 |
364 if not has_optional: | 376 if not has_optional: |
365 emitter.Emit( | 377 emitter.Emit( |
366 '\n' | 378 '\n' |
367 ' factory $CTOR($PARAMS) => ' | 379 ' factory $CTOR($PARAMS) => ' |
368 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', | 380 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', |
369 CTOR=self._ConstructorFullName(rename_type), | 381 CTOR=self._ConstructorFullName(rename_type), |
370 PARAMS=self.ParametersDeclaration(rename_type), | 382 PARAMS=self.ParametersDeclaration(rename_type), |
371 FACTORY=factory_provider, | 383 FACTORY=factory_name, |
372 CTOR_FACTORY_NAME=factory_name, | 384 CTOR_FACTORY_NAME=factory_constructor_name, |
373 FACTORY_PARAMS=self.ParametersAsArgumentList()) | 385 FACTORY_PARAMS=factory_parameters) |
374 return | 386 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) |
375 | 392 |
| 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.""" |
376 dispatcher_emitter = emitter.Emit( | 396 dispatcher_emitter = emitter.Emit( |
377 '\n' | 397 '\n' |
378 ' factory $CTOR($PARAMS) {\n' | 398 ' factory $CTOR($PARAMS) {\n' |
379 '$!DISPATCHER' | 399 '$!DISPATCHER' |
380 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | 400 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
381 ' }\n', | 401 ' }\n', |
382 CTOR=self._ConstructorFullName(rename_type), | 402 CTOR=self._ConstructorFullName(rename_type), |
383 PARAMS=self.ParametersDeclaration(rename_type), | 403 PARAMS=self.ParametersDeclaration(rename_type), |
384 FACTORY=factory_provider, | 404 FACTORY=factory_provider, |
385 CTOR_FACTORY_NAME=factory_name, | 405 CTOR_FACTORY_NAME=self.ConstructorFactoryName(rename_type), |
386 FACTORY_PARAMS=self.ParametersAsArgumentList()) | 406 FACTORY_PARAMS=self.ParametersAsArgumentList()) |
387 | 407 |
388 # If we have optional parameters, check to see if they are set | 408 # If we have optional parameters, check to see if they are set |
389 # and call the appropriate factory method. | 409 # and call the appropriate factory method. |
390 def EmitOptionalParameterInvocation(index): | 410 def EmitOptionalParameterInvocation(index): |
391 dispatcher_emitter.Emit( | 411 dispatcher_emitter.Emit( |
392 ' if (!?$OPT_PARAM_NAME) {\n' | 412 ' if (!?$OPT_PARAM_NAME) {\n' |
393 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | 413 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
394 ' }\n', | 414 ' }\n', |
395 OPT_PARAM_NAME=self.param_infos[index].name, | 415 OPT_PARAM_NAME=self.param_infos[index].name, |
396 FACTORY=factory_provider, | 416 FACTORY=factory_provider, |
397 CTOR_FACTORY_NAME=factory_name, | 417 CTOR_FACTORY_NAME=self.ConstructorFactoryName(rename_type), |
398 FACTORY_PARAMS=self.ParametersAsArgumentList(index)) | 418 FACTORY_PARAMS=self.ParametersAsArgumentList(index)) |
399 | 419 |
400 for index, param_info in enumerate(self.param_infos): | 420 for index, param_info in enumerate(self.param_infos): |
401 if param_info.is_optional: | 421 if param_info.is_optional: |
402 EmitOptionalParameterInvocation(index) | 422 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) |
403 | 445 |
404 def ConstantOutputOrder(a, b): | 446 def ConstantOutputOrder(a, b): |
405 """Canonical output ordering for constants.""" | 447 """Canonical output ordering for constants.""" |
406 if a.id < b.id: return -1 | 448 if a.id < b.id: return -1 |
407 if a.id > b.id: return 1 | 449 if a.id > b.id: return 1 |
408 return 0 | 450 return 0 |
409 | 451 |
410 | 452 |
411 def _FormatNameList(names): | 453 def _FormatNameList(names): |
412 """Returns JavaScript array literal expression with one name per line.""" | 454 """Returns JavaScript array literal expression with one name per line.""" |
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1084 else: | 1126 else: |
1085 dart_interface_name = type_name | 1127 dart_interface_name = type_name |
1086 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name, | 1128 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name, |
1087 self) | 1129 self) |
1088 | 1130 |
1089 if type_data.clazz == 'SVGTearOff': | 1131 if type_data.clazz == 'SVGTearOff': |
1090 return SVGTearOffIDLTypeInfo(type_name, type_data, self) | 1132 return SVGTearOffIDLTypeInfo(type_name, type_data, self) |
1091 | 1133 |
1092 class_name = '%sIDLTypeInfo' % type_data.clazz | 1134 class_name = '%sIDLTypeInfo' % type_data.clazz |
1093 return globals()[class_name](type_name, type_data) | 1135 return globals()[class_name](type_name, type_data) |
OLD | NEW |