| 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 20 matching lines...) Expand all Loading... |
| 31 Extends IdlType and IdlUnionType with C++-specific properties, methods, and | 31 Extends IdlType and IdlUnionType with C++-specific properties, methods, and |
| 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 from idl_types import IdlTypeBase, IdlType, IdlUnionType, TYPE_NAMES, IdlArrayOr
SequenceType | 41 from idl_types import IdlTypeBase, IdlType, IdlUnionType, TYPE_NAMES, IdlArrayOr
SequenceType, IdlSequenceType |
| 42 | 42 |
| 43 import dart_attributes # for IdlType.constructor_type_name | 43 import dart_attributes |
| 44 from dart_utilities import DartUtilities | 44 from dart_utilities import DartUtilities |
| 45 from v8_globals import includes | 45 from v8_globals import includes |
| 46 | 46 |
| 47 | 47 |
| 48 ################################################################################ | 48 ################################################################################ |
| 49 # CPP -specific handling of IDL types for Dart:Blink | 49 # CPP -specific handling of IDL types for Dart:Blink |
| 50 ################################################################################ | 50 ################################################################################ |
| 51 | 51 |
| 52 NON_WRAPPER_TYPES = frozenset([ | 52 NON_WRAPPER_TYPES = frozenset([ |
| 53 'CompareHow', | |
| 54 'Dictionary', | 53 'Dictionary', |
| 55 'EventHandler', | 54 'EventHandler', |
| 56 'EventListener', | 55 'EventListener', |
| 57 'MediaQueryListListener', | |
| 58 'NodeFilter', | 56 'NodeFilter', |
| 59 'SerializedScriptValue', | 57 'SerializedScriptValue', |
| 60 ]) | 58 ]) |
| 61 TYPED_ARRAYS = { | 59 TYPED_ARRAYS = { |
| 62 # (cpp_type, dart_type), used by constructor templates | 60 # (cpp_type, dart_type), used by constructor templates |
| 63 'ArrayBuffer': (None, 'ByteBuffer'), | 61 'ArrayBuffer': (None, 'ByteBuffer'), |
| 64 'ArrayBufferView': (None, 'ByteData'), | 62 'ArrayBufferView': (None, 'ByteData'), |
| 65 'Float32Array': ('float', 'Float32List'), | 63 'Float32Array': ('float', 'Float32List'), |
| 66 'Float64Array': ('double', 'Float64List'), | 64 'Float64Array': ('double', 'Float64List'), |
| 67 'Int8Array': ('signed char', 'Int8List'), | 65 'Int8Array': ('signed char', 'Int8List'), |
| (...skipping 30 matching lines...) Expand all Loading... |
| 98 'long', | 96 'long', |
| 99 'short', | 97 'short', |
| 100 ]) | 98 ]) |
| 101 CPP_UNSIGNED_TYPES = set([ | 99 CPP_UNSIGNED_TYPES = set([ |
| 102 'octet', | 100 'octet', |
| 103 'unsigned int', | 101 'unsigned int', |
| 104 'unsigned long', | 102 'unsigned long', |
| 105 'unsigned short', | 103 'unsigned short', |
| 106 ]) | 104 ]) |
| 107 CPP_SPECIAL_CONVERSION_RULES = { | 105 CPP_SPECIAL_CONVERSION_RULES = { |
| 108 'CompareHow': 'Range::CompareHow', | |
| 109 'Date': 'double', | 106 'Date': 'double', |
| 110 'Dictionary': 'Dictionary', | 107 'Dictionary': 'Dictionary', |
| 111 'EventHandler': 'EventListener*', | 108 'EventHandler': 'EventListener*', |
| 112 'MediaQueryListListener': 'RefPtrWillBeRawPtr<MediaQueryListListener>', | 109 'NodeFilter': 'RefPtrWillBeRawPtr<NodeFilter>', |
| 113 'Promise': 'ScriptPromise', | 110 'Promise': 'ScriptPromise', |
| 114 'ScriptValue': 'ScriptValue', | 111 'ScriptValue': 'ScriptValue', |
| 115 # FIXME: Eliminate custom bindings for XPathNSResolver http://crbug.com/345
529 | 112 # FIXME: Eliminate custom bindings for XPathNSResolver http://crbug.com/345
529 |
| 116 'XPathNSResolver': 'RefPtrWillBeRawPtr<XPathNSResolver>', | 113 'XPathNSResolver': 'RefPtrWillBeRawPtr<XPathNSResolver>', |
| 117 'boolean': 'bool', | 114 'boolean': 'bool', |
| 118 'unrestricted double': 'double', | 115 'unrestricted double': 'double', |
| 119 'unrestricted float': 'float', | 116 'unrestricted float': 'float', |
| 120 } | 117 } |
| 121 | 118 |
| 122 | 119 |
| 123 def cpp_type(idl_type, extended_attributes=None, raw_type=False, used_as_rvalue_
type=False, used_in_cpp_sequence=False): | 120 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): |
| 124 """Returns C++ type corresponding to IDL type. | 121 """Returns C++ type corresponding to IDL type. |
| 125 | 122 |
| 126 |idl_type| argument is of type IdlType, while return value is a string | 123 |idl_type| argument is of type IdlType, while return value is a string |
| 127 | 124 |
| 128 Args: | 125 Args: |
| 129 idl_type: | 126 idl_type: |
| 130 IdlType | 127 IdlType |
| 131 raw_type: | 128 raw_type: |
| 132 bool, True if idl_type's raw/primitive C++ type should be returned. | 129 bool, True if idl_type's raw/primitive C++ type should be returned. |
| 133 used_as_rvalue_type: | 130 used_as_rvalue_type: |
| (...skipping 28 matching lines...) Expand all Loading... |
| 162 | 159 |
| 163 if base_idl_type in NON_WRAPPER_TYPES: | 160 if base_idl_type in NON_WRAPPER_TYPES: |
| 164 return ('PassRefPtr<%s>' if used_as_rvalue_type else 'RefPtr<%s>') % bas
e_idl_type | 161 return ('PassRefPtr<%s>' if used_as_rvalue_type else 'RefPtr<%s>') % bas
e_idl_type |
| 165 if base_idl_type in ('DOMString', 'ByteString', 'ScalarValueString'): | 162 if base_idl_type in ('DOMString', 'ByteString', 'ScalarValueString'): |
| 166 if not raw_type: | 163 if not raw_type: |
| 167 return 'String' | 164 return 'String' |
| 168 return 'DartStringAdapter' | 165 return 'DartStringAdapter' |
| 169 | 166 |
| 170 if idl_type.is_typed_array_type and raw_type: | 167 if idl_type.is_typed_array_type and raw_type: |
| 171 return 'RefPtr<%s>' % base_idl_type | 168 return 'RefPtr<%s>' % base_idl_type |
| 172 if idl_type.is_callback_interface: | |
| 173 return 'OwnPtr<%s>' % base_idl_type | |
| 174 if idl_type.is_interface_type: | 169 if idl_type.is_interface_type: |
| 175 implemented_as_class = idl_type.implemented_as | 170 implemented_as_class = idl_type.implemented_as |
| 176 if raw_type: | 171 if raw_type: |
| 177 return implemented_as_class + '*' | 172 return implemented_as_class + '*' |
| 178 new_type = 'Member' if used_in_cpp_sequence else 'RawPtr' | 173 new_type = 'Member' if used_in_cpp_sequence else 'RawPtr' |
| 179 ptr_type = cpp_ptr_type(('PassRefPtr' if used_as_rvalue_type else 'RefPt
r'), new_type, idl_type.gc_type) | 174 ptr_type = cpp_ptr_type(('PassRefPtr' if used_as_rvalue_type else 'RefPt
r'), new_type, idl_type.gc_type) |
| 180 return cpp_template_type(ptr_type, implemented_as_class) | 175 return cpp_template_type(ptr_type, implemented_as_class) |
| 181 | 176 |
| 182 # Default, assume native type is a pointer with same type name as idl type | 177 # Default, assume native type is a pointer with same type name as idl type |
| 178 |
| 179 # FIXME: How to handle sequence<WebGLShader>? |
| 180 if base_idl_type is None: |
| 181 base_idl_type = idl_type.inner_type.element_type.base_type |
| 182 |
| 183 return base_idl_type + '*' | 183 return base_idl_type + '*' |
| 184 | 184 |
| 185 | 185 |
| 186 def cpp_type_union(idl_type, extended_attributes=None, used_as_rvalue_type=False
, will_be_in_heap_object=False): | 186 def cpp_type_union(idl_type, extended_attributes=None, used_as_rvalue_type=False
, will_be_in_heap_object=False): |
| 187 return (member_type.cpp_type for member_type in idl_type.member_types) | 187 return (member_type.cpp_type for member_type in idl_type.member_types) |
| 188 | 188 |
| 189 | 189 |
| 190 # Allow access as idl_type.cpp_type if no arguments | 190 # Allow access as idl_type.cpp_type if no arguments |
| 191 IdlTypeBase.cpp_type = property(cpp_type) | 191 IdlTypeBase.cpp_type = property(cpp_type) |
| 192 IdlTypeBase.cpp_type_args = cpp_type | 192 IdlTypeBase.cpp_type_args = cpp_type |
| 193 IdlUnionType.cpp_type = property(cpp_type_union) | 193 IdlUnionType.cpp_type = property(cpp_type_union) |
| 194 IdlUnionType.cpp_type_args = cpp_type_union | 194 IdlUnionType.cpp_type_args = cpp_type_union |
| 195 | 195 |
| 196 | 196 |
| 197 IdlTypeBase.native_array_element_type = None | 197 IdlTypeBase.native_array_element_type = None |
| 198 IdlArrayOrSequenceType.native_array_element_type = property( | 198 IdlArrayOrSequenceType.native_array_element_type = property( |
| 199 lambda self: self.element_type) | 199 lambda self: self.element_type) |
| 200 | 200 |
| 201 IdlTypeBase.enum_validation_expression = property(DartUtilities.enum_validation_
expression) |
| 202 |
| 201 | 203 |
| 202 def cpp_template_type(template, inner_type): | 204 def cpp_template_type(template, inner_type): |
| 203 """Returns C++ template specialized to type, with space added if needed.""" | 205 """Returns C++ template specialized to type, with space added if needed.""" |
| 204 if inner_type.endswith('>'): | 206 if inner_type.endswith('>'): |
| 205 format_string = '{template}<{inner_type} >' | 207 format_string = '{template}<{inner_type} >' |
| 206 else: | 208 else: |
| 207 format_string = '{template}<{inner_type}>' | 209 format_string = '{template}<{inner_type}>' |
| 208 return format_string.format(template=template, inner_type=inner_type) | 210 return format_string.format(template=template, inner_type=inner_type) |
| 209 | 211 |
| 210 | 212 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 ################################################################################ | 290 ################################################################################ |
| 289 # Includes | 291 # Includes |
| 290 ################################################################################ | 292 ################################################################################ |
| 291 | 293 |
| 292 def includes_for_cpp_class(class_name, relative_dir_posix): | 294 def includes_for_cpp_class(class_name, relative_dir_posix): |
| 293 return set([posixpath.join('bindings', relative_dir_posix, class_name + '.h'
)]) | 295 return set([posixpath.join('bindings', relative_dir_posix, class_name + '.h'
)]) |
| 294 | 296 |
| 295 # TODO(terry): Will we need this group header for dart:blink? | 297 # TODO(terry): Will we need this group header for dart:blink? |
| 296 INCLUDES_FOR_TYPE = { | 298 INCLUDES_FOR_TYPE = { |
| 297 'object': set(), | 299 'object': set(), |
| 298 'CompareHow': set(), | |
| 299 'Dictionary': set(['bindings/core/v8/Dictionary.h']), | 300 'Dictionary': set(['bindings/core/v8/Dictionary.h']), |
| 300 'EventHandler': set(), | 301 'EventHandler': set(), |
| 301 'EventListener': set(), | 302 'EventListener': set(), |
| 302 'HTMLCollection': set(['bindings/core/dart/DartHTMLCollection.h', | 303 'HTMLCollection': set(['bindings/core/dart/DartHTMLCollection.h', |
| 303 'core/dom/ClassCollection.h', | 304 'core/dom/ClassCollection.h', |
| 304 'core/dom/TagCollection.h', | 305 'core/dom/TagCollection.h', |
| 305 'core/html/HTMLCollection.h', | 306 'core/html/HTMLCollection.h', |
| 307 'core/html/HTMLDataListOptionsCollection.h', |
| 306 'core/html/HTMLFormControlsCollection.h', | 308 'core/html/HTMLFormControlsCollection.h', |
| 307 'core/html/HTMLTableRowsCollection.h']), | 309 'core/html/HTMLTableRowsCollection.h']), |
| 308 'MediaQueryListListener': set(['core/css/MediaQueryListListener.h']), | |
| 309 'NodeList': set(['bindings/core/dart/DartNodeList.h', | 310 'NodeList': set(['bindings/core/dart/DartNodeList.h', |
| 310 'core/dom/NameNodeList.h', | 311 'core/dom/NameNodeList.h', |
| 311 'core/dom/NodeList.h', | 312 'core/dom/NodeList.h', |
| 312 'core/dom/StaticNodeList.h', | 313 'core/dom/StaticNodeList.h', |
| 313 'core/html/LabelsNodeList.h']), | 314 'core/html/LabelsNodeList.h']), |
| 314 'Promise': set(), | 315 'Promise': set(['bindings/core/dart/DartScriptPromise.h']), |
| 315 'SerializedScriptValue': set(), | 316 'SerializedScriptValue': set(), |
| 316 'ScriptValue': set(['bindings/core/dart/DartScriptValue.h']), | 317 'ScriptValue': set(['bindings/core/dart/DartScriptValue.h']), |
| 317 } | 318 } |
| 318 | 319 |
| 319 | 320 |
| 320 def includes_for_type(idl_type): | 321 def includes_for_type(idl_type): |
| 321 idl_type = idl_type.preprocessed_type | 322 idl_type = idl_type.preprocessed_type |
| 322 | 323 |
| 323 # Composite types | 324 # Composite types |
| 324 if idl_type.native_array_element_type: | 325 if idl_type.native_array_element_type: |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 # FIXME(vsm): Inconsistent with V8. | 399 # FIXME(vsm): Inconsistent with V8. |
| 399 'byte': 'DartUtilities::dartToUnsigned(args, {index}, exception)', | 400 'byte': 'DartUtilities::dartToUnsigned(args, {index}, exception)', |
| 400 'octet': 'DartUtilities::dartToUnsigned(args, {index}, exception)', | 401 'octet': 'DartUtilities::dartToUnsigned(args, {index}, exception)', |
| 401 'short': 'DartUtilities::dartToInt(args, {index}, exception)', | 402 'short': 'DartUtilities::dartToInt(args, {index}, exception)', |
| 402 'unsigned short': 'DartUtilities::dartToUnsigned(args, {index}, exception)', | 403 'unsigned short': 'DartUtilities::dartToUnsigned(args, {index}, exception)', |
| 403 'long': 'DartUtilities::dartToInt(args, {index}, exception)', | 404 'long': 'DartUtilities::dartToInt(args, {index}, exception)', |
| 404 'unsigned long': 'DartUtilities::dartToUnsignedLongLong(args, {index}, excep
tion)', | 405 'unsigned long': 'DartUtilities::dartToUnsignedLongLong(args, {index}, excep
tion)', |
| 405 'long long': 'DartUtilities::dartToLongLong(args, {index}, exception)', | 406 'long long': 'DartUtilities::dartToLongLong(args, {index}, exception)', |
| 406 'unsigned long long': 'DartUtilities::dartToUnsignedLongLong(args, {index},
exception)', | 407 'unsigned long long': 'DartUtilities::dartToUnsignedLongLong(args, {index},
exception)', |
| 407 # Interface types | 408 # Interface types |
| 408 'CompareHow': 'static_cast<Range::CompareHow>(0) /* FIXME, DART_TO_CPP_VALUE
[CompareHow] */', | |
| 409 'Dictionary': 'DartUtilities::dartToDictionary{null_check}(args, {index}, ex
ception)', | 409 'Dictionary': 'DartUtilities::dartToDictionary{null_check}(args, {index}, ex
ception)', |
| 410 'EventTarget': '0 /* FIXME, DART_TO_CPP_VALUE[EventTarget] */', | 410 'EventTarget': '0 /* FIXME, DART_TO_CPP_VALUE[EventTarget] */', |
| 411 'MediaQueryListListener': 'nullptr /* FIXME, DART_TO_CPP_VALUE[MediaQueryLis
tener] */', | |
| 412 'NodeFilter': 'nullptr /* FIXME, DART_TO_CPP_VALUE[NodeFilter] */', | 411 'NodeFilter': 'nullptr /* FIXME, DART_TO_CPP_VALUE[NodeFilter] */', |
| 413 'Promise': 'nullptr /* FIXME, DART_TO_CPP_VALUE[Promise] */', | 412 'Promise': 'DartUtilities::dartToScriptPromise{null_check}(args, {index})', |
| 414 'SerializedScriptValue': 'nullptr /* FIXME, DART_TO_CPP_VALUE[SerializedScri
ptValue] */', | 413 'SerializedScriptValue': 'nullptr /* FIXME, DART_TO_CPP_VALUE[SerializedScri
ptValue] */', |
| 415 'ScriptValue': 'DartUtilities::dartToScriptValue{null_check}(args, {index})'
, | 414 'ScriptValue': 'DartUtilities::dartToScriptValue{null_check}(args, {index})'
, |
| 416 # FIXME(vsm): Why don't we have an entry for Window? V8 does. | 415 # FIXME(vsm): Why don't we have an entry for Window? V8 does. |
| 417 # I think I removed this as the Window object is more special in V8 - it's t
he | 416 # I think I removed this as the Window object is more special in V8 - it's t
he |
| 418 # global context as well. Do we need to special case it? | 417 # global context as well. Do we need to special case it? |
| 419 'XPathNSResolver': 'nullptr /* FIXME, DART_TO_CPP_VALUE[XPathNSResolver] */'
, | 418 'XPathNSResolver': 'nullptr /* FIXME, DART_TO_CPP_VALUE[XPathNSResolver] */'
, |
| 420 # FIXME(vsm): This is an enum type (defined in StorageQuota.idl). | 419 # FIXME(vsm): This is an enum type (defined in StorageQuota.idl). |
| 421 # We should handle it automatically, but map to a String for now. | 420 # We should handle it automatically, but map to a String for now. |
| 422 'StorageType': 'DartUtilities::dartToString(args, {index}, exception, {auto_
scope})', | 421 'StorageType': 'DartUtilities::dartToString(args, {index}, exception, {auto_
scope})', |
| 423 } | 422 } |
| 424 | 423 |
| 425 | 424 |
| 426 def dart_value_to_cpp_value(idl_type, interface_extended_attributes, extended_at
tributes, variable_name, | 425 def dart_dictionary_value_argument(idl_type, index): |
| 427 null_check, index, auto_scope=True): | 426 if idl_type.is_dictionary: |
| 427 argument_expression_format = 'DartUtilities::dartToDictionaryWithNullChe
ck(args, {index}, exception)' |
| 428 return argument_expression_format.format(index=index) |
| 429 |
| 430 return None |
| 431 |
| 432 |
| 433 def dart_dictionary_to_local_cpp_value(idl_type, index=None): |
| 434 """Returns an expression that converts a Dictionary value as a local value."
"" |
| 435 idl_type = idl_type.preprocessed_type |
| 436 |
| 437 cpp_value = dart_dictionary_value_argument(idl_type, index) |
| 438 |
| 439 return cpp_value |
| 440 |
| 441 IdlTypeBase.dart_dictionary_to_local_cpp_value = dart_dictionary_to_local_cpp_va
lue |
| 442 |
| 443 |
| 444 def dart_value_to_cpp_value(idl_type, extended_attributes, variable_name, |
| 445 null_check, has_type_checking_interface, |
| 446 index, auto_scope=True): |
| 428 # Composite types | 447 # Composite types |
| 429 native_array_element_type = idl_type.native_array_element_type | 448 native_array_element_type = idl_type.native_array_element_type |
| 430 if native_array_element_type: | 449 if native_array_element_type: |
| 431 return dart_value_to_cpp_value_array_or_sequence(native_array_element_ty
pe, variable_name, index) | 450 return dart_value_to_cpp_value_array_or_sequence(native_array_element_ty
pe, variable_name, index) |
| 432 | 451 |
| 433 # Simple types | 452 # Simple types |
| 434 idl_type = idl_type.preprocessed_type | 453 idl_type = idl_type.preprocessed_type |
| 435 add_includes_for_type(idl_type) | 454 add_includes_for_type(idl_type) |
| 436 base_idl_type = idl_type.base_type | 455 base_idl_type = idl_type.base_type |
| 437 | 456 |
| 438 if 'EnforceRange' in extended_attributes: | 457 if 'EnforceRange' in extended_attributes: |
| 439 arguments = ', '.join([variable_name, 'EnforceRange', 'exceptionState']) | 458 arguments = ', '.join([variable_name, 'EnforceRange', 'exceptionState']) |
| 440 elif idl_type.is_integer_type: # NormalConversion | 459 elif idl_type.is_integer_type: # NormalConversion |
| 441 arguments = ', '.join([variable_name, 'es']) | 460 arguments = ', '.join([variable_name, 'es']) |
| 442 else: | 461 else: |
| 443 arguments = variable_name | 462 arguments = variable_name |
| 444 | 463 |
| 445 if base_idl_type in DART_TO_CPP_VALUE: | 464 if base_idl_type in DART_TO_CPP_VALUE: |
| 446 cpp_expression_format = DART_TO_CPP_VALUE[base_idl_type] | 465 cpp_expression_format = DART_TO_CPP_VALUE[base_idl_type] |
| 447 elif idl_type.is_typed_array_type: | 466 elif idl_type.is_typed_array_type: |
| 448 # FIXME(vsm): V8 generates a type check here as well. Do we need one? | 467 # FIXME(vsm): V8 generates a type check here as well. Do we need one? |
| 449 # FIXME(vsm): When do we call the externalized version? E.g., see | 468 # FIXME(vsm): When do we call the externalized version? E.g., see |
| 450 # bindings/dart/custom/DartWaveShaperNodeCustom.cpp - it calls | 469 # bindings/dart/custom/DartWaveShaperNodeCustom.cpp - it calls |
| 451 # DartUtilities::dartToExternalizedArrayBufferView instead. | 470 # DartUtilities::dartToExternalizedArrayBufferView instead. |
| 452 # V8 always converts null here | 471 # V8 always converts null here |
| 453 cpp_expression_format = ('DartUtilities::dartTo{idl_type}WithNullCheck(a
rgs, {index}, exception)') | 472 cpp_expression_format = ('DartUtilities::dartTo{idl_type}WithNullCheck(a
rgs, {index}, exception)') |
| 454 elif idl_type.is_callback_interface: | 473 elif idl_type.is_callback_interface: |
| 455 cpp_expression_format = ('Dart{idl_type}::create{null_check}(args, {inde
x}, exception)') | 474 cpp_expression_format = ('Dart{idl_type}::create{null_check}(args, {inde
x}, exception)') |
| 475 elif idl_type.is_dictionary: |
| 476 # Value of dictionary is defined in method dart_dictionary_value_argumen
t. |
| 477 cpp_expression_format = 'Dart{idl_type}::toImpl(dictionary, es)' |
| 456 else: | 478 else: |
| 457 cpp_expression_format = ('Dart{idl_type}::toNative{null_check}(args, {in
dex}, exception)') | 479 cpp_expression_format = ('Dart{idl_type}::toNative{null_check}(args, {in
dex}, exception)') |
| 458 | 480 |
| 459 # We allow the calling context to force a null check to handle | 481 # We allow the calling context to force a null check to handle |
| 460 # some cases that require calling context info. V8 handles all | 482 # some cases that require calling context info. V8 handles all |
| 461 # of this differently, and we may wish to reconsider this approach | 483 # of this differently, and we may wish to reconsider this approach |
| 462 null_check = 'WithNullCheck' \ | 484 check_string = '' |
| 463 if null_check or allow_null(idl_type, interface_extended_attributes, ext
ended_attributes) else '' | 485 if null_check or allow_null(idl_type, extended_attributes, |
| 464 return cpp_expression_format.format(null_check=null_check, | 486 has_type_checking_interface): |
| 487 check_string = 'WithNullCheck' |
| 488 elif allow_empty(idl_type, extended_attributes): |
| 489 check_string = 'WithEmptyCheck' |
| 490 return cpp_expression_format.format(null_check=check_string, |
| 465 arguments=arguments, | 491 arguments=arguments, |
| 466 index=index, | 492 index=index, |
| 467 idl_type=base_idl_type, | 493 idl_type=base_idl_type, |
| 468 auto_scope=DartUtilities.bool_to_cpp(aut
o_scope)) | 494 auto_scope=DartUtilities.bool_to_cpp(aut
o_scope)) |
| 469 | 495 |
| 470 | 496 |
| 471 def dart_value_to_cpp_value_array_or_sequence(native_array_element_type, variabl
e_name, index): | 497 def dart_value_to_cpp_value_array_or_sequence(native_array_element_type, variabl
e_name, index): |
| 472 # Index is None for setters, index (starting at 0) for method arguments, | 498 # Index is None for setters, index (starting at 0) for method arguments, |
| 473 # and is used to provide a human-readable exception message | 499 # and is used to provide a human-readable exception message |
| 474 if index is None: | 500 if index is None: |
| 475 index = 0 # special case, meaning "setter" | 501 index = 0 # special case, meaning "setter" |
| 476 # else: | 502 # else: |
| 477 # index += 1 # human-readable index | 503 # index += 1 # human-readable index |
| 478 if (native_array_element_type.is_interface_type and | 504 if (native_array_element_type.is_interface_type and |
| 479 native_array_element_type.name != 'Dictionary'): | 505 native_array_element_type.name != 'Dictionary'): |
| 480 this_cpp_type = None | 506 this_cpp_type = None |
| 481 ref_ptr_type = cpp_ptr_type('RefPtr', 'Member', native_array_element_typ
e.gc_type) | 507 ref_ptr_type = cpp_ptr_type('RefPtr', 'Member', native_array_element_typ
e.gc_type) |
| 482 # FIXME(vsm): We're not using ref_ptr_type.... | 508 # FIXME(vsm): We're not using ref_ptr_type.... |
| 483 expression_format = 'DartUtilities::toNativeVector<{cpp_type} >(args, {i
ndex}, {variable_name}, exception)' | 509 expression_format = 'DartUtilities::toNativeVector<{native_array_element
_type} >(args, {index}, {variable_name}, exception)' |
| 484 add_includes_for_type(native_array_element_type) | 510 add_includes_for_type(native_array_element_type) |
| 485 else: | 511 else: |
| 486 ref_ptr_type = None | 512 ref_ptr_type = None |
| 487 this_cpp_type = native_array_element_type.cpp_type | 513 this_cpp_type = native_array_element_type.cpp_type |
| 488 expression_format = 'DartUtilities::toNativeVector<{cpp_type}>(args, {in
dex}, {variable_name}, exception)' | 514 expression_format = 'DartUtilities::toNativeVector<{cpp_type}>(args, {in
dex}, {variable_name}, exception)' |
| 489 | 515 |
| 490 expression = expression_format.format(native_array_element_type=native_array
_element_type.name, | 516 expression = expression_format.format(native_array_element_type=native_array
_element_type.name, |
| 491 cpp_type=this_cpp_type, index=index, r
ef_ptr_type=ref_ptr_type, | 517 cpp_type=this_cpp_type, index=index, r
ef_ptr_type=ref_ptr_type, |
| 492 variable_name=variable_name) | 518 variable_name=variable_name) |
| 493 return expression | 519 return expression |
| 494 | 520 |
| 495 def dart_value_to_local_cpp_value(idl_type, interface_extended_attributes, exten
ded_attributes, | 521 |
| 496 variable_name, null_check, index=None, auto_sc
ope=True): | 522 def dart_value_to_local_cpp_value(idl_type, extended_attributes, variable_name, |
| 523 null_check, has_type_checking_interface, |
| 524 index=None, auto_scope=True): |
| 497 """Returns an expression that converts a Dart value to a C++ value as a loca
l value.""" | 525 """Returns an expression that converts a Dart value to a C++ value as a loca
l value.""" |
| 498 idl_type = idl_type.preprocessed_type | 526 idl_type = idl_type.preprocessed_type |
| 499 | 527 |
| 500 cpp_value = dart_value_to_cpp_value( | 528 cpp_value = dart_value_to_cpp_value( |
| 501 idl_type, interface_extended_attributes, extended_attributes, | 529 idl_type, extended_attributes, variable_name, |
| 502 variable_name, null_check, index, auto_scope) | 530 null_check, has_type_checking_interface, |
| 531 index, auto_scope) |
| 503 | 532 |
| 504 return cpp_value | 533 return cpp_value |
| 505 | 534 |
| 506 IdlTypeBase.dart_value_to_local_cpp_value = dart_value_to_local_cpp_value | 535 IdlTypeBase.dart_value_to_local_cpp_value = dart_value_to_local_cpp_value |
| 507 #IdlUnionType.dart_value_to_local_cpp_value = dart_value_to_local_cpp_value | 536 #IdlUnionType.dart_value_to_local_cpp_value = dart_value_to_local_cpp_value |
| 508 | 537 |
| 509 | 538 |
| 510 # Insure that we don't use C++ reserved names. Today on default is a problem. | 539 # Insure that we don't use C++ reserved names. Today on default is a problem. |
| 511 def check_reserved_name(name): | 540 def check_reserved_name(name): |
| 512 return 'default_value' if (name == 'default') else name | 541 return 'default_value' if (name == 'default') else name |
| (...skipping 12 matching lines...) Expand all Loading... |
| 525 return idl_type | 554 return idl_type |
| 526 | 555 |
| 527 IdlTypeBase.preprocessed_type = property(preprocess_idl_type) | 556 IdlTypeBase.preprocessed_type = property(preprocess_idl_type) |
| 528 IdlUnionType.preprocessed_type = property(preprocess_idl_type) | 557 IdlUnionType.preprocessed_type = property(preprocess_idl_type) |
| 529 | 558 |
| 530 | 559 |
| 531 def preprocess_idl_type_and_value(idl_type, cpp_value, extended_attributes): | 560 def preprocess_idl_type_and_value(idl_type, cpp_value, extended_attributes): |
| 532 """Returns IDL type and value, with preliminary type conversions applied.""" | 561 """Returns IDL type and value, with preliminary type conversions applied.""" |
| 533 idl_type = idl_type.preprocessed_type | 562 idl_type = idl_type.preprocessed_type |
| 534 if idl_type.name == 'Promise': | 563 if idl_type.name == 'Promise': |
| 535 idl_type = IdlType('ScriptValue') | 564 idl_type = IdlType('ScriptPromise') |
| 565 |
| 536 # FIXME(vsm): V8 maps 'long long' and 'unsigned long long' to double | 566 # FIXME(vsm): V8 maps 'long long' and 'unsigned long long' to double |
| 537 # as they are not representable in ECMAScript. Should we do the same? | 567 # as they are not representable in ECMAScript. Should we do the same? |
| 538 | 568 |
| 539 # HTML5 says that unsigned reflected attributes should be in the range | 569 # HTML5 says that unsigned reflected attributes should be in the range |
| 540 # [0, 2^31). When a value isn't in this range, a default value (or 0) | 570 # [0, 2^31). When a value isn't in this range, a default value (or 0) |
| 541 # should be returned instead. | 571 # should be returned instead. |
| 542 extended_attributes = extended_attributes or {} | 572 extended_attributes = extended_attributes or {} |
| 543 if ('Reflect' in extended_attributes and | 573 if ('Reflect' in extended_attributes and |
| 544 idl_type.base_type in ['unsigned long', 'unsigned short']): | 574 idl_type.base_type in ['unsigned long', 'unsigned short']): |
| 545 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute', | 575 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute', |
| 546 'getIntegralAttribute') | 576 'getIntegralAttribute') |
| 547 cpp_value = 'std::max(0, %s)' % cpp_value | 577 cpp_value = 'std::max(0, %s)' % cpp_value |
| 548 return idl_type, cpp_value | 578 return idl_type, cpp_value |
| 549 | 579 |
| 550 | 580 |
| 551 def dart_conversion_type(idl_type, extended_attributes): | 581 def dart_conversion_type(idl_type, extended_attributes): |
| 552 """Returns Dart conversion type, adding any additional includes. | 582 """Returns Dart conversion type, adding any additional includes. |
| 553 | 583 |
| 554 The Dart conversion type is used to select the C++ -> Dart conversion functi
on | 584 The Dart conversion type is used to select the C++ -> Dart conversion functi
on |
| 555 or setDart*ReturnValue function; it can be an idl_type, a cpp_type, or a | 585 or setDart*ReturnValue function; it can be an idl_type, a cpp_type, or a |
| 556 separate name for the type of conversion (e.g., 'DOMWrapper'). | 586 separate name for the type of conversion (e.g., 'DOMWrapper'). |
| 557 """ | 587 """ |
| 558 extended_attributes = extended_attributes or {} | 588 extended_attributes = extended_attributes or {} |
| 559 | 589 |
| 560 # Composite types | 590 # Composite types |
| 561 native_array_element_type = idl_type.native_array_element_type | 591 native_array_element_type = idl_type.native_array_element_type |
| 592 |
| 593 # FIXME: Work around sequence behaving like an array. |
| 594 if (not native_array_element_type) and type(idl_type.inner_type) is IdlSeque
nceType: |
| 595 native_array_element_type = idl_type.inner_type.native_array_element_typ
e |
| 596 |
| 562 if native_array_element_type: | 597 if native_array_element_type: |
| 563 if native_array_element_type.is_interface_type: | 598 if native_array_element_type.is_interface_type: |
| 564 add_includes_for_type(native_array_element_type) | 599 add_includes_for_type(native_array_element_type) |
| 565 return 'array' | 600 return 'array' |
| 566 | 601 |
| 567 # Simple types | 602 # Simple types |
| 568 base_idl_type = idl_type.base_type | 603 base_idl_type = idl_type.base_type |
| 569 # Basic types, without additional includes | 604 # Basic types, without additional includes |
| 570 if base_idl_type in CPP_INT_TYPES or base_idl_type == 'long long': | 605 if base_idl_type in CPP_INT_TYPES or base_idl_type == 'long long': |
| 571 return 'int' | 606 return 'int' |
| 572 if base_idl_type in CPP_UNSIGNED_TYPES or base_idl_type == 'unsigned long lo
ng': | 607 if base_idl_type in CPP_UNSIGNED_TYPES or base_idl_type == 'unsigned long lo
ng': |
| 573 return 'unsigned' | 608 return 'unsigned' |
| 574 if base_idl_type == 'DOMString': | 609 if idl_type.is_string_type: |
| 610 if idl_type.is_nullable: |
| 611 return 'StringOrNull' |
| 575 if 'TreatReturnedNullStringAs' not in extended_attributes: | 612 if 'TreatReturnedNullStringAs' not in extended_attributes: |
| 576 return 'DOMString' | 613 return 'DOMString' |
| 577 treat_returned_null_string_as = extended_attributes['TreatReturnedNullSt
ringAs'] | 614 treat_returned_null_string_as = extended_attributes['TreatReturnedNullSt
ringAs'] |
| 578 if treat_returned_null_string_as == 'Null': | 615 if treat_returned_null_string_as == 'Null': |
| 579 return 'StringOrNull' | 616 return 'StringOrNull' |
| 580 if treat_returned_null_string_as == 'Undefined': | 617 if treat_returned_null_string_as == 'Undefined': |
| 581 return 'StringOrUndefined' | 618 return 'StringOrUndefined' |
| 582 raise 'Unrecognized TreatReturnNullStringAs value: "%s"' % treat_returne
d_null_string_as | 619 raise 'Unrecognized TreatReturnNullStringAs value: "%s"' % treat_returne
d_null_string_as |
| 583 if idl_type.is_basic_type or base_idl_type == 'ScriptValue': | 620 if idl_type.is_basic_type or base_idl_type == 'ScriptValue': |
| 584 return base_idl_type | 621 return base_idl_type |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 # We specialize these as well in Dart. | 655 # We specialize these as well in Dart. |
| 619 'float': 'Dart_SetDoubleReturnValue(args, {cpp_value})', | 656 'float': 'Dart_SetDoubleReturnValue(args, {cpp_value})', |
| 620 'unrestricted float': 'Dart_SetDoubleReturnValue(args, {cpp_value})', | 657 'unrestricted float': 'Dart_SetDoubleReturnValue(args, {cpp_value})', |
| 621 'double': 'Dart_SetDoubleReturnValue(args, {cpp_value})', | 658 'double': 'Dart_SetDoubleReturnValue(args, {cpp_value})', |
| 622 'unrestricted double': 'Dart_SetDoubleReturnValue(args, {cpp_value})', | 659 'unrestricted double': 'Dart_SetDoubleReturnValue(args, {cpp_value})', |
| 623 # No special function, but instead convert value to Dart_Handle | 660 # No special function, but instead convert value to Dart_Handle |
| 624 # and then use general Dart_SetReturnValue. | 661 # and then use general Dart_SetReturnValue. |
| 625 'array': 'Dart_SetReturnValue(args, {cpp_value})', | 662 'array': 'Dart_SetReturnValue(args, {cpp_value})', |
| 626 'Date': 'Dart_SetReturnValue(args, {cpp_value})', | 663 'Date': 'Dart_SetReturnValue(args, {cpp_value})', |
| 627 'EventHandler': DART_FIX_ME, | 664 'EventHandler': DART_FIX_ME, |
| 665 'ScriptPromise': 'Dart_SetReturnValue(args, {cpp_value})', |
| 628 'ScriptValue': 'Dart_SetReturnValue(args, {cpp_value})', | 666 'ScriptValue': 'Dart_SetReturnValue(args, {cpp_value})', |
| 629 'SerializedScriptValue': DART_FIX_ME, | 667 'SerializedScriptValue': DART_FIX_ME, |
| 630 # DOMWrapper | 668 # DOMWrapper |
| 631 # TODO(terry): Remove ForMainWorld stuff. | 669 # TODO(terry): Remove ForMainWorld stuff. |
| 632 'DOMWrapperForMainWorld': DART_FIX_ME, | 670 'DOMWrapperForMainWorld': DART_FIX_ME, |
| 633 # FIXME(vsm): V8 has a fast path. Do we? | 671 # FIXME(vsm): V8 has a fast path. Do we? |
| 634 'DOMWrapperFast': 'Dart{type_name}::returnToDart(args, WTF::getPtr({cpp_valu
e}), {auto_scope})', | 672 'DOMWrapperFast': 'Dart{type_name}::returnToDart(args, WTF::getPtr({cpp_valu
e}), {auto_scope})', |
| 635 'DOMWrapperDefault': 'Dart{type_name}::returnToDart(args, {cpp_value}, {auto
_scope})', | 673 'DOMWrapperDefault': 'Dart{type_name}::returnToDart(args, {cpp_value}, {auto
_scope})', |
| 636 # Typed arrays don't have special Dart* classes for Dart. | 674 # Typed arrays don't have special Dart* classes for Dart. |
| 637 'ArrayBuffer': 'Dart_SetReturnValue(args, DartUtilities::arrayBufferToDart({
cpp_value}))', | 675 'ArrayBuffer': 'Dart_SetReturnValue(args, DartUtilities::arrayBufferToDart({
cpp_value}))', |
| (...skipping 12 matching lines...) Expand all Loading... |
| 650 def dom_wrapper_conversion_type(): | 688 def dom_wrapper_conversion_type(): |
| 651 if not script_wrappable: | 689 if not script_wrappable: |
| 652 return 'DOMWrapperDefault' | 690 return 'DOMWrapperDefault' |
| 653 if for_main_world: | 691 if for_main_world: |
| 654 return 'DOMWrapperForMainWorld' | 692 return 'DOMWrapperForMainWorld' |
| 655 return 'DOMWrapperFast' | 693 return 'DOMWrapperFast' |
| 656 | 694 |
| 657 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext
ended_attributes) | 695 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext
ended_attributes) |
| 658 this_dart_conversion_type = idl_type.dart_conversion_type(extended_attribute
s) | 696 this_dart_conversion_type = idl_type.dart_conversion_type(extended_attribute
s) |
| 659 # SetReturn-specific overrides | 697 # SetReturn-specific overrides |
| 660 if this_dart_conversion_type in ['Date', 'EventHandler', 'ScriptValue', 'Ser
ializedScriptValue', 'array']: | 698 if this_dart_conversion_type in ['Date', 'EventHandler', 'ScriptPromise', 'S
criptValue', 'SerializedScriptValue', 'array']: |
| 661 # Convert value to Dart and then use general Dart_SetReturnValue | 699 # Convert value to Dart and then use general Dart_SetReturnValue |
| 662 # FIXME(vsm): Why do we differ from V8 here? It doesn't have a | 700 # FIXME(vsm): Why do we differ from V8 here? It doesn't have a |
| 663 # creation_context. | 701 # creation_context. |
| 664 creation_context = '' | 702 creation_context = '' |
| 665 if this_dart_conversion_type == 'array': | 703 if this_dart_conversion_type == 'array': |
| 666 # FIXME: This is not right if the base type is a primitive, DOMStrin
g, etc. | 704 # FIXME: This is not right if the base type is a primitive, DOMStrin
g, etc. |
| 667 # What is the right check for base type? | 705 # What is the right check for base type? |
| 668 base_type = str(idl_type.element_type) | 706 base_type = str(idl_type.element_type) |
| 669 if base_type not in DART_TO_CPP_VALUE: | 707 if base_type not in DART_TO_CPP_VALUE: |
| 670 if base_type == 'None': | 708 if base_type == 'None': |
| (...skipping 18 matching lines...) Expand all Loading... |
| 689 return statement | 727 return statement |
| 690 | 728 |
| 691 | 729 |
| 692 def dart_set_return_value_union(idl_type, cpp_value, extended_attributes=None, | 730 def dart_set_return_value_union(idl_type, cpp_value, extended_attributes=None, |
| 693 script_wrappable='', release=False, for_main_world
=False, | 731 script_wrappable='', release=False, for_main_world
=False, |
| 694 auto_scope=True): | 732 auto_scope=True): |
| 695 """ | 733 """ |
| 696 release: can be either False (False for all member types) or | 734 release: can be either False (False for all member types) or |
| 697 a sequence (list or tuple) of booleans (if specified individually). | 735 a sequence (list or tuple) of booleans (if specified individually). |
| 698 """ | 736 """ |
| 699 | |
| 700 return [ | 737 return [ |
| 701 # FIXME(vsm): Why do we use 'result' instead of cpp_value as V8? | 738 # FIXME(vsm): Why do we use 'result' instead of cpp_value as V8? |
| 702 member_type.dart_set_return_value('result' + str(i), | 739 member_type.dart_set_return_value('result' + str(i), |
| 703 extended_attributes, | 740 extended_attributes, |
| 704 script_wrappable, | 741 script_wrappable, |
| 705 release and release[i], | 742 release and release[i], |
| 706 for_main_world, | 743 for_main_world, |
| 707 auto_scope) | 744 auto_scope) |
| 708 for i, member_type in | 745 for i, member_type in |
| 709 enumerate(idl_type.member_types)] | 746 enumerate(idl_type.member_types)] |
| (...skipping 17 matching lines...) Expand all Loading... |
| 727 'unsigned': 'DartUtilities::unsignedLongLongToDart({cpp_value})', | 764 'unsigned': 'DartUtilities::unsignedLongLongToDart({cpp_value})', |
| 728 'float': 'DartUtilities::doubleToDart({cpp_value})', | 765 'float': 'DartUtilities::doubleToDart({cpp_value})', |
| 729 'unrestricted float': 'DartUtilities::doubleToDart({cpp_value})', | 766 'unrestricted float': 'DartUtilities::doubleToDart({cpp_value})', |
| 730 'double': 'DartUtilities::doubleToDart({cpp_value})', | 767 'double': 'DartUtilities::doubleToDart({cpp_value})', |
| 731 'unrestricted double': 'DartUtilities::doubleToDart({cpp_value})', | 768 'unrestricted double': 'DartUtilities::doubleToDart({cpp_value})', |
| 732 # FIXME(vsm): Dart_Null? | 769 # FIXME(vsm): Dart_Null? |
| 733 'void': '', | 770 'void': '', |
| 734 # Special cases | 771 # Special cases |
| 735 'EventHandler': '-----OOPS TO DART-EVENT---', | 772 'EventHandler': '-----OOPS TO DART-EVENT---', |
| 736 # We need to generate the NullCheck version in some cases. | 773 # We need to generate the NullCheck version in some cases. |
| 774 'ScriptPromise': 'DartUtilities::scriptPromiseToDart({cpp_value})', |
| 737 'ScriptValue': 'DartUtilities::scriptValueToDart({cpp_value})', | 775 'ScriptValue': 'DartUtilities::scriptValueToDart({cpp_value})', |
| 738 'SerializedScriptValue': 'DartUtilities::serializedScriptValueToDart({cpp_va
lue})', | 776 'SerializedScriptValue': 'DartUtilities::serializedScriptValueToDart({cpp_va
lue})', |
| 739 # General | 777 # General |
| 740 'array': 'DartDOMWrapper::vectorToDart{creation_context}({cpp_value})', | 778 'array': 'DartDOMWrapper::vectorToDart{creation_context}({cpp_value})', |
| 741 'DOMWrapper': 'Dart{idl_type}::toDart({cpp_value})', | 779 'DOMWrapper': 'Dart{idl_type}::toDart({cpp_value})', |
| 742 } | 780 } |
| 743 | 781 |
| 744 | 782 |
| 745 def cpp_value_to_dart_value(idl_type, cpp_value, creation_context='', extended_a
ttributes=None): | 783 def cpp_value_to_dart_value(idl_type, cpp_value, creation_context='', extended_a
ttributes=None): |
| 746 """Returns an expression that converts a C++ value to a Dart value.""" | 784 """Returns an expression that converts a C++ value to a Dart value.""" |
| 747 # the isolate parameter is needed for callback interfaces | 785 # the isolate parameter is needed for callback interfaces |
| 748 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext
ended_attributes) | 786 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext
ended_attributes) |
| 749 this_dart_conversion_type = idl_type.dart_conversion_type(extended_attribute
s) | 787 this_dart_conversion_type = idl_type.dart_conversion_type(extended_attribute
s) |
| 750 format_string = CPP_VALUE_TO_DART_VALUE[this_dart_conversion_type] | 788 format_string = CPP_VALUE_TO_DART_VALUE[this_dart_conversion_type] |
| 751 statement = format_string.format( | 789 statement = format_string.format( |
| 752 cpp_value=cpp_value, creation_context=creation_context, | 790 cpp_value=cpp_value, creation_context=creation_context, |
| 753 idl_type=idl_type.base_type) | 791 idl_type=idl_type.base_type) |
| 754 return statement | 792 return statement |
| 755 | 793 |
| 756 IdlTypeBase.cpp_value_to_dart_value = cpp_value_to_dart_value | 794 IdlTypeBase.cpp_value_to_dart_value = cpp_value_to_dart_value |
| 757 | 795 |
| 796 # FIXME(leafp) This is horrible, we should do better, but currently this is hard
to do |
| 797 # in a nice way. Best solution might be to extend DartStringAdapter to accomoda
te |
| 798 # initialization from constant strings, but better to do that once we're stable |
| 799 # on the bots so we can track any performance regression |
| 800 CPP_LITERAL_TO_DART_VALUE = { |
| 801 'DOMString': {'nullptr': 'DartStringAdapter(DartStringPeer::nullString())', |
| 802 'String("")': 'DartStringAdapter(DartStringPeer::emptyString()
)', |
| 803 '*': 'DartUtilities::dartToString(DartUtilities::stringToDart(
{cpp_literal}), exception)'}, |
| 804 'ScalarValueString': {'nullptr': 'DartStringAdapter(DartStringPeer::nullStri
ng())', |
| 805 'String("")': 'DartStringAdapter(DartStringPeer::empty
String())', |
| 806 '*': 'DartUtilities::dartToScalarValueString(DartUtili
ties::stringToDart({cpp_literal}), exception)'}, |
| 807 } |
| 808 |
| 809 |
| 810 def literal_cpp_value(idl_type, idl_literal): |
| 811 """Converts an expression that is a valid C++ literal for this type.""" |
| 812 # FIXME: add validation that idl_type and idl_literal are compatible |
| 813 literal_value = str(idl_literal) |
| 814 base_type = idl_type.preprocessed_type.base_type |
| 815 if base_type in CPP_UNSIGNED_TYPES: |
| 816 return literal_value + 'u' |
| 817 if base_type in CPP_LITERAL_TO_DART_VALUE: |
| 818 if literal_value in CPP_LITERAL_TO_DART_VALUE[base_type]: |
| 819 format_string = CPP_LITERAL_TO_DART_VALUE[base_type][literal_value] |
| 820 else: |
| 821 format_string = CPP_LITERAL_TO_DART_VALUE[base_type]['*'] |
| 822 return format_string.format(cpp_literal=literal_value) |
| 823 return literal_value |
| 824 |
| 825 IdlType.literal_cpp_value = literal_cpp_value |
| 826 |
| 827 |
| 828 CPP_DEFAULT_VALUE_FOR_CPP_TYPE = { |
| 829 'DOMString': 'DartStringAdapter(DartStringPeer::emptyString())', |
| 830 'ByteString': 'DartStringAdapter(DartStringPeer::emptyString())', |
| 831 'ScalarValueString': 'DartStringAdapter(DartStringPeer::emptyString())', |
| 832 'boolean': 'false', |
| 833 'float': '0.0f', |
| 834 'unrestricted float': '0.0f', |
| 835 'double': '0.0', |
| 836 'unrestricted double': '0.0', |
| 837 'byte': '0', |
| 838 'octet': '0', |
| 839 'short': '0', |
| 840 'unsigned short': '0', |
| 841 'long': '0', |
| 842 'unsigned long': '0', |
| 843 'long long': '0', |
| 844 'unsigned long long': '0', |
| 845 'Dictionary': 'Dictionary()', |
| 846 'ScriptValue': 'DartUtilities::dartToScriptValueWithNullCheck(Dart_Null())', |
| 847 'MediaQueryListListener': 'nullptr', |
| 848 'NodeFilter': 'nullptr', |
| 849 'SerializedScriptValue': 'nullptr', |
| 850 'XPathNSResolver': 'nullptr', |
| 851 } |
| 852 |
| 853 |
| 854 def default_cpp_value_for_cpp_type(idl_type): |
| 855 idl_type = idl_type.preprocessed_type |
| 856 add_includes_for_type(idl_type) |
| 857 base_idl_type = idl_type.base_type |
| 858 if base_idl_type in CPP_DEFAULT_VALUE_FOR_CPP_TYPE: |
| 859 return CPP_DEFAULT_VALUE_FOR_CPP_TYPE[base_idl_type] |
| 860 if base_idl_type in NON_WRAPPER_TYPES: |
| 861 return 'nullptr' |
| 862 format_str = 'Dart{idl_type}::toNativeWithNullCheck(Dart_Null(), exception)' |
| 863 return format_str.format(idl_type=idl_type) |
| 864 |
| 758 | 865 |
| 759 # Override idl_type.name to not suffix orNull to the name, in Dart we always | 866 # Override idl_type.name to not suffix orNull to the name, in Dart we always |
| 760 # test for null e.g., | 867 # test for null e.g., |
| 761 # | 868 # |
| 762 # bool isNull = false; | 869 # bool isNull = false; |
| 763 # TYPE* result = receiver->GETTER(isNull); | 870 # TYPE* result = receiver->GETTER(isNull); |
| 764 # if (isNull) | 871 # if (isNull) |
| 765 # return; | 872 # return; |
| 766 # | 873 # |
| 767 def dart_name(idl_type): | 874 def dart_name(idl_type): |
| 768 """Return type name. | 875 """Return type name. |
| 769 | 876 |
| 770 http://heycam.github.io/webidl/#dfn-type-name | 877 http://heycam.github.io/webidl/#dfn-type-name |
| 771 """ | 878 """ |
| 772 base_type = idl_type.base_type | 879 base_type = idl_type.base_type |
| 773 base_type_name = TYPE_NAMES.get(base_type, base_type) | 880 base_type_name = TYPE_NAMES.get(base_type, base_type) |
| 774 if idl_type.native_array_element_type: | 881 if idl_type.native_array_element_type: |
| 775 return idl_type.inner_name() | 882 return idl_type.inner_name() |
| 776 return base_type_name | 883 return base_type_name |
| 777 | 884 |
| 778 IdlType.name = property(dart_name) | 885 IdlType.name = property(dart_name) |
| 779 IdlUnionType.name = property(dart_name) | 886 IdlUnionType.name = property(dart_name) |
| 780 | 887 |
| 781 | 888 |
| 782 def typechecked_interface(extended_attributes): | |
| 783 return ('TypeChecking' in extended_attributes and\ | |
| 784 DartUtilities.extended_attribute_value_contains(extended_attributes[
'TypeChecking'], 'Interface')) | |
| 785 | |
| 786 | |
| 787 def typechecked_argument(idl_type, interface_extended_attributes, extended_attri
butes): | |
| 788 return (idl_type.is_wrapper_type and | |
| 789 (typechecked_interface(interface_extended_attributes) or | |
| 790 (typechecked_interface(extended_attributes)))) | |
| 791 | |
| 792 | |
| 793 # If True use the WithNullCheck version when converting. | 889 # If True use the WithNullCheck version when converting. |
| 794 def allow_null(idl_type, interface_extended_attributes, extended_attributes): | 890 def allow_null(idl_type, extended_attributes, has_type_checking_interface): |
| 795 if idl_type.base_type in ('DOMString', 'ByteString', 'ScalarValueString'): | 891 if idl_type.base_type in ('DOMString', 'ByteString', 'ScalarValueString'): |
| 796 # This logic is in cpp_types in v8_types.py, since they handle | 892 # This logic is in cpp_types in v8_types.py, since they handle |
| 797 # this using the V8StringResource type. We handle it here | 893 # this using the V8StringResource type. We handle it here |
| 798 if (extended_attributes.get('TreatNullAs') == 'NullString' or | 894 if (extended_attributes.get('TreatNullAs') == 'NullString' or |
| 799 extended_attributes.get('TreatUndefinedAs') == 'NullString'): | 895 extended_attributes.get('TreatUndefinedAs') == 'NullString'): |
| 800 return True | 896 return True |
| 801 | 897 |
| 802 if extended_attributes.get('Default') == 'NullString': | 898 if extended_attributes.get('Default') == 'NullString': |
| 803 return True | 899 return True |
| 804 | 900 |
| 805 if extended_attributes.get('Default') == 'Undefined': | 901 if extended_attributes.get('Default') == 'Undefined': |
| 806 return True | 902 return True |
| 807 | 903 |
| 808 if idl_type.is_nullable: | 904 if idl_type.is_nullable: |
| 809 return True | 905 return True |
| 810 | 906 |
| 811 return False | 907 return False |
| 812 else: | 908 else: |
| 813 # This logic is implemented in the methods.cpp template in V8 | 909 # This logic is implemented in the methods.cpp template in V8 |
| 814 if (idl_type.is_nullable or | 910 if (idl_type.is_nullable or not has_type_checking_interface): |
| 815 (not typechecked_argument(idl_type, interface_extended_attributes, ex
tended_attributes))): | |
| 816 return True | 911 return True |
| 817 | 912 |
| 818 if extended_attributes.get('Default') == 'Undefined': | 913 if extended_attributes.get('Default') == 'Undefined': |
| 819 return True | 914 return True |
| 820 | 915 |
| 821 return False | 916 return False |
| 917 |
| 918 |
| 919 # If True use the WithEmptyCheck version when converting. |
| 920 def allow_empty(idl_type, extended_attributes): |
| 921 if idl_type.base_type in ('DOMString', 'ByteString', 'ScalarValueString'): |
| 922 # This logic is in cpp_types in v8_types.py, since they handle |
| 923 # this using the V8StringResource type. We handle it here |
| 924 if (extended_attributes.get('TreatNullAs') == 'EmptyString' or |
| 925 extended_attributes.get('TreatUndefinedAs') == 'EmptyString'): |
| 926 return True |
| 927 |
| 928 if extended_attributes.get('Default') == 'EmptyString': |
| 929 return True |
| 930 |
| 931 return False |
| OLD | NEW |