| OLD | NEW |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 class methods. | 32 class methods. |
| 33 | 33 |
| 34 Spec: | 34 Spec: |
| 35 http://www.w3.org/TR/WebIDL/#es-type-mapping | 35 http://www.w3.org/TR/WebIDL/#es-type-mapping |
| 36 | 36 |
| 37 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler | 37 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler |
| 38 """ | 38 """ |
| 39 | 39 |
| 40 import posixpath | 40 import posixpath |
| 41 | 41 |
| 42 from idl_types import IdlTypeBase, IdlType, IdlUnionType | 42 from idl_types import IdlTypeBase, IdlType, IdlUnionType, IdlArrayOrSequenceType |
| 43 import v8_attributes # for IdlType.constructor_type_name | 43 import v8_attributes # for IdlType.constructor_type_name |
| 44 from v8_globals import includes | 44 from v8_globals import includes |
| 45 | 45 |
| 46 | 46 |
| 47 ################################################################################ | 47 ################################################################################ |
| 48 # V8-specific handling of IDL types | 48 # V8-specific handling of IDL types |
| 49 ################################################################################ | 49 ################################################################################ |
| 50 | 50 |
| 51 NON_WRAPPER_TYPES = frozenset([ | 51 NON_WRAPPER_TYPES = frozenset([ |
| 52 'CompareHow', | 52 'CompareHow', |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 return 'TreatNullAsEmptyString' | 145 return 'TreatNullAsEmptyString' |
| 146 if idl_type.is_nullable or extended_attributes.get('TreatNullAs') == 'Nu
llString': | 146 if idl_type.is_nullable or extended_attributes.get('TreatNullAs') == 'Nu
llString': |
| 147 if extended_attributes.get('TreatUndefinedAs') == 'NullString': | 147 if extended_attributes.get('TreatUndefinedAs') == 'NullString': |
| 148 return 'TreatNullAndUndefinedAsNullString' | 148 return 'TreatNullAndUndefinedAsNullString' |
| 149 return 'TreatNullAsNullString' | 149 return 'TreatNullAsNullString' |
| 150 return '' | 150 return '' |
| 151 | 151 |
| 152 extended_attributes = extended_attributes or {} | 152 extended_attributes = extended_attributes or {} |
| 153 idl_type = idl_type.preprocessed_type | 153 idl_type = idl_type.preprocessed_type |
| 154 | 154 |
| 155 # Composite types | 155 # Array or sequence types |
| 156 if used_as_variadic_argument: | 156 if used_as_variadic_argument: |
| 157 native_array_element_type = idl_type | 157 native_array_element_type = idl_type |
| 158 else: | 158 else: |
| 159 native_array_element_type = idl_type.native_array_element_type | 159 native_array_element_type = idl_type.native_array_element_type |
| 160 if native_array_element_type: | 160 if native_array_element_type: |
| 161 vector_type = cpp_ptr_type('Vector', 'HeapVector', native_array_element_
type.gc_type) | 161 vector_type = cpp_ptr_type('Vector', 'HeapVector', native_array_element_
type.gc_type) |
| 162 vector_template_type = cpp_template_type(vector_type, native_array_eleme
nt_type.cpp_type_args(used_in_cpp_sequence=True)) | 162 vector_template_type = cpp_template_type(vector_type, native_array_eleme
nt_type.cpp_type_args(used_in_cpp_sequence=True)) |
| 163 if used_as_rvalue_type: | 163 if used_as_rvalue_type: |
| 164 return 'const %s&' % vector_template_type | 164 return 'const %s&' % vector_template_type |
| 165 return vector_template_type | 165 return vector_template_type |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 220 |
| 221 # Allow access as idl_type.cpp_type if no arguments | 221 # Allow access as idl_type.cpp_type if no arguments |
| 222 IdlTypeBase.cpp_type = property(cpp_type) | 222 IdlTypeBase.cpp_type = property(cpp_type) |
| 223 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer) | 223 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer) |
| 224 IdlTypeBase.cpp_type_args = cpp_type | 224 IdlTypeBase.cpp_type_args = cpp_type |
| 225 IdlUnionType.cpp_type = property(cpp_type_union) | 225 IdlUnionType.cpp_type = property(cpp_type_union) |
| 226 IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union) | 226 IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union) |
| 227 IdlUnionType.cpp_type_args = cpp_type_union | 227 IdlUnionType.cpp_type_args = cpp_type_union |
| 228 | 228 |
| 229 | 229 |
| 230 IdlTypeBase.native_array_element_type = None |
| 231 IdlArrayOrSequenceType.native_array_element_type = property( |
| 232 lambda self: self.element_type) |
| 233 |
| 234 |
| 230 def cpp_template_type(template, inner_type): | 235 def cpp_template_type(template, inner_type): |
| 231 """Returns C++ template specialized to type, with space added if needed.""" | 236 """Returns C++ template specialized to type, with space added if needed.""" |
| 232 if inner_type.endswith('>'): | 237 if inner_type.endswith('>'): |
| 233 format_string = '{template}<{inner_type} >' | 238 format_string = '{template}<{inner_type} >' |
| 234 else: | 239 else: |
| 235 format_string = '{template}<{inner_type}>' | 240 format_string = '{template}<{inner_type}>' |
| 236 return format_string.format(template=template, inner_type=inner_type) | 241 return format_string.format(template=template, inner_type=inner_type) |
| 237 | 242 |
| 238 | 243 |
| 239 def cpp_ptr_type(old_type, new_type, gc_type): | 244 def cpp_ptr_type(old_type, new_type, gc_type): |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 'core/html/LabelsNodeList.h']), | 345 'core/html/LabelsNodeList.h']), |
| 341 'Promise': set(['bindings/core/v8/ScriptPromise.h']), | 346 'Promise': set(['bindings/core/v8/ScriptPromise.h']), |
| 342 'SerializedScriptValue': set(['bindings/core/v8/SerializedScriptValue.h']), | 347 'SerializedScriptValue': set(['bindings/core/v8/SerializedScriptValue.h']), |
| 343 'ScriptValue': set(['bindings/core/v8/ScriptValue.h']), | 348 'ScriptValue': set(['bindings/core/v8/ScriptValue.h']), |
| 344 } | 349 } |
| 345 | 350 |
| 346 | 351 |
| 347 def includes_for_type(idl_type): | 352 def includes_for_type(idl_type): |
| 348 idl_type = idl_type.preprocessed_type | 353 idl_type = idl_type.preprocessed_type |
| 349 | 354 |
| 350 # Composite types | |
| 351 native_array_element_type = idl_type.native_array_element_type | |
| 352 if native_array_element_type: | |
| 353 return includes_for_type(native_array_element_type) | |
| 354 | |
| 355 # Simple types | 355 # Simple types |
| 356 base_idl_type = idl_type.base_type | 356 base_idl_type = idl_type.base_type |
| 357 if base_idl_type in INCLUDES_FOR_TYPE: | 357 if base_idl_type in INCLUDES_FOR_TYPE: |
| 358 return INCLUDES_FOR_TYPE[base_idl_type] | 358 return INCLUDES_FOR_TYPE[base_idl_type] |
| 359 if idl_type.is_basic_type: | 359 if idl_type.is_basic_type: |
| 360 return set() | 360 return set() |
| 361 if idl_type.is_typed_array_element_type: | 361 if idl_type.is_typed_array_element_type: |
| 362 return set(['bindings/core/v8/custom/V8%sCustom.h' % base_idl_type]) | 362 return set(['bindings/core/v8/custom/V8%sCustom.h' % base_idl_type]) |
| 363 if base_idl_type.endswith('ConstructorConstructor'): | 363 if base_idl_type.endswith('ConstructorConstructor'): |
| 364 # FIXME: rename to NamedConstructor | 364 # FIXME: rename to NamedConstructor |
| 365 # FIXME: replace with a [NamedConstructorAttribute] extended attribute | 365 # FIXME: replace with a [NamedConstructorAttribute] extended attribute |
| 366 # Ending with 'ConstructorConstructor' indicates a named constructor, | 366 # Ending with 'ConstructorConstructor' indicates a named constructor, |
| 367 # and these do not have header files, as they are part of the generated | 367 # and these do not have header files, as they are part of the generated |
| 368 # bindings for the interface | 368 # bindings for the interface |
| 369 return set() | 369 return set() |
| 370 if base_idl_type.endswith('Constructor'): | 370 if base_idl_type.endswith('Constructor'): |
| 371 # FIXME: replace with a [ConstructorAttribute] extended attribute | 371 # FIXME: replace with a [ConstructorAttribute] extended attribute |
| 372 base_idl_type = idl_type.constructor_type_name | 372 base_idl_type = idl_type.constructor_type_name |
| 373 if base_idl_type not in component_dir: | 373 if base_idl_type not in component_dir: |
| 374 return set() | 374 return set() |
| 375 return set(['bindings/%s/v8/V8%s.h' % (component_dir[base_idl_type], | 375 return set(['bindings/%s/v8/V8%s.h' % (component_dir[base_idl_type], |
| 376 base_idl_type)]) | 376 base_idl_type)]) |
| 377 | 377 |
| 378 IdlType.includes_for_type = property(includes_for_type) | 378 IdlType.includes_for_type = property(includes_for_type) |
| 379 IdlUnionType.includes_for_type = property( | 379 IdlUnionType.includes_for_type = property( |
| 380 lambda self: set.union(*[includes_for_type(member_type) | 380 lambda self: set.union(*[includes_for_type(member_type) |
| 381 for member_type in self.member_types])) | 381 for member_type in self.member_types])) |
| 382 IdlArrayOrSequenceType.includes_for_type = property( |
| 383 lambda self: self.element_type.includes_for_type) |
| 382 | 384 |
| 383 | 385 |
| 384 def add_includes_for_type(idl_type): | 386 def add_includes_for_type(idl_type): |
| 385 includes.update(idl_type.includes_for_type) | 387 includes.update(idl_type.includes_for_type) |
| 386 | 388 |
| 387 IdlTypeBase.add_includes_for_type = add_includes_for_type | 389 IdlTypeBase.add_includes_for_type = add_includes_for_type |
| 388 | 390 |
| 389 | 391 |
| 390 def includes_for_interface(interface_name): | 392 def includes_for_interface(interface_name): |
| 391 return IdlType(interface_name).includes_for_type | 393 return IdlType(interface_name).includes_for_type |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 'ScriptValue': 'ScriptValue(ScriptState::current({isolate}), {v8_value})', | 467 'ScriptValue': 'ScriptValue(ScriptState::current({isolate}), {v8_value})', |
| 466 'Window': 'toDOMWindow({v8_value}, {isolate})', | 468 'Window': 'toDOMWindow({v8_value}, {isolate})', |
| 467 'XPathNSResolver': 'toXPathNSResolver({v8_value}, {isolate})', | 469 'XPathNSResolver': 'toXPathNSResolver({v8_value}, {isolate})', |
| 468 } | 470 } |
| 469 | 471 |
| 470 | 472 |
| 471 def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index, isolat
e): | 473 def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index, isolat
e): |
| 472 if idl_type.name == 'void': | 474 if idl_type.name == 'void': |
| 473 return '' | 475 return '' |
| 474 | 476 |
| 475 # Composite types | 477 # Array or sequence types |
| 476 native_array_element_type = idl_type.native_array_element_type | 478 native_array_element_type = idl_type.native_array_element_type |
| 477 if native_array_element_type: | 479 if native_array_element_type: |
| 478 return v8_value_to_cpp_value_array_or_sequence(native_array_element_type
, v8_value, index) | 480 return v8_value_to_cpp_value_array_or_sequence(native_array_element_type
, v8_value, index) |
| 479 | 481 |
| 480 # Simple types | 482 # Simple types |
| 481 idl_type = idl_type.preprocessed_type | 483 idl_type = idl_type.preprocessed_type |
| 482 add_includes_for_type(idl_type) | 484 add_includes_for_type(idl_type) |
| 483 base_idl_type = idl_type.base_type | 485 base_idl_type = idl_type.base_type |
| 484 | 486 |
| 485 if 'EnforceRange' in extended_attributes: | 487 if 'EnforceRange' in extended_attributes: |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 | 532 |
| 531 # FIXME: Support union type. | 533 # FIXME: Support union type. |
| 532 if idl_type.is_union_type: | 534 if idl_type.is_union_type: |
| 533 return '' | 535 return '' |
| 534 | 536 |
| 535 this_cpp_type = idl_type.cpp_type_args(extended_attributes=extended_attribut
es, raw_type=True) | 537 this_cpp_type = idl_type.cpp_type_args(extended_attributes=extended_attribut
es, raw_type=True) |
| 536 | 538 |
| 537 idl_type = idl_type.preprocessed_type | 539 idl_type = idl_type.preprocessed_type |
| 538 cpp_value = v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, i
ndex, isolate) | 540 cpp_value = v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, i
ndex, isolate) |
| 539 args = [variable_name, cpp_value] | 541 args = [variable_name, cpp_value] |
| 540 if idl_type.base_type == 'DOMString' and not idl_type.native_array_element_t
ype: | 542 if idl_type.base_type == 'DOMString': |
| 541 macro = 'TOSTRING_DEFAULT' if used_in_private_script else 'TOSTRING_VOID
' | 543 macro = 'TOSTRING_DEFAULT' if used_in_private_script else 'TOSTRING_VOID
' |
| 542 elif idl_type.may_raise_exception_on_conversion: | 544 elif idl_type.may_raise_exception_on_conversion: |
| 543 macro = 'TONATIVE_DEFAULT_EXCEPTIONSTATE' if used_in_private_script else
'TONATIVE_VOID_EXCEPTIONSTATE' | 545 macro = 'TONATIVE_DEFAULT_EXCEPTIONSTATE' if used_in_private_script else
'TONATIVE_VOID_EXCEPTIONSTATE' |
| 544 args.append('exceptionState') | 546 args.append('exceptionState') |
| 545 else: | 547 else: |
| 546 macro = 'TONATIVE_DEFAULT' if used_in_private_script else 'TONATIVE_VOID
' | 548 macro = 'TONATIVE_DEFAULT' if used_in_private_script else 'TONATIVE_VOID
' |
| 547 | 549 |
| 548 if used_in_private_script: | 550 if used_in_private_script: |
| 549 args.append('false') | 551 args.append('false') |
| 550 | 552 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 | 609 |
| 608 def v8_conversion_type(idl_type, extended_attributes): | 610 def v8_conversion_type(idl_type, extended_attributes): |
| 609 """Returns V8 conversion type, adding any additional includes. | 611 """Returns V8 conversion type, adding any additional includes. |
| 610 | 612 |
| 611 The V8 conversion type is used to select the C++ -> V8 conversion function | 613 The V8 conversion type is used to select the C++ -> V8 conversion function |
| 612 or v8SetReturnValue* function; it can be an idl_type, a cpp_type, or a | 614 or v8SetReturnValue* function; it can be an idl_type, a cpp_type, or a |
| 613 separate name for the type of conversion (e.g., 'DOMWrapper'). | 615 separate name for the type of conversion (e.g., 'DOMWrapper'). |
| 614 """ | 616 """ |
| 615 extended_attributes = extended_attributes or {} | 617 extended_attributes = extended_attributes or {} |
| 616 | 618 |
| 617 # Composite types | 619 # FIXME: Support union type. |
| 620 if idl_type.is_union_type: |
| 621 return '' |
| 622 |
| 623 # Array or sequence types |
| 618 native_array_element_type = idl_type.native_array_element_type | 624 native_array_element_type = idl_type.native_array_element_type |
| 619 if native_array_element_type: | 625 if native_array_element_type: |
| 620 if native_array_element_type.is_interface_type: | 626 if native_array_element_type.is_interface_type: |
| 621 add_includes_for_type(native_array_element_type) | 627 add_includes_for_type(native_array_element_type) |
| 622 return 'array' | 628 return 'array' |
| 623 | 629 |
| 624 # Simple types | 630 # Simple types |
| 625 base_idl_type = idl_type.base_type | 631 base_idl_type = idl_type.base_type |
| 626 # Basic types, without additional includes | 632 # Basic types, without additional includes |
| 627 if base_idl_type in CPP_INT_TYPES: | 633 if base_idl_type in CPP_INT_TYPES: |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 726 for i, member_type in | 732 for i, member_type in |
| 727 enumerate(idl_type.member_types)] | 733 enumerate(idl_type.member_types)] |
| 728 | 734 |
| 729 IdlTypeBase.v8_set_return_value = v8_set_return_value | 735 IdlTypeBase.v8_set_return_value = v8_set_return_value |
| 730 IdlUnionType.v8_set_return_value = v8_set_return_value_union | 736 IdlUnionType.v8_set_return_value = v8_set_return_value_union |
| 731 | 737 |
| 732 IdlType.release = property(lambda self: self.is_interface_type) | 738 IdlType.release = property(lambda self: self.is_interface_type) |
| 733 IdlUnionType.release = property( | 739 IdlUnionType.release = property( |
| 734 lambda self: [member_type.is_interface_type | 740 lambda self: [member_type.is_interface_type |
| 735 for member_type in self.member_types]) | 741 for member_type in self.member_types]) |
| 742 IdlArrayOrSequenceType.release = False |
| 736 | 743 |
| 737 | 744 |
| 738 CPP_VALUE_TO_V8_VALUE = { | 745 CPP_VALUE_TO_V8_VALUE = { |
| 739 # Built-in types | 746 # Built-in types |
| 740 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', | 747 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', |
| 741 'DOMString': 'v8String({isolate}, {cpp_value})', | 748 'DOMString': 'v8String({isolate}, {cpp_value})', |
| 742 'ByteString': 'v8String({isolate}, {cpp_value})', | 749 'ByteString': 'v8String({isolate}, {cpp_value})', |
| 743 'ScalarValueString': 'v8String({isolate}, {cpp_value})', | 750 'ScalarValueString': 'v8String({isolate}, {cpp_value})', |
| 744 'boolean': 'v8Boolean({cpp_value}, {isolate})', | 751 'boolean': 'v8Boolean({cpp_value}, {isolate})', |
| 745 'int': 'v8::Integer::New({isolate}, {cpp_value})', | 752 'int': 'v8::Integer::New({isolate}, {cpp_value})', |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 788 ################################################################################ | 795 ################################################################################ |
| 789 # Utility properties for nullable types | 796 # Utility properties for nullable types |
| 790 ################################################################################ | 797 ################################################################################ |
| 791 | 798 |
| 792 | 799 |
| 793 def cpp_type_has_null_value(idl_type): | 800 def cpp_type_has_null_value(idl_type): |
| 794 # - String types (String/AtomicString) represent null as a null string, | 801 # - String types (String/AtomicString) represent null as a null string, |
| 795 # i.e. one for which String::isNull() returns true. | 802 # i.e. one for which String::isNull() returns true. |
| 796 # - Wrapper types (raw pointer or RefPtr/PassRefPtr) represent null as | 803 # - Wrapper types (raw pointer or RefPtr/PassRefPtr) represent null as |
| 797 # a null pointer. | 804 # a null pointer. |
| 798 return ((idl_type.is_string_type or idl_type.is_wrapper_type) and | 805 return idl_type.is_string_type or idl_type.is_wrapper_type |
| 799 not idl_type.native_array_element_type) | |
| 800 | 806 |
| 801 IdlTypeBase.cpp_type_has_null_value = property(cpp_type_has_null_value) | 807 IdlTypeBase.cpp_type_has_null_value = property(cpp_type_has_null_value) |
| 802 | 808 |
| 803 | 809 |
| 804 def is_implicit_nullable(idl_type): | 810 def is_implicit_nullable(idl_type): |
| 805 # Nullable type where the corresponding C++ type supports a null value. | 811 # Nullable type where the corresponding C++ type supports a null value. |
| 806 return idl_type.is_nullable and idl_type.cpp_type_has_null_value | 812 return idl_type.is_nullable and idl_type.cpp_type_has_null_value |
| 807 | 813 |
| 808 | 814 |
| 809 def is_explicit_nullable(idl_type): | 815 def is_explicit_nullable(idl_type): |
| 810 # Nullable type that isn't implicit nullable (see above.) For such types, | 816 # Nullable type that isn't implicit nullable (see above.) For such types, |
| 811 # we use Nullable<T> or similar explicit ways to represent a null value. | 817 # we use Nullable<T> or similar explicit ways to represent a null value. |
| 812 return idl_type.is_nullable and not idl_type.is_implicit_nullable | 818 return idl_type.is_nullable and not idl_type.is_implicit_nullable |
| 813 | 819 |
| 814 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable) | 820 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable) |
| 815 IdlUnionType.is_implicit_nullable = False | 821 IdlUnionType.is_implicit_nullable = False |
| 816 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable) | 822 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable) |
| OLD | NEW |