Commit 8fceeae0 authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

bind-gen: Implement NativeValueTraits for interface

Makes NativeValueTraits work well with the new generated
code of IDL interface.

Adds NativeValueTraits::ArgumentValue in addition to
NativeValue in order to produce better error messages.

Bug: 839389
Change-Id: I050dfa5b53c11aea6e50de5ef0148292db7fb3cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1995241
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#731179}
parent c320bef9
......@@ -37,6 +37,20 @@ ScriptWrappable* NativeValueTraitsInterfaceNativeValue(
return ToScriptWrappable(value.As<v8::Object>());
}
ScriptWrappable* NativeValueTraitsInterfaceArgumentValue(
v8::Isolate* isolate,
const WrapperTypeInfo* wrapper_type_info,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
if (!V8PerIsolateData::From(isolate)->HasInstance(wrapper_type_info, value)) {
exception_state.ThrowTypeError(ExceptionMessages::ArgumentNotOfType(
argument_index, wrapper_type_info->interface_name));
return nullptr;
}
return ToScriptWrappable(value.As<v8::Object>());
}
} // namespace bindings
#define DEFINE_NATIVE_VALUE_TRAITS_BUFFER_SOURCE_TYPE(T, V8T) \
......
......@@ -31,6 +31,13 @@ CORE_EXPORT ScriptWrappable* NativeValueTraitsInterfaceNativeValue(
v8::Local<v8::Value> value,
ExceptionState& exception_state);
CORE_EXPORT ScriptWrappable* NativeValueTraitsInterfaceArgumentValue(
v8::Isolate* isolate,
const WrapperTypeInfo* wrapper_type_info,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state);
} // namespace bindings
// Boolean
......@@ -42,6 +49,13 @@ struct CORE_EXPORT NativeValueTraits<IDLBoolean>
ExceptionState& exception_state) {
return ToBoolean(isolate, value, exception_state);
}
static bool ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
// Integers
......@@ -53,6 +67,13 @@ struct CORE_EXPORT NativeValueTraits<IDLByte>
ExceptionState& exception_state) {
return ToInt8(isolate, value, kNormalConversion, exception_state);
}
static int8_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -63,6 +84,13 @@ struct CORE_EXPORT NativeValueTraits<IDLOctet>
ExceptionState& exception_state) {
return ToUInt8(isolate, value, kNormalConversion, exception_state);
}
static uint8_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -73,6 +101,13 @@ struct CORE_EXPORT NativeValueTraits<IDLShort>
ExceptionState& exception_state) {
return ToInt16(isolate, value, kNormalConversion, exception_state);
}
static int16_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -83,6 +118,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnsignedShort>
ExceptionState& exception_state) {
return ToUInt16(isolate, value, kNormalConversion, exception_state);
}
static uint16_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -93,6 +135,13 @@ struct CORE_EXPORT NativeValueTraits<IDLLong>
ExceptionState& exception_state) {
return ToInt32(isolate, value, kNormalConversion, exception_state);
}
static int32_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -103,6 +152,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnsignedLong>
ExceptionState& exception_state) {
return ToUInt32(isolate, value, kNormalConversion, exception_state);
}
static uint32_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -113,6 +169,13 @@ struct CORE_EXPORT NativeValueTraits<IDLLongLong>
ExceptionState& exception_state) {
return ToInt64(isolate, value, kNormalConversion, exception_state);
}
static int64_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -123,6 +186,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnsignedLongLong>
ExceptionState& exception_state) {
return ToUInt64(isolate, value, kNormalConversion, exception_state);
}
static uint64_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
// [Clamp] Integers
......@@ -134,6 +204,13 @@ struct CORE_EXPORT NativeValueTraits<IDLByteClamp>
ExceptionState& exception_state) {
return ToInt8(isolate, value, kClamp, exception_state);
}
static int8_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -144,6 +221,13 @@ struct CORE_EXPORT NativeValueTraits<IDLOctetClamp>
ExceptionState& exception_state) {
return ToUInt8(isolate, value, kClamp, exception_state);
}
static uint8_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -154,6 +238,13 @@ struct CORE_EXPORT NativeValueTraits<IDLShortClamp>
ExceptionState& exception_state) {
return ToInt16(isolate, value, kClamp, exception_state);
}
static int16_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -164,6 +255,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnsignedShortClamp>
ExceptionState& exception_state) {
return ToUInt16(isolate, value, kClamp, exception_state);
}
static uint16_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -174,6 +272,13 @@ struct CORE_EXPORT NativeValueTraits<IDLLongClamp>
ExceptionState& exception_state) {
return ToInt32(isolate, value, kClamp, exception_state);
}
static int32_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -184,6 +289,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnsignedLongClamp>
ExceptionState& exception_state) {
return ToUInt32(isolate, value, kClamp, exception_state);
}
static uint32_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -194,6 +306,13 @@ struct CORE_EXPORT NativeValueTraits<IDLLongLongClamp>
ExceptionState& exception_state) {
return ToInt64(isolate, value, kClamp, exception_state);
}
static int64_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -204,6 +323,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnsignedLongLongClamp>
ExceptionState& exception_state) {
return ToUInt64(isolate, value, kClamp, exception_state);
}
static uint64_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
// [EnforceRange] Integers
......@@ -215,6 +341,13 @@ struct CORE_EXPORT NativeValueTraits<IDLByteEnforceRange>
ExceptionState& exception_state) {
return ToInt8(isolate, value, kEnforceRange, exception_state);
}
static int8_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -225,6 +358,13 @@ struct CORE_EXPORT NativeValueTraits<IDLOctetEnforceRange>
ExceptionState& exception_state) {
return ToUInt8(isolate, value, kEnforceRange, exception_state);
}
static uint8_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -235,6 +375,13 @@ struct CORE_EXPORT NativeValueTraits<IDLShortEnforceRange>
ExceptionState& exception_state) {
return ToInt16(isolate, value, kEnforceRange, exception_state);
}
static int16_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -245,6 +392,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnsignedShortEnforceRange>
ExceptionState& exception_state) {
return ToUInt16(isolate, value, kEnforceRange, exception_state);
}
static uint16_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -255,6 +409,13 @@ struct CORE_EXPORT NativeValueTraits<IDLLongEnforceRange>
ExceptionState& exception_state) {
return ToInt32(isolate, value, kEnforceRange, exception_state);
}
static int32_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -265,6 +426,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnsignedLongEnforceRange>
ExceptionState& exception_state) {
return ToUInt32(isolate, value, kEnforceRange, exception_state);
}
static uint32_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -275,6 +443,13 @@ struct CORE_EXPORT NativeValueTraits<IDLLongLongEnforceRange>
ExceptionState& exception_state) {
return ToInt64(isolate, value, kEnforceRange, exception_state);
}
static int64_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -285,6 +460,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnsignedLongLongEnforceRange>
ExceptionState& exception_state) {
return ToUInt64(isolate, value, kEnforceRange, exception_state);
}
static uint64_t ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
// Strings
......@@ -434,6 +616,18 @@ struct NativeValueTraits<IDLByteStringBaseV2<mode>>
}
return bindings::NativeValueTraitsStringAdapter(v8_string);
}
static bindings::NativeValueTraitsStringAdapter ArgumentValue(
v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
static bindings::NativeValueTraitsStringAdapter NullValue() {
return bindings::NativeValueTraitsStringAdapter();
}
};
template <bindings::NativeValueTraitsStringConv mode>
......@@ -469,6 +663,18 @@ struct NativeValueTraits<IDLStringBaseV2<mode>>
}
return bindings::NativeValueTraitsStringAdapter(v8_string);
}
static bindings::NativeValueTraitsStringAdapter ArgumentValue(
v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
static bindings::NativeValueTraitsStringAdapter NullValue() {
return bindings::NativeValueTraitsStringAdapter();
}
};
template <bindings::NativeValueTraitsStringConv mode>
......@@ -485,6 +691,15 @@ struct NativeValueTraits<IDLUSVStringBaseV2<mode>>
return ReplaceUnmatchedSurrogates(string);
}
static String ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
static String NullValue() { return String(); }
};
// Floats and doubles
......@@ -496,6 +711,13 @@ struct CORE_EXPORT NativeValueTraits<IDLDouble>
ExceptionState& exception_state) {
return ToRestrictedDouble(isolate, value, exception_state);
}
static double ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -506,6 +728,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnrestrictedDouble>
ExceptionState& exception_state) {
return ToDouble(isolate, value, exception_state);
}
static double ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -516,6 +745,13 @@ struct CORE_EXPORT NativeValueTraits<IDLFloat>
ExceptionState& exception_state) {
return ToRestrictedFloat(isolate, value, exception_state);
}
static float ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
template <>
......@@ -526,6 +762,13 @@ struct CORE_EXPORT NativeValueTraits<IDLUnrestrictedFloat>
ExceptionState& exception_state) {
return ToFloat(isolate, value, exception_state);
}
static float ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
// Buffer source types
......@@ -643,6 +886,13 @@ struct NativeValueTraits<IDLSequence<T>>
return result;
}
static ImplType ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
private:
// Fast case: we're interating over an Array that adheres to
// %ArrayIteratorPrototype%'s protocol.
......@@ -830,13 +1080,20 @@ struct NativeValueTraits<IDLRecord<K, V>>
// "5. Return result."
return result;
}
static ImplType ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
// Callback functions
template <typename T>
struct NativeValueTraits<
T,
std::enable_if_t<std::is_base_of<CallbackFunctionBase, T>::value>>
typename std::enable_if_t<std::is_base_of<CallbackFunctionBase, T>::value>>
: public NativeValueTraitsBase<T> {
static T* NativeValue(v8::Isolate* isolate,
v8::Local<v8::Value> value,
......@@ -850,6 +1107,13 @@ struct NativeValueTraits<
<< "is not yet implemented.";
return nullptr;
}
static T* ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
// Dictionary
......@@ -864,6 +1128,13 @@ struct NativeValueTraits<
ExceptionState& exception_state) {
return T::Create(isolate, value, exception_state);
}
static T* ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
// Interface
......@@ -880,6 +1151,16 @@ struct NativeValueTraits<
->template ToImpl<T>();
}
static T* ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return bindings::NativeValueTraitsInterfaceArgumentValue(
isolate, T::GetStaticWrapperTypeInfo(), argument_index, value,
exception_state)
->template ToImpl<T>();
}
static constexpr T* NullValue() { return nullptr; }
};
......@@ -897,6 +1178,14 @@ struct NativeValueTraits<IDLNullable<InnerType>>
return NativeValueTraits<InnerType>::NativeValue(isolate, v8_value,
exception_state);
}
static typename IDLNullable<InnerType>::ResultType ArgumentValue(
v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
return NativeValue(isolate, value, exception_state);
}
};
} // namespace blink
......
......@@ -174,12 +174,13 @@ def native_value_tag(idl_type):
assert isinstance(idl_type, web_idl.IdlType)
real_type = idl_type.unwrap(typedef=True)
non_null_real_type = real_type.unwrap(nullable=True)
if (real_type.is_boolean or real_type.is_numeric or real_type.is_any
or real_type.is_object):
if (real_type.is_boolean or real_type.is_numeric
or non_null_real_type.is_any or non_null_real_type.is_object):
return "IDL{}".format(real_type.type_name)
if real_type.unwrap(nullable=True).is_string:
if non_null_real_type.is_string:
return "IDL{}V2".format(real_type.type_name)
if real_type.is_symbol:
......@@ -188,10 +189,10 @@ def native_value_tag(idl_type):
if real_type.is_void:
assert False, "Blink does not support/accept IDL void type."
if real_type.type_definition_object is not None:
return blink_type_info(real_type).value_t
if non_null_real_type.type_definition_object:
return blink_class_name(non_null_real_type.type_definition_object)
if real_type.is_sequence:
if real_type.is_sequence or real_type.is_frozen_array:
return "IDLSequence<{}>".format(
native_value_tag(real_type.element_type))
......@@ -209,7 +210,7 @@ def native_value_tag(idl_type):
if real_type.is_nullable:
return "IDLNullable<{}>".format(native_value_tag(real_type.inner_type))
assert False
assert False, "Unknown type: {}".format(idl_type.syntactic_form)
def make_default_value_expr(idl_type, default_value):
......@@ -307,6 +308,7 @@ def make_default_value_expr(idl_type, default_value):
def make_v8_to_blink_value(blink_var_name,
v8_value_expr,
idl_type,
argument_index=None,
default_value=None):
"""
Returns a SymbolNode whose definition converts a v8::Value to a Blink value.
......@@ -314,6 +316,7 @@ def make_v8_to_blink_value(blink_var_name,
assert isinstance(blink_var_name, str)
assert isinstance(v8_value_expr, str)
assert isinstance(idl_type, web_idl.IdlType)
assert (argument_index is None or isinstance(argument_index, (int, long)))
assert (default_value is None
or isinstance(default_value, web_idl.LiteralConstant))
......@@ -321,10 +324,22 @@ def make_v8_to_blink_value(blink_var_name,
F = lambda *args, **kwargs: T(_format(*args, **kwargs))
def create_definition(symbol_node):
if argument_index is None:
blink_value_expr = _format(
"NativeValueTraits<{_1}>::NativeValue({_2})",
_1=native_value_tag(idl_type),
_2=", ".join(["${isolate}", v8_value_expr, "${exception_state}"]))
_2=", ".join(
["${isolate}", v8_value_expr, "${exception_state}"]))
else:
blink_value_expr = _format(
"NativeValueTraits<{_1}>::ArgumentValue({_2})",
_1=native_value_tag(idl_type),
_2=", ".join([
"${isolate}",
str(argument_index),
v8_value_expr,
"${exception_state}",
]))
if default_value is None:
return SymbolDefinitionNode(symbol_node, [
......
......@@ -147,8 +147,12 @@ def bind_blink_api_arguments(code_node, cg_context):
else:
v8_value = "${{info}}[{}]".format(argument.index)
code_node.register_code_symbol(
make_v8_to_blink_value(name, v8_value, argument.idl_type,
argument.default_value))
make_v8_to_blink_value(
name,
v8_value,
argument.idl_type,
argument_index=index,
default_value=argument.default_value))
def bind_callback_local_vars(code_node, cg_context):
......@@ -2957,5 +2961,5 @@ def generate_interface(interface):
def generate_interfaces(web_idl_database):
interface = web_idl_database.find("Navigator")
interface = web_idl_database.find("Node")
generate_interface(interface)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment