Commit 98e49a7b authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

bind-gen: Implement NativeValueTraits<IDL callback function/interface>

Bug: 839389
Change-Id: I9b6249cc25cf1e2895f4064d0c17e95fa196760f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2057163
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#741770}
parent b26a287b
...@@ -193,12 +193,12 @@ struct IDLRecord final : public IDLBase { ...@@ -193,12 +193,12 @@ struct IDLRecord final : public IDLBase {
}; };
// Nullable types // Nullable types
template <typename InnerType> template <typename T>
struct IDLNullable final : public IDLBase { struct IDLNullable final : public IDLBase {
using ImplType = std::conditional_t< using ImplType = std::conditional_t<
NativeValueTraits<InnerType>::has_null_value, NativeValueTraits<T>::has_null_value,
typename NativeValueTraits<InnerType>::ImplType, typename NativeValueTraits<T>::ImplType,
base::Optional<typename NativeValueTraits<InnerType>::ImplType>>; base::Optional<typename NativeValueTraits<T>::ImplType>>;
}; };
// Date // Date
......
...@@ -20,7 +20,9 @@ ...@@ -20,7 +20,9 @@
namespace blink { namespace blink {
class CallbackFunctionBase; class CallbackFunctionBase;
class CallbackInterfaceBase;
class EventListener; class EventListener;
class FlexibleArrayBufferView;
class ScriptWrappable; class ScriptWrappable;
struct WrapperTypeInfo; struct WrapperTypeInfo;
...@@ -832,13 +834,107 @@ struct NativeValueTraits< ...@@ -832,13 +834,107 @@ struct NativeValueTraits<
static T* NativeValue(v8::Isolate* isolate, static T* NativeValue(v8::Isolate* isolate,
v8::Local<v8::Value> value, v8::Local<v8::Value> value,
ExceptionState& exception_state) { ExceptionState& exception_state) {
// Not implemented because of no use case so far. if (value->IsFunction())
CHECK(false) return T::Create(value.As<v8::Function>());
// Emit a message so that NativeValueTraitsImplTest.IDLCallbackFunction exception_state.ThrowTypeError("The given value is not a function.");
// test can confirm that it's hitting this specific failure. i.e. return nullptr;
// the template resolution is working as expected. }
<< "NativeValueTraits<CallbackFunctionBase>::NativeValue "
<< "is not yet implemented."; static T* ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
if (value->IsFunction())
return T::Create(value.As<v8::Function>());
exception_state.ThrowTypeError(
ExceptionMessages::ArgumentNotOfType(argument_index, "Function"));
return nullptr;
}
};
template <typename T>
struct NativeValueTraits<
IDLNullable<T>,
typename std::enable_if_t<std::is_base_of<CallbackFunctionBase, T>::value>>
: public NativeValueTraitsBase<IDLNullable<T>> {
static T* NativeValue(v8::Isolate* isolate,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
if (value->IsFunction())
return T::Create(value.As<v8::Function>());
if (value->IsNullOrUndefined())
return nullptr;
exception_state.ThrowTypeError("The given value is not a function.");
return nullptr;
}
static T* ArgumentValue(v8::Isolate* isolate,
int argument_index,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
if (value->IsFunction())
return T::Create(value.As<v8::Function>());
if (value->IsNullOrUndefined())
return nullptr;
exception_state.ThrowTypeError(
ExceptionMessages::ArgumentNotOfType(argument_index, "Function"));
return nullptr;
}
};
// Callback interface types
template <typename T>
struct NativeValueTraits<
T,
typename std::enable_if_t<std::is_base_of<CallbackInterfaceBase, T>::value>>
: public NativeValueTraitsBase<T*> {
static T* NativeValue(v8::Isolate* isolate,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
if (value->IsObject())
return T::Create(value.As<v8::Object>());
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 T::Create(value.As<v8::Object>());
exception_state.ThrowTypeError(
ExceptionMessages::ArgumentNotOfType(argument_index, "Object"));
return nullptr;
}
};
template <typename T>
struct NativeValueTraits<
IDLNullable<T>,
typename std::enable_if_t<std::is_base_of<CallbackInterfaceBase, T>::value>>
: public NativeValueTraitsBase<IDLNullable<T>> {
static T* NativeValue(v8::Isolate* isolate,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
if (value->IsObject())
return T::Create(value.As<v8::Object>());
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 T::Create(value.As<v8::Object>());
if (value->IsNullOrUndefined())
return nullptr;
exception_state.ThrowTypeError(
ExceptionMessages::ArgumentNotOfType(argument_index, "Object"));
return nullptr; return nullptr;
} }
}; };
......
...@@ -46,17 +46,6 @@ TEST(NativeValueTraitsImplTest, IDLInterface) { ...@@ -46,17 +46,6 @@ TEST(NativeValueTraitsImplTest, IDLInterface) {
EXPECT_EQ(nullptr, internals); EXPECT_EQ(nullptr, internals);
} }
TEST(NativeValueTraitsImplTest, IDLCallbackFunction) {
V8TestingScope scope;
DummyExceptionStateForTesting exception_state;
v8::Local<v8::Function> function =
v8::Function::New(scope.GetContext(), nullptr).ToLocalChecked();
ASSERT_DEATH_IF_SUPPORTED(
NativeValueTraits<V8TestSequenceCallback>::NativeValue(
scope.GetIsolate(), function, exception_state),
"");
}
TEST(NativeValueTraitsImplTest, IDLRecord) { TEST(NativeValueTraitsImplTest, IDLRecord) {
V8TestingScope scope; V8TestingScope scope;
{ {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_V8_SET_RETURN_VALUE_FOR_CORE_H_ #define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_V8_SET_RETURN_VALUE_FOR_CORE_H_
#include "third_party/blink/renderer/bindings/core/v8/js_event_handler.h" #include "third_party/blink/renderer/bindings/core/v8/js_event_handler.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
#include "third_party/blink/renderer/platform/bindings/v8_set_return_value.h" #include "third_party/blink/renderer/platform/bindings/v8_set_return_value.h"
namespace blink { namespace blink {
......
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