Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: Source/bindings/scripts/v8_types.py

Issue 680193003: IDL: Generate union type containers (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 native_array_element_type = idl_type 155 native_array_element_type = idl_type
156 else: 156 else:
157 native_array_element_type = idl_type.native_array_element_type 157 native_array_element_type = idl_type.native_array_element_type
158 if native_array_element_type: 158 if native_array_element_type:
159 vector_type = cpp_ptr_type('Vector', 'HeapVector', native_array_element_ type.gc_type) 159 vector_type = cpp_ptr_type('Vector', 'HeapVector', native_array_element_ type.gc_type)
160 vector_template_type = cpp_template_type(vector_type, native_array_eleme nt_type.cpp_type_args(used_in_cpp_sequence=True)) 160 vector_template_type = cpp_template_type(vector_type, native_array_eleme nt_type.cpp_type_args(used_in_cpp_sequence=True))
161 if used_as_rvalue_type: 161 if used_as_rvalue_type:
162 return 'const %s&' % vector_template_type 162 return 'const %s&' % vector_template_type
163 return vector_template_type 163 return vector_template_type
164 164
165 if idl_type.is_union_type:
166 return idl_type.name
haraken 2014/10/30 08:39:26 Can we probably move this down to just below the '
bashi 2014/10/30 12:20:48 IdlUnionType doesn't have base_type, so we need to
Jens Widell 2014/10/30 12:31:50 Isn't idl_type.base_type simply None for union typ
bashi 2014/10/30 23:19:18 You are right. Done.
167
165 # Simple types 168 # Simple types
166 base_idl_type = idl_type.base_type 169 base_idl_type = idl_type.base_type
167 170
168 if base_idl_type in CPP_TYPE_SAME_AS_IDL_TYPE: 171 if base_idl_type in CPP_TYPE_SAME_AS_IDL_TYPE:
169 return base_idl_type 172 return base_idl_type
170 if base_idl_type in CPP_INT_TYPES: 173 if base_idl_type in CPP_INT_TYPES:
171 return 'int' 174 return 'int'
172 if base_idl_type in CPP_UNSIGNED_TYPES: 175 if base_idl_type in CPP_UNSIGNED_TYPES:
173 return 'unsigned' 176 return 'unsigned'
174 if base_idl_type in CPP_SPECIAL_CONVERSION_RULES: 177 if base_idl_type in CPP_SPECIAL_CONVERSION_RULES:
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 return ' = false' 216 return ' = false'
214 if (base_idl_type in NON_WRAPPER_TYPES or 217 if (base_idl_type in NON_WRAPPER_TYPES or
215 base_idl_type in CPP_SPECIAL_CONVERSION_RULES or 218 base_idl_type in CPP_SPECIAL_CONVERSION_RULES or
216 base_idl_type == 'any' or 219 base_idl_type == 'any' or
217 idl_type.is_string_type or 220 idl_type.is_string_type or
218 idl_type.is_enum): 221 idl_type.is_enum):
219 return '' 222 return ''
220 return ' = nullptr' 223 return ' = nullptr'
221 224
222 225
223 def cpp_type_union(idl_type, extended_attributes=None, raw_type=False):
224 # FIXME: Need to revisit the design of union support.
225 # http://crbug.com/240176
226 return None
227
228
229 def cpp_type_initializer_union(idl_type):
230 return (member_type.cpp_type_initializer for member_type in idl_type.member_ types)
231
232
233 # Allow access as idl_type.cpp_type if no arguments 226 # Allow access as idl_type.cpp_type if no arguments
234 IdlTypeBase.cpp_type = property(cpp_type) 227 IdlTypeBase.cpp_type = property(cpp_type)
235 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer) 228 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer)
236 IdlTypeBase.cpp_type_args = cpp_type 229 IdlTypeBase.cpp_type_args = cpp_type
237 IdlUnionType.cpp_type = property(cpp_type_union) 230 IdlUnionType.cpp_type_initializer = ''
238 IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union)
239 IdlUnionType.cpp_type_args = cpp_type_union
240 231
241 232
242 IdlArrayOrSequenceType.native_array_element_type = property( 233 IdlArrayOrSequenceType.native_array_element_type = property(
243 lambda self: self.element_type) 234 lambda self: self.element_type)
244 235
245 236
246 def cpp_template_type(template, inner_type): 237 def cpp_template_type(template, inner_type):
247 """Returns C++ template specialized to type, with space added if needed.""" 238 """Returns C++ template specialized to type, with space added if needed."""
248 if inner_type.endswith('>'): 239 if inner_type.endswith('>'):
249 format_string = '{template}<{inner_type} >' 240 format_string = '{template}<{inner_type} >'
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 } 468 }
478 469
479 470
480 def v8_conversion_needs_exception_state(idl_type): 471 def v8_conversion_needs_exception_state(idl_type):
481 return (idl_type.is_numeric_type or 472 return (idl_type.is_numeric_type or
482 idl_type.is_dictionary or 473 idl_type.is_dictionary or
483 idl_type.name in ('ByteString', 'USVString', 'SerializedScriptValue' )) 474 idl_type.name in ('ByteString', 'USVString', 'SerializedScriptValue' ))
484 475
485 IdlType.v8_conversion_needs_exception_state = property(v8_conversion_needs_excep tion_state) 476 IdlType.v8_conversion_needs_exception_state = property(v8_conversion_needs_excep tion_state)
486 IdlArrayOrSequenceType.v8_conversion_needs_exception_state = True 477 IdlArrayOrSequenceType.v8_conversion_needs_exception_state = True
478 IdlUnionType.v8_conversion_needs_exception_state = True
487 479
488 480
489 TRIVIAL_CONVERSIONS = frozenset([ 481 TRIVIAL_CONVERSIONS = frozenset([
490 'any', 482 'any',
491 'boolean', 483 'boolean',
492 'Date', 484 'Date',
493 'Dictionary', 485 'Dictionary',
494 'NodeFilter', 486 'NodeFilter',
495 'XPathNSResolver', 487 'XPathNSResolver',
496 'Promise' 488 'Promise'
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 669
678 def v8_conversion_type(idl_type, extended_attributes): 670 def v8_conversion_type(idl_type, extended_attributes):
679 """Returns V8 conversion type, adding any additional includes. 671 """Returns V8 conversion type, adding any additional includes.
680 672
681 The V8 conversion type is used to select the C++ -> V8 conversion function 673 The V8 conversion type is used to select the C++ -> V8 conversion function
682 or v8SetReturnValue* function; it can be an idl_type, a cpp_type, or a 674 or v8SetReturnValue* function; it can be an idl_type, a cpp_type, or a
683 separate name for the type of conversion (e.g., 'DOMWrapper'). 675 separate name for the type of conversion (e.g., 'DOMWrapper').
684 """ 676 """
685 extended_attributes = extended_attributes or {} 677 extended_attributes = extended_attributes or {}
686 678
687 # FIXME: Support union type. 679 if idl_type.is_dictionary or idl_type.is_union_type:
688 if idl_type.is_union_type: 680 return 'DictionaryOrUnion'
689 return ''
690
691 if idl_type.is_dictionary:
692 return 'IDLDictionary'
693 681
694 # Array or sequence types 682 # Array or sequence types
695 native_array_element_type = idl_type.native_array_element_type 683 native_array_element_type = idl_type.native_array_element_type
696 if native_array_element_type: 684 if native_array_element_type:
697 if native_array_element_type.is_interface_type: 685 if native_array_element_type.is_interface_type:
698 add_includes_for_type(native_array_element_type) 686 add_includes_for_type(native_array_element_type)
699 return 'array' 687 return 'array'
700 688
701 # Simple types 689 # Simple types
702 base_idl_type = idl_type.base_type 690 base_idl_type = idl_type.base_type
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 # and then use general v8SetReturnValue. 738 # and then use general v8SetReturnValue.
751 'array': 'v8SetReturnValue(info, {cpp_value})', 739 'array': 'v8SetReturnValue(info, {cpp_value})',
752 'Date': 'v8SetReturnValue(info, {cpp_value})', 740 'Date': 'v8SetReturnValue(info, {cpp_value})',
753 'EventHandler': 'v8SetReturnValue(info, {cpp_value})', 741 'EventHandler': 'v8SetReturnValue(info, {cpp_value})',
754 'ScriptValue': 'v8SetReturnValue(info, {cpp_value})', 742 'ScriptValue': 'v8SetReturnValue(info, {cpp_value})',
755 'SerializedScriptValue': 'v8SetReturnValue(info, {cpp_value})', 743 'SerializedScriptValue': 'v8SetReturnValue(info, {cpp_value})',
756 # DOMWrapper 744 # DOMWrapper
757 'DOMWrapperForMainWorld': 'v8SetReturnValueForMainWorld(info, WTF::getPtr({c pp_value}))', 745 'DOMWrapperForMainWorld': 'v8SetReturnValueForMainWorld(info, WTF::getPtr({c pp_value}))',
758 'DOMWrapperFast': 'v8SetReturnValueFast(info, WTF::getPtr({cpp_value}), {scr ipt_wrappable})', 746 'DOMWrapperFast': 'v8SetReturnValueFast(info, WTF::getPtr({cpp_value}), {scr ipt_wrappable})',
759 'DOMWrapperDefault': 'v8SetReturnValue(info, {cpp_value})', 747 'DOMWrapperDefault': 'v8SetReturnValue(info, {cpp_value})',
760 'IDLDictionary': 'v8SetReturnValue(info, result)', 748 # Union types or dictionaries
749 'DictionaryOrUnion': 'v8SetReturnValue(info, result)',
761 } 750 }
762 751
763 752
764 def v8_set_return_value(idl_type, cpp_value, extended_attributes=None, script_wr appable='', release=False, for_main_world=False): 753 def v8_set_return_value(idl_type, cpp_value, extended_attributes=None, script_wr appable='', release=False, for_main_world=False):
765 """Returns a statement that converts a C++ value to a V8 value and sets it a s a return value. 754 """Returns a statement that converts a C++ value to a V8 value and sets it a s a return value.
766 755
767 """ 756 """
768 def dom_wrapper_conversion_type(): 757 def dom_wrapper_conversion_type():
769 if not script_wrappable: 758 if not script_wrappable:
770 return 'DOMWrapperDefault' 759 return 'DOMWrapperDefault'
(...skipping 11 matching lines...) Expand all
782 this_v8_conversion_type = dom_wrapper_conversion_type() 771 this_v8_conversion_type = dom_wrapper_conversion_type()
783 772
784 format_string = V8_SET_RETURN_VALUE[this_v8_conversion_type] 773 format_string = V8_SET_RETURN_VALUE[this_v8_conversion_type]
785 # FIXME: oilpan: Remove .release() once we remove all RefPtrs from generated code. 774 # FIXME: oilpan: Remove .release() once we remove all RefPtrs from generated code.
786 if release: 775 if release:
787 cpp_value = '%s.release()' % cpp_value 776 cpp_value = '%s.release()' % cpp_value
788 statement = format_string.format(cpp_value=cpp_value, script_wrappable=scrip t_wrappable) 777 statement = format_string.format(cpp_value=cpp_value, script_wrappable=scrip t_wrappable)
789 return statement 778 return statement
790 779
791 780
792 def v8_set_return_value_union(idl_type, cpp_value, extended_attributes=None, scr ipt_wrappable='', release=False, for_main_world=False):
793 # FIXME: Need to revisit the design of union support.
794 # http://crbug.com/240176
795 return None
796
797
798 IdlTypeBase.v8_set_return_value = v8_set_return_value 781 IdlTypeBase.v8_set_return_value = v8_set_return_value
799 IdlUnionType.v8_set_return_value = v8_set_return_value_union
800 782
801 IdlType.release = property(lambda self: self.is_interface_type) 783 IdlType.release = property(lambda self: self.is_interface_type)
802 IdlUnionType.release = property( 784 IdlUnionType.release = False
803 lambda self: [member_type.is_interface_type
804 for member_type in self.member_types])
805 785
806 786
807 CPP_VALUE_TO_V8_VALUE = { 787 CPP_VALUE_TO_V8_VALUE = {
808 # Built-in types 788 # Built-in types
809 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', 789 'Date': 'v8DateOrNaN({cpp_value}, {isolate})',
810 'DOMString': 'v8String({isolate}, {cpp_value})', 790 'DOMString': 'v8String({isolate}, {cpp_value})',
811 'ByteString': 'v8String({isolate}, {cpp_value})', 791 'ByteString': 'v8String({isolate}, {cpp_value})',
812 'USVString': 'v8String({isolate}, {cpp_value})', 792 'USVString': 'v8String({isolate}, {cpp_value})',
813 'boolean': 'v8Boolean({cpp_value}, {isolate})', 793 'boolean': 'v8Boolean({cpp_value}, {isolate})',
814 'int': 'v8::Integer::New({isolate}, {cpp_value})', 794 'int': 'v8::Integer::New({isolate}, {cpp_value})',
815 'unsigned': 'v8::Integer::NewFromUnsigned({isolate}, {cpp_value})', 795 'unsigned': 'v8::Integer::NewFromUnsigned({isolate}, {cpp_value})',
816 'float': 'v8::Number::New({isolate}, {cpp_value})', 796 'float': 'v8::Number::New({isolate}, {cpp_value})',
817 'unrestricted float': 'v8::Number::New({isolate}, {cpp_value})', 797 'unrestricted float': 'v8::Number::New({isolate}, {cpp_value})',
818 'double': 'v8::Number::New({isolate}, {cpp_value})', 798 'double': 'v8::Number::New({isolate}, {cpp_value})',
819 'unrestricted double': 'v8::Number::New({isolate}, {cpp_value})', 799 'unrestricted double': 'v8::Number::New({isolate}, {cpp_value})',
820 'void': 'v8Undefined()', 800 'void': 'v8Undefined()',
821 # [TreatReturnedNullStringAs] 801 # [TreatReturnedNullStringAs]
822 'StringOrNull': '{cpp_value}.isNull() ? v8::Handle<v8::Value>(v8::Null({isol ate})) : v8String({isolate}, {cpp_value})', 802 'StringOrNull': '{cpp_value}.isNull() ? v8::Handle<v8::Value>(v8::Null({isol ate})) : v8String({isolate}, {cpp_value})',
823 'StringOrUndefined': '{cpp_value}.isNull() ? v8Undefined() : v8String({isola te}, {cpp_value})', 803 'StringOrUndefined': '{cpp_value}.isNull() ? v8Undefined() : v8String({isola te}, {cpp_value})',
824 # Special cases 804 # Special cases
825 'EventHandler': '{cpp_value} ? v8::Handle<v8::Value>(V8AbstractEventListener ::cast({cpp_value})->getListenerObject(impl->executionContext())) : v8::Handle<v 8::Value>(v8::Null({isolate}))', 805 'EventHandler': '{cpp_value} ? v8::Handle<v8::Value>(V8AbstractEventListener ::cast({cpp_value})->getListenerObject(impl->executionContext())) : v8::Handle<v 8::Value>(v8::Null({isolate}))',
826 'ScriptValue': '{cpp_value}.v8Value()', 806 'ScriptValue': '{cpp_value}.v8Value()',
827 'SerializedScriptValue': '{cpp_value} ? {cpp_value}->deserialize() : v8::Han dle<v8::Value>(v8::Null({isolate}))', 807 'SerializedScriptValue': '{cpp_value} ? {cpp_value}->deserialize() : v8::Han dle<v8::Value>(v8::Null({isolate}))',
828 # General 808 # General
829 'array': 'v8Array({cpp_value}, {creation_context}, {isolate})', 809 'array': 'v8Array({cpp_value}, {creation_context}, {isolate})',
830 'DOMWrapper': 'toV8({cpp_value}, {creation_context}, {isolate})', 810 'DOMWrapper': 'toV8({cpp_value}, {creation_context}, {isolate})',
831 'IDLDictionary': 'toV8({cpp_value}, {creation_context}, {isolate})', 811 # Union types or dictionaries
812 'DictionaryOrUnion': 'toV8({cpp_value}, {creation_context}, {isolate})',
832 } 813 }
833 814
834 815
835 def cpp_value_to_v8_value(idl_type, cpp_value, isolate='info.GetIsolate()', crea tion_context='info.Holder()', extended_attributes=None): 816 def cpp_value_to_v8_value(idl_type, cpp_value, isolate='info.GetIsolate()', crea tion_context='info.Holder()', extended_attributes=None):
836 """Returns an expression that converts a C++ value to a V8 value.""" 817 """Returns an expression that converts a C++ value to a V8 value."""
837 # the isolate parameter is needed for callback interfaces 818 # the isolate parameter is needed for callback interfaces
838 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext ended_attributes) 819 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext ended_attributes)
839 this_v8_conversion_type = idl_type.v8_conversion_type(extended_attributes) 820 this_v8_conversion_type = idl_type.v8_conversion_type(extended_attributes)
840 format_string = CPP_VALUE_TO_V8_VALUE[this_v8_conversion_type] 821 format_string = CPP_VALUE_TO_V8_VALUE[this_v8_conversion_type]
841 statement = format_string.format(cpp_value=cpp_value, isolate=isolate, creat ion_context=creation_context) 822 statement = format_string.format(cpp_value=cpp_value, isolate=isolate, creat ion_context=creation_context)
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
879 860
880 861
881 def is_explicit_nullable(idl_type): 862 def is_explicit_nullable(idl_type):
882 # Nullable type that isn't implicit nullable (see above.) For such types, 863 # Nullable type that isn't implicit nullable (see above.) For such types,
883 # we use Nullable<T> or similar explicit ways to represent a null value. 864 # we use Nullable<T> or similar explicit ways to represent a null value.
884 return idl_type.is_nullable and not idl_type.is_implicit_nullable 865 return idl_type.is_nullable and not idl_type.is_implicit_nullable
885 866
886 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable) 867 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable)
887 IdlUnionType.is_implicit_nullable = False 868 IdlUnionType.is_implicit_nullable = False
888 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable) 869 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698