5#ifndef INCLUDE_V8_FAST_API_CALLS_H_
6#define INCLUDE_V8_FAST_API_CALLS_H_
226#include <type_traits>
273 "There is no special support in V8 anymore, there is no need to"
274 "use a SequenceType") SequenceType : uint8_t {
289 : type_(type), sequence_type_(SequenceType::kScalar), flags_(flags) {}
294 : type_(type), sequence_type_(sequence_type), flags_(flags) {}
299 static_cast<SequenceType>((identifier >> 8) & 255),
300 static_cast<
Flags>(identifier & 255)) {}
302 return static_cast<uint8_t
>(type_) << 16 |
303 static_cast<uint8_t
>(sequence_type_) << 8 |
304 static_cast<uint8_t
>(flags_);
329 SequenceType sequence_type_;
360 return HasOptions() ? arg_count_ - 1 : arg_count_;
373 return arg_count_ > 0 && arg_info_[arg_count_ - 1].GetType() ==
379 const Int64Representation repr_;
380 const unsigned int arg_count_;
384struct FastApiCallbackOptions;
390#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
394 AnyCType& operator=(
const AnyCType& other) {
395 int64_value = other.int64_value;
416 "The union AnyCType should have size == 64 bits, as this is assumed "
417 "by EffectControlLinearizer.");
421 constexpr CFunction() : address_(nullptr), type_info_(nullptr) {}
426 return type_info_->ArgumentInfo(index);
433 return type_info_->GetInt64Representation();
439 template <
typename F>
443 CFunction result = ArgUnwrap<F*>::Make(func, int64_rep);
449 template <
typename R,
typename... Args,
typename R_Patch,
450 typename... Args_Patch>
452 R_Patch (*patching_func)(Args_Patch...),
457 sizeof...(Args_Patch) ==
sizeof...(Args),
458 "The patching function must have the same number of arguments.");
459 c_func.address_ =
reinterpret_cast<void*
>(patching_func);
466 const void* address_;
469 template <
typename F>
471 static_assert(
sizeof(F) !=
sizeof(F),
472 "CFunction must be created from a function pointer.");
475 template <
typename R,
typename... Args>
476 class ArgUnwrap<R (*)(Args...)> {
478 static CFunction Make(R (*func)(Args...),
510template <
typename T,
typename... List>
511struct count : std::integral_constant<int, 0> {};
512template <
typename T,
typename... Args>
514 : std::integral_constant<std::size_t, 1 + count<T, Args...>::value> {};
515template <
typename T,
typename U,
typename... Args>
519 typename RetBuilder,
typename... ArgBuilders>
521 static constexpr int kOptionsArgCount =
523 static constexpr int kReceiverCount = 1;
525 static_assert(kOptionsArgCount == 0 || kOptionsArgCount == 1,
526 "Only one options parameter is supported.");
528 static_assert(
sizeof...(ArgBuilders) >= kOptionsArgCount + kReceiverCount,
529 "The receiver or the options argument is missing.");
534 arg_info_storage_, Representation),
535 arg_info_storage_{ArgBuilders::Build()...} {
547 "String and api object values are not currently "
548 "supported return types.");
552 const CTypeInfo arg_info_storage_[
sizeof...(ArgBuilders)];
557 static_assert(
sizeof(T) !=
sizeof(T),
"This type is not supported");
560#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum) \
562 struct TypeInfoHelper<T> { \
563 static constexpr CTypeInfo::Flags Flags() { \
564 return CTypeInfo::Flags::kNone; \
567 static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \
570template <CTypeInfo::Type type>
573#define DEFINE_TYPE_INFO_TRAITS(CType, Enum) \
575 struct CTypeInfoTraits<CTypeInfo::Type::Enum> { \
576 using ctype = CType; \
579#define PRIMITIVE_C_TYPES(V) \
583 V(uint32_t, kUint32) \
585 V(uint64_t, kUint64) \
587 V(double, kFloat64) \
591#define ALL_C_TYPES(V) \
592 PRIMITIVE_C_TYPES(V) \
594 V(v8::Local<v8::Value>, kV8Value) \
595 V(v8::Local<v8::Object>, kV8Value) \
596 V(v8::Local<v8::Array>, kV8Value) \
606#undef PRIMITIVE_C_TYPES
609#undef TYPED_ARRAY_C_TYPES
629#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG) \
630 static_assert(((COND) == 0) || (ASSERTION), MSG)
647 "kEnforceRangeBit is only allowed for integral types.");
651 "kClampBit is only allowed for integral types.");
655 "kIsRestrictedBit is only allowed for floating point types.");
662 template <
typename... Rest>
667 static constexpr CTypeInfo::Flags MergeFlags() {
return CTypeInfo::Flags(0); }
671template <
typename RetBuilder,
typename... ArgBuilders>
680 ArgBuilders...>(fn_);
687 return ArgImpl<N, Flags...>(
688 std::make_index_sequence<
sizeof...(ArgBuilders)>());
692 template <
typename Ret,
typename... Args>
695 sizeof...(Args) ==
sizeof...(ArgBuilders),
696 "The patching function must have the same number of arguments.");
697 fn_ =
reinterpret_cast<void*
>(patching_func);
711 struct GetArgBuilder;
716 struct GetArgBuilder<false, N, Flags...> {
718 typename std::tuple_element<N, std::tuple<ArgBuilders...>>::type;
724 struct GetArgBuilder<true, N, Flags...> {
726 typename std::tuple_element<N,
727 std::tuple<ArgBuilders...>>::type::BaseType,
728 std::tuple_element<N, std::tuple<ArgBuilders...>>::type::Build()
737 constexpr auto ArgImpl(std::index_sequence<I...>) {
739 RetBuilder,
typename GetArgBuilder<N == I, I, Flags...>::type...>(fn_);
749 template <
typename R,
typename... Args>
750 constexpr auto Fn(R (*fn)(Args...)) {
753 reinterpret_cast<const void*
>(fn));
760template <
typename R,
typename... Args>
761CFunction CFunction::ArgUnwrap<R (*)(Args...)>::Make(
764 return internal::CFunctionBuilder().Fn(func).Build();
768 .template Build<CFunctionInfo::Int64Representation::kBigInt>();
774static constexpr CTypeInfo kTypeInfoFloat64 =
789template <CTypeInfo::Identifier type_info_
id,
typename T>
797 uint32_t max_length);
803 uint32_t max_length);
809 uint32_t max_length);
815 uint32_t max_length);
CFunction(const void *address, const CFunctionInfo *type_info)
const CTypeInfo & ArgumentInfo(unsigned int index) const
CFunctionInfo::Int64Representation GetInt64Representation() const
const void * GetAddress() const
const CFunctionInfo * GetTypeInfo() const
unsigned int ArgumentCount() const
static CFunction Make(F *func, CFunctionInfo::Int64Representation int64_rep=CFunctionInfo::Int64Representation::kNumber)
const CTypeInfo & ReturnInfo() const
static CFunction Make(R(*func)(Args...), R_Patch(*patching_func)(Args_Patch...), CFunctionInfo::Int64Representation int64_rep=CFunctionInfo::Int64Representation::kNumber)
Int64Representation GetInt64Representation() const
const CTypeInfo & ArgumentInfo(unsigned int index) const
unsigned int ArgumentCount() const
CFunctionInfo(const CTypeInfo &return_info, unsigned int arg_count, const CTypeInfo *arg_info, Int64Representation repr=Int64Representation::kNumber)
const CTypeInfo & ReturnInfo() const
static constexpr CTypeInfo Build()
constexpr CTypeInfo(Type type, Flags flags=Flags::kNone)
constexpr Identifier GetId() const
constexpr SequenceType GetSequenceType() const
static constexpr Type kCallbackOptionsType
static constexpr bool IsIntegralType(Type type)
constexpr CTypeInfo(Identifier identifier)
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()
internal::CFunctionBuilder CFunctionBuilder
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer(Local< Array > src, T *dst, uint32_t max_length)
static FastApiCallbackOptions CreateForTesting(Isolate *isolate)
v8::Local< v8::Value > data
static constexpr CTypeInfo::Flags Flags()
static constexpr CTypeInfo::Type Type()
static constexpr CTypeInfo::Flags Flags()
static constexpr CTypeInfo::Type Type()
const FastOneByteString * string_value
FastApiCallbackOptions * options_value
Local< Object > object_value
Local< Array > sequence_value
#define DEFINE_TYPE_INFO_TRAITS(CType, Enum)
#define PRIMITIVE_C_TYPES(V)
#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum)
#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG)
#define V8_DEPRECATE_SOON(message)
#define END_ALLOW_USE_DEPRECATED()
#define V8_WARN_UNUSED_RESULT
#define START_ALLOW_USE_DEPRECATED()