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 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 12 matching lines...) Expand all Loading... |
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 IdlType.is_typed_array_type = property( | 75 IdlTypeBase.is_typed_array_element_type = False |
| 76 IdlType.is_typed_array_element_type = property( |
76 lambda self: self.base_type in TYPED_ARRAYS) | 77 lambda self: self.base_type in TYPED_ARRAYS) |
77 | 78 |
78 | 79 |
| 80 IdlTypeBase.is_wrapper_type = False |
79 IdlType.is_wrapper_type = property( | 81 IdlType.is_wrapper_type = property( |
80 lambda self: (self.is_interface_type and | 82 lambda self: (self.is_interface_type and |
81 self.base_type not in NON_WRAPPER_TYPES)) | 83 self.base_type not in NON_WRAPPER_TYPES)) |
82 | 84 |
83 | 85 |
84 ################################################################################ | 86 ################################################################################ |
85 # C++ types | 87 # C++ types |
86 ################################################################################ | 88 ################################################################################ |
87 | 89 |
88 CPP_TYPE_SAME_AS_IDL_TYPE = set([ | 90 CPP_TYPE_SAME_AS_IDL_TYPE = set([ |
(...skipping 23 matching lines...) Expand all Loading... |
112 'Promise': 'ScriptPromise', | 114 'Promise': 'ScriptPromise', |
113 'ScriptValue': 'ScriptValue', | 115 'ScriptValue': 'ScriptValue', |
114 # FIXME: Eliminate custom bindings for XPathNSResolver http://crbug.com/345
529 | 116 # FIXME: Eliminate custom bindings for XPathNSResolver http://crbug.com/345
529 |
115 'XPathNSResolver': 'RefPtrWillBeRawPtr<XPathNSResolver>', | 117 'XPathNSResolver': 'RefPtrWillBeRawPtr<XPathNSResolver>', |
116 'boolean': 'bool', | 118 'boolean': 'bool', |
117 'unrestricted double': 'double', | 119 'unrestricted double': 'double', |
118 'unrestricted float': 'float', | 120 'unrestricted float': 'float', |
119 } | 121 } |
120 | 122 |
121 | 123 |
122 def cpp_type(idl_type, extended_attributes=None, used_as_argument=False, used_as
_variadic_argument=False, used_in_cpp_sequence=False): | 124 def cpp_type(idl_type, extended_attributes=None, raw_type=False, used_as_rvalue_
type=False, used_as_variadic_argument=False, used_in_cpp_sequence=False): |
123 """Returns C++ type corresponding to IDL type. | 125 """Returns C++ type corresponding to IDL type. |
124 | 126 |
125 |idl_type| argument is of type IdlType, while return value is a string | 127 |idl_type| argument is of type IdlType, while return value is a string |
126 | 128 |
127 Args: | 129 Args: |
128 idl_type: | 130 idl_type: |
129 IdlType | 131 IdlType |
130 used_as_argument: | 132 raw_type: |
131 bool, True if idl_type's raw/primitive C++ type should be returned. | 133 bool, True if idl_type's raw/primitive C++ type should be returned. |
| 134 used_as_rvalue_type: |
| 135 bool, True if the C++ type is used as an argument or the return |
| 136 type of a method. |
| 137 used_as_variadic_argument: |
| 138 bool, True if the C++ type is used as a variadic argument of a metho
d. |
132 used_in_cpp_sequence: | 139 used_in_cpp_sequence: |
133 bool, True if the C++ type is used as an element of an array or sequ
ence. | 140 bool, True if the C++ type is used as an element of a container. |
| 141 Containers can be an array, a sequence or a dictionary. |
134 """ | 142 """ |
135 def string_mode(): | 143 def string_mode(): |
136 # FIXME: the Web IDL spec requires 'EmptyString', not 'NullString', | 144 if extended_attributes.get('TreatNullAs') == 'EmptyString': |
137 # but we use NullString for performance. | 145 return 'TreatNullAsEmptyString' |
138 if extended_attributes.get('TreatNullAs') != 'NullString': | 146 if idl_type.is_nullable or extended_attributes.get('TreatNullAs') == 'Nu
llString': |
139 return '' | 147 if extended_attributes.get('TreatUndefinedAs') == 'NullString': |
140 if extended_attributes.get('TreatUndefinedAs') != 'NullString': | 148 return 'TreatNullAndUndefinedAsNullString' |
141 return 'WithNullCheck' | 149 return 'TreatNullAsNullString' |
142 return 'WithUndefinedOrNullCheck' | 150 return '' |
143 | 151 |
144 extended_attributes = extended_attributes or {} | 152 extended_attributes = extended_attributes or {} |
145 idl_type = idl_type.preprocessed_type | 153 idl_type = idl_type.preprocessed_type |
146 | 154 |
147 # Composite types | 155 # Array or sequence types |
148 if used_as_variadic_argument: | 156 if used_as_variadic_argument: |
149 array_or_sequence_type = idl_type | 157 native_array_element_type = idl_type |
150 else: | 158 else: |
151 array_or_sequence_type = idl_type.array_or_sequence_type | 159 native_array_element_type = idl_type.native_array_element_type |
152 if array_or_sequence_type: | 160 if native_array_element_type: |
153 vector_type = cpp_ptr_type('Vector', 'HeapVector', array_or_sequence_typ
e.gc_type) | 161 vector_type = cpp_ptr_type('Vector', 'HeapVector', native_array_element_
type.gc_type) |
154 return cpp_template_type(vector_type, array_or_sequence_type.cpp_type_ar
gs(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: |
| 164 return 'const %s&' % vector_template_type |
| 165 return vector_template_type |
155 | 166 |
156 # Simple types | 167 # Simple types |
157 base_idl_type = idl_type.base_type | 168 base_idl_type = idl_type.base_type |
158 | 169 |
159 if base_idl_type in CPP_TYPE_SAME_AS_IDL_TYPE: | 170 if base_idl_type in CPP_TYPE_SAME_AS_IDL_TYPE: |
160 return base_idl_type | 171 return base_idl_type |
161 if base_idl_type in CPP_INT_TYPES: | 172 if base_idl_type in CPP_INT_TYPES: |
162 return 'int' | 173 return 'int' |
163 if base_idl_type in CPP_UNSIGNED_TYPES: | 174 if base_idl_type in CPP_UNSIGNED_TYPES: |
164 return 'unsigned' | 175 return 'unsigned' |
165 if base_idl_type in CPP_SPECIAL_CONVERSION_RULES: | 176 if base_idl_type in CPP_SPECIAL_CONVERSION_RULES: |
166 return CPP_SPECIAL_CONVERSION_RULES[base_idl_type] | 177 return CPP_SPECIAL_CONVERSION_RULES[base_idl_type] |
167 | 178 |
168 if base_idl_type in NON_WRAPPER_TYPES: | 179 if base_idl_type in NON_WRAPPER_TYPES: |
169 return 'RefPtr<%s>' % base_idl_type | 180 return ('PassRefPtr<%s>' if used_as_rvalue_type else 'RefPtr<%s>') % bas
e_idl_type |
170 if base_idl_type in ('DOMString', 'ByteString', 'ScalarValueString'): | 181 if idl_type.is_string_type: |
171 if not used_as_argument: | 182 if not raw_type: |
172 return 'String' | 183 return 'String' |
173 return 'V8StringResource<%s>' % string_mode() | 184 return 'V8StringResource<%s>' % string_mode() |
174 | 185 |
175 if idl_type.is_typed_array_type and used_as_argument: | 186 if idl_type.is_typed_array_element_type and raw_type: |
176 return base_idl_type + '*' | 187 return base_idl_type + '*' |
177 if idl_type.is_interface_type: | 188 if idl_type.is_interface_type: |
178 implemented_as_class = idl_type.implemented_as | 189 implemented_as_class = idl_type.implemented_as |
179 if used_as_argument: | 190 if raw_type: |
180 return implemented_as_class + '*' | 191 return implemented_as_class + '*' |
181 new_type = 'Member' if used_in_cpp_sequence else 'RawPtr' | 192 new_type = 'Member' if used_in_cpp_sequence else 'RawPtr' |
182 ptr_type = cpp_ptr_type('RefPtr', new_type, idl_type.gc_type) | 193 ptr_type = cpp_ptr_type(('PassRefPtr' if used_as_rvalue_type else 'RefPt
r'), new_type, idl_type.gc_type) |
183 return cpp_template_type(ptr_type, implemented_as_class) | 194 return cpp_template_type(ptr_type, implemented_as_class) |
184 # Default, assume native type is a pointer with same type name as idl type | 195 # Default, assume native type is a pointer with same type name as idl type |
185 return base_idl_type + '*' | 196 return base_idl_type + '*' |
186 | 197 |
187 | 198 |
188 def cpp_type_union(idl_type, extended_attributes=None, used_as_argument=False): | 199 def cpp_type_initializer(idl_type): |
189 return (member_type.cpp_type for member_type in idl_type.member_types) | 200 """Returns a string containing a C++ initialization statement for the |
| 201 corresponding type. |
| 202 |
| 203 |idl_type| argument is of type IdlType. |
| 204 """ |
| 205 |
| 206 if (idl_type.is_numeric_type): |
| 207 return ' = 0' |
| 208 if idl_type.base_type == 'boolean': |
| 209 return ' = false' |
| 210 if idl_type.base_type == 'Promise': |
| 211 return '(nullptr)' |
| 212 return '' |
| 213 |
| 214 |
| 215 def cpp_type_union(idl_type, extended_attributes=None, raw_type=False): |
| 216 # FIXME: Need to revisit the design of union support. |
| 217 # http://crbug.com/240176 |
| 218 return None |
| 219 |
| 220 |
| 221 def cpp_type_initializer_union(idl_type): |
| 222 return (member_type.cpp_type_initializer for member_type in idl_type.member_
types) |
190 | 223 |
191 | 224 |
192 # Allow access as idl_type.cpp_type if no arguments | 225 # Allow access as idl_type.cpp_type if no arguments |
193 IdlType.cpp_type = property(cpp_type) | 226 IdlTypeBase.cpp_type = property(cpp_type) |
| 227 IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer) |
| 228 IdlTypeBase.cpp_type_args = cpp_type |
194 IdlUnionType.cpp_type = property(cpp_type_union) | 229 IdlUnionType.cpp_type = property(cpp_type_union) |
195 IdlType.cpp_type_args = cpp_type | 230 IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union) |
196 IdlUnionType.cpp_type_args = cpp_type_union | 231 IdlUnionType.cpp_type_args = cpp_type_union |
197 | 232 |
198 | 233 |
| 234 IdlTypeBase.native_array_element_type = None |
| 235 IdlArrayOrSequenceType.native_array_element_type = property( |
| 236 lambda self: self.element_type) |
| 237 |
| 238 |
199 def cpp_template_type(template, inner_type): | 239 def cpp_template_type(template, inner_type): |
200 """Returns C++ template specialized to type, with space added if needed.""" | 240 """Returns C++ template specialized to type, with space added if needed.""" |
201 if inner_type.endswith('>'): | 241 if inner_type.endswith('>'): |
202 format_string = '{template}<{inner_type} >' | 242 format_string = '{template}<{inner_type} >' |
203 else: | 243 else: |
204 format_string = '{template}<{inner_type}>' | 244 format_string = '{template}<{inner_type}>' |
205 return format_string.format(template=template, inner_type=inner_type) | 245 return format_string.format(template=template, inner_type=inner_type) |
206 | 246 |
207 | 247 |
208 def cpp_ptr_type(old_type, new_type, gc_type): | 248 def cpp_ptr_type(old_type, new_type, gc_type): |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 IdlType.implemented_as = property(implemented_as) | 280 IdlType.implemented_as = property(implemented_as) |
241 | 281 |
242 IdlType.set_implemented_as_interfaces = classmethod( | 282 IdlType.set_implemented_as_interfaces = classmethod( |
243 lambda cls, new_implemented_as_interfaces: | 283 lambda cls, new_implemented_as_interfaces: |
244 cls.implemented_as_interfaces.update(new_implemented_as_interfaces)) | 284 cls.implemented_as_interfaces.update(new_implemented_as_interfaces)) |
245 | 285 |
246 | 286 |
247 # [GarbageCollected] | 287 # [GarbageCollected] |
248 IdlType.garbage_collected_types = set() | 288 IdlType.garbage_collected_types = set() |
249 | 289 |
| 290 IdlTypeBase.is_garbage_collected = False |
250 IdlType.is_garbage_collected = property( | 291 IdlType.is_garbage_collected = property( |
251 lambda self: self.base_type in IdlType.garbage_collected_types) | 292 lambda self: self.base_type in IdlType.garbage_collected_types) |
252 | 293 |
253 IdlType.set_garbage_collected_types = classmethod( | 294 IdlType.set_garbage_collected_types = classmethod( |
254 lambda cls, new_garbage_collected_types: | 295 lambda cls, new_garbage_collected_types: |
255 cls.garbage_collected_types.update(new_garbage_collected_types)) | 296 cls.garbage_collected_types.update(new_garbage_collected_types)) |
256 | 297 |
257 | 298 |
258 # [WillBeGarbageCollected] | 299 # [WillBeGarbageCollected] |
259 IdlType.will_be_garbage_collected_types = set() | 300 IdlType.will_be_garbage_collected_types = set() |
260 | 301 |
| 302 IdlTypeBase.is_will_be_garbage_collected = False |
261 IdlType.is_will_be_garbage_collected = property( | 303 IdlType.is_will_be_garbage_collected = property( |
262 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) |
263 | 305 |
264 IdlType.set_will_be_garbage_collected_types = classmethod( | 306 IdlType.set_will_be_garbage_collected_types = classmethod( |
265 lambda cls, new_will_be_garbage_collected_types: | 307 lambda cls, new_will_be_garbage_collected_types: |
266 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)) |
267 | 309 |
268 | 310 |
269 def gc_type(idl_type): | 311 def gc_type(idl_type): |
270 if idl_type.is_garbage_collected: | 312 if idl_type.is_garbage_collected: |
271 return 'GarbageCollectedObject' | 313 return 'GarbageCollectedObject' |
272 if idl_type.is_will_be_garbage_collected: | 314 if idl_type.is_will_be_garbage_collected: |
273 return 'WillBeGarbageCollectedObject' | 315 return 'WillBeGarbageCollectedObject' |
274 return 'RefCountedObject' | 316 return 'RefCountedObject' |
275 | 317 |
276 IdlType.gc_type = property(gc_type) | 318 IdlTypeBase.gc_type = property(gc_type) |
277 | 319 |
278 | 320 |
279 ################################################################################ | 321 ################################################################################ |
280 # Includes | 322 # Includes |
281 ################################################################################ | 323 ################################################################################ |
282 | 324 |
283 def includes_for_cpp_class(class_name, relative_dir_posix): | 325 def includes_for_cpp_class(class_name, relative_dir_posix): |
284 return set([posixpath.join('bindings', relative_dir_posix, class_name + '.h'
)]) | 326 return set([posixpath.join('bindings', relative_dir_posix, class_name + '.h'
)]) |
285 | 327 |
286 | 328 |
287 INCLUDES_FOR_TYPE = { | 329 INCLUDES_FOR_TYPE = { |
288 'object': set(), | 330 'object': set(), |
289 'CompareHow': set(), | 331 'CompareHow': set(), |
290 'Dictionary': set(['bindings/common/Dictionary.h']), | 332 'Dictionary': set(['bindings/core/v8/Dictionary.h']), |
291 'EventHandler': set(['bindings/v8/V8AbstractEventListener.h', | 333 'EventHandler': set(['bindings/core/v8/V8AbstractEventListener.h', |
292 'bindings/v8/V8EventListenerList.h']), | 334 'bindings/core/v8/V8EventListenerList.h']), |
293 'EventListener': set(['bindings/common/BindingSecurity.h', | 335 'EventListener': set(['bindings/common/BindingSecurity.h', |
294 'bindings/v8/V8EventListenerList.h', | 336 'bindings/core/v8/V8EventListenerList.h', |
295 'core/frame/LocalDOMWindow.h']), | 337 'core/frame/LocalDOMWindow.h']), |
296 'HTMLCollection': set(['bindings/core/v8/V8HTMLCollection.h', | 338 'HTMLCollection': set(['bindings/core/v8/V8HTMLCollection.h', |
297 'core/dom/ClassCollection.h', | 339 'core/dom/ClassCollection.h', |
298 'core/dom/TagCollection.h', | 340 'core/dom/TagCollection.h', |
299 'core/html/HTMLCollection.h', | 341 'core/html/HTMLCollection.h', |
300 'core/html/HTMLFormControlsCollection.h', | 342 'core/html/HTMLFormControlsCollection.h', |
301 'core/html/HTMLTableRowsCollection.h']), | 343 'core/html/HTMLTableRowsCollection.h']), |
302 'MediaQueryListListener': set(['core/css/MediaQueryListListener.h']), | 344 'MediaQueryListListener': set(['core/css/MediaQueryListListener.h']), |
303 'NodeList': set(['bindings/core/v8/V8NodeList.h', | 345 'NodeList': set(['bindings/core/v8/V8NodeList.h', |
304 'core/dom/NameNodeList.h', | 346 'core/dom/NameNodeList.h', |
305 'core/dom/NodeList.h', | 347 'core/dom/NodeList.h', |
306 'core/dom/StaticNodeList.h', | 348 'core/dom/StaticNodeList.h', |
307 'core/html/LabelsNodeList.h']), | 349 'core/html/LabelsNodeList.h']), |
308 'Promise': set(['bindings/v8/ScriptPromise.h']), | 350 'Promise': set(['bindings/core/v8/V8ScriptPromise.h']), |
309 'SerializedScriptValue': set(['bindings/v8/SerializedScriptValue.h']), | 351 'SerializedScriptValue': set(['bindings/core/v8/SerializedScriptValue.h']), |
310 'ScriptValue': set(['bindings/common/ScriptValue.h']), | 352 'ScriptValue': set(['bindings/common/ScriptValue.h']), |
311 } | 353 } |
312 | 354 |
313 | 355 |
314 def includes_for_type(idl_type): | 356 def includes_for_type(idl_type): |
315 idl_type = idl_type.preprocessed_type | 357 idl_type = idl_type.preprocessed_type |
316 | 358 |
317 # Composite types | |
318 array_or_sequence_type = idl_type.array_or_sequence_type | |
319 if array_or_sequence_type: | |
320 return includes_for_type(array_or_sequence_type) | |
321 | |
322 # Simple types | 359 # Simple types |
323 base_idl_type = idl_type.base_type | 360 base_idl_type = idl_type.base_type |
324 if base_idl_type in INCLUDES_FOR_TYPE: | 361 if base_idl_type in INCLUDES_FOR_TYPE: |
325 return INCLUDES_FOR_TYPE[base_idl_type] | 362 return INCLUDES_FOR_TYPE[base_idl_type] |
326 if idl_type.is_basic_type: | 363 if idl_type.is_basic_type: |
327 return set() | 364 return set() |
328 if idl_type.is_typed_array_type: | 365 if idl_type.is_typed_array_element_type: |
329 return set(['bindings/v8/custom/V8%sCustom.h' % base_idl_type]) | 366 return set(['bindings/core/v8/custom/V8%sCustom.h' % base_idl_type]) |
330 if base_idl_type.endswith('ConstructorConstructor'): | 367 if base_idl_type.endswith('ConstructorConstructor'): |
331 # FIXME: rename to NamedConstructor | 368 # FIXME: rename to NamedConstructor |
332 # FIXME: replace with a [NamedConstructorAttribute] extended attribute | 369 # FIXME: replace with a [NamedConstructorAttribute] extended attribute |
333 # Ending with 'ConstructorConstructor' indicates a named constructor, | 370 # Ending with 'ConstructorConstructor' indicates a named constructor, |
334 # and these do not have header files, as they are part of the generated | 371 # and these do not have header files, as they are part of the generated |
335 # bindings for the interface | 372 # bindings for the interface |
336 return set() | 373 return set() |
337 if base_idl_type.endswith('Constructor'): | 374 if base_idl_type.endswith('Constructor'): |
338 # FIXME: replace with a [ConstructorAttribute] extended attribute | 375 # FIXME: replace with a [ConstructorAttribute] extended attribute |
339 base_idl_type = idl_type.constructor_type_name | 376 base_idl_type = idl_type.constructor_type_name |
| 377 if base_idl_type not in component_dir: |
| 378 return set() |
340 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], |
341 base_idl_type)]) | 380 base_idl_type)]) |
342 | 381 |
343 IdlType.includes_for_type = property(includes_for_type) | 382 IdlType.includes_for_type = property(includes_for_type) |
344 IdlUnionType.includes_for_type = property( | 383 IdlUnionType.includes_for_type = property( |
345 lambda self: set.union(*[includes_for_type(member_type) | 384 lambda self: set.union(*[includes_for_type(member_type) |
346 for member_type in self.member_types])) | 385 for member_type in self.member_types])) |
| 386 IdlArrayOrSequenceType.includes_for_type = property( |
| 387 lambda self: self.element_type.includes_for_type) |
347 | 388 |
348 | 389 |
349 def add_includes_for_type(idl_type): | 390 def add_includes_for_type(idl_type): |
350 includes.update(idl_type.includes_for_type) | 391 includes.update(idl_type.includes_for_type) |
351 | 392 |
352 IdlType.add_includes_for_type = add_includes_for_type | 393 IdlTypeBase.add_includes_for_type = add_includes_for_type |
353 IdlUnionType.add_includes_for_type = add_includes_for_type | |
354 | 394 |
355 | 395 |
356 def includes_for_interface(interface_name): | 396 def includes_for_interface(interface_name): |
357 return IdlType(interface_name).includes_for_type | 397 return IdlType(interface_name).includes_for_type |
358 | 398 |
359 | 399 |
360 def add_includes_for_interface(interface_name): | 400 def add_includes_for_interface(interface_name): |
361 includes.update(includes_for_interface(interface_name)) | 401 includes.update(includes_for_interface(interface_name)) |
362 | 402 |
| 403 |
| 404 def impl_should_use_nullable_container(idl_type): |
| 405 return not(idl_type.cpp_type_has_null_value) |
| 406 |
| 407 IdlTypeBase.impl_should_use_nullable_container = property( |
| 408 impl_should_use_nullable_container) |
| 409 |
| 410 |
| 411 def impl_includes_for_type(idl_type, interfaces_info): |
| 412 includes_for_type = set() |
| 413 if idl_type.impl_should_use_nullable_container: |
| 414 includes_for_type.add('bindings/common/Nullable.h') |
| 415 |
| 416 idl_type = idl_type.preprocessed_type |
| 417 native_array_element_type = idl_type.native_array_element_type |
| 418 if native_array_element_type: |
| 419 includes_for_type.update(impl_includes_for_type( |
| 420 native_array_element_type, interfaces_info)) |
| 421 includes_for_type.add('wtf/Vector.h') |
| 422 |
| 423 if idl_type.is_string_type: |
| 424 includes_for_type.add('wtf/text/WTFString.h') |
| 425 if idl_type.name in interfaces_info: |
| 426 interface_info = interfaces_info[idl_type.name] |
| 427 includes_for_type.add(interface_info['include_path']) |
| 428 return includes_for_type |
| 429 |
| 430 IdlTypeBase.impl_includes_for_type = impl_includes_for_type |
| 431 |
| 432 |
363 component_dir = {} | 433 component_dir = {} |
364 | 434 |
365 | 435 |
366 def set_component_dirs(new_component_dirs): | 436 def set_component_dirs(new_component_dirs): |
367 component_dir.update(new_component_dirs) | 437 component_dir.update(new_component_dirs) |
368 | 438 |
369 | 439 |
370 ################################################################################ | 440 ################################################################################ |
371 # V8 -> C++ | 441 # V8 -> C++ |
372 ################################################################################ | 442 ################################################################################ |
373 | 443 |
374 V8_VALUE_TO_CPP_VALUE = { | 444 V8_VALUE_TO_CPP_VALUE = { |
375 # Basic | 445 # Basic |
376 'Date': 'toCoreDate({v8_value})', | 446 'Date': 'toCoreDate({v8_value})', |
377 'DOMString': '{v8_value}', | 447 'DOMString': '{v8_value}', |
378 'ByteString': 'toByteString({arguments})', | 448 'ByteString': 'toByteString({arguments})', |
379 'ScalarValueString': 'toScalarValueString({arguments})', | 449 'ScalarValueString': 'toScalarValueString({arguments})', |
380 'boolean': '{v8_value}->BooleanValue()', | 450 'boolean': '{v8_value}->BooleanValue()', |
381 'float': 'static_cast<float>({v8_value}->NumberValue())', | 451 'float': 'static_cast<float>({v8_value}->NumberValue())', |
382 'unrestricted float': 'static_cast<float>({v8_value}->NumberValue())', | 452 'unrestricted float': 'static_cast<float>({v8_value}->NumberValue())', |
383 'double': 'static_cast<double>({v8_value}->NumberValue())', | 453 'double': 'static_cast<double>({v8_value}->NumberValue())', |
384 'unrestricted double': 'static_cast<double>({v8_value}->NumberValue())', | 454 'unrestricted double': 'static_cast<double>({v8_value}->NumberValue())', |
385 'byte': 'toInt8({arguments})', | 455 'byte': 'toInt8({arguments})', |
386 'octet': 'toUInt8({arguments})', | 456 'octet': 'toUInt8({arguments})', |
387 'short': 'toInt16({arguments})', | 457 'short': 'toInt16({arguments})', |
388 'unsigned short': 'toUInt16({arguments})', | 458 'unsigned short': 'toUInt16({arguments})', |
389 'long': 'toInt32({arguments})', | 459 'long': 'toInt32({arguments})', |
390 'unsigned long': 'toUInt32({arguments})', | 460 'unsigned long': 'toUInt32({arguments})', |
391 'long long': 'toInt64({arguments})', | 461 'long long': 'toInt64({arguments})', |
392 'unsigned long long': 'toUInt64({arguments})', | 462 'unsigned long long': 'toUInt64({arguments})', |
393 # Interface types | 463 # Interface types |
394 'CompareHow': 'static_cast<Range::CompareHow>({v8_value}->Int32Value())', | 464 'CompareHow': 'static_cast<Range::CompareHow>({v8_value}->Int32Value())', |
395 'Dictionary': 'Dictionary({v8_value}, info.GetIsolate())', | 465 'Dictionary': 'Dictionary({v8_value}, {isolate})', |
396 'EventTarget': 'V8DOMWrapper::isDOMWrapper({v8_value}) ? toWrapperTypeInfo(v
8::Handle<v8::Object>::Cast({v8_value}))->toEventTarget(v8::Handle<v8::Object>::
Cast({v8_value})) : 0', | 466 'EventTarget': 'V8DOMWrapper::isDOMWrapper({v8_value}) ? toWrapperTypeInfo(v
8::Handle<v8::Object>::Cast({v8_value}))->toEventTarget(v8::Handle<v8::Object>::
Cast({v8_value})) : 0', |
397 'MediaQueryListListener': 'MediaQueryListListener::create(V8ScriptState::cur
rent(info.GetIsolate()), ScriptValue(V8ScriptState::current(info.GetIsolate()),
{v8_value}))', | 467 'MediaQueryListListener': 'MediaQueryListListener::create(V8ScriptState::cur
rent({isolate}), ScriptValue(V8ScriptState::current({isolate}), {v8_value}))', |
398 'NodeFilter': 'toNodeFilter({v8_value}, info.Holder(), V8ScriptState::curren
t(info.GetIsolate()))', | 468 'NodeFilter': 'toNodeFilter({v8_value}, info.Holder(), V8ScriptState::curren
t({isolate}))', |
399 'Promise': 'ScriptPromise::cast(V8ScriptState::current(info.GetIsolate()), {
v8_value})', | 469 'Promise': 'V8ScriptPromise::cast(V8ScriptState::current({isolate}), {v8_val
ue})', |
400 'SerializedScriptValue': 'SerializedScriptValue::create({v8_value}, info.Get
Isolate())', | 470 'SerializedScriptValue': 'SerializedScriptValue::create({v8_value}, {isolate
})', |
401 'ScriptValue': 'ScriptValue(V8ScriptState::current(info.GetIsolate()), {v8_v
alue})', | 471 'ScriptValue': 'ScriptValue(V8ScriptState::current({isolate}), {v8_value})', |
402 'Window': 'toDOMWindow({v8_value}, info.GetIsolate())', | 472 'Window': 'toDOMWindow({v8_value}, {isolate})', |
403 'XPathNSResolver': 'toXPathNSResolver({v8_value}, info.GetIsolate())', | 473 'XPathNSResolver': 'toXPathNSResolver({v8_value}, {isolate})', |
404 } | 474 } |
405 | 475 |
406 | 476 |
407 def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index): | 477 def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index, isolat
e): |
408 # Composite types | 478 if idl_type.name == 'void': |
409 array_or_sequence_type = idl_type.array_or_sequence_type | 479 return '' |
410 if array_or_sequence_type: | 480 |
411 return v8_value_to_cpp_value_array_or_sequence(array_or_sequence_type, v
8_value, index) | 481 # Array or sequence types |
| 482 native_array_element_type = idl_type.native_array_element_type |
| 483 if native_array_element_type: |
| 484 return v8_value_to_cpp_value_array_or_sequence(native_array_element_type
, v8_value, index) |
412 | 485 |
413 # Simple types | 486 # Simple types |
414 idl_type = idl_type.preprocessed_type | 487 idl_type = idl_type.preprocessed_type |
415 add_includes_for_type(idl_type) | 488 add_includes_for_type(idl_type) |
416 base_idl_type = idl_type.base_type | 489 base_idl_type = idl_type.base_type |
417 | 490 |
418 if 'EnforceRange' in extended_attributes: | 491 if 'EnforceRange' in extended_attributes: |
419 arguments = ', '.join([v8_value, 'EnforceRange', 'exceptionState']) | 492 arguments = ', '.join([v8_value, 'EnforceRange', 'exceptionState']) |
420 elif (idl_type.is_integer_type or # NormalConversion | 493 elif idl_type.may_raise_exception_on_conversion: |
421 idl_type.name in ('ByteString', 'ScalarValueString')): | |
422 arguments = ', '.join([v8_value, 'exceptionState']) | 494 arguments = ', '.join([v8_value, 'exceptionState']) |
423 else: | 495 else: |
424 arguments = v8_value | 496 arguments = v8_value |
425 | 497 |
426 if base_idl_type in V8_VALUE_TO_CPP_VALUE: | 498 if base_idl_type in V8_VALUE_TO_CPP_VALUE: |
427 cpp_expression_format = V8_VALUE_TO_CPP_VALUE[base_idl_type] | 499 cpp_expression_format = V8_VALUE_TO_CPP_VALUE[base_idl_type] |
428 elif idl_type.is_typed_array_type: | 500 elif idl_type.is_typed_array_element_type: |
429 cpp_expression_format = ( | 501 cpp_expression_format = ( |
430 '{v8_value}->Is{idl_type}() ? ' | 502 '{v8_value}->Is{idl_type}() ? ' |
431 'V8{idl_type}::toNative(v8::Handle<v8::{idl_type}>::Cast({v8_value})
) : 0') | 503 'V8{idl_type}::toNative(v8::Handle<v8::{idl_type}>::Cast({v8_value})
) : 0') |
| 504 elif idl_type.is_dictionary: |
| 505 cpp_expression_format = 'V8{idl_type}::toNative({isolate}, {v8_value})' |
432 else: | 506 else: |
433 cpp_expression_format = ( | 507 cpp_expression_format = ( |
434 'V8{idl_type}::toNativeWithTypeCheck(info.GetIsolate(), {v8_value})'
) | 508 'V8{idl_type}::toNativeWithTypeCheck({isolate}, {v8_value})') |
435 | 509 |
436 return cpp_expression_format.format(arguments=arguments, idl_type=base_idl_t
ype, v8_value=v8_value) | 510 return cpp_expression_format.format(arguments=arguments, idl_type=base_idl_t
ype, v8_value=v8_value, isolate=isolate) |
437 | 511 |
438 | 512 |
439 def v8_value_to_cpp_value_array_or_sequence(array_or_sequence_type, v8_value, in
dex): | 513 def v8_value_to_cpp_value_array_or_sequence(native_array_element_type, v8_value,
index, isolate='info.GetIsolate()'): |
440 # Index is None for setters, index (starting at 0) for method arguments, | 514 # Index is None for setters, index (starting at 0) for method arguments, |
441 # and is used to provide a human-readable exception message | 515 # and is used to provide a human-readable exception message |
442 if index is None: | 516 if index is None: |
443 index = 0 # special case, meaning "setter" | 517 index = 0 # special case, meaning "setter" |
444 else: | 518 else: |
445 index += 1 # human-readable index | 519 index += 1 # human-readable index |
446 if (array_or_sequence_type.is_interface_type and | 520 if (native_array_element_type.is_interface_type and |
447 array_or_sequence_type.name != 'Dictionary'): | 521 native_array_element_type.name != 'Dictionary'): |
448 this_cpp_type = None | 522 this_cpp_type = None |
449 ref_ptr_type = cpp_ptr_type('RefPtr', 'Member', array_or_sequence_type.g
c_type) | 523 ref_ptr_type = cpp_ptr_type('RefPtr', 'Member', native_array_element_typ
e.gc_type) |
450 expression_format = '(to{ref_ptr_type}NativeArray<{array_or_sequence_typ
e}, V8{array_or_sequence_type}>({v8_value}, {index}, info.GetIsolate()))' | 524 expression_format = '(to{ref_ptr_type}NativeArray<{native_array_element_
type}, V8{native_array_element_type}>({v8_value}, {index}, {isolate}))' |
451 add_includes_for_type(array_or_sequence_type) | 525 add_includes_for_type(native_array_element_type) |
452 else: | 526 else: |
453 ref_ptr_type = None | 527 ref_ptr_type = None |
454 this_cpp_type = array_or_sequence_type.cpp_type | 528 this_cpp_type = native_array_element_type.cpp_type |
455 expression_format = 'toNativeArray<{cpp_type}>({v8_value}, {index}, info
.GetIsolate())' | 529 expression_format = 'toNativeArray<{cpp_type}>({v8_value}, {index}, {iso
late})' |
456 expression = expression_format.format(array_or_sequence_type=array_or_sequen
ce_type.name, cpp_type=this_cpp_type, index=index, ref_ptr_type=ref_ptr_type, v8
_value=v8_value) | 530 expression = expression_format.format(native_array_element_type=native_array
_element_type.name, cpp_type=this_cpp_type, index=index, ref_ptr_type=ref_ptr_ty
pe, v8_value=v8_value, isolate=isolate) |
457 return expression | 531 return expression |
458 | 532 |
459 | 533 |
460 def v8_value_to_local_cpp_value(idl_type, extended_attributes, v8_value, variabl
e_name, index=None, declare_variable=True): | 534 def v8_value_to_local_cpp_value(idl_type, extended_attributes, v8_value, variabl
e_name, index=None, declare_variable=True, isolate='info.GetIsolate()', used_in_
private_script=False, return_promise=False): |
461 """Returns an expression that converts a V8 value to a C++ value and stores
it as a local value.""" | 535 """Returns an expression that converts a V8 value to a C++ value and stores
it as a local value.""" |
462 this_cpp_type = idl_type.cpp_type_args(extended_attributes=extended_attribut
es, used_as_argument=True) | 536 |
| 537 # FIXME: Support union type. |
| 538 if idl_type.is_union_type: |
| 539 return '' |
| 540 |
| 541 this_cpp_type = idl_type.cpp_type_args(extended_attributes=extended_attribut
es, raw_type=True) |
463 | 542 |
464 idl_type = idl_type.preprocessed_type | 543 idl_type = idl_type.preprocessed_type |
465 cpp_value = v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, i
ndex) | 544 cpp_value = v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, i
ndex, isolate) |
466 args = [variable_name, cpp_value] | 545 args = [variable_name, cpp_value] |
467 if idl_type.base_type == 'DOMString' and not idl_type.array_or_sequence_type
: | 546 if idl_type.base_type == 'DOMString': |
468 macro = 'TOSTRING_VOID' | 547 macro = 'TOSTRING_DEFAULT' if used_in_private_script else 'TOSTRING_VOID
' |
469 elif (idl_type.is_integer_type or | 548 elif idl_type.may_raise_exception_on_conversion: |
470 idl_type.name in ('ByteString', 'ScalarValueString')): | 549 macro = 'TONATIVE_DEFAULT_EXCEPTIONSTATE' if used_in_private_script else
'TONATIVE_VOID_EXCEPTIONSTATE' |
471 macro = 'TONATIVE_VOID_EXCEPTIONSTATE' | |
472 args.append('exceptionState') | 550 args.append('exceptionState') |
473 else: | 551 else: |
474 macro = 'TONATIVE_VOID' | 552 macro = 'TONATIVE_DEFAULT' if used_in_private_script else 'TONATIVE_VOID
' |
| 553 |
| 554 if used_in_private_script: |
| 555 args.append('false') |
475 | 556 |
476 # Macros come in several variants, to minimize expensive creation of | 557 # Macros come in several variants, to minimize expensive creation of |
477 # v8::TryCatch. | 558 # v8::TryCatch. |
478 suffix = '' | 559 suffix = '' |
479 | 560 |
| 561 if return_promise: |
| 562 suffix += '_PROMISE' |
| 563 args.append('info') |
| 564 if macro == 'TONATIVE_VOID_EXCEPTIONSTATE': |
| 565 args.append('V8ScriptState::current(%s)' % isolate) |
| 566 |
480 if declare_variable: | 567 if declare_variable: |
481 args.insert(0, this_cpp_type) | 568 args.insert(0, this_cpp_type) |
482 else: | 569 else: |
483 suffix += '_INTERNAL' | 570 suffix += '_INTERNAL' |
484 | 571 |
485 return '%s(%s)' % (macro + suffix, ', '.join(args)) | 572 return '%s(%s)' % (macro + suffix, ', '.join(args)) |
486 | 573 |
487 | 574 IdlTypeBase.v8_value_to_local_cpp_value = v8_value_to_local_cpp_value |
488 IdlType.v8_value_to_local_cpp_value = v8_value_to_local_cpp_value | |
489 IdlUnionType.v8_value_to_local_cpp_value = v8_value_to_local_cpp_value | |
490 | 575 |
491 | 576 |
492 ################################################################################ | 577 ################################################################################ |
493 # C++ -> V8 | 578 # C++ -> V8 |
494 ################################################################################ | 579 ################################################################################ |
495 | 580 |
496 def preprocess_idl_type(idl_type): | 581 def preprocess_idl_type(idl_type): |
497 if idl_type.is_enum: | 582 if idl_type.is_enum: |
498 # Enumerations are internally DOMStrings | 583 # Enumerations are internally DOMStrings |
499 return IdlType('DOMString') | 584 return IdlType('DOMString') |
500 if (idl_type.name == 'Any' or idl_type.is_callback_function): | 585 if (idl_type.name == 'Any' or idl_type.is_callback_function): |
501 return IdlType('ScriptValue') | 586 return IdlType('ScriptValue') |
502 return idl_type | 587 return idl_type |
503 | 588 |
504 IdlType.preprocessed_type = property(preprocess_idl_type) | 589 IdlTypeBase.preprocessed_type = property(preprocess_idl_type) |
505 IdlUnionType.preprocessed_type = property(preprocess_idl_type) | |
506 | 590 |
507 | 591 |
508 def preprocess_idl_type_and_value(idl_type, cpp_value, extended_attributes): | 592 def preprocess_idl_type_and_value(idl_type, cpp_value, extended_attributes): |
509 """Returns IDL type and value, with preliminary type conversions applied.""" | 593 """Returns IDL type and value, with preliminary type conversions applied.""" |
510 idl_type = idl_type.preprocessed_type | 594 idl_type = idl_type.preprocessed_type |
511 if idl_type.name == 'Promise': | 595 if idl_type.name == 'Promise': |
512 idl_type = IdlType('ScriptValue') | 596 idl_type = IdlType('ScriptValue') |
513 if idl_type.base_type in ['long long', 'unsigned long long']: | 597 if idl_type.base_type in ['long long', 'unsigned long long']: |
514 # long long and unsigned long long are not representable in ECMAScript; | 598 # long long and unsigned long long are not representable in ECMAScript; |
515 # we represent them as doubles. | 599 # we represent them as doubles. |
516 idl_type = IdlType('double', is_nullable=idl_type.is_nullable) | 600 idl_type = IdlType('double', is_nullable=idl_type.is_nullable) |
517 cpp_value = 'static_cast<double>(%s)' % cpp_value | 601 cpp_value = 'static_cast<double>(%s)' % cpp_value |
518 # HTML5 says that unsigned reflected attributes should be in the range | 602 # HTML5 says that unsigned reflected attributes should be in the range |
519 # [0, 2^31). When a value isn't in this range, a default value (or 0) | 603 # [0, 2^31). When a value isn't in this range, a default value (or 0) |
520 # should be returned instead. | 604 # should be returned instead. |
521 extended_attributes = extended_attributes or {} | 605 extended_attributes = extended_attributes or {} |
522 if ('Reflect' in extended_attributes and | 606 if ('Reflect' in extended_attributes and |
523 idl_type.base_type in ['unsigned long', 'unsigned short']): | 607 idl_type.base_type in ['unsigned long', 'unsigned short']): |
524 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute', | 608 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute', |
525 'getIntegralAttribute') | 609 'getIntegralAttribute') |
526 cpp_value = 'std::max(0, %s)' % cpp_value | 610 cpp_value = 'std::max(0, static_cast<int>(%s))' % cpp_value |
527 return idl_type, cpp_value | 611 return idl_type, cpp_value |
528 | 612 |
529 | 613 |
530 def v8_conversion_type(idl_type, extended_attributes): | 614 def v8_conversion_type(idl_type, extended_attributes): |
531 """Returns V8 conversion type, adding any additional includes. | 615 """Returns V8 conversion type, adding any additional includes. |
532 | 616 |
533 The V8 conversion type is used to select the C++ -> V8 conversion function | 617 The V8 conversion type is used to select the C++ -> V8 conversion function |
534 or v8SetReturnValue* function; it can be an idl_type, a cpp_type, or a | 618 or v8SetReturnValue* function; it can be an idl_type, a cpp_type, or a |
535 separate name for the type of conversion (e.g., 'DOMWrapper'). | 619 separate name for the type of conversion (e.g., 'DOMWrapper'). |
536 """ | 620 """ |
537 extended_attributes = extended_attributes or {} | 621 extended_attributes = extended_attributes or {} |
538 | 622 |
539 # Composite types | 623 # FIXME: Support union type. |
540 array_or_sequence_type = idl_type.array_or_sequence_type | 624 if idl_type.is_union_type: |
541 if array_or_sequence_type: | 625 return '' |
542 if array_or_sequence_type.is_interface_type: | 626 |
543 add_includes_for_type(array_or_sequence_type) | 627 # Array or sequence types |
| 628 native_array_element_type = idl_type.native_array_element_type |
| 629 if native_array_element_type: |
| 630 if native_array_element_type.is_interface_type: |
| 631 add_includes_for_type(native_array_element_type) |
544 return 'array' | 632 return 'array' |
545 | 633 |
546 # Simple types | 634 # Simple types |
547 base_idl_type = idl_type.base_type | 635 base_idl_type = idl_type.base_type |
548 # Basic types, without additional includes | 636 # Basic types, without additional includes |
549 if base_idl_type in CPP_INT_TYPES: | 637 if base_idl_type in CPP_INT_TYPES: |
550 return 'int' | 638 return 'int' |
551 if base_idl_type in CPP_UNSIGNED_TYPES: | 639 if base_idl_type in CPP_UNSIGNED_TYPES: |
552 return 'unsigned' | 640 return 'unsigned' |
553 if base_idl_type in ('DOMString', 'ByteString', 'ScalarValueString'): | 641 if idl_type.is_string_type: |
| 642 if idl_type.is_nullable: |
| 643 return 'StringOrNull' |
554 if 'TreatReturnedNullStringAs' not in extended_attributes: | 644 if 'TreatReturnedNullStringAs' not in extended_attributes: |
555 return base_idl_type | 645 return base_idl_type |
556 treat_returned_null_string_as = extended_attributes['TreatReturnedNullSt
ringAs'] | 646 treat_returned_null_string_as = extended_attributes['TreatReturnedNullSt
ringAs'] |
557 if treat_returned_null_string_as == 'Null': | 647 if treat_returned_null_string_as == 'Null': |
558 return 'StringOrNull' | 648 return 'StringOrNull' |
559 if treat_returned_null_string_as == 'Undefined': | 649 if treat_returned_null_string_as == 'Undefined': |
560 return 'StringOrUndefined' | 650 return 'StringOrUndefined' |
561 raise 'Unrecognized TreatReturnNullStringAs value: "%s"' % treat_returne
d_null_string_as | 651 raise 'Unrecognized TreatReturnedNullStringAs value: "%s"' % treat_retur
ned_null_string_as |
562 if idl_type.is_basic_type or base_idl_type == 'ScriptValue': | 652 if idl_type.is_basic_type or base_idl_type == 'ScriptValue': |
563 return base_idl_type | 653 return base_idl_type |
564 | 654 |
565 # Data type with potential additional includes | 655 # Data type with potential additional includes |
566 add_includes_for_type(idl_type) | 656 add_includes_for_type(idl_type) |
567 if base_idl_type in V8_SET_RETURN_VALUE: # Special v8SetReturnValue treatme
nt | 657 if base_idl_type in V8_SET_RETURN_VALUE: # Special v8SetReturnValue treatme
nt |
568 return base_idl_type | 658 return base_idl_type |
569 | 659 |
570 # Pointer type | 660 # Pointer type |
571 return 'DOMWrapper' | 661 return 'DOMWrapper' |
572 | 662 |
573 IdlType.v8_conversion_type = v8_conversion_type | 663 IdlTypeBase.v8_conversion_type = v8_conversion_type |
574 | 664 |
575 | 665 |
576 V8_SET_RETURN_VALUE = { | 666 V8_SET_RETURN_VALUE = { |
577 'boolean': 'v8SetReturnValueBool(info, {cpp_value})', | 667 'boolean': 'v8SetReturnValueBool(info, {cpp_value})', |
578 'int': 'v8SetReturnValueInt(info, {cpp_value})', | 668 'int': 'v8SetReturnValueInt(info, {cpp_value})', |
579 'unsigned': 'v8SetReturnValueUnsigned(info, {cpp_value})', | 669 'unsigned': 'v8SetReturnValueUnsigned(info, {cpp_value})', |
580 'DOMString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsolate())', | 670 'DOMString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsolate())', |
581 'ByteString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsolate())'
, | 671 'ByteString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsolate())'
, |
582 'ScalarValueString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsol
ate())', | 672 'ScalarValueString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsol
ate())', |
583 # [TreatNullReturnValueAs] | 673 # [TreatReturnedNullStringAs] |
584 'StringOrNull': 'v8SetReturnValueStringOrNull(info, {cpp_value}, info.GetIso
late())', | 674 'StringOrNull': 'v8SetReturnValueStringOrNull(info, {cpp_value}, info.GetIso
late())', |
585 'StringOrUndefined': 'v8SetReturnValueStringOrUndefined(info, {cpp_value}, i
nfo.GetIsolate())', | 675 'StringOrUndefined': 'v8SetReturnValueStringOrUndefined(info, {cpp_value}, i
nfo.GetIsolate())', |
586 'void': '', | 676 'void': '', |
587 # No special v8SetReturnValue* function (set value directly) | 677 # No special v8SetReturnValue* function (set value directly) |
588 'float': 'v8SetReturnValue(info, {cpp_value})', | 678 'float': 'v8SetReturnValue(info, {cpp_value})', |
589 'unrestricted float': 'v8SetReturnValue(info, {cpp_value})', | 679 'unrestricted float': 'v8SetReturnValue(info, {cpp_value})', |
590 'double': 'v8SetReturnValue(info, {cpp_value})', | 680 'double': 'v8SetReturnValue(info, {cpp_value})', |
591 'unrestricted double': 'v8SetReturnValue(info, {cpp_value})', | 681 'unrestricted double': 'v8SetReturnValue(info, {cpp_value})', |
592 # No special v8SetReturnValue* function, but instead convert value to V8 | 682 # No special v8SetReturnValue* function, but instead convert value to V8 |
593 # and then use general v8SetReturnValue. | 683 # and then use general v8SetReturnValue. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 | 715 |
626 format_string = V8_SET_RETURN_VALUE[this_v8_conversion_type] | 716 format_string = V8_SET_RETURN_VALUE[this_v8_conversion_type] |
627 # FIXME: oilpan: Remove .release() once we remove all RefPtrs from generated
code. | 717 # FIXME: oilpan: Remove .release() once we remove all RefPtrs from generated
code. |
628 if release: | 718 if release: |
629 cpp_value = '%s.release()' % cpp_value | 719 cpp_value = '%s.release()' % cpp_value |
630 statement = format_string.format(cpp_value=cpp_value, script_wrappable=scrip
t_wrappable) | 720 statement = format_string.format(cpp_value=cpp_value, script_wrappable=scrip
t_wrappable) |
631 return statement | 721 return statement |
632 | 722 |
633 | 723 |
634 def v8_set_return_value_union(idl_type, cpp_value, extended_attributes=None, scr
ipt_wrappable='', release=False, for_main_world=False): | 724 def v8_set_return_value_union(idl_type, cpp_value, extended_attributes=None, scr
ipt_wrappable='', release=False, for_main_world=False): |
635 """ | 725 # FIXME: Need to revisit the design of union support. |
636 release: can be either False (False for all member types) or | 726 # http://crbug.com/240176 |
637 a sequence (list or tuple) of booleans (if specified individually). | 727 return None |
638 """ | |
639 | 728 |
640 return [ | |
641 member_type.v8_set_return_value(cpp_value + str(i), | |
642 extended_attributes, | |
643 script_wrappable, | |
644 release and release[i], | |
645 for_main_world) | |
646 for i, member_type in | |
647 enumerate(idl_type.member_types)] | |
648 | 729 |
649 IdlType.v8_set_return_value = v8_set_return_value | 730 IdlTypeBase.v8_set_return_value = v8_set_return_value |
650 IdlUnionType.v8_set_return_value = v8_set_return_value_union | 731 IdlUnionType.v8_set_return_value = v8_set_return_value_union |
651 | 732 |
652 IdlType.release = property(lambda self: self.is_interface_type) | 733 IdlType.release = property(lambda self: self.is_interface_type) |
653 IdlUnionType.release = property( | 734 IdlUnionType.release = property( |
654 lambda self: [member_type.is_interface_type | 735 lambda self: [member_type.is_interface_type |
655 for member_type in self.member_types]) | 736 for member_type in self.member_types]) |
| 737 IdlArrayOrSequenceType.release = False |
656 | 738 |
657 | 739 |
658 CPP_VALUE_TO_V8_VALUE = { | 740 CPP_VALUE_TO_V8_VALUE = { |
659 # Built-in types | 741 # Built-in types |
660 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', | 742 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', |
661 'DOMString': 'v8String({isolate}, {cpp_value})', | 743 'DOMString': 'v8String({isolate}, {cpp_value})', |
662 'ByteString': 'v8String({isolate}, {cpp_value})', | 744 'ByteString': 'v8String({isolate}, {cpp_value})', |
663 'ScalarValueString': 'v8String({isolate}, {cpp_value})', | 745 'ScalarValueString': 'v8String({isolate}, {cpp_value})', |
664 'boolean': 'v8Boolean({cpp_value}, {isolate})', | 746 'boolean': 'v8Boolean({cpp_value}, {isolate})', |
665 'int': 'v8::Integer::New({isolate}, {cpp_value})', | 747 'int': 'v8::Integer::New({isolate}, {cpp_value})', |
666 'unsigned': 'v8::Integer::NewFromUnsigned({isolate}, {cpp_value})', | 748 'unsigned': 'v8::Integer::NewFromUnsigned({isolate}, {cpp_value})', |
667 'float': 'v8::Number::New({isolate}, {cpp_value})', | 749 'float': 'v8::Number::New({isolate}, {cpp_value})', |
668 'unrestricted float': 'v8::Number::New({isolate}, {cpp_value})', | 750 'unrestricted float': 'v8::Number::New({isolate}, {cpp_value})', |
669 'double': 'v8::Number::New({isolate}, {cpp_value})', | 751 'double': 'v8::Number::New({isolate}, {cpp_value})', |
670 'unrestricted double': 'v8::Number::New({isolate}, {cpp_value})', | 752 'unrestricted double': 'v8::Number::New({isolate}, {cpp_value})', |
671 'void': 'v8Undefined()', | 753 'void': 'v8Undefined()', |
| 754 # [TreatReturnedNullStringAs] |
| 755 'StringOrNull': '{cpp_value}.isNull() ? v8::Handle<v8::Value>(v8::Null({isol
ate})) : v8String({isolate}, {cpp_value})', |
| 756 'StringOrUndefined': '{cpp_value}.isNull() ? v8Undefined() : v8String({isola
te}, {cpp_value})', |
672 # Special cases | 757 # Special cases |
673 'EventHandler': '{cpp_value} ? v8::Handle<v8::Value>(V8AbstractEventListener
::cast({cpp_value})->getListenerObject(impl->executionContext())) : v8::Handle<v
8::Value>(v8::Null({isolate}))', | 758 'EventHandler': '{cpp_value} ? v8::Handle<v8::Value>(V8AbstractEventListener
::cast({cpp_value})->getListenerObject(impl->executionContext())) : v8::Handle<v
8::Value>(v8::Null({isolate}))', |
674 'ScriptValue': '{cpp_value}.v8Value()', | 759 'ScriptValue': '{cpp_value}.v8Value()', |
675 'SerializedScriptValue': '{cpp_value} ? {cpp_value}->deserialize() : v8::Han
dle<v8::Value>(v8::Null({isolate}))', | 760 'SerializedScriptValue': '{cpp_value} ? {cpp_value}->deserialize() : v8::Han
dle<v8::Value>(v8::Null({isolate}))', |
676 # General | 761 # General |
677 'array': 'v8Array({cpp_value}, {creation_context}, {isolate})', | 762 'array': 'v8Array({cpp_value}, {creation_context}, {isolate})', |
678 'DOMWrapper': 'toV8({cpp_value}, {creation_context}, {isolate})', | 763 'DOMWrapper': 'toV8({cpp_value}, {creation_context}, {isolate})', |
679 } | 764 } |
680 | 765 |
681 | 766 |
682 def cpp_value_to_v8_value(idl_type, cpp_value, isolate='info.GetIsolate()', crea
tion_context='info.Holder()', extended_attributes=None): | 767 def cpp_value_to_v8_value(idl_type, cpp_value, isolate='info.GetIsolate()', crea
tion_context='info.Holder()', extended_attributes=None): |
683 """Returns an expression that converts a C++ value to a V8 value.""" | 768 """Returns an expression that converts a C++ value to a V8 value.""" |
684 # the isolate parameter is needed for callback interfaces | 769 # the isolate parameter is needed for callback interfaces |
685 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext
ended_attributes) | 770 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext
ended_attributes) |
686 this_v8_conversion_type = idl_type.v8_conversion_type(extended_attributes) | 771 this_v8_conversion_type = idl_type.v8_conversion_type(extended_attributes) |
687 format_string = CPP_VALUE_TO_V8_VALUE[this_v8_conversion_type] | 772 format_string = CPP_VALUE_TO_V8_VALUE[this_v8_conversion_type] |
688 statement = format_string.format(cpp_value=cpp_value, isolate=isolate, creat
ion_context=creation_context) | 773 statement = format_string.format(cpp_value=cpp_value, isolate=isolate, creat
ion_context=creation_context) |
689 return statement | 774 return statement |
690 | 775 |
691 IdlType.cpp_value_to_v8_value = cpp_value_to_v8_value | 776 IdlTypeBase.cpp_value_to_v8_value = cpp_value_to_v8_value |
| 777 |
| 778 |
| 779 def literal_cpp_value(idl_type, idl_literal): |
| 780 """Converts an expression that is a valid C++ literal for this type.""" |
| 781 # FIXME: add validation that idl_type and idl_literal are compatible |
| 782 literal_value = str(idl_literal) |
| 783 if idl_type.base_type in CPP_UNSIGNED_TYPES: |
| 784 return literal_value + 'u' |
| 785 return literal_value |
| 786 |
| 787 IdlType.literal_cpp_value = literal_cpp_value |
| 788 |
| 789 |
| 790 ################################################################################ |
| 791 # Utility properties for nullable types |
| 792 ################################################################################ |
| 793 |
| 794 |
| 795 def cpp_type_has_null_value(idl_type): |
| 796 # - String types (String/AtomicString) represent null as a null string, |
| 797 # i.e. one for which String::isNull() returns true. |
| 798 # - Wrapper types (raw pointer or RefPtr/PassRefPtr) represent null as |
| 799 # a null pointer. |
| 800 return idl_type.is_string_type or idl_type.is_wrapper_type |
| 801 |
| 802 IdlTypeBase.cpp_type_has_null_value = property(cpp_type_has_null_value) |
| 803 |
| 804 |
| 805 def is_implicit_nullable(idl_type): |
| 806 # Nullable type where the corresponding C++ type supports a null value. |
| 807 return idl_type.is_nullable and idl_type.cpp_type_has_null_value |
| 808 |
| 809 |
| 810 def is_explicit_nullable(idl_type): |
| 811 # Nullable type that isn't implicit nullable (see above.) For such types, |
| 812 # we use Nullable<T> or similar explicit ways to represent a null value. |
| 813 return idl_type.is_nullable and not idl_type.is_implicit_nullable |
| 814 |
| 815 IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable) |
| 816 IdlUnionType.is_implicit_nullable = False |
| 817 IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable) |
OLD | NEW |