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 re | 9 import re |
10 | 10 |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 else: | 342 else: |
343 return '\n' | 343 return '\n' |
344 return ''.join(FormatLine(line) for line in text.split('\n')) | 344 return ''.join(FormatLine(line) for line in text.split('\n')) |
345 | 345 |
346 # Given a sorted sequence of type identifiers, return an appropriate type | 346 # Given a sorted sequence of type identifiers, return an appropriate type |
347 # name | 347 # name |
348 def TypeName(typeIds, interface): | 348 def TypeName(typeIds, interface): |
349 # Dynamically type this field for now. | 349 # Dynamically type this field for now. |
350 return 'var' | 350 return 'var' |
351 | 351 |
| 352 # ------------------------------------------------------------------------------ |
| 353 |
| 354 class IDLTypeInfo(object): |
| 355 def __init__(self, idl_type, native_type=None, ref_counted=True, |
| 356 has_dart_wrapper=True, conversion_template=None, |
| 357 custom_to_dart=False): |
| 358 self._idl_type = idl_type |
| 359 self._native_type = native_type |
| 360 self._ref_counted = ref_counted |
| 361 self._has_dart_wrapper = has_dart_wrapper |
| 362 self._conversion_template = conversion_template |
| 363 self._custom_to_dart = custom_to_dart |
| 364 |
| 365 def idl_type(self): |
| 366 return self._idl_type |
| 367 |
| 368 def native_type(self): |
| 369 if self._native_type: |
| 370 return self._native_type |
| 371 return self._idl_type |
| 372 |
| 373 def parameter_adapter_info(self): |
| 374 native_type = self.native_type() |
| 375 if self._ref_counted: |
| 376 native_type = 'RefPtr< %s >' % native_type |
| 377 if self._has_dart_wrapper: |
| 378 wrapper_type = 'Dart%s' % self.idl_type() |
| 379 adapter_type = 'ParameterAdapter<%s, %s>' % (native_type, wrapper_type) |
| 380 return (adapter_type, wrapper_type) |
| 381 return ('ParameterAdapter< %s >' % native_type, self._idl_type) |
| 382 |
| 383 def parameter_type(self): |
| 384 return '%s*' % self.native_type() |
| 385 |
| 386 def webcore_include(self): |
| 387 if self._idl_type == 'SVGNumber' or self._idl_type == 'SVGPoint': |
| 388 return None |
| 389 if self._idl_type.startswith('SVGPathSeg'): |
| 390 return self._idl_type.replace('Abs', '').replace('Rel', '') |
| 391 return self._idl_type |
| 392 |
| 393 def receiver(self): |
| 394 return 'receiver->' |
| 395 |
| 396 def conversion_include(self): |
| 397 return 'Dart%s' % self._idl_type |
| 398 |
| 399 def conversion_cast(self, expression): |
| 400 if self._conversion_template: |
| 401 return self._conversion_template % expression |
| 402 return expression |
| 403 |
| 404 def custom_to_dart(self): |
| 405 return self._custom_to_dart |
| 406 |
| 407 class PrimitiveIDLTypeInfo(IDLTypeInfo): |
| 408 def __init__(self, idl_type, native_type=None, ref_counted=False, |
| 409 conversion_template=None, |
| 410 webcore_getter_name='getAttribute', |
| 411 webcore_setter_name='setAttribute'): |
| 412 super(PrimitiveIDLTypeInfo, self).__init__(idl_type, |
| 413 native_type=native_type, ref_counted=ref_counted, |
| 414 conversion_template=conversion_template) |
| 415 self._webcore_getter_name = webcore_getter_name |
| 416 self._webcore_setter_name = webcore_setter_name |
| 417 |
| 418 def parameter_adapter_info(self): |
| 419 native_type = self.native_type() |
| 420 if self._ref_counted: |
| 421 native_type = 'RefPtr< %s >' % native_type |
| 422 return ('ParameterAdapter< %s >' % native_type, None) |
| 423 |
| 424 def parameter_type(self): |
| 425 if self.native_type() == 'String': |
| 426 return 'const String&' |
| 427 return self.native_type() |
| 428 |
| 429 def conversion_include(self): |
| 430 return None |
| 431 |
| 432 def webcore_getter_name(self): |
| 433 return self._webcore_getter_name |
| 434 |
| 435 def webcore_setter_name(self): |
| 436 return self._webcore_setter_name |
| 437 |
| 438 class SVGTearOffIDLTypeInfo(IDLTypeInfo): |
| 439 def __init__(self, idl_type, native_type='', ref_counted=True): |
| 440 super(SVGTearOffIDLTypeInfo, self).__init__(idl_type, |
| 441 native_type=native_type, |
| 442 ref_counted=ref_counted) |
| 443 |
| 444 def native_type(self): |
| 445 if self._native_type: |
| 446 return self._native_type |
| 447 tear_off_type = 'SVGPropertyTearOff' |
| 448 if self._idl_type.endswith('List'): |
| 449 tear_off_type = 'SVGListPropertyTearOff' |
| 450 return '%s<%s>' % (tear_off_type, self._idl_type) |
| 451 |
| 452 def receiver(self): |
| 453 if self._idl_type.endswith('List'): |
| 454 return 'receiver->' |
| 455 return 'receiver->propertyReference().' |
| 456 |
| 457 |
| 458 _idl_type_registry = { |
| 459 # There is GC3Dboolean which is not a bool, but unsigned char for OpenGL co
mpatibility. |
| 460 'boolean': PrimitiveIDLTypeInfo('boolean', native_type='bool', |
| 461 conversion_template='static_cast<bool>(%s)', |
| 462 webcore_getter_name='hasAttribute', |
| 463 webcore_setter_name='setBooleanAttribute'), |
| 464 # Some IDL's unsigned shorts/shorts are mapped to WebCore C++ enums, so we |
| 465 # use a static_cast<int> here not to provide overloads for all enums. |
| 466 'short': PrimitiveIDLTypeInfo('short', native_type='int', conversion_templat
e='static_cast<int>(%s)'), |
| 467 'unsigned short': PrimitiveIDLTypeInfo('unsigned short', native_type='int',
conversion_template='static_cast<int>(%s)'), |
| 468 'int': PrimitiveIDLTypeInfo('int'), |
| 469 'unsigned int': PrimitiveIDLTypeInfo('unsigned int', native_type='unsigned')
, |
| 470 'long': PrimitiveIDLTypeInfo('long', native_type='int', |
| 471 webcore_getter_name='getIntegralAttribute', |
| 472 webcore_setter_name='setIntegralAttribute'), |
| 473 'unsigned long': PrimitiveIDLTypeInfo('unsigned long', native_type='unsigned
', |
| 474 webcore_getter_name='getUnsignedIntegralAttribute', |
| 475 webcore_setter_name='setUnsignedIntegralAttribute'), |
| 476 'long long': PrimitiveIDLTypeInfo('long long'), |
| 477 'unsigned long long': PrimitiveIDLTypeInfo('unsigned long long'), |
| 478 'double': PrimitiveIDLTypeInfo('double'), |
| 479 |
| 480 'Date': PrimitiveIDLTypeInfo('Date', native_type='double'), |
| 481 'DOMString': PrimitiveIDLTypeInfo('DOMString', native_type='String'), |
| 482 'DOMTimeStamp': PrimitiveIDLTypeInfo('DOMTimeStamp'), |
| 483 'object': PrimitiveIDLTypeInfo('object', native_type='ScriptValue'), |
| 484 'SerializedScriptValue': PrimitiveIDLTypeInfo('SerializedScriptValue', ref_c
ounted=True), |
| 485 |
| 486 'DOMException': IDLTypeInfo('DOMCoreException'), |
| 487 'DOMWindow': IDLTypeInfo('DOMWindow', custom_to_dart=True), |
| 488 'Element': IDLTypeInfo('Element', custom_to_dart=True), |
| 489 'EventListener': IDLTypeInfo('EventListener', has_dart_wrapper=False), |
| 490 'EventTarget': IDLTypeInfo('EventTarget', has_dart_wrapper=False), |
| 491 'HTMLElement': IDLTypeInfo('HTMLElement', custom_to_dart=True), |
| 492 'MediaQueryListListener': IDLTypeInfo('MediaQueryListListener', has_dart_wra
pper=False), |
| 493 'OptionsObject': IDLTypeInfo('OptionsObject', has_dart_wrapper=False), |
| 494 'SVGElement': IDLTypeInfo('SVGElement', custom_to_dart=True), |
| 495 |
| 496 'SVGAngle': SVGTearOffIDLTypeInfo('SVGAngle'), |
| 497 'SVGLength': SVGTearOffIDLTypeInfo('SVGLength'), |
| 498 'SVGLengthList': SVGTearOffIDLTypeInfo('SVGLengthList', ref_counted=False), |
| 499 'SVGMatrix': SVGTearOffIDLTypeInfo('SVGMatrix'), |
| 500 'SVGNumber': SVGTearOffIDLTypeInfo('SVGNumber', native_type='SVGPropertyTear
Off<float>'), |
| 501 'SVGNumberList': SVGTearOffIDLTypeInfo('SVGNumberList', ref_counted=False), |
| 502 'SVGPathSegList': SVGTearOffIDLTypeInfo('SVGPathSegList', native_type='SVGPa
thSegListPropertyTearOff', ref_counted=False), |
| 503 'SVGPoint': SVGTearOffIDLTypeInfo('SVGPoint', native_type='SVGPropertyTearOf
f<FloatPoint>'), |
| 504 'SVGPointList': SVGTearOffIDLTypeInfo('SVGPointList', ref_counted=False), |
| 505 'SVGPreserveAspectRatio': SVGTearOffIDLTypeInfo('SVGPreserveAspectRatio'), |
| 506 'SVGRect': SVGTearOffIDLTypeInfo('SVGRect', native_type='SVGPropertyTearOff<
FloatRect>'), |
| 507 'SVGStringList': SVGTearOffIDLTypeInfo('SVGStringList', native_type='SVGStat
icListPropertyTearOff<SVGStringList>', ref_counted=False), |
| 508 'SVGTransform': SVGTearOffIDLTypeInfo('SVGTransform'), |
| 509 'SVGTransformList': SVGTearOffIDLTypeInfo('SVGTransformList', native_type='S
VGTransformListPropertyTearOff', ref_counted=False) |
| 510 } |
| 511 |
| 512 original_idl_types = { |
| 513 } |
| 514 |
| 515 def GetIDLTypeInfo(idl_type): |
| 516 idl_type_name = original_idl_types.get(idl_type, idl_type.id) |
| 517 return GetIDLTypeInfoByName(idl_type_name) |
| 518 |
| 519 def GetIDLTypeInfoByName(idl_type_name): |
| 520 return _idl_type_registry.get(idl_type_name, IDLTypeInfo(idl_type_name)) |
OLD | NEW |