Commit 6ff9c686 authored by Paolo Severini's avatar Paolo Severini Committed by V8 LUCI CQ

[fastcall] Refactor TryCopyAndConvertArrayToCppBuffer

Refactor TryCopyAndConvertArrayToCppBuffer to avoid using a CTypeInfo*
pointer as template argument. Use instead a uint32 encoded value
sufficient to reconstruct the CTypeInfo.

Bug: v8:11739
Change-Id: I74052e59b3fa5ebed00cdb938504ba1947d959d7
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3138832
Commit-Queue: Paolo Severini <paolosev@microsoft.com>
Reviewed-by: 's avatarMaya Lekova <mslekova@chromium.org>
Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76887}
parent ccfdb729
......@@ -277,6 +277,17 @@ class CTypeInfo {
Flags flags = Flags::kNone)
: type_(type), sequence_type_(sequence_type), flags_(flags) {}
typedef uint32_t Identifier;
explicit constexpr CTypeInfo(Identifier identifier)
: CTypeInfo(static_cast<Type>(identifier >> 16),
static_cast<SequenceType>((identifier >> 8) & 255),
static_cast<Flags>(identifier & 255)) {}
constexpr Identifier GetId() const {
return static_cast<uint8_t>(type_) << 16 |
static_cast<uint8_t>(sequence_type_) << 8 |
static_cast<uint8_t>(flags_);
}
constexpr Type GetType() const { return type_; }
constexpr SequenceType GetSequenceType() const { return sequence_type_; }
constexpr Flags GetFlags() const { return flags_; }
......@@ -816,23 +827,54 @@ static constexpr CTypeInfo kTypeInfoFloat64 =
* returns true on success. `type_info` will be used for conversions.
*/
template <const CTypeInfo* type_info, typename T>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryCopyAndConvertArrayToCppBuffer(
Local<Array> src, T* dst, uint32_t max_length);
V8_DEPRECATE_SOON(
"Use TryToCopyAndConvertArrayToCppBuffer<CTypeInfo::Identifier, T>()")
bool V8_EXPORT V8_WARN_UNUSED_RESULT
TryCopyAndConvertArrayToCppBuffer(Local<Array> src, T* dst,
uint32_t max_length);
template <>
V8_DEPRECATE_SOON(
"Use TryToCopyAndConvertArrayToCppBuffer<CTypeInfo::Identifier, T>()")
inline bool V8_WARN_UNUSED_RESULT
TryCopyAndConvertArrayToCppBuffer<&kTypeInfoInt32, int32_t>(
Local<Array> src, int32_t* dst, uint32_t max_length) {
return CopyAndConvertArrayToCppBufferInt32(src, dst, max_length);
TryCopyAndConvertArrayToCppBuffer<&kTypeInfoInt32, int32_t>(
Local<Array> src, int32_t* dst, uint32_t max_length) {
return false;
}
template <>
V8_DEPRECATE_SOON(
"Use TryToCopyAndConvertArrayToCppBuffer<CTypeInfo::Identifier, T>()")
inline bool V8_WARN_UNUSED_RESULT
TryCopyAndConvertArrayToCppBuffer<&kTypeInfoFloat64, double>(
Local<Array> src, double* dst, uint32_t max_length) {
return CopyAndConvertArrayToCppBufferFloat64(src, dst, max_length);
TryCopyAndConvertArrayToCppBuffer<&kTypeInfoFloat64, double>(
Local<Array> src, double* dst, uint32_t max_length) {
return false;
}
template <CTypeInfo::Identifier type_info_id, typename T>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer(
Local<Array> src, T* dst, uint32_t max_length);
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer<
internal::CTypeInfoBuilder<uint32_t>::Build().GetId(), int32_t>(
Local<Array> src, int32_t* dst, uint32_t max_length);
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer<
internal::CTypeInfoBuilder<uint32_t>::Build().GetId(), uint32_t>(
Local<Array> src, uint32_t* dst, uint32_t max_length);
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer<
internal::CTypeInfoBuilder<float>::Build().GetId(), float>(
Local<Array> src, float* dst, uint32_t max_length);
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer<
internal::CTypeInfoBuilder<double>::Build().GetId(), double>(
Local<Array> src, double* dst, uint32_t max_length);
} // namespace v8
#endif // INCLUDE_V8_FAST_API_CALLS_H_
......@@ -565,14 +565,6 @@ class BackingStoreBase {};
} // namespace internal
V8_EXPORT bool CopyAndConvertArrayToCppBufferInt32(Local<Array> src,
int32_t* dst,
uint32_t max_length);
V8_EXPORT bool CopyAndConvertArrayToCppBufferFloat64(Local<Array> src,
double* dst,
uint32_t max_length);
} // namespace v8
#endif // INCLUDE_V8_INTERNAL_H_
......@@ -264,12 +264,12 @@ void CopyDoubleElementsToTypedBuffer(T* dst, uint32_t length,
}
}
template <const CTypeInfo* type_info, typename T>
template <CTypeInfo::Identifier type_info_id, typename T>
bool CopyAndConvertArrayToCppBuffer(Local<Array> src, T* dst,
uint32_t max_length) {
static_assert(
std::is_same<
T, typename i::CTypeInfoTraits<type_info->GetType()>::ctype>::value,
std::is_same<T, typename i::CTypeInfoTraits<
CTypeInfo(type_info_id).GetType()>::ctype>::value,
"Type mismatch between the expected CTypeInfo::Type and the destination "
"array");
......@@ -299,11 +299,20 @@ bool CopyAndConvertArrayToCppBuffer(Local<Array> src, T* dst,
}
}
// Deprecated; to be removed.
template <const CTypeInfo* type_info, typename T>
inline bool V8_EXPORT TryCopyAndConvertArrayToCppBuffer(Local<Array> src,
T* dst,
uint32_t max_length) {
return CopyAndConvertArrayToCppBuffer<type_info, T>(src, dst, max_length);
return CopyAndConvertArrayToCppBuffer<type_info->GetId(), T>(src, dst,
max_length);
}
template <CTypeInfo::Identifier type_info_id, typename T>
inline bool V8_EXPORT TryToCopyAndConvertArrayToCppBuffer(Local<Array> src,
T* dst,
uint32_t max_length) {
return CopyAndConvertArrayToCppBuffer<type_info_id, T>(src, dst, max_length);
}
namespace internal {
......
......@@ -10455,16 +10455,44 @@ bool ConvertDouble(double d) {
} // namespace internal
bool CopyAndConvertArrayToCppBufferInt32(Local<Array> src, int32_t* dst,
uint32_t max_length) {
return CopyAndConvertArrayToCppBuffer<&v8::kTypeInfoInt32, int32_t>(
src, dst, max_length);
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer<
internal::CTypeInfoBuilder<int32_t>::Build().GetId(), int32_t>(
Local<Array> src, int32_t* dst, uint32_t max_length) {
return CopyAndConvertArrayToCppBuffer<
CTypeInfo(CTypeInfo::Type::kInt32, CTypeInfo::SequenceType::kIsSequence)
.GetId(),
int32_t>(src, dst, max_length);
}
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer<
internal::CTypeInfoBuilder<uint32_t>::Build().GetId(), uint32_t>(
Local<Array> src, uint32_t* dst, uint32_t max_length) {
return CopyAndConvertArrayToCppBuffer<
CTypeInfo(CTypeInfo::Type::kUint32, CTypeInfo::SequenceType::kIsSequence)
.GetId(),
uint32_t>(src, dst, max_length);
}
bool CopyAndConvertArrayToCppBufferFloat64(Local<Array> src, double* dst,
uint32_t max_length) {
return CopyAndConvertArrayToCppBuffer<&v8::kTypeInfoFloat64, double>(
src, dst, max_length);
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer<
internal::CTypeInfoBuilder<float>::Build().GetId(), float>(
Local<Array> src, float* dst, uint32_t max_length) {
return CopyAndConvertArrayToCppBuffer<
CTypeInfo(CTypeInfo::Type::kFloat32, CTypeInfo::SequenceType::kIsSequence)
.GetId(),
float>(src, dst, max_length);
}
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer<
internal::CTypeInfoBuilder<double>::Build().GetId(), double>(
Local<Array> src, double* dst, uint32_t max_length) {
return CopyAndConvertArrayToCppBuffer<
CTypeInfo(CTypeInfo::Type::kFloat64, CTypeInfo::SequenceType::kIsSequence)
.GetId(),
double>(src, dst, max_length);
}
} // namespace v8
......
......@@ -95,10 +95,8 @@ class FastCApiObject {
#ifdef V8_ENABLE_FP_PARAMS_IN_C_LINKAGE
typedef double Type;
#define type_info kTypeInfoFloat64
#else
typedef int32_t Type;
#define type_info kTypeInfoInt32
#endif // V8_ENABLE_FP_PARAMS_IN_C_LINKAGE
static Type AddAllSequenceFastCallback(Local<Object> receiver,
bool should_fallback,
......@@ -120,8 +118,9 @@ class FastCApiObject {
}
Type buffer[1024];
bool result = TryCopyAndConvertArrayToCppBuffer<&type_info, Type>(
seq_arg, buffer, 1024);
bool result = TryToCopyAndConvertArrayToCppBuffer<
i::CTypeInfoBuilder<Type>::Build().GetId(), Type>(seq_arg, buffer,
1024);
if (!result) {
options.fallback = 1;
return 0;
......
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