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

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

Issue 474173002: IDL: Use IdlNullableType wrapper to represent nullable types (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: cleanup Created 6 years, 4 months 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 21 matching lines...) Expand all
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 12 matching lines...) Expand all
65 'Float64Array': ('double', 'v8::kExternalDoubleArray'), 65 'Float64Array': ('double', 'v8::kExternalDoubleArray'),
66 'Int8Array': ('signed char', 'v8::kExternalByteArray'), 66 'Int8Array': ('signed char', 'v8::kExternalByteArray'),
67 'Int16Array': ('short', 'v8::kExternalShortArray'), 67 'Int16Array': ('short', 'v8::kExternalShortArray'),
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
76 IdlType.is_typed_array_element_type = property( 75 IdlType.is_typed_array_element_type = property(
77 lambda self: self.base_type in TYPED_ARRAYS) 76 lambda self: self.base_type in TYPED_ARRAYS)
78 77
79
80 IdlTypeBase.is_wrapper_type = False
81 IdlType.is_wrapper_type = property( 78 IdlType.is_wrapper_type = property(
82 lambda self: (self.is_interface_type and 79 lambda self: (self.is_interface_type and
83 self.base_type not in NON_WRAPPER_TYPES)) 80 self.base_type not in NON_WRAPPER_TYPES))
84 81
85 82
86 ################################################################################ 83 ################################################################################
87 # C++ types 84 # C++ types
88 ################################################################################ 85 ################################################################################
89 86
90 CPP_TYPE_SAME_AS_IDL_TYPE = set([ 87 CPP_TYPE_SAME_AS_IDL_TYPE = set([
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 229
233 # Allow access as idl_type.cpp_type if no arguments 230 # Allow access as idl_type.cpp_type if no arguments
234 IdlTypeBase.cpp_type = property(cpp_type) 231 IdlTypeBase.cpp_type = property(cpp_type)
235 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer) 232 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer)
236 IdlTypeBase.cpp_type_args = cpp_type 233 IdlTypeBase.cpp_type_args = cpp_type
237 IdlUnionType.cpp_type = property(cpp_type_union) 234 IdlUnionType.cpp_type = property(cpp_type_union)
238 IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union) 235 IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union)
239 IdlUnionType.cpp_type_args = cpp_type_union 236 IdlUnionType.cpp_type_args = cpp_type_union
240 237
241 238
242 IdlTypeBase.native_array_element_type = None
243 IdlArrayOrSequenceType.native_array_element_type = property( 239 IdlArrayOrSequenceType.native_array_element_type = property(
244 lambda self: self.element_type) 240 lambda self: self.element_type)
245 241
246 242
247 def cpp_template_type(template, inner_type): 243 def cpp_template_type(template, inner_type):
248 """Returns C++ template specialized to type, with space added if needed.""" 244 """Returns C++ template specialized to type, with space added if needed."""
249 if inner_type.endswith('>'): 245 if inner_type.endswith('>'):
250 format_string = '{template}<{inner_type} >' 246 format_string = '{template}<{inner_type} >'
251 else: 247 else:
252 format_string = '{template}<{inner_type}>' 248 format_string = '{template}<{inner_type}>'
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 IdlType.implemented_as = property(implemented_as) 284 IdlType.implemented_as = property(implemented_as)
289 285
290 IdlType.set_implemented_as_interfaces = classmethod( 286 IdlType.set_implemented_as_interfaces = classmethod(
291 lambda cls, new_implemented_as_interfaces: 287 lambda cls, new_implemented_as_interfaces:
292 cls.implemented_as_interfaces.update(new_implemented_as_interfaces)) 288 cls.implemented_as_interfaces.update(new_implemented_as_interfaces))
293 289
294 290
295 # [GarbageCollected] 291 # [GarbageCollected]
296 IdlType.garbage_collected_types = set() 292 IdlType.garbage_collected_types = set()
297 293
298 IdlTypeBase.is_garbage_collected = False
299 IdlType.is_garbage_collected = property( 294 IdlType.is_garbage_collected = property(
300 lambda self: self.base_type in IdlType.garbage_collected_types) 295 lambda self: self.base_type in IdlType.garbage_collected_types)
301 296
302 IdlType.set_garbage_collected_types = classmethod( 297 IdlType.set_garbage_collected_types = classmethod(
303 lambda cls, new_garbage_collected_types: 298 lambda cls, new_garbage_collected_types:
304 cls.garbage_collected_types.update(new_garbage_collected_types)) 299 cls.garbage_collected_types.update(new_garbage_collected_types))
305 300
306 301
307 # [WillBeGarbageCollected] 302 # [WillBeGarbageCollected]
308 IdlType.will_be_garbage_collected_types = set() 303 IdlType.will_be_garbage_collected_types = set()
309 304
310 IdlTypeBase.is_will_be_garbage_collected = False
311 IdlType.is_will_be_garbage_collected = property( 305 IdlType.is_will_be_garbage_collected = property(
312 lambda self: self.base_type in IdlType.will_be_garbage_collected_types) 306 lambda self: self.base_type in IdlType.will_be_garbage_collected_types)
313 307
314 IdlType.set_will_be_garbage_collected_types = classmethod( 308 IdlType.set_will_be_garbage_collected_types = classmethod(
315 lambda cls, new_will_be_garbage_collected_types: 309 lambda cls, new_will_be_garbage_collected_types:
316 cls.will_be_garbage_collected_types.update(new_will_be_garbage_collected _types)) 310 cls.will_be_garbage_collected_types.update(new_will_be_garbage_collected _types))
317 311
318 312
319 def gc_type(idl_type): 313 def gc_type(idl_type):
320 if idl_type.is_garbage_collected: 314 if idl_type.is_garbage_collected:
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 592
599 593
600 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):
601 """Returns IDL type and value, with preliminary type conversions applied.""" 595 """Returns IDL type and value, with preliminary type conversions applied."""
602 idl_type = idl_type.preprocessed_type 596 idl_type = idl_type.preprocessed_type
603 if idl_type.name == 'Promise': 597 if idl_type.name == 'Promise':
604 idl_type = IdlType('ScriptValue') 598 idl_type = IdlType('ScriptValue')
605 if idl_type.base_type in ['long long', 'unsigned long long']: 599 if idl_type.base_type in ['long long', 'unsigned long long']:
606 # long long and unsigned long long are not representable in ECMAScript; 600 # long long and unsigned long long are not representable in ECMAScript;
607 # we represent them as doubles. 601 # we represent them as doubles.
608 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)
609 cpp_value = 'static_cast<double>(%s)' % cpp_value 606 cpp_value = 'static_cast<double>(%s)' % cpp_value
610 # HTML5 says that unsigned reflected attributes should be in the range 607 # 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) 608 # [0, 2^31). When a value isn't in this range, a default value (or 0)
612 # should be returned instead. 609 # should be returned instead.
613 extended_attributes = extended_attributes or {} 610 extended_attributes = extended_attributes or {}
614 if ('Reflect' in extended_attributes and 611 if ('Reflect' in extended_attributes and
615 idl_type.base_type in ['unsigned long', 'unsigned short']): 612 idl_type.base_type in ['unsigned long', 'unsigned short']):
616 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute', 613 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute',
617 'getIntegralAttribute') 614 'getIntegralAttribute')
618 cpp_value = 'std::max(0, static_cast<int>(%s))' % cpp_value 615 cpp_value = 'std::max(0, static_cast<int>(%s))' % cpp_value
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 return None 732 return None
736 733
737 734
738 IdlTypeBase.v8_set_return_value = v8_set_return_value 735 IdlTypeBase.v8_set_return_value = v8_set_return_value
739 IdlUnionType.v8_set_return_value = v8_set_return_value_union 736 IdlUnionType.v8_set_return_value = v8_set_return_value_union
740 737
741 IdlType.release = property(lambda self: self.is_interface_type) 738 IdlType.release = property(lambda self: self.is_interface_type)
742 IdlUnionType.release = property( 739 IdlUnionType.release = property(
743 lambda self: [member_type.is_interface_type 740 lambda self: [member_type.is_interface_type
744 for member_type in self.member_types]) 741 for member_type in self.member_types])
745 IdlArrayOrSequenceType.release = False
746 742
747 743
748 CPP_VALUE_TO_V8_VALUE = { 744 CPP_VALUE_TO_V8_VALUE = {
749 # Built-in types 745 # Built-in types
750 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', 746 'Date': 'v8DateOrNaN({cpp_value}, {isolate})',
751 'DOMString': 'v8String({isolate}, {cpp_value})', 747 'DOMString': 'v8String({isolate}, {cpp_value})',
752 'ByteString': 'v8String({isolate}, {cpp_value})', 748 'ByteString': 'v8String({isolate}, {cpp_value})',
753 'ScalarValueString': 'v8String({isolate}, {cpp_value})', 749 'ScalarValueString': 'v8String({isolate}, {cpp_value})',
754 'boolean': 'v8Boolean({cpp_value}, {isolate})', 750 'boolean': 'v8Boolean({cpp_value}, {isolate})',
755 'int': 'v8::Integer::New({isolate}, {cpp_value})', 751 'int': 'v8::Integer::New({isolate}, {cpp_value})',
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 812
817 813
818 def is_explicit_nullable(idl_type): 814 def is_explicit_nullable(idl_type):
819 # Nullable type that isn't implicit nullable (see above.) For such types, 815 # 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. 816 # 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 817 return idl_type.is_nullable and not idl_type.is_implicit_nullable
822 818
823 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable) 819 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable)
824 IdlUnionType.is_implicit_nullable = False 820 IdlUnionType.is_implicit_nullable = False
825 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable) 821 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable)
OLDNEW
« no previous file with comments | « Source/bindings/scripts/v8_dictionary.py ('k') | Source/bindings/tests/results/V8TestObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698