Commit c018b81a authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

bind-gen: Implement IDLNullable<old dict> and variadic args

Implements NativeValueTraits<IDLNullable<old dict>> and
bindings::VariadicArgumentsToNativeValues.

Bug: 839389
Change-Id: Ifc5712b885bd8dd0841f61eb95e4f942db132372
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2068264
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#744216}
parent d8b25b55
......@@ -2,15 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file is for functions that are used only by generated code.
// CAUTION:
// All functions defined in this file should be used by generated code only.
// If you want to use them from hand-written code, please find appropriate
// location and move them to that location.
// This file provides utilities to be used only by generated bindings code.
//
// CAUTION: Do not use this header outside generated bindings code.
#ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_GENERATED_CODE_HELPER_H_
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_GENERATED_CODE_HELPER_H_
#include "third_party/blink/renderer/bindings/core/v8/idl_types.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
......@@ -94,6 +93,29 @@ void V8SetReflectedNullableDOMStringAttribute(
namespace bindings {
template <typename T>
typename IDLSequence<T>::ImplType VariadicArgumentsToNativeValues(
v8::Isolate* isolate,
const v8::FunctionCallbackInfo<v8::Value>& info,
int start_index,
ExceptionState& exception_state) {
using VectorType = typename IDLSequence<T>::ImplType;
const int length = info.Length();
if (start_index >= length)
return VectorType();
VectorType result;
result.ReserveInitialCapacity(length - start_index);
for (int i = start_index; i < length; ++i) {
result.UncheckedAppend(NativeValueTraits<T>::ArgumentValue(
isolate, i, info[i], exception_state));
if (exception_state.HadException())
return VectorType();
}
return std::move(result);
}
base::Optional<size_t> FindIndexInEnumStringTable(
v8::Isolate* isolate,
v8::Local<v8::Value> value,
......
......@@ -23,6 +23,7 @@ class CallbackFunctionBase;
class CallbackInterfaceBase;
class EventListener;
class FlexibleArrayBufferView;
class IDLDictionaryBase;
class ScriptWrappable;
struct WrapperTypeInfo;
......@@ -953,6 +954,36 @@ struct NativeValueTraits<
}
};
template <typename T>
struct NativeValueTraits<
IDLNullable<T>,
typename std::enable_if_t<std::is_base_of<IDLDictionaryBase, T>::value>>
: public NativeValueTraitsBase<IDLNullable<T>> {
static T* NativeValue(v8::Isolate* isolate,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
if (value->IsObject())
return NativeValueTraits<T>::NativeValue(isolate, value, exception_state);
if (value->IsNullOrUndefined())
return nullptr;
exception_state.ThrowTypeError("The given value is not an object.");
return nullptr;
}
static T* ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
if (value->IsObject())
return NativeValueTraits<T>::NativeValue(isolate, value, exception_state);
if (value->IsNullOrUndefined())
return nullptr;
exception_state.ThrowTypeError(
ExceptionMessages::ArgumentNotOfType(argument_index, "Object"));
return nullptr;
}
};
// Enumeration types
template <typename T>
struct NativeValueTraits<
......
......@@ -487,10 +487,14 @@ def make_v8_to_blink_value_variadic(blink_var_name, v8_array,
assert isinstance(v8_array_start_index, (int, long))
assert isinstance(idl_type, web_idl.IdlType)
pattern = "auto&& ${{{_1}}} = ToImplArguments<{_2}>({_3});"
pattern = ("auto&& ${{{_1}}} = "
"bindings::VariadicArgumentsToNativeValues<{_2}>({_3});")
_1 = blink_var_name
_2 = native_value_tag(idl_type.element_type)
_3 = [v8_array, str(v8_array_start_index), "${exception_state}"]
_3 = [
"${isolate}", v8_array,
str(v8_array_start_index), "${exception_state}"
]
text = _format(pattern, _1=_1, _2=_2, _3=", ".join(_3))
def create_definition(symbol_node):
......
......@@ -30,7 +30,7 @@ inline void V8SetReturnValue(const CallbackInfo& callbackInfo, {{cpp_class}}* im
}
template <>
struct NativeValueTraits<{{cpp_class}}> : public NativeValueTraitsBase<{{cpp_class}}> {
struct NativeValueTraits<{{cpp_class}}> : public NativeValueTraitsBase<{{cpp_class}}*> {
{{exported}}static {{cpp_class}}* NativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&);
};
......
......@@ -41,7 +41,7 @@ inline void V8SetReturnValue(const CallbackInfo& callbackInfo, TestDictionary* i
}
template <>
struct NativeValueTraits<TestDictionary> : public NativeValueTraitsBase<TestDictionary> {
struct NativeValueTraits<TestDictionary> : public NativeValueTraitsBase<TestDictionary*> {
CORE_EXPORT static TestDictionary* NativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&);
};
......
......@@ -41,7 +41,7 @@ inline void V8SetReturnValue(const CallbackInfo& callbackInfo, TestDictionaryDer
}
template <>
struct NativeValueTraits<TestDictionaryDerivedImplementedAs> : public NativeValueTraitsBase<TestDictionaryDerivedImplementedAs> {
struct NativeValueTraits<TestDictionaryDerivedImplementedAs> : public NativeValueTraitsBase<TestDictionaryDerivedImplementedAs*> {
CORE_EXPORT static TestDictionaryDerivedImplementedAs* NativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&);
};
......
......@@ -41,7 +41,7 @@ inline void V8SetReturnValue(const CallbackInfo& callbackInfo, TestInterfaceEven
}
template <>
struct NativeValueTraits<TestInterfaceEventInit> : public NativeValueTraitsBase<TestInterfaceEventInit> {
struct NativeValueTraits<TestInterfaceEventInit> : public NativeValueTraitsBase<TestInterfaceEventInit*> {
CORE_EXPORT static TestInterfaceEventInit* NativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&);
};
......
......@@ -41,7 +41,7 @@ inline void V8SetReturnValue(const CallbackInfo& callbackInfo, TestPermissiveDic
}
template <>
struct NativeValueTraits<TestPermissiveDictionary> : public NativeValueTraitsBase<TestPermissiveDictionary> {
struct NativeValueTraits<TestPermissiveDictionary> : public NativeValueTraitsBase<TestPermissiveDictionary*> {
CORE_EXPORT static TestPermissiveDictionary* NativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&);
};
......
......@@ -41,7 +41,7 @@ inline void V8SetReturnValue(const CallbackInfo& callbackInfo, TestDictionary2*
}
template <>
struct NativeValueTraits<TestDictionary2> : public NativeValueTraitsBase<TestDictionary2> {
struct NativeValueTraits<TestDictionary2> : public NativeValueTraitsBase<TestDictionary2*> {
MODULES_EXPORT static TestDictionary2* NativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&);
};
......
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