Index: bindings/scripts/v8_types.py |
diff --git a/bindings/scripts/v8_types.py b/bindings/scripts/v8_types.py |
index a332f4a0bbc5300c43e4ccb5d6e4fcb16fbe1539..f5c92bcfc28ebc2c4a955765cc627b3c43accbd1 100644 |
--- a/bindings/scripts/v8_types.py |
+++ b/bindings/scripts/v8_types.py |
@@ -55,26 +55,32 @@ NON_WRAPPER_TYPES = frozenset([ |
'NodeFilter', |
'SerializedScriptValue', |
]) |
-TYPED_ARRAYS = { |
- # (cpp_type, v8_type), used by constructor templates |
- 'ArrayBuffer': None, |
- 'ArrayBufferView': None, |
- 'Float32Array': ('float', 'v8::kExternalFloatArray'), |
- 'Float64Array': ('double', 'v8::kExternalDoubleArray'), |
- 'Int8Array': ('signed char', 'v8::kExternalByteArray'), |
- 'Int16Array': ('short', 'v8::kExternalShortArray'), |
- 'Int32Array': ('int', 'v8::kExternalIntArray'), |
- 'Uint8Array': ('unsigned char', 'v8::kExternalUnsignedByteArray'), |
- 'Uint8ClampedArray': ('unsigned char', 'v8::kExternalPixelArray'), |
- 'Uint16Array': ('unsigned short', 'v8::kExternalUnsignedShortArray'), |
- 'Uint32Array': ('unsigned int', 'v8::kExternalUnsignedIntArray'), |
-} |
+TYPED_ARRAY_TYPES = frozenset([ |
+ 'Float32Array', |
+ 'Float64Array', |
+ 'Int8Array', |
+ 'Int16Array', |
+ 'Int32Array', |
+ 'Uint8Array', |
+ 'Uint8ClampedArray', |
+ 'Uint16Array', |
+ 'Uint32Array', |
+]) |
+ARRAY_BUFFER_AND_VIEW_TYPES = TYPED_ARRAY_TYPES.union(frozenset([ |
+ 'ArrayBuffer', |
+ 'ArrayBufferView', |
+ 'DataView', |
+])) |
-IdlType.is_typed_array_element_type = property( |
- lambda self: self.base_type in TYPED_ARRAYS) |
+IdlType.is_array_buffer_or_view = property( |
+ lambda self: self.base_type in ARRAY_BUFFER_AND_VIEW_TYPES) |
+ |
+IdlType.is_typed_array = property( |
+ lambda self: self.base_type in TYPED_ARRAY_TYPES) |
IdlType.is_wrapper_type = property( |
lambda self: (self.is_interface_type and |
+ not self.is_callback_interface and |
self.base_type not in NON_WRAPPER_TYPES)) |
@@ -107,7 +113,7 @@ CPP_SPECIAL_CONVERSION_RULES = { |
'Promise': 'ScriptPromise', |
'ScriptValue': 'ScriptValue', |
# FIXME: Eliminate custom bindings for XPathNSResolver http://crbug.com/345529 |
- 'XPathNSResolver': 'RefPtrWillBeRawPtr<XPathNSResolver>', |
+ 'XPathNSResolver': 'RawPtr<XPathNSResolver>', |
'boolean': 'bool', |
'unrestricted double': 'double', |
'unrestricted float': 'float', |
@@ -176,15 +182,28 @@ def cpp_type(idl_type, extended_attributes=None, raw_type=False, used_as_rvalue_ |
return 'String' |
return 'V8StringResource<%s>' % string_mode() |
- if idl_type.is_typed_array_element_type and raw_type: |
- return base_idl_type + '*' |
+ if idl_type.is_array_buffer_or_view and raw_type: |
+ return idl_type.implemented_as + '*' |
if idl_type.is_interface_type: |
implemented_as_class = idl_type.implemented_as |
- if raw_type: |
+ if raw_type or (used_as_rvalue_type and idl_type.is_garbage_collected): |
return implemented_as_class + '*' |
new_type = 'Member' if used_in_cpp_sequence else 'RawPtr' |
ptr_type = cpp_ptr_type(('PassRefPtr' if used_as_rvalue_type else 'RefPtr'), new_type, idl_type.gc_type) |
return cpp_template_type(ptr_type, implemented_as_class) |
+ if idl_type.is_dictionary: |
+ return base_idl_type |
+ if idl_type.is_union_type: |
+ # Avoid "AOrNullOrB" for cpp type of (A? or B) because we generate |
+ # V8AOrBOrNull to handle nulle for (A? or B), (A or B?) and (A or B)? |
+ def member_cpp_name(idl_type): |
+ if idl_type.is_nullable: |
+ return idl_type.inner_type.name |
+ return idl_type.name |
+ idl_type_name = "Or".join(member_cpp_name(member) |
+ for member in idl_type.member_types) |
+ return 'const %s&' % idl_type_name if used_as_rvalue_type else idl_type_name |
+ |
# Default, assume native type is a pointer with same type name as idl type |
return base_idl_type + '*' |
@@ -204,9 +223,6 @@ def cpp_type_initializer(idl_type): |
return ' = 0' |
if base_idl_type == 'boolean': |
return ' = false' |
- if idl_type.base_type == 'Promise': |
- return '(nullptr)' |
- |
if (base_idl_type in NON_WRAPPER_TYPES or |
base_idl_type in CPP_SPECIAL_CONVERSION_RULES or |
base_idl_type == 'any' or |
@@ -216,23 +232,11 @@ def cpp_type_initializer(idl_type): |
return ' = nullptr' |
-def cpp_type_union(idl_type, extended_attributes=None, raw_type=False): |
- # FIXME: Need to revisit the design of union support. |
- # http://crbug.com/240176 |
- return None |
- |
- |
-def cpp_type_initializer_union(idl_type): |
- return (member_type.cpp_type_initializer for member_type in idl_type.member_types) |
- |
- |
# Allow access as idl_type.cpp_type if no arguments |
IdlTypeBase.cpp_type = property(cpp_type) |
IdlTypeBase.cpp_type_initializer = property(cpp_type_initializer) |
IdlTypeBase.cpp_type_args = cpp_type |
-IdlUnionType.cpp_type = property(cpp_type_union) |
-IdlUnionType.cpp_type_initializer = property(cpp_type_initializer_union) |
-IdlUnionType.cpp_type_args = cpp_type_union |
+IdlUnionType.cpp_type_initializer = '' |
IdlArrayOrSequenceType.native_array_element_type = property( |
@@ -240,11 +244,8 @@ IdlArrayOrSequenceType.native_array_element_type = property( |
def cpp_template_type(template, inner_type): |
- """Returns C++ template specialized to type, with space added if needed.""" |
- if inner_type.endswith('>'): |
- format_string = '{template}<{inner_type} >' |
- else: |
- format_string = '{template}<{inner_type}>' |
+ """Returns C++ template specialized to type.""" |
+ format_string = '{template}<{inner_type}>' |
return format_string.format(template=template, inner_type=inner_type) |
@@ -310,7 +311,7 @@ IdlType.set_will_be_garbage_collected_types = classmethod( |
def gc_type(idl_type): |
- if idl_type.is_garbage_collected: |
+ if idl_type.is_garbage_collected or idl_type.is_dictionary or idl_type.is_union_type: |
return 'GarbageCollectedObject' |
if idl_type.is_will_be_garbage_collected: |
return 'WillBeGarbageCollectedObject' |
@@ -319,6 +320,17 @@ def gc_type(idl_type): |
IdlTypeBase.gc_type = property(gc_type) |
+def is_traceable(idl_type): |
+ return (idl_type.is_garbage_collected |
+ or idl_type.is_will_be_garbage_collected |
+ or idl_type.is_dictionary) |
+ |
+IdlTypeBase.is_traceable = property(is_traceable) |
+IdlUnionType.is_traceable = property(lambda self: True) |
+IdlArrayOrSequenceType.is_traceable = property( |
+ lambda self: self.element_type.is_traceable) |
+ |
+ |
################################################################################ |
# Includes |
################################################################################ |
@@ -332,7 +344,7 @@ INCLUDES_FOR_TYPE = { |
'Dictionary': set(['bindings/core/v8/Dictionary.h']), |
'EventHandler': set(['bindings/core/v8/V8AbstractEventListener.h', |
'bindings/core/v8/V8EventListenerList.h']), |
- 'EventListener': set(['bindings/common/BindingSecurity.h', |
+ 'EventListener': set(['bindings/core/v8/BindingSecurity.h', |
'bindings/core/v8/V8EventListenerList.h', |
'core/frame/LocalDOMWindow.h']), |
'HTMLCollection': set(['bindings/core/v8/V8HTMLCollection.h', |
@@ -347,14 +359,16 @@ INCLUDES_FOR_TYPE = { |
'core/dom/NodeList.h', |
'core/dom/StaticNodeList.h', |
'core/html/LabelsNodeList.h']), |
- 'Promise': set(['bindings/core/v8/V8ScriptPromise.h']), |
- 'SerializedScriptValue': set(['bindings/core/v8/SerializedScriptValue.h']), |
- 'ScriptValue': set(['bindings/common/ScriptValue.h']), |
+ 'Promise': set(['bindings/core/v8/ScriptPromise.h']), |
+ 'SerializedScriptValue': set(['bindings/core/v8/SerializedScriptValue.h', |
+ 'bindings/core/v8/SerializedScriptValueFactory.h']), |
+ 'ScriptValue': set(['bindings/core/v8/ScriptValue.h']), |
} |
-def includes_for_type(idl_type): |
+def includes_for_type(idl_type, extended_attributes=None): |
idl_type = idl_type.preprocessed_type |
+ extended_attributes = extended_attributes or {} |
# Simple types |
base_idl_type = idl_type.base_type |
@@ -362,8 +376,6 @@ def includes_for_type(idl_type): |
return INCLUDES_FOR_TYPE[base_idl_type] |
if idl_type.is_basic_type: |
return set() |
- if idl_type.is_typed_array_element_type: |
- return set(['bindings/core/v8/custom/V8%sCustom.h' % base_idl_type]) |
if base_idl_type.endswith('ConstructorConstructor'): |
# FIXME: rename to NamedConstructor |
# FIXME: replace with a [NamedConstructorAttribute] extended attribute |
@@ -379,22 +391,30 @@ def includes_for_type(idl_type): |
return set(['bindings/%s/v8/V8%s.h' % (component_dir[base_idl_type], |
base_idl_type)]) |
-IdlType.includes_for_type = property(includes_for_type) |
-IdlUnionType.includes_for_type = property( |
- lambda self: set.union(*[member_type.includes_for_type |
- for member_type in self.member_types])) |
-IdlArrayOrSequenceType.includes_for_type = property( |
- lambda self: self.element_type.includes_for_type) |
+IdlType.includes_for_type = includes_for_type |
-def add_includes_for_type(idl_type): |
- includes.update(idl_type.includes_for_type) |
+def includes_for_union_type(idl_type, extended_attributes=None): |
+ return set.union(*[member_type.includes_for_type(extended_attributes) |
+ for member_type in idl_type.member_types]) |
+ |
+IdlUnionType.includes_for_type = includes_for_union_type |
+ |
+ |
+def includes_for_array_or_sequence_type(idl_type, extended_attributes=None): |
+ return idl_type.element_type.includes_for_type(extended_attributes) |
+ |
+IdlArrayOrSequenceType.includes_for_type = includes_for_array_or_sequence_type |
+ |
+ |
+def add_includes_for_type(idl_type, extended_attributes=None): |
+ includes.update(idl_type.includes_for_type(extended_attributes)) |
IdlTypeBase.add_includes_for_type = add_includes_for_type |
def includes_for_interface(interface_name): |
- return IdlType(interface_name).includes_for_type |
+ return IdlType(interface_name).includes_for_type() |
def add_includes_for_interface(interface_name): |
@@ -411,7 +431,7 @@ IdlTypeBase.impl_should_use_nullable_container = property( |
def impl_includes_for_type(idl_type, interfaces_info): |
includes_for_type = set() |
if idl_type.impl_should_use_nullable_container: |
- includes_for_type.add('bindings/common/Nullable.h') |
+ includes_for_type.add('bindings/core/v8/Nullable.h') |
idl_type = idl_type.preprocessed_type |
native_array_element_type = idl_type.native_array_element_type |
@@ -425,12 +445,23 @@ def impl_includes_for_type(idl_type, interfaces_info): |
includes_for_type.add('wtf/text/WTFString.h') |
if base_idl_type in interfaces_info: |
interface_info = interfaces_info[idl_type.base_type] |
- includes_for_type.add(interface_info['include_path']) |
+ if interface_info['include_path']: |
+ includes_for_type.add(interface_info['include_path']) |
if base_idl_type in INCLUDES_FOR_TYPE: |
includes_for_type.update(INCLUDES_FOR_TYPE[base_idl_type]) |
+ if idl_type.is_typed_array: |
+ return set(['core/dom/DOMTypedArray.h']) |
+ return includes_for_type |
+ |
+ |
+def impl_includes_for_type_union(idl_type, interfaces_info): |
+ includes_for_type = set() |
+ for member_type in idl_type.member_types: |
+ includes_for_type.update(member_type.impl_includes_for_type(interfaces_info)) |
return includes_for_type |
IdlTypeBase.impl_includes_for_type = impl_includes_for_type |
+IdlUnionType.impl_includes_for_type = impl_includes_for_type_union |
component_dir = {} |
@@ -446,42 +477,44 @@ def set_component_dirs(new_component_dirs): |
V8_VALUE_TO_CPP_VALUE = { |
# Basic |
- 'Date': 'toCoreDate({v8_value})', |
+ 'Date': 'toCoreDate({isolate}, {v8_value})', |
'DOMString': '{v8_value}', |
- 'ByteString': 'toByteString({arguments})', |
- 'ScalarValueString': 'toScalarValueString({arguments})', |
- 'boolean': '{v8_value}->BooleanValue()', |
- 'float': 'toFloat({arguments})', |
- 'unrestricted float': 'toFloat({arguments})', |
- 'double': 'toDouble({arguments})', |
- 'unrestricted double': 'toDouble({arguments})', |
- 'byte': 'toInt8({arguments})', |
- 'octet': 'toUInt8({arguments})', |
- 'short': 'toInt16({arguments})', |
- 'unsigned short': 'toUInt16({arguments})', |
- 'long': 'toInt32({arguments})', |
- 'unsigned long': 'toUInt32({arguments})', |
- 'long long': 'toInt64({arguments})', |
- 'unsigned long long': 'toUInt64({arguments})', |
+ 'ByteString': 'toByteString({isolate}, {arguments})', |
+ 'USVString': 'toUSVString({isolate}, {arguments})', |
+ 'boolean': 'toBoolean({isolate}, {arguments})', |
+ 'float': 'toRestrictedFloat({isolate}, {arguments})', |
+ 'unrestricted float': 'toFloat({isolate}, {arguments})', |
+ 'double': 'toRestrictedDouble({isolate}, {arguments})', |
+ 'unrestricted double': 'toDouble({isolate}, {arguments})', |
+ 'byte': 'toInt8({isolate}, {arguments})', |
+ 'octet': 'toUInt8({isolate}, {arguments})', |
+ 'short': 'toInt16({isolate}, {arguments})', |
+ 'unsigned short': 'toUInt16({isolate}, {arguments})', |
+ 'long': 'toInt32({isolate}, {arguments})', |
+ 'unsigned long': 'toUInt32({isolate}, {arguments})', |
+ 'long long': 'toInt64({isolate}, {arguments})', |
+ 'unsigned long long': 'toUInt64({isolate}, {arguments})', |
# Interface types |
- 'Dictionary': 'Dictionary({v8_value}, {isolate})', |
- 'EventTarget': 'V8DOMWrapper::isDOMWrapper({v8_value}) ? toWrapperTypeInfo(v8::Handle<v8::Object>::Cast({v8_value}))->toEventTarget(v8::Handle<v8::Object>::Cast({v8_value})) : 0', |
- 'NodeFilter': 'toNodeFilter({v8_value}, info.Holder(), V8ScriptState::current({isolate}))', |
- 'Promise': 'V8ScriptPromise::cast(V8ScriptState::current({isolate}), {v8_value})', |
- 'SerializedScriptValue': 'SerializedScriptValue::create({v8_value}, 0, 0, exceptionState, {isolate})', |
- 'ScriptValue': 'ScriptValue(V8ScriptState::current({isolate}), {v8_value})', |
- 'Window': 'toDOMWindow({v8_value}, {isolate})', |
- 'XPathNSResolver': 'toXPathNSResolver({v8_value}, {isolate})', |
+ 'Dictionary': 'Dictionary({v8_value}, {isolate}, exceptionState)', |
+ 'EventTarget': 'toEventTarget({isolate}, {v8_value})', |
+ 'NodeFilter': 'toNodeFilter({v8_value}, info.Holder(), ScriptState::current({isolate}))', |
+ 'Promise': 'ScriptPromise::cast(ScriptState::current({isolate}), {v8_value})', |
+ 'SerializedScriptValue': 'SerializedScriptValueFactory::instance().create({isolate}, {v8_value}, 0, 0, exceptionState)', |
+ 'ScriptValue': 'ScriptValue(ScriptState::current({isolate}), {v8_value})', |
+ 'Window': 'toDOMWindow({isolate}, {v8_value})', |
+ 'XPathNSResolver': 'toXPathNSResolver(ScriptState::current({isolate}), {v8_value})', |
} |
def v8_conversion_needs_exception_state(idl_type): |
return (idl_type.is_numeric_type or |
+ idl_type.is_enum or |
idl_type.is_dictionary or |
- idl_type.name in ('ByteString', 'ScalarValueString', 'SerializedScriptValue')) |
+ idl_type.name in ('Boolean', 'ByteString', 'Dictionary', 'USVString', 'SerializedScriptValue')) |
IdlType.v8_conversion_needs_exception_state = property(v8_conversion_needs_exception_state) |
IdlArrayOrSequenceType.v8_conversion_needs_exception_state = True |
+IdlUnionType.v8_conversion_needs_exception_state = True |
TRIVIAL_CONVERSIONS = frozenset([ |
@@ -504,42 +537,45 @@ def v8_conversion_is_trivial(idl_type): |
IdlType.v8_conversion_is_trivial = property(v8_conversion_is_trivial) |
-def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index, isolate): |
+def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, variable_name, index, isolate, restricted_float=False): |
if idl_type.name == 'void': |
return '' |
# Array or sequence types |
native_array_element_type = idl_type.native_array_element_type |
if native_array_element_type: |
- return v8_value_to_cpp_value_array_or_sequence(native_array_element_type, v8_value, index) |
+ return v8_value_to_cpp_value_array_or_sequence(native_array_element_type, v8_value, index, isolate) |
# Simple types |
idl_type = idl_type.preprocessed_type |
- add_includes_for_type(idl_type) |
- base_idl_type = idl_type.base_type |
- |
- if 'EnforceRange' in extended_attributes: |
- arguments = ', '.join([v8_value, 'EnforceRange', 'exceptionState']) |
- elif 'Clamp' in extended_attributes: |
- arguments = ', '.join([v8_value, 'Clamp', 'exceptionState']) |
+ base_idl_type = idl_type.as_union_type.name if idl_type.is_union_type else idl_type.base_type |
+ |
+ if idl_type.is_integer_type: |
+ configuration = 'NormalConversion' |
+ if 'EnforceRange' in extended_attributes: |
+ configuration = 'EnforceRange' |
+ elif 'Clamp' in extended_attributes: |
+ configuration = 'Clamp' |
+ arguments = ', '.join([v8_value, configuration, 'exceptionState']) |
elif idl_type.v8_conversion_needs_exception_state: |
arguments = ', '.join([v8_value, 'exceptionState']) |
else: |
arguments = v8_value |
- |
if base_idl_type in V8_VALUE_TO_CPP_VALUE: |
cpp_expression_format = V8_VALUE_TO_CPP_VALUE[base_idl_type] |
- elif idl_type.is_typed_array_element_type: |
+ elif idl_type.is_array_buffer_or_view: |
cpp_expression_format = ( |
'{v8_value}->Is{idl_type}() ? ' |
- 'V8{idl_type}::toImpl(v8::Handle<v8::{idl_type}>::Cast({v8_value})) : 0') |
- elif idl_type.is_dictionary: |
- cpp_expression_format = 'V8{idl_type}::toImpl({isolate}, {v8_value}, exceptionState)' |
+ 'V8{idl_type}::toImpl(v8::Local<v8::{idl_type}>::Cast({v8_value})) : 0') |
+ elif idl_type.use_output_parameter_for_result: |
+ if idl_type.includes_nullable_type: |
+ base_idl_type = idl_type.cpp_type + 'OrNull' |
+ cpp_expression_format = 'V8{idl_type}::toImpl({isolate}, {v8_value}, {variable_name}, exceptionState)' |
else: |
cpp_expression_format = ( |
'V8{idl_type}::toImplWithTypeCheck({isolate}, {v8_value})') |
- return cpp_expression_format.format(arguments=arguments, idl_type=base_idl_type, v8_value=v8_value, isolate=isolate) |
+ return cpp_expression_format.format(arguments=arguments, idl_type=base_idl_type, v8_value=v8_value, variable_name=variable_name, isolate=isolate) |
def v8_value_to_cpp_value_array_or_sequence(native_array_element_type, v8_value, index, isolate='info.GetIsolate()'): |
@@ -554,87 +590,102 @@ def v8_value_to_cpp_value_array_or_sequence(native_array_element_type, v8_value, |
this_cpp_type = None |
ref_ptr_type = cpp_ptr_type('RefPtr', 'Member', native_array_element_type.gc_type) |
expression_format = '(to{ref_ptr_type}NativeArray<{native_array_element_type}, V8{native_array_element_type}>({v8_value}, {index}, {isolate}, exceptionState))' |
- add_includes_for_type(native_array_element_type) |
else: |
ref_ptr_type = None |
this_cpp_type = native_array_element_type.cpp_type |
- expression_format = 'toImplArray<{cpp_type}>({v8_value}, {index}, {isolate}, exceptionState)' |
+ if native_array_element_type.is_dictionary or native_array_element_type.is_union_type: |
+ vector_type = 'HeapVector' |
+ else: |
+ vector_type = 'Vector' |
+ expression_format = 'toImplArray<%s<{cpp_type}>>({v8_value}, {index}, {isolate}, exceptionState)' % vector_type |
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_type, v8_value=v8_value, isolate=isolate) |
return expression |
-def v8_value_to_local_cpp_value(idl_type, extended_attributes, v8_value, variable_name, index=None, declare_variable=True, isolate='info.GetIsolate()', used_in_private_script=False, return_promise=False): |
+# FIXME: this function should be refactored, as this takes too many flags. |
+def v8_value_to_local_cpp_value(idl_type, extended_attributes, v8_value, variable_name, index=None, declare_variable=True, isolate='info.GetIsolate()', bailout_return_value=None, use_exception_state=False, restricted_float=False): |
"""Returns an expression that converts a V8 value to a C++ value and stores it as a local value.""" |
- # FIXME: Support union type. |
- if idl_type.is_union_type: |
- return '/* no V8 -> C++ conversion for IDL union type: %s */' % idl_type.name |
- |
this_cpp_type = idl_type.cpp_type_args(extended_attributes=extended_attributes, raw_type=True) |
idl_type = idl_type.preprocessed_type |
- if idl_type.base_type in ('void', 'object', 'EventHandler', 'EventListener'): |
- return '/* no V8 -> C++ conversion for IDL type: %s */' % idl_type.name |
+ cpp_value = v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, variable_name, index, isolate, restricted_float=restricted_float) |
- cpp_value = v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index, isolate) |
- if idl_type.is_string_type or idl_type.v8_conversion_needs_exception_state: |
- # Types that need error handling and use one of a group of (C++) macros |
- # to take care of this. |
+ # Optional expression that returns a value to be assigned to the local variable. |
+ assign_expression = None |
+ # Optional void expression executed unconditionally. |
+ set_expression = None |
+ # Optional expression that returns true if the conversion fails. |
+ check_expression = None |
+ # Optional expression used as the return value when returning. Only |
+ # meaningful if 'check_expression' is not None. |
+ return_expression = bailout_return_value |
- args = [variable_name, cpp_value] |
+ if idl_type.is_string_type or idl_type.v8_conversion_needs_exception_state: |
+ # Types for which conversion can fail and that need error handling. |
- if idl_type.v8_conversion_needs_exception_state: |
- macro = 'TONATIVE_DEFAULT_EXCEPTIONSTATE' if used_in_private_script else 'TONATIVE_VOID_EXCEPTIONSTATE' |
- elif return_promise: |
- macro = 'TOSTRING_VOID_EXCEPTIONSTATE' |
+ if use_exception_state: |
+ check_expression = 'exceptionState.hadException()' |
else: |
- macro = 'TOSTRING_DEFAULT' if used_in_private_script else 'TOSTRING_VOID' |
- |
- if macro.endswith('_EXCEPTIONSTATE'): |
- args.append('exceptionState') |
- |
- if used_in_private_script: |
- args.append('false') |
+ check_expression = 'exceptionState.throwIfNeeded()' |
- suffix = '' |
- |
- if return_promise: |
- suffix += '_PROMISE' |
- args.append('info') |
- if macro.endswith('_EXCEPTIONSTATE'): |
- args.append('V8ScriptState::current(%s)' % isolate) |
- |
- if declare_variable: |
- args.insert(0, this_cpp_type) |
+ if idl_type.is_dictionary or idl_type.is_union_type: |
+ set_expression = cpp_value |
else: |
- suffix += '_INTERNAL' |
- |
- return '%s(%s)' % (macro + suffix, ', '.join(args)) |
+ assign_expression = cpp_value |
+ # Note: 'not idl_type.v8_conversion_needs_exception_state' implies |
+ # 'idl_type.is_string_type', but there are types for which both are |
+ # true (ByteString and USVString), so using idl_type.is_string_type |
+ # as the condition here would be wrong. |
+ if not idl_type.v8_conversion_needs_exception_state: |
+ if use_exception_state: |
+ check_expression = '!%s.prepare(exceptionState)' % variable_name |
+ else: |
+ check_expression = '!%s.prepare()' % variable_name |
+ elif not idl_type.v8_conversion_is_trivial: |
+ return { |
+ 'error_message': 'no V8 -> C++ conversion for IDL type: %s' % idl_type.name |
+ } |
+ else: |
+ assign_expression = cpp_value |
# Types that don't need error handling, and simply assign a value to the |
# local variable. |
- if not idl_type.v8_conversion_is_trivial: |
- raise Exception('unclassified V8 -> C++ conversion for IDL type: %s' % idl_type.name) |
- |
- assignment = '%s = %s' % (variable_name, cpp_value) |
- if declare_variable: |
- return '%s %s' % (this_cpp_type, assignment) |
- return assignment |
+ return { |
+ 'assign_expression': assign_expression, |
+ 'check_expression': check_expression, |
+ 'cpp_type': this_cpp_type, |
+ 'cpp_name': variable_name, |
+ 'declare_variable': declare_variable, |
+ 'return_expression': bailout_return_value, |
+ 'set_expression': set_expression, |
+ } |
IdlTypeBase.v8_value_to_local_cpp_value = v8_value_to_local_cpp_value |
+def use_output_parameter_for_result(idl_type): |
+ """True when methods/getters which return the given idl_type should |
+ take the output argument. |
+ """ |
+ return idl_type.is_dictionary or idl_type.is_union_type |
+ |
+IdlTypeBase.use_output_parameter_for_result = property(use_output_parameter_for_result) |
+ |
+ |
################################################################################ |
# C++ -> V8 |
################################################################################ |
def preprocess_idl_type(idl_type): |
+ if idl_type.is_nullable: |
+ return IdlNullableType(idl_type.inner_type.preprocessed_type) |
if idl_type.is_enum: |
# Enumerations are internally DOMStrings |
return IdlType('DOMString') |
- if (idl_type.name in ['Any', 'Object'] or idl_type.is_callback_function): |
+ if idl_type.base_type in ['any', 'object'] or idl_type.is_callback_function: |
return IdlType('ScriptValue') |
return idl_type |
@@ -675,15 +726,17 @@ def v8_conversion_type(idl_type, extended_attributes): |
""" |
extended_attributes = extended_attributes or {} |
- # FIXME: Support union type. |
- if idl_type.is_union_type: |
- return '' |
+ # Nullable dictionaries need to be handled differently than either |
+ # non-nullable dictionaries or unions. |
+ if idl_type.is_dictionary and idl_type.is_nullable: |
+ return 'NullableDictionary' |
+ |
+ if idl_type.is_dictionary or idl_type.is_union_type: |
+ return 'DictionaryOrUnion' |
# Array or sequence types |
native_array_element_type = idl_type.native_array_element_type |
if native_array_element_type: |
- if native_array_element_type.is_interface_type: |
- add_includes_for_type(native_array_element_type) |
return 'array' |
# Simple types |
@@ -706,9 +759,11 @@ def v8_conversion_type(idl_type, extended_attributes): |
raise 'Unrecognized TreatReturnedNullStringAs value: "%s"' % treat_returned_null_string_as |
if idl_type.is_basic_type or base_idl_type == 'ScriptValue': |
return base_idl_type |
+ # Generic dictionary type |
+ if base_idl_type == 'Dictionary': |
+ return 'Dictionary' |
# Data type with potential additional includes |
- add_includes_for_type(idl_type) |
if base_idl_type in V8_SET_RETURN_VALUE: # Special v8SetReturnValue treatment |
return base_idl_type |
@@ -724,7 +779,7 @@ V8_SET_RETURN_VALUE = { |
'unsigned': 'v8SetReturnValueUnsigned(info, {cpp_value})', |
'DOMString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsolate())', |
'ByteString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsolate())', |
- 'ScalarValueString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsolate())', |
+ 'USVString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsolate())', |
# [TreatReturnedNullStringAs] |
'StringOrNull': 'v8SetReturnValueStringOrNull(info, {cpp_value}, info.GetIsolate())', |
'StringOrUndefined': 'v8SetReturnValueStringOrUndefined(info, {cpp_value}, info.GetIsolate())', |
@@ -745,14 +800,30 @@ V8_SET_RETURN_VALUE = { |
'DOMWrapperForMainWorld': 'v8SetReturnValueForMainWorld(info, WTF::getPtr({cpp_value}))', |
'DOMWrapperFast': 'v8SetReturnValueFast(info, WTF::getPtr({cpp_value}), {script_wrappable})', |
'DOMWrapperDefault': 'v8SetReturnValue(info, {cpp_value})', |
+ # Note that static attributes and operations do not check whether |this| is |
+ # an instance of the interface nor |this|'s creation context is the same as |
+ # the current context. So we must always use the current context as the |
+ # creation context of the DOM wrapper for the return value. |
+ 'DOMWrapperStatic': 'v8SetReturnValue(info, {cpp_value}, info.GetIsolate()->GetCurrentContext()->Global())', |
+ # Generic dictionary type |
+ 'Dictionary': 'v8SetReturnValue(info, {cpp_value})', |
+ 'DictionaryStatic': '#error not implemented yet', |
+ # Nullable dictionaries |
+ 'NullableDictionary': 'v8SetReturnValue(info, result.get())', |
+ 'NullableDictionaryStatic': '#error not implemented yet', |
+ # Union types or dictionaries |
+ 'DictionaryOrUnion': 'v8SetReturnValue(info, result)', |
+ 'DictionaryOrUnionStatic': '#error not implemented yet', |
} |
-def v8_set_return_value(idl_type, cpp_value, extended_attributes=None, script_wrappable='', release=False, for_main_world=False): |
+def v8_set_return_value(idl_type, cpp_value, extended_attributes=None, script_wrappable='', release=False, for_main_world=False, is_static=False): |
"""Returns a statement that converts a C++ value to a V8 value and sets it as a return value. |
""" |
def dom_wrapper_conversion_type(): |
+ if is_static: |
+ return 'DOMWrapperStatic' |
if not script_wrappable: |
return 'DOMWrapperDefault' |
if for_main_world: |
@@ -767,6 +838,8 @@ def v8_set_return_value(idl_type, cpp_value, extended_attributes=None, script_wr |
cpp_value = idl_type.cpp_value_to_v8_value(cpp_value, extended_attributes=extended_attributes) |
if this_v8_conversion_type == 'DOMWrapper': |
this_v8_conversion_type = dom_wrapper_conversion_type() |
+ if is_static and this_v8_conversion_type in ('Dictionary', 'NullableDictionary', 'DictionaryOrUnion'): |
+ this_v8_conversion_type += 'Static' |
format_string = V8_SET_RETURN_VALUE[this_v8_conversion_type] |
# FIXME: oilpan: Remove .release() once we remove all RefPtrs from generated code. |
@@ -776,27 +849,18 @@ def v8_set_return_value(idl_type, cpp_value, extended_attributes=None, script_wr |
return statement |
-def v8_set_return_value_union(idl_type, cpp_value, extended_attributes=None, script_wrappable='', release=False, for_main_world=False): |
- # FIXME: Need to revisit the design of union support. |
- # http://crbug.com/240176 |
- return None |
- |
- |
IdlTypeBase.v8_set_return_value = v8_set_return_value |
-IdlUnionType.v8_set_return_value = v8_set_return_value_union |
IdlType.release = property(lambda self: self.is_interface_type) |
-IdlUnionType.release = property( |
- lambda self: [member_type.is_interface_type |
- for member_type in self.member_types]) |
+IdlUnionType.release = False |
CPP_VALUE_TO_V8_VALUE = { |
# Built-in types |
- 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', |
+ 'Date': 'v8DateOrNaN({isolate}, {cpp_value})', |
'DOMString': 'v8String({isolate}, {cpp_value})', |
'ByteString': 'v8String({isolate}, {cpp_value})', |
- 'ScalarValueString': 'v8String({isolate}, {cpp_value})', |
+ 'USVString': 'v8String({isolate}, {cpp_value})', |
'boolean': 'v8Boolean({cpp_value}, {isolate})', |
'int': 'v8::Integer::New({isolate}, {cpp_value})', |
'unsigned': 'v8::Integer::NewFromUnsigned({isolate}, {cpp_value})', |
@@ -806,15 +870,27 @@ CPP_VALUE_TO_V8_VALUE = { |
'unrestricted double': 'v8::Number::New({isolate}, {cpp_value})', |
'void': 'v8Undefined()', |
# [TreatReturnedNullStringAs] |
- 'StringOrNull': '{cpp_value}.isNull() ? v8::Handle<v8::Value>(v8::Null({isolate})) : v8String({isolate}, {cpp_value})', |
+ 'StringOrNull': '{cpp_value}.isNull() ? v8::Local<v8::Value>(v8::Null({isolate})) : v8String({isolate}, {cpp_value})', |
'StringOrUndefined': '{cpp_value}.isNull() ? v8Undefined() : v8String({isolate}, {cpp_value})', |
# Special cases |
- 'EventHandler': '{cpp_value} ? v8::Handle<v8::Value>(V8AbstractEventListener::cast({cpp_value})->getListenerObject(impl->executionContext())) : v8::Handle<v8::Value>(v8::Null({isolate}))', |
+ 'Dictionary': '{cpp_value}.v8Value()', |
+ 'EventHandler': '{cpp_value} ? v8::Local<v8::Value>(V8AbstractEventListener::cast({cpp_value})->getListenerObject(impl->executionContext())) : v8::Local<v8::Value>(v8::Null({isolate}))', |
'ScriptValue': '{cpp_value}.v8Value()', |
- 'SerializedScriptValue': '{cpp_value} ? {cpp_value}->deserialize() : v8::Handle<v8::Value>(v8::Null({isolate}))', |
+ 'SerializedScriptValue': '{cpp_value} ? {cpp_value}->deserialize() : v8::Local<v8::Value>(v8::Null({isolate}))', |
# General |
- 'array': 'v8Array({cpp_value}, {creation_context}, {isolate})', |
+ 'array': 'toV8({cpp_value}, {creation_context}, {isolate})', |
'DOMWrapper': 'toV8({cpp_value}, {creation_context}, {isolate})', |
+ # Passing nullable dictionaries isn't a pattern currently used |
+ # anywhere in the web platform, and more work would be needed in |
+ # the code generator to distinguish between passing null, and |
+ # passing an object which happened to not contain any of the |
+ # dictionary's defined attributes. For now, don't define |
+ # NullableDictionary here, which will cause an exception to be |
+ # thrown during code generation if an argument to a method is a |
+ # nullable dictionary type. |
+ # |
+ # Union types or dictionaries |
+ 'DictionaryOrUnion': 'toV8({cpp_value}, {creation_context}, {isolate})', |
} |
@@ -833,12 +909,40 @@ IdlTypeBase.cpp_value_to_v8_value = cpp_value_to_v8_value |
def literal_cpp_value(idl_type, idl_literal): |
"""Converts an expression that is a valid C++ literal for this type.""" |
# FIXME: add validation that idl_type and idl_literal are compatible |
+ if idl_type.base_type in ('any', 'object') and idl_literal.is_null: |
+ return 'ScriptValue()' |
literal_value = str(idl_literal) |
if idl_type.base_type in CPP_UNSIGNED_TYPES: |
return literal_value + 'u' |
return literal_value |
+ |
+def union_literal_cpp_value(idl_type, idl_literal): |
+ if idl_literal.is_null: |
+ return idl_type.name + '()' |
+ elif idl_literal.idl_type == 'DOMString': |
+ member_type = idl_type.string_member_type |
+ elif idl_literal.idl_type in ('integer', 'float'): |
+ member_type = idl_type.numeric_member_type |
+ elif idl_literal.idl_type == 'boolean': |
+ member_type = idl_type.boolean_member_type |
+ else: |
+ raise ValueError('Unsupported literal type: ' + idl_literal.idl_type) |
+ |
+ return '%s::from%s(%s)' % (idl_type.name, member_type.name, |
+ member_type.literal_cpp_value(idl_literal)) |
+ |
+ |
+def array_or_sequence_literal_cpp_value(idl_type, idl_literal): |
+ # Only support empty arrays. |
+ if idl_literal.value == '[]': |
+ return cpp_type(idl_type) + '()' |
+ raise ValueError('Unsupported literal type: ' + idl_literal.idl_type) |
+ |
+ |
IdlType.literal_cpp_value = literal_cpp_value |
+IdlUnionType.literal_cpp_value = union_literal_cpp_value |
+IdlArrayOrSequenceType.literal_cpp_value = array_or_sequence_literal_cpp_value |
################################################################################ |
@@ -850,13 +954,14 @@ def cpp_type_has_null_value(idl_type): |
# - String types (String/AtomicString) represent null as a null string, |
# i.e. one for which String::isNull() returns true. |
# - Enum types, as they are implemented as Strings. |
- # - Wrapper types (raw pointer or RefPtr/PassRefPtr) represent null as |
+ # - Interface types (raw pointer or RefPtr/PassRefPtr) represent null as |
# a null pointer. |
- # - Dictionary types represent null as a null pointer. They are garbage |
- # collected so their type is raw pointer. |
- # - 'Object' type. We use ScriptValue for object type. |
- return (idl_type.is_string_type or idl_type.is_wrapper_type or |
- idl_type.is_enum or idl_type.is_dictionary or idl_type.base_type == 'object') |
+ # - Union types, as thier container classes can represent null value. |
+ # - 'Object' and 'any' type. We use ScriptValue for object type. |
+ return (idl_type.is_string_type or idl_type.is_interface_type or |
+ idl_type.is_enum or idl_type.is_union_type |
+ or idl_type.base_type == 'object' or idl_type.base_type == 'any' |
+ or idl_type.is_callback_function or idl_type.is_callback_interface) |
IdlTypeBase.cpp_type_has_null_value = property(cpp_type_has_null_value) |
@@ -874,3 +979,27 @@ def is_explicit_nullable(idl_type): |
IdlTypeBase.is_implicit_nullable = property(is_implicit_nullable) |
IdlUnionType.is_implicit_nullable = False |
IdlTypeBase.is_explicit_nullable = property(is_explicit_nullable) |
+ |
+ |
+def number_of_nullable_member_types_union(idl_type): |
+ # http://heycam.github.io/webidl/#dfn-number-of-nullable-member-types |
+ count = 0 |
+ for member in idl_type.member_types: |
+ if member.is_nullable: |
+ count += 1 |
+ member = member.inner_type |
+ if member.is_union_type: |
+ count += number_of_nullable_member_types_union(member) |
+ return count |
+ |
+IdlUnionType.number_of_nullable_member_types = property( |
+ number_of_nullable_member_types_union) |
+ |
+ |
+def includes_nullable_type_union(idl_type): |
+ # http://heycam.github.io/webidl/#dfn-includes-a-nullable-type |
+ return idl_type.number_of_nullable_member_types == 1 |
+ |
+IdlTypeBase.includes_nullable_type = False |
+IdlNullableType.includes_nullable_type = True |
+IdlUnionType.includes_nullable_type = property(includes_nullable_type_union) |