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( |
| 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( |
| 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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer) | 227 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer) |
224 IdlTypeBase.cpp_type_args = cpp_type | 228 IdlTypeBase.cpp_type_args = cpp_type |
225 IdlUnionType.cpp_type = property(cpp_type_union) | 229 IdlUnionType.cpp_type = property(cpp_type_union) |
226 IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union) | 230 IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union) |
227 IdlUnionType.cpp_type_args = cpp_type_union | 231 IdlUnionType.cpp_type_args = cpp_type_union |
228 | 232 |
229 | 233 |
230 IdlTypeBase.native_array_element_type = None | 234 IdlTypeBase.native_array_element_type = None |
231 IdlArrayOrSequenceType.native_array_element_type = property( | 235 IdlArrayOrSequenceType.native_array_element_type = property( |
232 lambda self: self.element_type) | 236 lambda self: self.element_type) |
| 237 IdlNullableType.native_array_element_type = property( |
| 238 lambda self: self.inner_type.native_array_element_type) |
233 | 239 |
234 | 240 |
235 def cpp_template_type(template, inner_type): | 241 def cpp_template_type(template, inner_type): |
236 """Returns C++ template specialized to type, with space added if needed.""" | 242 """Returns C++ template specialized to type, with space added if needed.""" |
237 if inner_type.endswith('>'): | 243 if inner_type.endswith('>'): |
238 format_string = '{template}<{inner_type} >' | 244 format_string = '{template}<{inner_type} >' |
239 else: | 245 else: |
240 format_string = '{template}<{inner_type}>' | 246 format_string = '{template}<{inner_type}>' |
241 return format_string.format(template=template, inner_type=inner_type) | 247 return format_string.format(template=template, inner_type=inner_type) |
242 | 248 |
(...skipping 23 matching lines...) Expand all Loading... |
266 IdlType.implemented_as_interfaces = {} | 272 IdlType.implemented_as_interfaces = {} |
267 | 273 |
268 | 274 |
269 def implemented_as(idl_type): | 275 def implemented_as(idl_type): |
270 base_idl_type = idl_type.base_type | 276 base_idl_type = idl_type.base_type |
271 if base_idl_type in IdlType.implemented_as_interfaces: | 277 if base_idl_type in IdlType.implemented_as_interfaces: |
272 return IdlType.implemented_as_interfaces[base_idl_type] | 278 return IdlType.implemented_as_interfaces[base_idl_type] |
273 return base_idl_type | 279 return base_idl_type |
274 | 280 |
275 | 281 |
276 IdlType.implemented_as = property(implemented_as) | 282 IdlTypeBase.implemented_as = property(implemented_as) |
277 | 283 |
278 IdlType.set_implemented_as_interfaces = classmethod( | 284 IdlType.set_implemented_as_interfaces = classmethod( |
279 lambda cls, new_implemented_as_interfaces: | 285 lambda cls, new_implemented_as_interfaces: |
280 cls.implemented_as_interfaces.update(new_implemented_as_interfaces)) | 286 cls.implemented_as_interfaces.update(new_implemented_as_interfaces)) |
281 | 287 |
282 | 288 |
283 # [GarbageCollected] | 289 # [GarbageCollected] |
284 IdlType.garbage_collected_types = set() | 290 IdlType.garbage_collected_types = set() |
285 | 291 |
286 IdlTypeBase.is_garbage_collected = False | 292 IdlTypeBase.is_garbage_collected = property( |
287 IdlType.is_garbage_collected = property( | |
288 lambda self: self.base_type in IdlType.garbage_collected_types) | 293 lambda self: self.base_type in IdlType.garbage_collected_types) |
289 | 294 |
290 IdlType.set_garbage_collected_types = classmethod( | 295 IdlType.set_garbage_collected_types = classmethod( |
291 lambda cls, new_garbage_collected_types: | 296 lambda cls, new_garbage_collected_types: |
292 cls.garbage_collected_types.update(new_garbage_collected_types)) | 297 cls.garbage_collected_types.update(new_garbage_collected_types)) |
293 | 298 |
294 | 299 |
295 # [WillBeGarbageCollected] | 300 # [WillBeGarbageCollected] |
296 IdlType.will_be_garbage_collected_types = set() | 301 IdlType.will_be_garbage_collected_types = set() |
297 | 302 |
298 IdlTypeBase.is_will_be_garbage_collected = False | 303 IdlTypeBase.is_will_be_garbage_collected = property( |
299 IdlType.is_will_be_garbage_collected = property( | |
300 lambda self: self.base_type in IdlType.will_be_garbage_collected_types) | 304 lambda self: self.base_type in IdlType.will_be_garbage_collected_types) |
301 | 305 |
302 IdlType.set_will_be_garbage_collected_types = classmethod( | 306 IdlType.set_will_be_garbage_collected_types = classmethod( |
303 lambda cls, new_will_be_garbage_collected_types: | 307 lambda cls, new_will_be_garbage_collected_types: |
304 cls.will_be_garbage_collected_types.update(new_will_be_garbage_collected
_types)) | 308 cls.will_be_garbage_collected_types.update(new_will_be_garbage_collected
_types)) |
305 | 309 |
306 | 310 |
307 def gc_type(idl_type): | 311 def gc_type(idl_type): |
308 if idl_type.is_garbage_collected: | 312 if idl_type.is_garbage_collected: |
309 return 'GarbageCollectedObject' | 313 return 'GarbageCollectedObject' |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 return set() | 378 return set() |
375 return set(['bindings/%s/v8/V8%s.h' % (component_dir[base_idl_type], | 379 return set(['bindings/%s/v8/V8%s.h' % (component_dir[base_idl_type], |
376 base_idl_type)]) | 380 base_idl_type)]) |
377 | 381 |
378 IdlType.includes_for_type = property(includes_for_type) | 382 IdlType.includes_for_type = property(includes_for_type) |
379 IdlUnionType.includes_for_type = property( | 383 IdlUnionType.includes_for_type = property( |
380 lambda self: set.union(*[includes_for_type(member_type) | 384 lambda self: set.union(*[includes_for_type(member_type) |
381 for member_type in self.member_types])) | 385 for member_type in self.member_types])) |
382 IdlArrayOrSequenceType.includes_for_type = property( | 386 IdlArrayOrSequenceType.includes_for_type = property( |
383 lambda self: self.element_type.includes_for_type) | 387 lambda self: self.element_type.includes_for_type) |
| 388 IdlNullableType.includes_for_type = property( |
| 389 lambda self: self.inner_type.includes_for_type) |
384 | 390 |
385 | 391 |
386 def add_includes_for_type(idl_type): | 392 def add_includes_for_type(idl_type): |
387 includes.update(idl_type.includes_for_type) | 393 includes.update(idl_type.includes_for_type) |
388 | 394 |
389 IdlTypeBase.add_includes_for_type = add_includes_for_type | 395 IdlTypeBase.add_includes_for_type = add_includes_for_type |
390 | 396 |
391 | 397 |
392 def includes_for_interface(interface_name): | 398 def includes_for_interface(interface_name): |
393 return IdlType(interface_name).includes_for_type | 399 return IdlType(interface_name).includes_for_type |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 | 592 |
587 | 593 |
588 def preprocess_idl_type_and_value(idl_type, cpp_value, extended_attributes): | 594 def preprocess_idl_type_and_value(idl_type, cpp_value, extended_attributes): |
589 """Returns IDL type and value, with preliminary type conversions applied.""" | 595 """Returns IDL type and value, with preliminary type conversions applied.""" |
590 idl_type = idl_type.preprocessed_type | 596 idl_type = idl_type.preprocessed_type |
591 if idl_type.name == 'Promise': | 597 if idl_type.name == 'Promise': |
592 idl_type = IdlType('ScriptValue') | 598 idl_type = IdlType('ScriptValue') |
593 if idl_type.base_type in ['long long', 'unsigned long long']: | 599 if idl_type.base_type in ['long long', 'unsigned long long']: |
594 # long long and unsigned long long are not representable in ECMAScript; | 600 # long long and unsigned long long are not representable in ECMAScript; |
595 # we represent them as doubles. | 601 # we represent them as doubles. |
596 idl_type = IdlType('double', is_nullable=idl_type.is_nullable) | 602 is_nullable = idl_type.is_nullable |
| 603 idl_type = IdlType('double') |
| 604 if is_nullable: |
| 605 idl_type = IdlNullableType(idl_type) |
597 cpp_value = 'static_cast<double>(%s)' % cpp_value | 606 cpp_value = 'static_cast<double>(%s)' % cpp_value |
598 # HTML5 says that unsigned reflected attributes should be in the range | 607 # HTML5 says that unsigned reflected attributes should be in the range |
599 # [0, 2^31). When a value isn't in this range, a default value (or 0) | 608 # [0, 2^31). When a value isn't in this range, a default value (or 0) |
600 # should be returned instead. | 609 # should be returned instead. |
601 extended_attributes = extended_attributes or {} | 610 extended_attributes = extended_attributes or {} |
602 if ('Reflect' in extended_attributes and | 611 if ('Reflect' in extended_attributes and |
603 idl_type.base_type in ['unsigned long', 'unsigned short']): | 612 idl_type.base_type in ['unsigned long', 'unsigned short']): |
604 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute', | 613 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute', |
605 'getIntegralAttribute') | 614 'getIntegralAttribute') |
606 cpp_value = 'std::max(0, static_cast<int>(%s))' % cpp_value | 615 cpp_value = 'std::max(0, static_cast<int>(%s))' % cpp_value |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 extended_attributes, | 737 extended_attributes, |
729 script_wrappable, | 738 script_wrappable, |
730 release and release[i], | 739 release and release[i], |
731 for_main_world) | 740 for_main_world) |
732 for i, member_type in | 741 for i, member_type in |
733 enumerate(idl_type.member_types)] | 742 enumerate(idl_type.member_types)] |
734 | 743 |
735 IdlTypeBase.v8_set_return_value = v8_set_return_value | 744 IdlTypeBase.v8_set_return_value = v8_set_return_value |
736 IdlUnionType.v8_set_return_value = v8_set_return_value_union | 745 IdlUnionType.v8_set_return_value = v8_set_return_value_union |
737 | 746 |
738 IdlType.release = property(lambda self: self.is_interface_type) | 747 IdlTypeBase.release = property(lambda self: self.is_interface_type) |
739 IdlUnionType.release = property( | 748 IdlUnionType.release = property( |
740 lambda self: [member_type.is_interface_type | 749 lambda self: [member_type.is_interface_type |
741 for member_type in self.member_types]) | 750 for member_type in self.member_types]) |
742 IdlArrayOrSequenceType.release = False | 751 IdlArrayOrSequenceType.release = False |
743 | 752 |
744 | 753 |
745 CPP_VALUE_TO_V8_VALUE = { | 754 CPP_VALUE_TO_V8_VALUE = { |
746 # Built-in types | 755 # Built-in types |
747 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', | 756 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', |
748 'DOMString': 'v8String({isolate}, {cpp_value})', | 757 'DOMString': 'v8String({isolate}, {cpp_value})', |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
782 | 791 |
783 | 792 |
784 def literal_cpp_value(idl_type, idl_literal): | 793 def literal_cpp_value(idl_type, idl_literal): |
785 """Converts an expression that is a valid C++ literal for this type.""" | 794 """Converts an expression that is a valid C++ literal for this type.""" |
786 # FIXME: add validation that idl_type and idl_literal are compatible | 795 # FIXME: add validation that idl_type and idl_literal are compatible |
787 literal_value = str(idl_literal) | 796 literal_value = str(idl_literal) |
788 if idl_type.base_type in CPP_UNSIGNED_TYPES: | 797 if idl_type.base_type in CPP_UNSIGNED_TYPES: |
789 return literal_value + 'u' | 798 return literal_value + 'u' |
790 return literal_value | 799 return literal_value |
791 | 800 |
792 IdlType.literal_cpp_value = literal_cpp_value | 801 IdlTypeBase.literal_cpp_value = literal_cpp_value |
793 | 802 |
794 | 803 |
795 ################################################################################ | 804 ################################################################################ |
796 # Utility properties for nullable types | 805 # Utility properties for nullable types |
797 ################################################################################ | 806 ################################################################################ |
798 | 807 |
799 | 808 |
800 def cpp_type_has_null_value(idl_type): | 809 def cpp_type_has_null_value(idl_type): |
801 # - String types (String/AtomicString) represent null as a null string, | 810 # - String types (String/AtomicString) represent null as a null string, |
802 # i.e. one for which String::isNull() returns true. | 811 # i.e. one for which String::isNull() returns true. |
(...skipping 10 matching lines...) Expand all Loading... |
813 | 822 |
814 | 823 |
815 def is_explicit_nullable(idl_type): | 824 def is_explicit_nullable(idl_type): |
816 # Nullable type that isn't implicit nullable (see above.) For such types, | 825 # Nullable type that isn't implicit nullable (see above.) For such types, |
817 # we use Nullable<T> or similar explicit ways to represent a null value. | 826 # we use Nullable<T> or similar explicit ways to represent a null value. |
818 return idl_type.is_nullable and not idl_type.is_implicit_nullable | 827 return idl_type.is_nullable and not idl_type.is_implicit_nullable |
819 | 828 |
820 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable) | 829 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable) |
821 IdlUnionType.is_implicit_nullable = False | 830 IdlUnionType.is_implicit_nullable = False |
822 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable) | 831 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable) |
OLD | NEW |