219#ifndef INCLUDE_V8_FAST_API_CALLS_H_
220#define INCLUDE_V8_FAST_API_CALLS_H_
226#include <type_traits>
287 : type_(type), sequence_type_(sequence_type), flags_(flags) {}
293 static_cast<
Flags>(identifier & 255)) {}
295 return static_cast<uint8_t
>(type_) << 16 |
296 static_cast<uint8_t
>(sequence_type_) << 8 |
297 static_cast<uint8_t
>(flags_);
343 memcpy(&tmp,
reinterpret_cast<T*
>(data_) + index,
sizeof(T));
348 if (
reinterpret_cast<uintptr_t
>(data_) %
alignof(T) != 0) {
351 *elements =
reinterpret_cast<T*
>(data_);
395 return HasOptions() ? arg_count_ - 1 : arg_count_;
406 return arg_count_ > 0 && arg_info_[arg_count_ - 1].GetType() ==
407 CTypeInfo::kCallbackOptionsType;
412 const unsigned int arg_count_;
416struct FastApiCallbackOptions;
443 sizeof(AnyCType) == 8,
444 "The AnyCType struct should have size == 64 bits, as this is assumed "
445 "by EffectControlLinearizer.");
449 constexpr CFunction() : address_(nullptr), type_info_(nullptr) {}
454 return type_info_->ArgumentInfo(index);
472 return OverloadResolution::kAtCompileTime;
477 for (
unsigned int i = 0; i < ArgumentCount(); ++i) {
478 if (ArgumentInfo(i).GetSequenceType() !=
480 if (diff_index >= 0) {
481 return OverloadResolution::kImpossible;
486 if (ArgumentInfo(i).GetSequenceType() ==
487 CTypeInfo::SequenceType::kScalar ||
489 CTypeInfo::SequenceType::kScalar) {
490 return OverloadResolution::kImpossible;
495 return OverloadResolution::kAtRuntime;
498 template <
typename F>
500 return ArgUnwrap<F*>::Make(func);
504 template <
typename R,
typename... Args,
typename R_Patch,
505 typename... Args_Patch>
507 R_Patch (*patching_func)(Args_Patch...)) {
508 CFunction c_func = ArgUnwrap<R (*)(Args...)>::Make(func);
510 sizeof...(Args_Patch) ==
sizeof...(Args),
511 "The patching function must have the same number of arguments.");
512 c_func.address_ =
reinterpret_cast<void*
>(patching_func);
519 const void* address_;
522 template <
typename F>
524 static_assert(
sizeof(F) !=
sizeof(F),
525 "CFunction must be created from a function pointer.");
528 template <
typename R,
typename... Args>
529 class ArgUnwrap<R (*)(Args...)> {
531 static CFunction Make(R (*func)(Args...));
576template <
typename T,
typename... List>
577struct count : std::integral_constant<int, 0> {};
578template <
typename T,
typename... Args>
580 : std::integral_constant<std::size_t, 1 + count<T, Args...>::value> {};
581template <
typename T,
typename U,
typename... Args>
584template <
typename RetBuilder,
typename... ArgBuilders>
586 static constexpr int kOptionsArgCount =
588 static constexpr int kReceiverCount = 1;
590 static_assert(kOptionsArgCount == 0 || kOptionsArgCount == 1,
591 "Only one options parameter is supported.");
593 static_assert(
sizeof...(ArgBuilders) >= kOptionsArgCount + kReceiverCount,
594 "The receiver or the options argument is missing.");
600 arg_info_storage_{ArgBuilders::Build()...} {
609 "64-bit int and api object values are not currently "
610 "supported return types.");
614 const CTypeInfo arg_info_storage_[
sizeof...(ArgBuilders)];
619 static_assert(
sizeof(T) !=
sizeof(T),
"This type is not supported");
622#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum) \
624 struct TypeInfoHelper<T> { \
625 static constexpr CTypeInfo::Flags Flags() { \
626 return CTypeInfo::Flags::kNone; \
629 static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \
630 static constexpr CTypeInfo::SequenceType SequenceType() { \
631 return CTypeInfo::SequenceType::kScalar; \
635template <CTypeInfo::Type type>
638#define DEFINE_TYPE_INFO_TRAITS(CType, Enum) \
640 struct CTypeInfoTraits<CTypeInfo::Type::Enum> { \
641 using ctype = CType; \
644#define PRIMITIVE_C_TYPES(V) \
647 V(uint32_t, kUint32) \
649 V(uint64_t, kUint64) \
654#define ALL_C_TYPES(V) \
655 PRIMITIVE_C_TYPES(V) \
657 V(v8::Local<v8::Value>, kV8Value) \
658 V(v8::Local<v8::Object>, kV8Value) \
668#undef PRIMITIVE_C_TYPES
671#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR_TA(T, Enum) \
673 struct TypeInfoHelper<const FastApiTypedArray<T>&> { \
674 static constexpr CTypeInfo::Flags Flags() { \
675 return CTypeInfo::Flags::kNone; \
678 static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \
679 static constexpr CTypeInfo::SequenceType SequenceType() { \
680 return CTypeInfo::SequenceType::kIsTypedArray; \
684#define TYPED_ARRAY_C_TYPES(V) \
686 V(uint32_t, kUint32) \
688 V(uint64_t, kUint64) \
694#undef TYPED_ARRAY_C_TYPES
728#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG) \
729 static_assert(((COND) == 0) || (ASSERTION), MSG)
746 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kAllowSharedBit),
747 (kSequenceType == CTypeInfo::SequenceType::kIsTypedArray ||
748 kSequenceType == CTypeInfo::SequenceType::kIsArrayBuffer),
749 "kAllowSharedBit is only allowed for TypedArrays and ArrayBuffers.");
751 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kEnforceRangeBit),
752 CTypeInfo::IsIntegralType(kType),
753 "kEnforceRangeBit is only allowed for integral types.");
755 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kClampBit),
756 CTypeInfo::IsIntegralType(kType),
757 "kClampBit is only allowed for integral types.");
759 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kIsRestrictedBit),
760 CTypeInfo::IsFloatingPointType(kType),
761 "kIsRestrictedBit is only allowed for floating point types.");
763 kType == CTypeInfo::Type::kVoid,
764 "Sequences are only supported from void type.");
766 kSequenceType == CTypeInfo::SequenceType::kIsTypedArray,
767 CTypeInfo::IsPrimitive(kType) || kType == CTypeInfo::Type::kVoid,
768 "TypedArrays are only supported from primitive types or void.");
776 template <
typename... Rest>
781 static constexpr CTypeInfo::Flags MergeFlags() {
return CTypeInfo::Flags(0); }
785template <
typename RetBuilder,
typename... ArgBuilders>
794 ArgBuilders...>(fn_);
801 return ArgImpl<N, Flags...>(
802 std::make_index_sequence<
sizeof...(ArgBuilders)>());
812 struct GetArgBuilder;
817 struct GetArgBuilder<false, N, Flags...> {
819 typename std::tuple_element<N, std::tuple<ArgBuilders...>>::type;
825 struct GetArgBuilder<true, N, Flags...> {
827 typename std::tuple_element<N,
828 std::tuple<ArgBuilders...>>::type::BaseType,
829 std::tuple_element<N, std::tuple<ArgBuilders...>>::type::Build()
838 constexpr auto ArgImpl(std::index_sequence<I...>) {
839 return CFunctionBuilderWithFunction<
840 RetBuilder,
typename GetArgBuilder<N == I, I, Flags...>::type...>(fn_);
850 template <
typename R,
typename... Args>
851 constexpr auto Fn(R (*fn)(Args...)) {
854 reinterpret_cast<const void*
>(fn));
861template <
typename R,
typename... Args>
862CFunction CFunction::ArgUnwrap<R (*)(Args...)>::Make(R (*func)(Args...)) {
863 return internal::CFunctionBuilder().Fn(func).
Build();
869static constexpr CTypeInfo kTypeInfoFloat64 =
884template <const CTypeInfo* type_info,
typename T>
886 "Use TryToCopyAndConvertArrayToCppBuffer<CTypeInfo::Identifier, T>()")
895 TryCopyAndConvertArrayToCppBuffer<&kTypeInfoInt32,
int32_t>(
902 "Use TryToCopyAndConvertArrayToCppBuffer<CTypeInfo::Identifier, T>()")
904 TryCopyAndConvertArrayToCppBuffer<&kTypeInfoFloat64,
double>(
909template <CTypeInfo::Identifier type_info_
id,
typename T>
915TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<int32_t>::Build().GetId(),
921TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<uint32_t>::Build().GetId(),
927TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<float>::Build().GetId(),
933TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<double>::Build().GetId(),
CFunction(const void *address, const CFunctionInfo *type_info)
static CFunction Make(R(*func)(Args...), R_Patch(*patching_func)(Args_Patch...))
const CTypeInfo & ArgumentInfo(unsigned int index) const
OverloadResolution GetOverloadResolution(const CFunction *other)
const void * GetAddress() const
const CFunctionInfo * GetTypeInfo() const
unsigned int ArgumentCount() const
const CTypeInfo & ReturnInfo() const
static CFunction Make(F *func)
CFunctionInfo(const CTypeInfo &return_info, unsigned int arg_count, const CTypeInfo *arg_info)
const CTypeInfo & ArgumentInfo(unsigned int index) const
unsigned int ArgumentCount() const
const CTypeInfo & ReturnInfo() const
static constexpr CTypeInfo Build()
constexpr Identifier GetId() const
constexpr SequenceType GetSequenceType() const
static constexpr Type kCallbackOptionsType
static constexpr bool IsIntegralType(Type type)
constexpr CTypeInfo(Identifier identifier)
constexpr CTypeInfo(Type type, SequenceType sequence_type=SequenceType::kScalar, Flags flags=Flags::kNone)
constexpr Flags GetFlags() const
static constexpr bool IsFloatingPointType(Type type)
constexpr Type GetType() const
static constexpr bool IsPrimitive(Type type)
constexpr CFunctionBuilder()
constexpr auto Fn(R(*fn)(Args...))
constexpr CFunctionBuilderWithFunction(const void *fn)
constexpr CFunctionInfoImpl()
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer(Local< Array > src, T *dst, uint32_t max_length)
int32_t(Local< Array > src, int32_t *dst, uint32_t max_length)
const FastApiTypedArray< int32_t > * int32_ta_value
const FastApiTypedArray< uint32_t > * uint32_ta_value
FastApiCallbackOptions * options_value
const FastApiTypedArray< float > * float_ta_value
const FastApiTypedArray< double > * double_ta_value
Local< Object > object_value
Local< Array > sequence_value
const FastApiTypedArray< int64_t > * int64_ta_value
const FastApiTypedArray< uint64_t > * uint64_ta_value
static FastApiCallbackOptions CreateForTesting(Isolate *isolate)
void V8_EXPORT ValidateIndex(size_t index) const
size_t V8_EXPORT length() const
V8_INLINE T get(size_t index) const
bool getStorageIfAligned(T **elements) const
static constexpr CTypeInfo::Flags Flags()
static constexpr CTypeInfo::Type Type()
static constexpr CTypeInfo::SequenceType SequenceType()
static constexpr CTypeInfo::Flags Flags()
static constexpr CTypeInfo::Type Type()
static constexpr CTypeInfo::SequenceType SequenceType()
static constexpr CTypeInfo::Flags Flags()
static constexpr CTypeInfo::Type Type()
static constexpr CTypeInfo::SequenceType SequenceType()
#define DEFINE_TYPE_INFO_TRAITS(CType, Enum)
#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR_TA(T, Enum)
#define PRIMITIVE_C_TYPES(V)
#define TYPED_ARRAY_C_TYPES(V)
#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum)
#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG)
#define V8_DEPRECATED(message)
#define V8_WARN_UNUSED_RESULT