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

Unified Diff: Source/bindings/scripts/unstable/v8_types.py

Issue 24257003: IDL compiler: Types (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix copyright + license Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/bindings/scripts/unstable/v8_types.py
diff --git a/Source/bindings/scripts/unstable/v8_types.py b/Source/bindings/scripts/unstable/v8_types.py
index 041a6d8767f5ae8f8002e7b6d51cd21fd269f6ad..98f7ad54befaeeaf76cbdd904245bd67b3f2e687 100644
--- a/Source/bindings/scripts/unstable/v8_types.py
+++ b/Source/bindings/scripts/unstable/v8_types.py
@@ -35,37 +35,17 @@ Until then, please work on the Perl IDL compiler.
For details, see bug http://crbug.com/239771
"""
+import posixpath
import re
-CPP_TYPE_SAME_AS_IDL_TYPE = set([
- 'double',
- 'float',
- 'int', # FIXME: int is not an IDL type
- 'long long',
- 'unsigned long long',
-])
-CPP_INT_TYPES = set([
- 'byte',
- 'long',
- 'short',
-])
-CPP_UNSIGNED_TYPES = set([
- 'octet',
- 'unsigned int',
- 'unsigned long',
- 'unsigned short',
-])
-# Promise is not yet in the Web IDL spec but is going to be speced
-# as primitive types in the future.
-# Since V8 dosn't provide Promise primitive object currently,
-# PRIMITIVE_TYPES doesn't contain Promise.
-CPP_TYPE_SPECIAL_CONVERSION_RULES = {
- 'boolean': 'bool',
- 'DOMString': 'const String&',
- 'Promise': 'ScriptPromise',
-}
+import idl_definitions # for UnionType
+
+################################################################################
+# IDL types
+################################################################################
+
PRIMITIVE_TYPES = set([
- # http://www.w3.org/TR/WebIDL/#dfn-primitive-type
+ # http://www.w3.org/TR/WebIDL/#dfn-primitive-type
'boolean',
'float',
# unrestricted float is not supported
@@ -86,21 +66,27 @@ PRIMITIVE_TYPES = set([
'Date',
'void',
])
-CPP_VALUE_TO_V8_VALUE_DICT = {
- 'boolean': 'v8Boolean({cpp_value}, {isolate})',
- # long long and unsigned long long are not representable in ECMAScript.
- 'long long': 'v8::Number::New({isolate}, static_cast<double>({cpp_value}))',
- 'unsigned long long': 'v8::Number::New({isolate}, static_cast<double>({cpp_value}))',
- 'float': 'v8::Number::New({isolate}, {cpp_value})',
- 'double': 'v8::Number::New({isolate}, {cpp_value})',
- 'DOMTimeStamp': 'v8::Number::New({isolate}, static_cast<double>({cpp_value}))',
- 'DOMString': 'v8String({cpp_value}, {isolate})',
-}
-CPP_VALUE_TO_V8_VALUE_ARRAY_OR_SEQUENCE_TYPE = 'v8Array({cpp_value}, {isolate})'
-CPP_VALUE_TO_V8_VALUE_DEFAULT = 'toV8({cpp_value}, {creation_context}, {isolate})'
-V8_SET_RETURN_VALUE_DICT = {
- 'unsigned': 'v8SetReturnValueUnsigned({callback_info}, {cpp_value});',
-}
+DOM_NODE_TYPES = set([
+ 'Attr',
+ 'CDATASection',
+ 'CharacterData',
+ 'Comment',
+ 'Document',
+ 'DocumentFragment',
+ 'DocumentType',
+ 'Element',
+ 'Entity',
+ 'HTMLDocument',
+ 'Node',
+ 'Notation',
+ 'ProcessingInstruction',
+ 'ShadowRoot',
+ 'SVGDocument',
+ 'Text',
+ 'TestNode',
+])
+
+callback_function_types = set()
def array_or_sequence_type(idl_type):
@@ -112,75 +98,357 @@ def array_type(idl_type):
return matched and matched.group(1)
+def callback_function_type(idl_type):
+ return idl_type in callback_function_types
+
+
+def set_callback_function_types(callback_functions):
+ callback_function_types.update(callback_functions.keys())
+
+
+def dom_node_type(idl_type):
+ return (idl_type in DOM_NODE_TYPES or
+ (idl_type.startswith(('HTML', 'SVG')) and
+ idl_type.endswith('Element')))
+
+
def primitive_type(idl_type):
return idl_type in PRIMITIVE_TYPES
+def ref_ptr_type(idl_type):
+ return not(
+ array_type(idl_type) or
+ callback_function_type(idl_type) or
+ primitive_type(idl_type) or
+ sequence_type(idl_type) or
+ union_type(idl_type) or
+ idl_type in ['any', 'DOMString'])
+
+
def sequence_type(idl_type):
matched = re.match(r'sequence<([\w\s]+)>', idl_type)
return matched and matched.group(1)
-def v8_type(interface_type):
- return 'V8' + interface_type
+def union_type(idl_type):
+ return isinstance(idl_type, idl_definitions.IdlUnionType)
-def cpp_type(idl_type, pointer_type=None):
- """Returns the C++ type corresponding to the IDL type.
+def wrapper_type(idl_type):
+ return ref_ptr_type(idl_type)
- If unidentified, fall back to a pointer.
- Args:
- idl_type: IDL type
- pointer_type: If specified, return 'pointer_type<idl_type>'
- (e.g. RefPtr<Foo>, PassRefPtr<Foo>)
- else defaults to returning raw pointer form (e.g. 'Foo*').
- """
+################################################################################
+# C++ types
+################################################################################
+
+CPP_TYPE_SAME_AS_IDL_TYPE = set([
+ 'double',
+ 'float',
+ 'long long',
+ 'unsigned long long',
+])
+CPP_INT_TYPES = set([
+ 'byte',
+ 'long',
+ 'short',
+])
+CPP_UNSIGNED_TYPES = set([
+ 'octet',
+ 'unsigned int',
+ 'unsigned long',
+ 'unsigned short',
+])
+
+
+def cpp_type(idl_type, extended_attributes=None, used_as_argument=False):
+ """Returns C++ type corresponding to IDL type."""
+ extended_attributes = extended_attributes or {}
if idl_type in CPP_TYPE_SAME_AS_IDL_TYPE:
return idl_type
if idl_type in CPP_INT_TYPES:
return 'int'
if idl_type in CPP_UNSIGNED_TYPES:
return 'unsigned'
- if idl_type in CPP_TYPE_SPECIAL_CONVERSION_RULES:
- return CPP_TYPE_SPECIAL_CONVERSION_RULES[idl_type]
- this_array_or_sequence_type = array_or_sequence_type(idl_type)
- if this_array_or_sequence_type:
- return 'const Vector<{cpp_type} >&'.format(cpp_type=cpp_type(this_array_or_sequence_type, 'RefPtr'))
- if pointer_type:
- return '{pointer_type}<{idl_type}>'.format(pointer_type=pointer_type, idl_type=idl_type)
- return idl_type + '*' # raw pointer
-
-
-def cpp_value_to_v8_value(idl_type, cpp_value, isolate, creation_context=''):
- """Returns an expression converting a C++ value to a V8 value."""
- if idl_type in CPP_VALUE_TO_V8_VALUE_DICT:
- expression_format_string = CPP_VALUE_TO_V8_VALUE_DICT[idl_type]
- elif primitive_type(idl_type): # primitive but not in dict
- raise Exception('unexpected IDL type %s' % idl_type)
- elif array_or_sequence_type(idl_type):
- expression_format_string = CPP_VALUE_TO_V8_VALUE_ARRAY_OR_SEQUENCE_TYPE
- else:
- expression_format_string = CPP_VALUE_TO_V8_VALUE_DEFAULT
- return expression_format_string.format(cpp_value=cpp_value, creation_context=creation_context, isolate=isolate)
+ if idl_type == 'boolean':
+ return 'bool'
+ if idl_type == 'Promise':
+ return 'ScriptPromise'
+ if idl_type == 'DOMString':
+ if used_as_argument:
+ return 'V8StringResource<>'
+ return 'String'
+ if callback_function_type(idl_type):
+ return 'ScriptValue'
+ if union_type(idl_type):
+ raise Exception('UnionType is not supported')
+ # FIXME: fix Perl code reading:
+ # return "RefPtr<${type}>" if IsRefPtrType($type) and not $isParameter;
+ if ref_ptr_type(idl_type):
+ if used_as_argument:
+ return cpp_template_type('PassRefPtr', idl_type)
+ return cpp_template_type('RefPtr', idl_type)
+ # Default, assume native type is a pointer with same type name as idl type
+ return idl_type + '*'
-def v8_set_return_value(idl_type, cpp_value, callback_info=''):
- """Returns a statement converting a C++ value to a V8 value and setting it as a return value."""
- this_cpp_type = cpp_type(idl_type)
- if this_cpp_type in V8_SET_RETURN_VALUE_DICT:
- expression_format_string = V8_SET_RETURN_VALUE_DICT[this_cpp_type]
+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:
- raise Exception('unexpected IDL type %s' % idl_type)
- return expression_format_string.format(callback_info=callback_info, cpp_value=cpp_value)
+ format_string = '{template}<{inner_type}>'
+ return format_string.format(template=template, inner_type=inner_type)
+
+
+def v8_type(interface_type):
+ return 'V8' + interface_type
+
+
+################################################################################
+# Includes
+################################################################################
+
+
+def includes_for_cpp_class(class_name, relative_dir_posix):
+ return set([posixpath.join('bindings', relative_dir_posix, class_name + '.h')])
+
+
+def skip_includes(idl_type):
+ return (primitive_type(idl_type) or
+ callback_function_type(idl_type) or
+ idl_type == 'DOMString')
def includes_for_type(idl_type):
- if primitive_type(idl_type) or idl_type == 'DOMString':
+ if skip_includes(idl_type):
return set()
if idl_type == 'Promise':
return set(['ScriptPromise.h'])
+ if idl_type in ['EventListener', 'EventHandler']:
+ return set(['core/dom/EventListener.h'])
this_array_or_sequence_type = array_or_sequence_type(idl_type)
if this_array_or_sequence_type:
return includes_for_type(this_array_or_sequence_type)
return set(['V8%s.h' % idl_type])
+
+
+################################################################################
+# V8 -> C++
+################################################################################
+
+V8_VALUE_TO_CPP_VALUE_PRIMITIVE = {
+ 'boolean': '{v8_value}->BooleanValue()',
+ 'float': 'static_cast<float>({v8_value}->NumberValue())',
+ 'double': 'static_cast<double>({v8_value}->NumberValue())',
+ 'byte': 'toInt8({arguments})',
+ 'octet': 'toUInt8({arguments})',
+ 'short': 'toInt32({arguments})',
+ 'unsigned short': 'toUInt32({arguments})',
+ 'long': 'toInt32({arguments})',
+ 'unsigned long': 'toUInt32({arguments})',
+ 'long long': 'toInt64({arguments})',
+ 'unsigned long long': 'toUInt64({arguments})',
+}
+V8_VALUE_TO_CPP_VALUE_AND_INCLUDES = {
+ # idl_type -> (cpp_expression_format, includes)
+ 'any': ('ScriptValue({v8_value}, {isolate})',
+ set(['bindings/v8/ScriptValue.h'])),
+ 'Dictionary': ('Dictionary({v8_value}, {isolate})',
+ set(['bindings/v8/Dictionary.h'])),
+ 'MediaQueryListListener': ('MediaQueryListListener::create({v8_value})',
+ set(['core/css/MediaQueryListListener.h'])),
+ 'SerializedScriptValue': (
+ 'SerializedScriptValue::create({v8_value}, {isolate})',
+ set(['bindings/v8/SerializedScriptValue.h'])),
+}
+
+
+def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, isolate):
+ this_array_or_sequence_type = array_or_sequence_type(idl_type)
+ if this_array_or_sequence_type:
+ return v8_value_to_cpp_value_array_or_sequence(this_array_or_sequence_type, v8_value, isolate)
+
+ if callback_function_type(idl_type):
+ idl_type = 'any'
+
+ if 'EnforceRange' in extended_attributes:
+ arguments = ', '.join([v8_value, 'EnforceRange', 'ok'])
+ else: # NormalConversion
+ arguments = v8_value
+
+ if idl_type in V8_VALUE_TO_CPP_VALUE_PRIMITIVE:
+ cpp_expression_format = V8_VALUE_TO_CPP_VALUE_PRIMITIVE[idl_type]
+ includes = set()
+ elif idl_type in V8_VALUE_TO_CPP_VALUE_AND_INCLUDES:
+ cpp_expression_format, includes = V8_VALUE_TO_CPP_VALUE_AND_INCLUDES[idl_type]
+ else:
+ cpp_expression_format = (
+ 'V8{idl_type}::HasInstance({v8_value}, {isolate}, worldType({isolate})) ? '
+ 'V8{idl_type}::toNative(v8::Handle<v8::Object>::Cast({v8_value})) : 0')
+ includes = includes_for_type(idl_type)
+ includes.add('V8%s.h' % idl_type)
+
+ cpp_expression = cpp_expression_format.format(arguments=arguments, idl_type=idl_type, isolate=isolate, v8_value=v8_value)
+ return cpp_expression, includes
+
+
+def v8_value_to_cpp_value_array_or_sequence(this_array_or_sequence_type, v8_value, isolate):
+ if ref_ptr_type(this_array_or_sequence_type):
+ this_cpp_type = None
+ expression_format = '(toRefPtrNativeArray<{array_or_sequence_type}, V8{array_or_sequence_type}>({v8_value}, {isolate}))'
+ includes = set(['V8%s.h' % this_array_or_sequence_type])
+ else:
+ this_cpp_type = cpp_type(this_array_or_sequence_type)
+ expression_format = 'toNativeArray<{cpp_type}>({v8_value}, {isolate})'
+ includes = set()
+ expression = expression_format.format(array_or_sequence_type=this_array_or_sequence_type, cpp_type=this_cpp_type, isolate=isolate, v8_value=v8_value)
+ return expression, includes
+
+
+def v8_value_to_cpp_value_statement(idl_type, extended_attributes, v8_value, variable_name, isolate):
+ this_cpp_type = cpp_type(idl_type, extended_attributes=extended_attributes, used_as_argument=True)
+
+ if idl_type == 'DOMString':
+ format_string = 'V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID({cpp_type}, {variable_name}, {cpp_value});'
+ elif 'EnforceRange' in extended_attributes:
+ format_string = 'V8TRYCATCH_WITH_TYPECHECK_VOID({cpp_type}, {variable_name}, {cpp_value}, {isolate});'
+ else:
+ format_string = 'V8TRYCATCH_VOID({cpp_type}, {variable_name}, {cpp_value});'
+
+ cpp_value, includes = v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, isolate)
+ statement = format_string.format(cpp_type=this_cpp_type, cpp_value=cpp_value, isolate=isolate, variable_name=variable_name)
+ return statement, includes
+
+
+################################################################################
+# C++ -> V8
+################################################################################
+
+
+def preprocess_type_and_value(idl_type, cpp_value, extended_attributes):
+ """Returns type and value, with preliminary type conversions applied."""
+ if idl_type in ['long long', 'unsigned long long', 'DOMTimeStamp']:
+ # long long and unsigned long long are not representable in ECMAScript;
+ # we represent them as doubles.
+ # Also, DOMTimeStamp is a typedef for unsigned long long:
+ # http://dev.w3.org/2006/webapi/WebIDL/#common-DOMTimeStamp
+ idl_type = 'double'
+ cpp_value = 'static_cast<double>(%s)' % cpp_value
+ # HTML5 says that unsigned reflected attributes should be in the range
+ # [0, 2^31). When a value isn't in this range, a default value (or 0)
+ # should be returned instead.
+ extended_attributes = extended_attributes or {}
+ if ('Reflect' in extended_attributes and
+ idl_type in ['unsigned long', 'unsigned short']):
+ cpp_value = cpp_value.replace('getUnsignedIntegralAttribute',
+ 'getIntegralAttribute')
+ cpp_value = 'std::max(0, %s)' % cpp_value
+ return idl_type, cpp_value
+
+
+def v8_conversion_type_and_includes(idl_type):
+ """Returns V8 conversion type and any additional includes.
+
+ The V8 conversion type is used to select the C++ -> V8 conversion function
+ or v8SetReturnValue* function; it can be an idl_type, a cpp_type, or a
+ separate name for the type of conversion (e.g., 'DOMWrapper').
+ """
+ # Simple cases, without additional includes
+ if idl_type in ['boolean', 'float', 'double', 'void', 'DOMString', 'Date']:
+ return idl_type, set()
+ if idl_type in CPP_INT_TYPES:
+ return 'int', set()
+ if idl_type in CPP_UNSIGNED_TYPES:
+ return 'unsigned', set()
+ if callback_function_type(idl_type):
+ return 'ScriptValue', set()
+ if primitive_type(idl_type): # primitive but not yet handled
+ raise Exception('unexpected type %s' % idl_type)
+
+ # Data type with potential additional includes
+ this_array_or_sequence_type = array_or_sequence_type(idl_type)
+ if this_array_or_sequence_type:
+ if ref_ptr_type(this_array_or_sequence_type):
+ includes = includes_for_type(this_array_or_sequence_type)
+ else:
+ includes = set()
+ return 'array', includes
+
+ includes = includes_for_type(idl_type)
+ if idl_type == 'SerializedScriptValue':
+ return 'SerializedScriptValue', includes
+
+ # Pointer type
+ includes.add('wtf/GetPtr.h') # FIXME: Is this necessary?
+ includes.add('wtf/RefPtr.h')
+ return 'DOMWrapper', includes
+
+
+V8_SET_RETURN_VALUE = {
+ 'boolean': 'v8SetReturnValueBool({callback_info}, {cpp_value});',
+ 'int': 'v8SetReturnValueInt({callback_info}, {cpp_value});',
+ 'unsigned': 'v8SetReturnValueUnsigned({callback_info}, {cpp_value});',
+ 'DOMString': 'v8SetReturnValueString({callback_info}, {cpp_value}, {isolate});',
+ 'void': '',
+ # No special v8SetReturnValue* function (set value directly)
+ 'float': 'v8SetReturnValue({callback_info}, {cpp_value});',
+ 'double': 'v8SetReturnValue({callback_info}, {cpp_value});',
+ # No special v8SetReturnValue* function, but instead convert value to V8
+ # and then use general v8SetReturnValue.
+ 'array': 'v8SetReturnValue({callback_info}, {cpp_value});',
+ 'Date': 'v8SetReturnValue({callback_info}, {cpp_value});',
+ 'ScriptValue': 'v8SetReturnValue({callback_info}, {cpp_value});',
+ 'SerializedScriptValue': 'v8SetReturnValue({callback_info}, {cpp_value});',
+ # DOMWrapper
+ 'DOMWrapperFast': 'v8SetReturnValueFast({callback_info}, {cpp_value}, {script_wrappable});',
+ 'DOMWrapperDefault': 'v8SetReturnValue({callback_info}, {cpp_value}, {creation_context});',
+}
+
+
+def v8_set_return_value(idl_type, cpp_value, callback_info, isolate, creation_context='', extended_attributes=None, script_wrappable=''):
+ """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 not script_wrappable:
+ return 'DOMWrapperDefault'
+ return 'DOMWrapperFast'
+
+ idl_type, cpp_value = preprocess_type_and_value(idl_type, cpp_value, extended_attributes)
+ v8_conversion_type, includes = v8_conversion_type_and_includes(idl_type)
+ # SetReturn-specific overrides
+ if v8_conversion_type in ['array', 'Date', 'ScriptValue', 'SerializedScriptValue']:
+ # Convert value to V8 and then use general v8SetReturnValue
+ cpp_value, _ = cpp_value_to_v8_value(idl_type, cpp_value, isolate, callback_info=callback_info)
+ if v8_conversion_type == 'DOMWrapper':
+ v8_conversion_type = dom_wrapper_conversion_type()
+
+ format_string = V8_SET_RETURN_VALUE[v8_conversion_type]
+ statement = format_string.format(callback_info=callback_info, cpp_value=cpp_value, creation_context=creation_context, isolate=isolate, script_wrappable=script_wrappable)
+ return statement, includes
+
+
+CPP_VALUE_TO_V8_VALUE = {
+ 'Date': 'v8DateOrNull({cpp_value}, {isolate})',
+ 'DOMString': 'v8String({cpp_value}, {isolate})',
+ 'ScriptValue': '{cpp_value}.v8Value()',
+ 'SerializedScriptValue': '{cpp_value} ? {cpp_value}->deserialize() : v8::Handle<v8::Value>(v8::Null({isolate}))',
+ 'boolean': 'v8Boolean({cpp_value}, {isolate})',
+ 'int': 'v8::Integer::New({cpp_value}, {isolate})',
+ 'unsigned': 'v8::Integer::NewFromUnsigned({cpp_value}, {isolate})',
+ 'float': 'v8::Number::New({cpp_value})',
+ 'double': 'v8::Number::New({cpp_value})',
+ 'void': 'v8Undefined()',
+ # General
+ 'array': 'v8Array({cpp_value}, {isolate})',
+ 'default': 'toV8({cpp_value}, {creation_context}, {isolate})',
+}
+
+
+def cpp_value_to_v8_value(idl_type, cpp_value, isolate, callback_info='', creation_context='', extended_attributes=None):
+ """Returns an expression that converts a C++ value to a V8 value."""
+ idl_type, cpp_value = preprocess_type_and_value(idl_type, cpp_value, extended_attributes)
+ v8_conversion_type, includes = v8_conversion_type_and_includes(idl_type)
+ format_string = CPP_VALUE_TO_V8_VALUE[v8_conversion_type]
+ statement = format_string.format(callback_info=callback_info, cpp_value=cpp_value, creation_context=creation_context, isolate=isolate)
+ return statement, includes
« no previous file with comments | « Source/bindings/scripts/unstable/v8_callback_interface.py ('k') | Source/bindings/tests/idls/TestObjectPython.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698