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, IdlArrayOrSequenceType | 42 from idl_types import IdlTypeBase, IdlType, IdlUnionType, IdlArrayOrSequenceType , IdlNullableType |
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 15 matching lines...) Expand all Loading... | |
68 'Int32Array': ('int', 'v8::kExternalIntArray'), | 68 'Int32Array': ('int', 'v8::kExternalIntArray'), |
69 'Uint8Array': ('unsigned char', 'v8::kExternalUnsignedByteArray'), | 69 'Uint8Array': ('unsigned char', 'v8::kExternalUnsignedByteArray'), |
70 'Uint8ClampedArray': ('unsigned char', 'v8::kExternalPixelArray'), | 70 'Uint8ClampedArray': ('unsigned char', 'v8::kExternalPixelArray'), |
71 'Uint16Array': ('unsigned short', 'v8::kExternalUnsignedShortArray'), | 71 'Uint16Array': ('unsigned short', 'v8::kExternalUnsignedShortArray'), |
72 'Uint32Array': ('unsigned int', 'v8::kExternalUnsignedIntArray'), | 72 'Uint32Array': ('unsigned int', 'v8::kExternalUnsignedIntArray'), |
73 } | 73 } |
74 | 74 |
75 IdlTypeBase.is_typed_array_element_type = False | 75 IdlTypeBase.is_typed_array_element_type = False |
76 IdlType.is_typed_array_element_type = property( | 76 IdlType.is_typed_array_element_type = property( |
77 lambda self: self.base_type in TYPED_ARRAYS) | 77 lambda self: self.base_type in TYPED_ARRAYS) |
78 IdlNullableType.is_typed_array_element_type = property( | |
Nils Barth (inactive)
2014/08/19 13:53:38
Remove?
| |
79 lambda self: self.inner_type.is_typed_array_element_type) | |
78 | 80 |
79 | 81 |
80 IdlTypeBase.is_wrapper_type = False | 82 IdlTypeBase.is_wrapper_type = False |
81 IdlType.is_wrapper_type = property( | 83 IdlType.is_wrapper_type = property( |
82 lambda self: (self.is_interface_type and | 84 lambda self: (self.is_interface_type and |
83 self.base_type not in NON_WRAPPER_TYPES)) | 85 self.base_type not in NON_WRAPPER_TYPES)) |
86 IdlNullableType.is_wrapper_type = property( | |
Nils Barth (inactive)
2014/08/19 13:53:38
Remove?
| |
87 lambda self: self.inner_type.is_wrapper_type) | |
84 | 88 |
85 | 89 |
86 ################################################################################ | 90 ################################################################################ |
87 # C++ types | 91 # C++ types |
88 ################################################################################ | 92 ################################################################################ |
89 | 93 |
90 CPP_TYPE_SAME_AS_IDL_TYPE = set([ | 94 CPP_TYPE_SAME_AS_IDL_TYPE = set([ |
91 'double', | 95 'double', |
92 'float', | 96 'float', |
93 'long long', | 97 'long long', |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer) | 239 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer) |
236 IdlTypeBase.cpp_type_args = cpp_type | 240 IdlTypeBase.cpp_type_args = cpp_type |
237 IdlUnionType.cpp_type = property(cpp_type_union) | 241 IdlUnionType.cpp_type = property(cpp_type_union) |
238 IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union) | 242 IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union) |
239 IdlUnionType.cpp_type_args = cpp_type_union | 243 IdlUnionType.cpp_type_args = cpp_type_union |
240 | 244 |
241 | 245 |
242 IdlTypeBase.native_array_element_type = None | 246 IdlTypeBase.native_array_element_type = None |
243 IdlArrayOrSequenceType.native_array_element_type = property( | 247 IdlArrayOrSequenceType.native_array_element_type = property( |
244 lambda self: self.element_type) | 248 lambda self: self.element_type) |
249 IdlNullableType.native_array_element_type = property( | |
bashi
2014/08/19 13:51:42
If we take this approach, can we remove this?
| |
250 lambda self: self.inner_type.native_array_element_type) | |
245 | 251 |
246 | 252 |
247 def cpp_template_type(template, inner_type): | 253 def cpp_template_type(template, inner_type): |
248 """Returns C++ template specialized to type, with space added if needed.""" | 254 """Returns C++ template specialized to type, with space added if needed.""" |
249 if inner_type.endswith('>'): | 255 if inner_type.endswith('>'): |
250 format_string = '{template}<{inner_type} >' | 256 format_string = '{template}<{inner_type} >' |
251 else: | 257 else: |
252 format_string = '{template}<{inner_type}>' | 258 format_string = '{template}<{inner_type}>' |
253 return format_string.format(template=template, inner_type=inner_type) | 259 return format_string.format(template=template, inner_type=inner_type) |
254 | 260 |
(...skipping 23 matching lines...) Expand all Loading... | |
278 IdlType.implemented_as_interfaces = {} | 284 IdlType.implemented_as_interfaces = {} |
279 | 285 |
280 | 286 |
281 def implemented_as(idl_type): | 287 def implemented_as(idl_type): |
282 base_idl_type = idl_type.base_type | 288 base_idl_type = idl_type.base_type |
283 if base_idl_type in IdlType.implemented_as_interfaces: | 289 if base_idl_type in IdlType.implemented_as_interfaces: |
284 return IdlType.implemented_as_interfaces[base_idl_type] | 290 return IdlType.implemented_as_interfaces[base_idl_type] |
285 return base_idl_type | 291 return base_idl_type |
286 | 292 |
287 | 293 |
288 IdlType.implemented_as = property(implemented_as) | 294 IdlTypeBase.implemented_as = property(implemented_as) |
289 | 295 |
290 IdlType.set_implemented_as_interfaces = classmethod( | 296 IdlType.set_implemented_as_interfaces = classmethod( |
291 lambda cls, new_implemented_as_interfaces: | 297 lambda cls, new_implemented_as_interfaces: |
292 cls.implemented_as_interfaces.update(new_implemented_as_interfaces)) | 298 cls.implemented_as_interfaces.update(new_implemented_as_interfaces)) |
293 | 299 |
294 | 300 |
295 # [GarbageCollected] | 301 # [GarbageCollected] |
296 IdlType.garbage_collected_types = set() | 302 IdlType.garbage_collected_types = set() |
297 | 303 |
298 IdlTypeBase.is_garbage_collected = False | 304 IdlTypeBase.is_garbage_collected = property( |
299 IdlType.is_garbage_collected = property( | |
300 lambda self: self.base_type in IdlType.garbage_collected_types) | 305 lambda self: self.base_type in IdlType.garbage_collected_types) |
301 | 306 |
302 IdlType.set_garbage_collected_types = classmethod( | 307 IdlType.set_garbage_collected_types = classmethod( |
303 lambda cls, new_garbage_collected_types: | 308 lambda cls, new_garbage_collected_types: |
304 cls.garbage_collected_types.update(new_garbage_collected_types)) | 309 cls.garbage_collected_types.update(new_garbage_collected_types)) |
305 | 310 |
306 | 311 |
307 # [WillBeGarbageCollected] | 312 # [WillBeGarbageCollected] |
308 IdlType.will_be_garbage_collected_types = set() | 313 IdlType.will_be_garbage_collected_types = set() |
309 | 314 |
310 IdlTypeBase.is_will_be_garbage_collected = False | 315 IdlTypeBase.is_will_be_garbage_collected = property( |
311 IdlType.is_will_be_garbage_collected = property( | |
312 lambda self: self.base_type in IdlType.will_be_garbage_collected_types) | 316 lambda self: self.base_type in IdlType.will_be_garbage_collected_types) |
313 | 317 |
314 IdlType.set_will_be_garbage_collected_types = classmethod( | 318 IdlType.set_will_be_garbage_collected_types = classmethod( |
315 lambda cls, new_will_be_garbage_collected_types: | 319 lambda cls, new_will_be_garbage_collected_types: |
316 cls.will_be_garbage_collected_types.update(new_will_be_garbage_collected _types)) | 320 cls.will_be_garbage_collected_types.update(new_will_be_garbage_collected _types)) |
317 | 321 |
318 | 322 |
319 def gc_type(idl_type): | 323 def gc_type(idl_type): |
320 if idl_type.is_garbage_collected: | 324 if idl_type.is_garbage_collected: |
321 return 'GarbageCollectedObject' | 325 return 'GarbageCollectedObject' |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
386 return set() | 390 return set() |
387 return set(['bindings/%s/v8/V8%s.h' % (component_dir[base_idl_type], | 391 return set(['bindings/%s/v8/V8%s.h' % (component_dir[base_idl_type], |
388 base_idl_type)]) | 392 base_idl_type)]) |
389 | 393 |
390 IdlType.includes_for_type = property(includes_for_type) | 394 IdlType.includes_for_type = property(includes_for_type) |
391 IdlUnionType.includes_for_type = property( | 395 IdlUnionType.includes_for_type = property( |
392 lambda self: set.union(*[includes_for_type(member_type) | 396 lambda self: set.union(*[includes_for_type(member_type) |
393 for member_type in self.member_types])) | 397 for member_type in self.member_types])) |
394 IdlArrayOrSequenceType.includes_for_type = property( | 398 IdlArrayOrSequenceType.includes_for_type = property( |
395 lambda self: self.element_type.includes_for_type) | 399 lambda self: self.element_type.includes_for_type) |
400 IdlNullableType.includes_for_type = property( | |
Nils Barth (inactive)
2014/08/19 13:53:38
Remove?
| |
401 lambda self: self.inner_type.includes_for_type) | |
396 | 402 |
397 | 403 |
398 def add_includes_for_type(idl_type): | 404 def add_includes_for_type(idl_type): |
399 includes.update(idl_type.includes_for_type) | 405 includes.update(idl_type.includes_for_type) |
400 | 406 |
401 IdlTypeBase.add_includes_for_type = add_includes_for_type | 407 IdlTypeBase.add_includes_for_type = add_includes_for_type |
402 | 408 |
403 | 409 |
404 def includes_for_interface(interface_name): | 410 def includes_for_interface(interface_name): |
405 return IdlType(interface_name).includes_for_type | 411 return IdlType(interface_name).includes_for_type |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
598 | 604 |
599 | 605 |
600 def preprocess_idl_type_and_value(idl_type, cpp_value, extended_attributes): | 606 def preprocess_idl_type_and_value(idl_type, cpp_value, extended_attributes): |
601 """Returns IDL type and value, with preliminary type conversions applied.""" | 607 """Returns IDL type and value, with preliminary type conversions applied.""" |
602 idl_type = idl_type.preprocessed_type | 608 idl_type = idl_type.preprocessed_type |
603 if idl_type.name == 'Promise': | 609 if idl_type.name == 'Promise': |
604 idl_type = IdlType('ScriptValue') | 610 idl_type = IdlType('ScriptValue') |
605 if idl_type.base_type in ['long long', 'unsigned long long']: | 611 if idl_type.base_type in ['long long', 'unsigned long long']: |
606 # long long and unsigned long long are not representable in ECMAScript; | 612 # long long and unsigned long long are not representable in ECMAScript; |
607 # we represent them as doubles. | 613 # we represent them as doubles. |
608 idl_type = IdlType('double', is_nullable=idl_type.is_nullable) | 614 is_nullable = idl_type.is_nullable |
615 idl_type = IdlType('double') | |
616 if is_nullable: | |
617 idl_type = IdlNullableType(idl_type) | |
609 cpp_value = 'static_cast<double>(%s)' % cpp_value | 618 cpp_value = 'static_cast<double>(%s)' % cpp_value |
610 # HTML5 says that unsigned reflected attributes should be in the range | 619 # HTML5 says that unsigned reflected attributes should be in the range |
611 # [0, 2^31). When a value isn't in this range, a default value (or 0) | 620 # [0, 2^31). When a value isn't in this range, a default value (or 0) |
612 # should be returned instead. | 621 # should be returned instead. |
613 extended_attributes = extended_attributes or {} | 622 extended_attributes = extended_attributes or {} |
614 if ('Reflect' in extended_attributes and | 623 if ('Reflect' in extended_attributes and |
615 idl_type.base_type in ['unsigned long', 'unsigned short']): | 624 idl_type.base_type in ['unsigned long', 'unsigned short']): |
616 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute', | 625 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute', |
617 'getIntegralAttribute') | 626 'getIntegralAttribute') |
618 cpp_value = 'std::max(0, static_cast<int>(%s))' % cpp_value | 627 cpp_value = 'std::max(0, static_cast<int>(%s))' % cpp_value |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
731 | 740 |
732 def v8_set_return_value_union(idl_type, cpp_value, extended_attributes=None, scr ipt_wrappable='', release=False, for_main_world=False): | 741 def v8_set_return_value_union(idl_type, cpp_value, extended_attributes=None, scr ipt_wrappable='', release=False, for_main_world=False): |
733 # FIXME: Need to revisit the design of union support. | 742 # FIXME: Need to revisit the design of union support. |
734 # http://crbug.com/240176 | 743 # http://crbug.com/240176 |
735 return None | 744 return None |
736 | 745 |
737 | 746 |
738 IdlTypeBase.v8_set_return_value = v8_set_return_value | 747 IdlTypeBase.v8_set_return_value = v8_set_return_value |
739 IdlUnionType.v8_set_return_value = v8_set_return_value_union | 748 IdlUnionType.v8_set_return_value = v8_set_return_value_union |
740 | 749 |
741 IdlType.release = property(lambda self: self.is_interface_type) | 750 IdlTypeBase.release = property(lambda self: self.is_interface_type) |
742 IdlUnionType.release = property( | 751 IdlUnionType.release = property( |
743 lambda self: [member_type.is_interface_type | 752 lambda self: [member_type.is_interface_type |
744 for member_type in self.member_types]) | 753 for member_type in self.member_types]) |
745 IdlArrayOrSequenceType.release = False | 754 IdlArrayOrSequenceType.release = False |
746 | 755 |
747 | 756 |
748 CPP_VALUE_TO_V8_VALUE = { | 757 CPP_VALUE_TO_V8_VALUE = { |
749 # Built-in types | 758 # Built-in types |
750 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', | 759 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', |
751 'DOMString': 'v8String({isolate}, {cpp_value})', | 760 'DOMString': 'v8String({isolate}, {cpp_value})', |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
785 | 794 |
786 | 795 |
787 def literal_cpp_value(idl_type, idl_literal): | 796 def literal_cpp_value(idl_type, idl_literal): |
788 """Converts an expression that is a valid C++ literal for this type.""" | 797 """Converts an expression that is a valid C++ literal for this type.""" |
789 # FIXME: add validation that idl_type and idl_literal are compatible | 798 # FIXME: add validation that idl_type and idl_literal are compatible |
790 literal_value = str(idl_literal) | 799 literal_value = str(idl_literal) |
791 if idl_type.base_type in CPP_UNSIGNED_TYPES: | 800 if idl_type.base_type in CPP_UNSIGNED_TYPES: |
792 return literal_value + 'u' | 801 return literal_value + 'u' |
793 return literal_value | 802 return literal_value |
794 | 803 |
795 IdlType.literal_cpp_value = literal_cpp_value | 804 IdlTypeBase.literal_cpp_value = literal_cpp_value |
796 | 805 |
797 | 806 |
798 ################################################################################ | 807 ################################################################################ |
799 # Utility properties for nullable types | 808 # Utility properties for nullable types |
800 ################################################################################ | 809 ################################################################################ |
801 | 810 |
802 | 811 |
803 def cpp_type_has_null_value(idl_type): | 812 def cpp_type_has_null_value(idl_type): |
804 # - String types (String/AtomicString) represent null as a null string, | 813 # - String types (String/AtomicString) represent null as a null string, |
805 # i.e. one for which String::isNull() returns true. | 814 # i.e. one for which String::isNull() returns true. |
(...skipping 10 matching lines...) Expand all Loading... | |
816 | 825 |
817 | 826 |
818 def is_explicit_nullable(idl_type): | 827 def is_explicit_nullable(idl_type): |
819 # Nullable type that isn't implicit nullable (see above.) For such types, | 828 # Nullable type that isn't implicit nullable (see above.) For such types, |
820 # we use Nullable<T> or similar explicit ways to represent a null value. | 829 # we use Nullable<T> or similar explicit ways to represent a null value. |
821 return idl_type.is_nullable and not idl_type.is_implicit_nullable | 830 return idl_type.is_nullable and not idl_type.is_implicit_nullable |
822 | 831 |
823 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable) | 832 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable) |
824 IdlUnionType.is_implicit_nullable = False | 833 IdlUnionType.is_implicit_nullable = False |
825 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable) | 834 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable) |
OLD | NEW |