From 940af9f2c87b436559b97c53763aa9eaaf1254eb Mon Sep 17 00:00:00 2001 From: Jeremy Roman Date: Wed, 15 Nov 2023 16:24:54 +0000 Subject: [PATCH] Use C++20 features to simplify blink::NativeValueTraitsBase. These allow some of the metaprogramming bits to be simplified a little. Change-Id: I052b4397586d21348401616e1792afdb9662f975 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5030335 Reviewed-by: Yuki Shiino Commit-Queue: Jeremy Roman Cr-Commit-Position: refs/heads/main@{#1224978} --- .../bindings/core/v8/native_value_traits.h | 54 ++---- .../v8/native_value_traits_buffer_sources.cc | 166 ++++++++---------- .../core/v8/native_value_traits_impl.h | 159 +++++++---------- 3 files changed, 151 insertions(+), 228 deletions(-) diff --git a/third_party/blink/renderer/bindings/core/v8/native_value_traits.h b/third_party/blink/renderer/bindings/core/v8/native_value_traits.h index 7fc91d14acc71a2..1e5a0790df6da81 100644 --- a/third_party/blink/renderer/bindings/core/v8/native_value_traits.h +++ b/third_party/blink/renderer/bindings/core/v8/native_value_traits.h @@ -5,6 +5,7 @@ #ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_NATIVE_VALUE_TRAITS_H_ #define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_NATIVE_VALUE_TRAITS_H_ +#include #include #include "third_party/blink/renderer/bindings/core/v8/idl_types_base.h" @@ -30,7 +31,7 @@ class ExceptionState; // return toInt32(isolate, value, exceptionState, NormalConversion); // } // } -template +template struct NativeValueTraits; // This declaration serves only as a blueprint for specializations: the @@ -45,22 +46,15 @@ struct NativeValueTraits; namespace bindings { -template -struct NativeValueTraitsHasIsNull : std::false_type {}; - template -struct NativeValueTraitsHasIsNull< - T, - std::void_t().IsNull())>> : std::true_type {}; +struct ImplTypeFor { + using type = T; +}; template -struct NativeValueTraitsHasNullValue { - // true if |T| supports IDL null value. - static constexpr bool value = - // ScriptValue, String, and union types have IsNull member function. - bindings::NativeValueTraitsHasIsNull::value || - // Pointer types have nullptr as IDL null value. - std::is_pointer::value; + requires std::derived_from +struct ImplTypeFor { + using type = typename T::ImplType; }; } // namespace bindings @@ -78,37 +72,17 @@ struct NativeValueTraitsHasNullValue { // If present, |NullValue()| will be used when converting from the nullable type // T?, and should be used if the impl type has an existing "null" state. If not // present, WTF::Optional will be used to wrap the type. -template -struct NativeValueTraitsBase { - STATIC_ONLY(NativeValueTraitsBase); - - using ImplType = T; - - static constexpr bool has_null_value = - bindings::NativeValueTraitsHasNullValue::value; - - template - static decltype(auto) ArgumentValue(v8::Isolate* isolate, - int argument_index, - v8::Local value, - ExceptionState& exception_state, - ExtraArgs... extra_args) { - return NativeValueTraits>::NativeValue( - isolate, value, exception_state, - std::forward(extra_args)...); - } -}; - template -struct NativeValueTraitsBase< - T, - std::enable_if_t::value>> { +struct NativeValueTraitsBase { STATIC_ONLY(NativeValueTraitsBase); - using ImplType = typename T::ImplType; + using ImplType = bindings::ImplTypeFor::type; + // Pointer types have nullptr as IDL null value. + // ScriptValue, String, and union types have IsNull member function. static constexpr bool has_null_value = - bindings::NativeValueTraitsHasNullValue::value; + std::is_pointer_v || + requires(ImplType value) { value.IsNull(); }; template static decltype(auto) ArgumentValue(v8::Isolate* isolate, diff --git a/third_party/blink/renderer/bindings/core/v8/native_value_traits_buffer_sources.cc b/third_party/blink/renderer/bindings/core/v8/native_value_traits_buffer_sources.cc index 508ea6d8eea481e..18de71d84023f0c 100644 --- a/third_party/blink/renderer/bindings/core/v8/native_value_traits_buffer_sources.cc +++ b/third_party/blink/renderer/bindings/core/v8/native_value_traits_buffer_sources.cc @@ -7,6 +7,7 @@ #include "third_party/blink/renderer/core/core_export.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/frame/web_feature.h" +#include "third_party/blink/renderer/core/typed_arrays/flexible_array_buffer_view.h" #include "third_party/blink/renderer/core/typed_arrays/typed_flexible_array_buffer_view.h" namespace blink { @@ -698,12 +699,11 @@ DOMArrayBufferBase* NativeValueTraits< // ArrayBufferView template -NotShared NativeValueTraits< - NotShared, - typename std::enable_if_t::value>>:: - NativeValue(v8::Isolate* isolate, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +NotShared NativeValueTraits>::NativeValue( + v8::Isolate* isolate, + v8::Local value, + ExceptionState& exception_state) { return NativeValueImpl< RecipeTrait>, ToDOMViewType, Nullablity::kIsNotNullable, BufferSizeCheck::kCheck, @@ -712,13 +712,12 @@ NotShared NativeValueTraits< } template -NotShared NativeValueTraits< - NotShared, - typename std::enable_if_t::value>>:: - ArgumentValue(v8::Isolate* isolate, - int argument_index, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +NotShared NativeValueTraits>::ArgumentValue( + v8::Isolate* isolate, + int argument_index, + v8::Local value, + ExceptionState& exception_state) { return ArgumentValueImpl< RecipeTrait>, ToDOMViewType, Nullablity::kIsNotNullable, BufferSizeCheck::kCheck, @@ -729,12 +728,11 @@ NotShared NativeValueTraits< // [AllowShared] ArrayBufferView template -MaybeShared NativeValueTraits< - MaybeShared, - typename std::enable_if_t::value>>:: - NativeValue(v8::Isolate* isolate, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +MaybeShared NativeValueTraits>::NativeValue( + v8::Isolate* isolate, + v8::Local value, + ExceptionState& exception_state) { return NativeValueImpl>, ToDOMViewType, Nullablity::kIsNotNullable, BufferSizeCheck::kCheck, @@ -743,13 +741,12 @@ MaybeShared NativeValueTraits< } template -MaybeShared NativeValueTraits< - MaybeShared, - typename std::enable_if_t::value>>:: - ArgumentValue(v8::Isolate* isolate, - int argument_index, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +MaybeShared NativeValueTraits>::ArgumentValue( + v8::Isolate* isolate, + int argument_index, + v8::Local value, + ExceptionState& exception_state) { return ArgumentValueImpl>, ToDOMViewType, Nullablity::kIsNotNullable, BufferSizeCheck::kCheck, @@ -760,12 +757,12 @@ MaybeShared NativeValueTraits< // [AllowShared, BufferSourceTypeNoSizeLimit] ArrayBufferView template -MaybeShared NativeValueTraits< - IDLBufferSourceTypeNoSizeLimit>, - typename std::enable_if_t::value>>:: - NativeValue(v8::Isolate* isolate, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +MaybeShared +NativeValueTraits>>::NativeValue( + v8::Isolate* isolate, + v8::Local value, + ExceptionState& exception_state) { return NativeValueImpl< RecipeTrait>, ToDOMViewType, Nullablity::kIsNotNullable, BufferSizeCheck::kDoNotCheck, @@ -774,13 +771,12 @@ MaybeShared NativeValueTraits< } template -MaybeShared NativeValueTraits< - IDLBufferSourceTypeNoSizeLimit>, - typename std::enable_if_t::value>>:: - ArgumentValue(v8::Isolate* isolate, - int argument_index, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +MaybeShared NativeValueTraits>>::ArgumentValue(v8::Isolate* isolate, + int argument_index, + v8::Local value, + ExceptionState& exception_state) { return ArgumentValueImpl< RecipeTrait>, ToDOMViewType, Nullablity::kIsNotNullable, BufferSizeCheck::kDoNotCheck, @@ -791,12 +787,11 @@ MaybeShared NativeValueTraits< // Nullable ArrayBufferView template -NotShared NativeValueTraits< - IDLNullable>, - typename std::enable_if_t::value>>:: - NativeValue(v8::Isolate* isolate, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +NotShared NativeValueTraits>>::NativeValue( + v8::Isolate* isolate, + v8::Local value, + ExceptionState& exception_state) { return NativeValueImpl< RecipeTrait>, ToDOMViewType, Nullablity::kIsNullable, BufferSizeCheck::kCheck, @@ -805,13 +800,12 @@ NotShared NativeValueTraits< } template -NotShared NativeValueTraits< - IDLNullable>, - typename std::enable_if_t::value>>:: - ArgumentValue(v8::Isolate* isolate, - int argument_index, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +NotShared NativeValueTraits>>::ArgumentValue( + v8::Isolate* isolate, + int argument_index, + v8::Local value, + ExceptionState& exception_state) { return ArgumentValueImpl< RecipeTrait>, ToDOMViewType, Nullablity::kIsNullable, BufferSizeCheck::kCheck, @@ -822,12 +816,11 @@ NotShared NativeValueTraits< // Nullable [AllowShared] ArrayBufferView template -MaybeShared NativeValueTraits< - IDLNullable>, - typename std::enable_if_t::value>>:: - NativeValue(v8::Isolate* isolate, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +MaybeShared NativeValueTraits>>::NativeValue( + v8::Isolate* isolate, + v8::Local value, + ExceptionState& exception_state) { return NativeValueImpl>, ToDOMViewType, Nullablity::kIsNullable, BufferSizeCheck::kCheck, @@ -836,13 +829,12 @@ MaybeShared NativeValueTraits< } template -MaybeShared NativeValueTraits< - IDLNullable>, - typename std::enable_if_t::value>>:: - ArgumentValue(v8::Isolate* isolate, - int argument_index, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +MaybeShared NativeValueTraits>>::ArgumentValue( + v8::Isolate* isolate, + int argument_index, + v8::Local value, + ExceptionState& exception_state) { return ArgumentValueImpl>, ToDOMViewType, Nullablity::kIsNullable, BufferSizeCheck::kCheck, @@ -853,9 +845,9 @@ MaybeShared NativeValueTraits< // Nullable [AllowShared, BufferSourceTypeNoSizeLimit] ArrayBufferView template -MaybeShared NativeValueTraits< - IDLNullable>>, - typename std::enable_if_t::value>>:: + requires std::derived_from +MaybeShared +NativeValueTraits>>>:: ArgumentValue(v8::Isolate* isolate, int argument_index, v8::Local value, @@ -870,13 +862,11 @@ MaybeShared NativeValueTraits< // [AllowShared, FlexibleArrayBufferView] ArrayBufferView template -T NativeValueTraits::value>>:: - ArgumentValue(v8::Isolate* isolate, - int argument_index, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +T NativeValueTraits::ArgumentValue(v8::Isolate* isolate, + int argument_index, + v8::Local value, + ExceptionState& exception_state) { return ArgumentValueImpl, ToFlexibleArrayBufferView, Nullablity::kIsNotNullable, BufferSizeCheck::kCheck, ResizableAllowance::kDisallowResizable, @@ -888,13 +878,12 @@ T NativeValueTraits -T NativeValueTraits, - typename std::enable_if_t< - std::is_base_of::value>>:: - ArgumentValue(v8::Isolate* isolate, - int argument_index, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +T NativeValueTraits>::ArgumentValue( + v8::Isolate* isolate, + int argument_index, + v8::Local value, + ExceptionState& exception_state) { return ArgumentValueImpl< RecipeTrait, ToFlexibleArrayBufferView, Nullablity::kIsNotNullable, BufferSizeCheck::kDoNotCheck, ResizableAllowance::kDisallowResizable, @@ -905,13 +894,12 @@ T NativeValueTraits, // Nullable [AllowShared, FlexibleArrayBufferView] ArrayBufferView template -T NativeValueTraits, - typename std::enable_if_t< - std::is_base_of::value>>:: - ArgumentValue(v8::Isolate* isolate, - int argument_index, - v8::Local value, - ExceptionState& exception_state) { + requires std::derived_from +T NativeValueTraits>::ArgumentValue( + v8::Isolate* isolate, + int argument_index, + v8::Local value, + ExceptionState& exception_state) { return ArgumentValueImpl, ToFlexibleArrayBufferView, Nullablity::kIsNullable, BufferSizeCheck::kCheck, ResizableAllowance::kDisallowResizable, diff --git a/third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h b/third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h index 899929dcf49f90a..5011503dcf1c0c8 100644 --- a/third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h +++ b/third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h @@ -5,6 +5,9 @@ #ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_NATIVE_VALUE_TRAITS_IMPL_H_ #define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_NATIVE_VALUE_TRAITS_IMPL_H_ +#include +#include + #include "third_party/abseil-cpp/absl/types/optional.h" #include "third_party/blink/renderer/bindings/core/v8/idl_types.h" #include "third_party/blink/renderer/bindings/core/v8/native_value_traits.h" @@ -715,9 +718,8 @@ struct CORE_EXPORT NativeValueTraits< }; template -struct NativeValueTraits< - T, - typename std::enable_if_t::value>> { + requires std::derived_from +struct NativeValueTraits { // NotShared or MaybeShared should be used instead. static T* NativeValue(v8::Isolate* isolate, v8::Local value, @@ -729,9 +731,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - IDLNullable, - typename std::enable_if_t::value>> { + requires std::derived_from +struct NativeValueTraits> { // NotShared or MaybeShared should be used instead. static T* NativeValue(v8::Isolate* isolate, v8::Local value, @@ -743,9 +744,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - NotShared, - typename std::enable_if_t::value>> + requires std::derived_from +struct NativeValueTraits> : public NativeValueTraitsBase> { static NotShared NativeValue(v8::Isolate* isolate, v8::Local value, @@ -758,9 +758,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - IDLNullable>, - typename std::enable_if_t::value>> + requires std::derived_from +struct NativeValueTraits>> : public NativeValueTraitsBase> { static NotShared NativeValue(v8::Isolate* isolate, v8::Local value, @@ -773,9 +772,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - MaybeShared, - typename std::enable_if_t::value>> + requires std::derived_from +struct NativeValueTraits> : public NativeValueTraitsBase> { static MaybeShared NativeValue(v8::Isolate* isolate, v8::Local value, @@ -788,9 +786,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - IDLBufferSourceTypeNoSizeLimit>, - typename std::enable_if_t::value>> + requires std::derived_from +struct NativeValueTraits>> : public NativeValueTraitsBase> { // FlexibleArrayBufferView uses this in its implementation, so we cannot // delete it. @@ -805,9 +802,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - IDLNullable>, - typename std::enable_if_t::value>> + requires std::derived_from +struct NativeValueTraits>> : public NativeValueTraitsBase> { static MaybeShared NativeValue(v8::Isolate* isolate, v8::Local value, @@ -820,9 +816,9 @@ struct NativeValueTraits< }; template + requires std::derived_from struct NativeValueTraits< - IDLNullable>>, - typename std::enable_if_t::value>> + IDLNullable>>> : public NativeValueTraitsBase> { // BufferSourceTypeNoSizeLimit must be used only as arguments. static MaybeShared NativeValue(v8::Isolate* isolate, @@ -836,11 +832,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - T, - typename std::enable_if_t< - std::is_base_of::value>> - : public NativeValueTraitsBase { + requires std::derived_from +struct NativeValueTraits : public NativeValueTraitsBase { // FlexibleArrayBufferView must be used only as arguments. static T NativeValue(v8::Isolate* isolate, v8::Local value, @@ -853,10 +846,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - IDLBufferSourceTypeNoSizeLimit, - typename std::enable_if_t< - std::is_base_of::value>> + requires std::derived_from +struct NativeValueTraits> : public NativeValueTraitsBase { // BufferSourceTypeNoSizeLimit and FlexibleArrayBufferView must be used only // as arguments. @@ -871,11 +862,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - IDLNullable, - typename std::enable_if_t< - std::is_base_of::value>> - : public NativeValueTraitsBase { + requires std::derived_from +struct NativeValueTraits> : public NativeValueTraitsBase { // FlexibleArrayBufferView must be used only as arguments. static T NativeValue(v8::Isolate* isolate, v8::Local value, @@ -1134,9 +1122,8 @@ NativeValueTraits>::NativeValue( } template -struct NativeValueTraits>, - typename std::enable_if_t< - NativeValueTraits>::has_null_value>> + requires NativeValueTraits>::has_null_value +struct NativeValueTraits>> : public NativeValueTraitsBase>*> { using ImplType = typename NativeValueTraits>::ImplType*; @@ -1203,9 +1190,8 @@ struct NativeValueTraits> : public NativeValueTraits> {}; template -struct NativeValueTraits>, - typename std::enable_if_t< - NativeValueTraits>::has_null_value>> + requires NativeValueTraits>::has_null_value +struct NativeValueTraits>> : public NativeValueTraits>> {}; // Record types @@ -1335,10 +1321,8 @@ struct NativeValueTraits> // Callback function types template -struct NativeValueTraits< - T, - typename std::enable_if_t::value>> - : public NativeValueTraitsBase { + requires std::derived_from +struct NativeValueTraits : public NativeValueTraitsBase { static T* NativeValue(v8::Isolate* isolate, v8::Local value, ExceptionState& exception_state) { @@ -1361,9 +1345,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - IDLNullable, - typename std::enable_if_t::value>> + requires std::derived_from +struct NativeValueTraits> : public NativeValueTraitsBase> { static T* NativeValue(v8::Isolate* isolate, v8::Local value, @@ -1392,10 +1375,8 @@ struct NativeValueTraits< // Callback interface types template -struct NativeValueTraits< - T, - typename std::enable_if_t::value>> - : public NativeValueTraitsBase { + requires std::derived_from +struct NativeValueTraits : public NativeValueTraitsBase { static T* NativeValue(v8::Isolate* isolate, v8::Local value, ExceptionState& exception_state) { @@ -1418,9 +1399,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - IDLNullable, - typename std::enable_if_t::value>> + requires std::derived_from +struct NativeValueTraits> : public NativeValueTraitsBase> { static T* NativeValue(v8::Isolate* isolate, v8::Local value, @@ -1449,11 +1429,8 @@ struct NativeValueTraits< // Dictionary types template -struct NativeValueTraits< - T, - typename std::enable_if_t< - std::is_base_of::value>> - : public NativeValueTraitsBase { + requires std::derived_from +struct NativeValueTraits : public NativeValueTraitsBase { static T* NativeValue(v8::Isolate* isolate, v8::Local value, ExceptionState& exception_state) { @@ -1464,14 +1441,11 @@ struct NativeValueTraits< // We don't support nullable dictionary types in general since it's quite // confusing and often misused. template -struct NativeValueTraits< - IDLNullable, - typename std::enable_if_t< - std::is_base_of::value && - (std::is_same::value || - std::is_same::value || - std::is_same::value)>> - : public NativeValueTraitsBase { + requires std::derived_from && + (std::same_as || + std::same_as || + std::same_as) +struct NativeValueTraits> : public NativeValueTraitsBase { static T* NativeValue(v8::Isolate* isolate, v8::Local value, ExceptionState& exception_state) { @@ -1483,11 +1457,8 @@ struct NativeValueTraits< // Enumeration types template -struct NativeValueTraits< - T, - typename std::enable_if_t< - std::is_base_of::value>> - : public NativeValueTraitsBase { + requires std::derived_from +struct NativeValueTraits : public NativeValueTraitsBase { static T NativeValue(v8::Isolate* isolate, v8::Local value, ExceptionState& exception_state) { @@ -1497,10 +1468,8 @@ struct NativeValueTraits< // Interface types template -struct NativeValueTraits< - T, - typename std::enable_if_t::value>> - : public NativeValueTraitsBase { + requires std::derived_from +struct NativeValueTraits : public NativeValueTraitsBase { static inline T* NativeValue(v8::Isolate* isolate, v8::Local value, ExceptionState& exception_state) { @@ -1528,9 +1497,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - IDLNullable, - typename std::enable_if_t::value>> + requires std::derived_from +struct NativeValueTraits> : public NativeValueTraitsBase> { static inline T* NativeValue(v8::Isolate* isolate, v8::Local value, @@ -1565,10 +1533,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - T, - typename std::enable_if_t::value>> - : public NativeValueTraitsBase { + requires std::derived_from +struct NativeValueTraits : public NativeValueTraitsBase { static T* NativeValue(v8::Isolate* isolate, v8::Local value, ExceptionState& exception_state) { @@ -1584,10 +1550,8 @@ struct NativeValueTraits< }; template -struct NativeValueTraits< - IDLNullable, - typename std::enable_if_t::value>> - : public NativeValueTraitsBase { + requires std::derived_from +struct NativeValueTraits> : public NativeValueTraitsBase { static T* NativeValue(v8::Isolate* isolate, v8::Local value, ExceptionState& exception_state) { @@ -1608,9 +1572,8 @@ struct NativeValueTraits< // Nullable types template -struct NativeValueTraits< - IDLNullable, - typename std::enable_if_t::has_null_value>> + requires(!NativeValueTraits::has_null_value) +struct NativeValueTraits> : public NativeValueTraitsBase> { // https://webidl.spec.whatwg.org/#es-nullable-type using ImplType = @@ -1642,9 +1605,8 @@ struct NativeValueTraits>>; // Optional types template -struct NativeValueTraits, - typename std::enable_if_t::ImplType>::value>> + requires std::is_arithmetic_v::ImplType> +struct NativeValueTraits> : public NativeValueTraitsBase::ImplType> { using ImplType = typename NativeValueTraits::ImplType; @@ -1666,9 +1628,8 @@ struct NativeValueTraits, }; template -struct NativeValueTraits, - typename std::enable_if_t::ImplType>::value>> + requires std::is_pointer_v::ImplType> +struct NativeValueTraits> : public NativeValueTraitsBase::ImplType> { using ImplType = typename NativeValueTraits::ImplType;