219#ifndef INCLUDE_V8_FAST_API_CALLS_H_
220#define INCLUDE_V8_FAST_API_CALLS_H_
226#include <type_traits>
290 : type_(type), sequence_type_(sequence_type), flags_(flags) {}
296 static_cast<
Flags>(identifier & 255)) {}
298 return static_cast<uint8_t
>(type_) << 16 |
299 static_cast<uint8_t
>(sequence_type_) << 8 |
300 static_cast<uint8_t
>(flags_);
347 memcpy(&tmp,
reinterpret_cast<T*
>(data_) + index,
sizeof(T));
352 if (
reinterpret_cast<uintptr_t
>(data_) %
alignof(T) != 0) {
355 *elements =
reinterpret_cast<T*
>(data_);
404 return HasOptions() ? arg_count_ - 1 : arg_count_;
415 return arg_count_ > 0 && arg_info_[arg_count_ - 1].GetType() ==
416 CTypeInfo::kCallbackOptionsType;
421 const unsigned int arg_count_;
425struct FastApiCallbackOptions;
455 sizeof(AnyCType) == 8,
456 "The AnyCType struct should have size == 64 bits, as this is assumed "
457 "by EffectControlLinearizer.");
461 constexpr CFunction() : address_(nullptr), type_info_(nullptr) {}
466 return type_info_->ArgumentInfo(index);
484 return OverloadResolution::kAtCompileTime;
489 for (
unsigned int i = 0; i < ArgumentCount(); ++i) {
490 if (ArgumentInfo(i).GetSequenceType() !=
492 if (diff_index >= 0) {
493 return OverloadResolution::kImpossible;
498 if (ArgumentInfo(i).GetSequenceType() ==
499 CTypeInfo::SequenceType::kScalar ||
501 CTypeInfo::SequenceType::kScalar) {
502 return OverloadResolution::kImpossible;
507 return OverloadResolution::kAtRuntime;
510 template <
typename F>
512 return ArgUnwrap<F*>::Make(func);
516 template <
typename R,
typename... Args,
typename R_Patch,
517 typename... Args_Patch>
519 R_Patch (*patching_func)(Args_Patch...)) {
520 CFunction c_func = ArgUnwrap<R (*)(Args...)>::Make(func);
522 sizeof...(Args_Patch) ==
sizeof...(Args),
523 "The patching function must have the same number of arguments.");
524 c_func.address_ =
reinterpret_cast<void*
>(patching_func);
531 const void* address_;
534 template <
typename F>
536 static_assert(
sizeof(F) !=
sizeof(F),
537 "CFunction must be created from a function pointer.");
540 template <
typename R,
typename... Args>
541 class ArgUnwrap<R (*)(Args...)> {
543 static CFunction Make(R (*func)(Args...));
559 return {
false, {0},
nullptr};
593template <
typename T,
typename... List>
594struct count : std::integral_constant<int, 0> {};
595template <
typename T,
typename... Args>
597 : std::integral_constant<std::size_t, 1 + count<T, Args...>::value> {};
598template <
typename T,
typename U,
typename... Args>
601template <
typename RetBuilder,
typename... ArgBuilders>
603 static constexpr int kOptionsArgCount =
605 static constexpr int kReceiverCount = 1;
607 static_assert(kOptionsArgCount == 0 || kOptionsArgCount == 1,
608 "Only one options parameter is supported.");
610 static_assert(
sizeof...(ArgBuilders) >= kOptionsArgCount + kReceiverCount,
611 "The receiver or the options argument is missing.");
617 arg_info_storage_{ArgBuilders::Build()...} {
627 "64-bit int, string and api object values are not currently "
628 "supported return types.");
632 const CTypeInfo arg_info_storage_[
sizeof...(ArgBuilders)];
637 static_assert(
sizeof(T) !=
sizeof(T),
"This type is not supported");
640#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum) \
642 struct TypeInfoHelper<T> { \
643 static constexpr CTypeInfo::Flags Flags() { \
644 return CTypeInfo::Flags::kNone; \
647 static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \
648 static constexpr CTypeInfo::SequenceType SequenceType() { \
649 return CTypeInfo::SequenceType::kScalar; \
653template <CTypeInfo::Type type>
656#define DEFINE_TYPE_INFO_TRAITS(CType, Enum) \
658 struct CTypeInfoTraits<CTypeInfo::Type::Enum> { \
659 using ctype = CType; \
662#define PRIMITIVE_C_TYPES(V) \
666 V(uint32_t, kUint32) \
668 V(uint64_t, kUint64) \
670 V(double, kFloat64) \
674#define ALL_C_TYPES(V) \
675 PRIMITIVE_C_TYPES(V) \
677 V(v8::Local<v8::Value>, kV8Value) \
678 V(v8::Local<v8::Object>, kV8Value) \
688#undef PRIMITIVE_C_TYPES
691#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR_TA(T, Enum) \
693 struct TypeInfoHelper<const FastApiTypedArray<T>&> { \
694 static constexpr CTypeInfo::Flags Flags() { \
695 return CTypeInfo::Flags::kNone; \
698 static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \
699 static constexpr CTypeInfo::SequenceType SequenceType() { \
700 return CTypeInfo::SequenceType::kIsTypedArray; \
704#define TYPED_ARRAY_C_TYPES(V) \
707 V(uint32_t, kUint32) \
709 V(uint64_t, kUint64) \
715#undef TYPED_ARRAY_C_TYPES
761#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG) \
762 static_assert(((COND) == 0) || (ASSERTION), MSG)
779 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kAllowSharedBit),
780 (kSequenceType == CTypeInfo::SequenceType::kIsTypedArray ||
781 kSequenceType == CTypeInfo::SequenceType::kIsArrayBuffer),
782 "kAllowSharedBit is only allowed for TypedArrays and ArrayBuffers.");
784 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kEnforceRangeBit),
785 CTypeInfo::IsIntegralType(kType),
786 "kEnforceRangeBit is only allowed for integral types.");
788 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kClampBit),
789 CTypeInfo::IsIntegralType(kType),
790 "kClampBit is only allowed for integral types.");
792 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kIsRestrictedBit),
793 CTypeInfo::IsFloatingPointType(kType),
794 "kIsRestrictedBit is only allowed for floating point types.");
796 kType == CTypeInfo::Type::kVoid,
797 "Sequences are only supported from void type.");
799 kSequenceType == CTypeInfo::SequenceType::kIsTypedArray,
800 CTypeInfo::IsPrimitive(kType) || kType == CTypeInfo::Type::kVoid,
801 "TypedArrays are only supported from primitive types or void.");
809 template <
typename... Rest>
814 static constexpr CTypeInfo::Flags MergeFlags() {
return CTypeInfo::Flags(0); }
818template <
typename RetBuilder,
typename... ArgBuilders>
827 ArgBuilders...>(fn_);
834 return ArgImpl<N, Flags...>(
835 std::make_index_sequence<
sizeof...(ArgBuilders)>());
839 template <
typename Ret,
typename... Args>
842 sizeof...(Args) ==
sizeof...(ArgBuilders),
843 "The patching function must have the same number of arguments.");
844 fn_ =
reinterpret_cast<void*
>(patching_func);
855 struct GetArgBuilder;
860 struct GetArgBuilder<false, N, Flags...> {
862 typename std::tuple_element<N, std::tuple<ArgBuilders...>>::type;
868 struct GetArgBuilder<true, N, Flags...> {
870 typename std::tuple_element<N,
871 std::tuple<ArgBuilders...>>::type::BaseType,
872 std::tuple_element<N, std::tuple<ArgBuilders...>>::type::Build()
881 constexpr auto ArgImpl(std::index_sequence<I...>) {
882 return CFunctionBuilderWithFunction<
883 RetBuilder,
typename GetArgBuilder<N == I, I, Flags...>::type...>(fn_);
893 template <
typename R,
typename... Args>
894 constexpr auto Fn(R (*fn)(Args...)) {
897 reinterpret_cast<const void*
>(fn));
904template <
typename R,
typename... Args>
905CFunction CFunction::ArgUnwrap<R (*)(Args...)>::Make(R (*func)(Args...)) {
906 return internal::CFunctionBuilder().Fn(func).
Build();
912static constexpr CTypeInfo kTypeInfoFloat64 =
927template <CTypeInfo::Identifier type_info_
id,
typename T>
933TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<int32_t>::Build().GetId(),
935 uint32_t max_length);
939TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<uint32_t>::Build().GetId(),
941 uint32_t max_length);
945TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<float>::Build().GetId(),
947 uint32_t max_length);
951TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<double>::Build().GetId(),
953 uint32_t max_length);
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)
auto Patch(Ret(*patching_func)(Args...))
constexpr CFunctionInfoImpl()
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer(Local< Array > src, T *dst, uint32_t max_length)
const FastOneByteString * string_value
const FastApiTypedArray< int32_t > * int32_ta_value
const FastApiTypedArray< uint8_t > * uint8_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
FastApiTypedArray< uint8_t > *const wasm_memory
static FastApiCallbackOptions CreateForTesting(Isolate *isolate)
v8::Local< v8::Value > data
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()
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_WARN_UNUSED_RESULT