219#ifndef INCLUDE_V8_FAST_API_CALLS_H_
220#define INCLUDE_V8_FAST_API_CALLS_H_
226#include <type_traits>
275 "TypedArrays are not supported directly anymore."),
291 : type_(type), sequence_type_(sequence_type), flags_(flags) {}
297 static_cast<
Flags>(identifier & 255)) {}
299 return static_cast<uint8_t
>(type_) << 16 |
300 static_cast<uint8_t
>(sequence_type_) << 8 |
301 static_cast<uint8_t
>(flags_);
330 "With the removal of FastApiTypedArray this type is not needed "
378 return HasOptions() ? arg_count_ - 1 : arg_count_;
391 return arg_count_ > 0 && arg_info_[arg_count_ - 1].GetType() ==
392 CTypeInfo::kCallbackOptionsType;
397 const Int64Representation repr_;
398 const unsigned int arg_count_;
402struct FastApiCallbackOptions;
408#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
412 AnyCType& operator=(
const AnyCType& other) {
413 int64_value = other.int64_value;
434 "The union AnyCType should have size == 64 bits, as this is assumed "
435 "by EffectControlLinearizer.");
439 constexpr CFunction() : address_(nullptr), type_info_(nullptr) {}
444 return type_info_->ArgumentInfo(index);
451 return type_info_->GetInt64Representation();
461 "Overload resolution is only based on the parameter count. If the "
462 "parameter count is different, overload resolution is possible and "
463 "happens at compile time. Otherwise overload resolution is impossible.")
468 if (ArgumentCount() != other->ArgumentCount()) {
469 return OverloadResolution::kAtCompileTime;
474 for (
unsigned int i = 0; i < ArgumentCount(); ++i) {
475 if (ArgumentInfo(i).GetSequenceType() !=
476 other->ArgumentInfo(i).GetSequenceType()) {
477 if (diff_index >= 0) {
478 return OverloadResolution::kImpossible;
483 if (ArgumentInfo(i).GetSequenceType() ==
484 CTypeInfo::SequenceType::kScalar ||
485 other->ArgumentInfo(i).GetSequenceType() ==
486 CTypeInfo::SequenceType::kScalar) {
487 return OverloadResolution::kImpossible;
492 return OverloadResolution::kAtRuntime;
495 template <
typename F>
498 CFunctionInfo::Int64Representation::kNumber) {
499 CFunction result = ArgUnwrap<F*>::Make(func, int64_rep);
505 template <
typename R,
typename... Args,
typename R_Patch,
506 typename... Args_Patch>
508 R_Patch (*patching_func)(Args_Patch...),
510 CFunctionInfo::Int64Representation::kNumber) {
511 CFunction c_func = ArgUnwrap<R (*)(Args...)>::Make(func, int64_rep);
513 sizeof...(Args_Patch) ==
sizeof...(Args),
514 "The patching function must have the same number of arguments.");
515 c_func.address_ =
reinterpret_cast<void*
>(patching_func);
522 const void* address_;
525 template <
typename F>
527 static_assert(
sizeof(F) !=
sizeof(F),
528 "CFunction must be created from a function pointer.");
531 template <
typename R,
typename... Args>
532 class ArgUnwrap<R (*)(Args...)> {
534 static CFunction Make(R (*func)(Args...),
536 CFunctionInfo::Int64Representation::kNumber);
566template <
typename T,
typename... List>
567struct count : std::integral_constant<int, 0> {};
568template <
typename T,
typename... Args>
570 : std::integral_constant<std::size_t, 1 + count<T, Args...>::value> {};
571template <
typename T,
typename U,
typename... Args>
575 typename RetBuilder,
typename... ArgBuilders>
577 static constexpr int kOptionsArgCount =
579 static constexpr int kReceiverCount = 1;
581 static_assert(kOptionsArgCount == 0 || kOptionsArgCount == 1,
582 "Only one options parameter is supported.");
584 static_assert(
sizeof...(ArgBuilders) >= kOptionsArgCount + kReceiverCount,
585 "The receiver or the options argument is missing.");
590 arg_info_storage_, Representation),
591 arg_info_storage_{ArgBuilders::Build()...} {
603 "String and api object values are not currently "
604 "supported return types.");
608 const CTypeInfo arg_info_storage_[
sizeof...(ArgBuilders)];
613 static_assert(
sizeof(T) !=
sizeof(T),
"This type is not supported");
616#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum) \
618 struct TypeInfoHelper<T> { \
619 static constexpr CTypeInfo::Flags Flags() { \
620 return CTypeInfo::Flags::kNone; \
623 static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \
624 static constexpr CTypeInfo::SequenceType SequenceType() { \
625 return CTypeInfo::SequenceType::kScalar; \
629template <CTypeInfo::Type type>
632#define DEFINE_TYPE_INFO_TRAITS(CType, Enum) \
634 struct CTypeInfoTraits<CTypeInfo::Type::Enum> { \
635 using ctype = CType; \
638#define PRIMITIVE_C_TYPES(V) \
642 V(uint32_t, kUint32) \
644 V(uint64_t, kUint64) \
646 V(double, kFloat64) \
650#define ALL_C_TYPES(V) \
651 PRIMITIVE_C_TYPES(V) \
653 V(v8::Local<v8::Value>, kV8Value) \
654 V(v8::Local<v8::Object>, kV8Value) \
664#undef PRIMITIVE_C_TYPES
667#undef TYPED_ARRAY_C_TYPES
681 "TypedArrays are not supported directly anymore. Use Local<Value> instead.")
685 static constexpr CTypeInfo::Type Type() {
return CTypeInfo::Type::kUint32; }
687 return CTypeInfo::SequenceType::kIsTypedArray;
715#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG) \
716 static_assert(((COND) == 0) || (ASSERTION), MSG)
734 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kAllowSharedBit),
735 (kSequenceType == CTypeInfo::SequenceType::kIsTypedArray ||
736 kSequenceType == CTypeInfo::SequenceType::kIsArrayBuffer),
737 "kAllowSharedBit is only allowed for TypedArrays and ArrayBuffers.");
739 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kEnforceRangeBit),
740 CTypeInfo::IsIntegralType(kType),
741 "kEnforceRangeBit is only allowed for integral types.");
743 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kClampBit),
744 CTypeInfo::IsIntegralType(kType),
745 "kClampBit is only allowed for integral types.");
747 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kIsRestrictedBit),
748 CTypeInfo::IsFloatingPointType(kType),
749 "kIsRestrictedBit is only allowed for floating point types.");
751 kType == CTypeInfo::Type::kVoid,
752 "Sequences are only supported from void type.");
754 kSequenceType == CTypeInfo::SequenceType::kIsTypedArray,
755 CTypeInfo::IsPrimitive(kType) || kType == CTypeInfo::Type::kVoid,
756 "TypedArrays are only supported from primitive types or void.");
765 template <typename... Rest>
770 static constexpr CTypeInfo::Flags MergeFlags() {
return CTypeInfo::Flags(0); }
774template <
typename RetBuilder,
typename... ArgBuilders>
780 constexpr auto Ret() {
783 ArgBuilders...>(fn_);
787 constexpr auto Arg() {
790 return ArgImpl<N, Flags...>(
791 std::make_index_sequence<
sizeof...(ArgBuilders)>());
795 template <
typename Ret,
typename... Args>
796 auto Patch(
Ret (*patching_func)(Args...)) {
798 sizeof...(Args) ==
sizeof...(ArgBuilders),
799 "The patching function must have the same number of arguments.");
800 fn_ =
reinterpret_cast<void*
>(patching_func);
814 struct GetArgBuilder;
819 struct GetArgBuilder<false, N, Flags...> {
821 typename std::tuple_element<N, std::tuple<ArgBuilders...>>::type;
827 struct GetArgBuilder<true, N, Flags...> {
829 typename std::tuple_element<N,
830 std::tuple<ArgBuilders...>>::type::BaseType,
831 std::tuple_element<N, std::tuple<ArgBuilders...>>::type::Build()
840 constexpr auto ArgImpl(std::index_sequence<I...>) {
841 return CFunctionBuilderWithFunction<
842 RetBuilder,
typename GetArgBuilder<N == I, I, Flags...>::type...>(fn_);
852 template <
typename R,
typename... Args>
853 constexpr auto Fn(R (*fn)(Args...)) {
856 reinterpret_cast<const void*
>(fn));
863template <
typename R,
typename... Args>
864CFunction CFunction::ArgUnwrap<R (*)(Args...)>::Make(
867 return internal::CFunctionBuilder().Fn(func).
Build();
869 return internal::CFunctionBuilder()
871 .template Build<CFunctionInfo::Int64Representation::kBigInt>();
877static constexpr CTypeInfo kTypeInfoFloat64 =
892template <CTypeInfo::Identifier type_info_
id,
typename T>
894 Local<Array> src, T* dst, uint32_t max_length);
898TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<int32_t>::Build().GetId(),
899 int32_t>(Local<Array> src, int32_t* dst,
900 uint32_t max_length);
904TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<uint32_t>::Build().GetId(),
905 uint32_t>(Local<Array> src, uint32_t* dst,
906 uint32_t max_length);
910TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<float>::Build().GetId(),
911 float>(Local<Array> src,
float* dst,
912 uint32_t max_length);
916TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<double>::Build().GetId(),
917 double>(Local<Array> src,
double* dst,
918 uint32_t max_length);
Definition: v8-fast-api-calls.h:357
Int64Representation GetInt64Representation() const
Definition: v8-fast-api-calls.h:381
Int64Representation
Definition: v8-fast-api-calls.h:359
const CTypeInfo & ReturnInfo() const
Definition: v8-fast-api-calls.h:373
const CTypeInfo & ArgumentInfo(unsigned int index) const
bool HasOptions() const
Definition: v8-fast-api-calls.h:389
unsigned int ArgumentCount() const
Definition: v8-fast-api-calls.h:377
CFunctionInfo(const CTypeInfo &return_info, unsigned int arg_count, const CTypeInfo *arg_info, Int64Representation repr=Int64Representation::kNumber)
Definition: v8-fast-api-calls.h:437
const CTypeInfo & ReturnInfo() const
Definition: v8-fast-api-calls.h:441
constexpr CFunction()
Definition: v8-fast-api-calls.h:439
const CTypeInfo & ArgumentInfo(unsigned int index) const
Definition: v8-fast-api-calls.h:443
unsigned int ArgumentCount() const
Definition: v8-fast-api-calls.h:447
CFunctionInfo::Int64Representation GetInt64Representation() const
Definition: v8-fast-api-calls.h:450
CFunction(const void *address, const CFunctionInfo *type_info)
static CFunction Make(R(*func)(Args...), R_Patch(*patching_func)(Args_Patch...), CFunctionInfo::Int64Representation int64_rep=CFunctionInfo::Int64Representation::kNumber)
Definition: v8-fast-api-calls.h:507
const void * GetAddress() const
Definition: v8-fast-api-calls.h:449
OverloadResolution
Definition: v8-fast-api-calls.h:455
const CFunctionInfo * GetTypeInfo() const
Definition: v8-fast-api-calls.h:453
static CFunction Make(F *func, CFunctionInfo::Int64Representation int64_rep=CFunctionInfo::Int64Representation::kNumber)
Definition: v8-fast-api-calls.h:496
Definition: v8-fast-api-calls.h:718
static constexpr CTypeInfo Build()
Definition: v8-fast-api-calls.h:723
T BaseType
Definition: v8-fast-api-calls.h:720
Definition: v8-fast-api-calls.h:238
Flags
Definition: v8-fast-api-calls.h:280
constexpr CTypeInfo(Type type, SequenceType sequence_type=SequenceType::kScalar, Flags flags=Flags::kNone)
Definition: v8-fast-api-calls.h:288
static constexpr bool IsFloatingPointType(Type type)
Definition: v8-fast-api-calls.h:314
static constexpr bool IsPrimitive(Type type)
Definition: v8-fast-api-calls.h:318
static constexpr Type kCallbackOptionsType
Definition: v8-fast-api-calls.h:269
static constexpr bool IsIntegralType(Type type)
Definition: v8-fast-api-calls.h:308
SequenceType
Definition: v8-fast-api-calls.h:271
constexpr Identifier GetId() const
Definition: v8-fast-api-calls.h:298
constexpr Type GetType() const
Definition: v8-fast-api-calls.h:304
constexpr Flags GetFlags() const
Definition: v8-fast-api-calls.h:306
Type
Definition: v8-fast-api-calls.h:240
uint32_t Identifier
Definition: v8-fast-api-calls.h:293
constexpr CTypeInfo(Identifier identifier)
Definition: v8-fast-api-calls.h:294
constexpr SequenceType GetSequenceType() const
Definition: v8-fast-api-calls.h:305
Definition: v8-isolate.h:212
Definition: v8-local-handle.h:266
Definition: v8-fast-api-calls.h:772
auto Build()
Definition: v8-fast-api-calls.h:803
constexpr auto Ret()
Definition: v8-fast-api-calls.h:777
constexpr auto Arg()
Definition: v8-fast-api-calls.h:784
auto Patch(Ret(*patching_func)(Args...))
Definition: v8-fast-api-calls.h:793
Definition: v8-fast-api-calls.h:845
constexpr CFunctionBuilder()
Definition: v8-fast-api-calls.h:847
constexpr auto Fn(R(*fn)(Args...))
Definition: v8-fast-api-calls.h:850
Definition: v8-fast-api-calls.h:576
constexpr CFunctionInfoImpl()
Definition: v8-fast-api-calls.h:588
Definition: libplatform.h:15
bool TryToCopyAndConvertArrayToCppBuffer(Local< Array > src, T *dst, uint32_t max_length)
internal::CFunctionBuilder CFunctionBuilder
Definition: v8-fast-api-calls.h:871
Definition: v8-fast-api-calls.h:342
size_t byte_length
Definition: v8-fast-api-calls.h:344
void * data
Definition: v8-fast-api-calls.h:343
Definition: v8-fast-api-calls.h:347
void * data
Definition: v8-fast-api-calls.h:348
size_t byte_length
Definition: v8-fast-api-calls.h:349
Definition: v8-fast-api-calls.h:546
v8::Isolate * isolate
Definition: v8-fast-api-calls.h:555
v8::Local< v8::Value > data
Definition: v8-fast-api-calls.h:560
static FastApiCallbackOptions CreateForTesting(Isolate *isolate)
Definition: v8-fast-api-calls.h:551
Definition: v8-fast-api-calls.h:331
size_t length() const
Definition: v8-fast-api-calls.h:334
void ValidateIndex(size_t index) const
Definition: v8-fast-api-calls.h:352
const char * data
Definition: v8-fast-api-calls.h:353
uint32_t length
Definition: v8-fast-api-calls.h:354
Definition: v8-fast-api-calls.h:630
Definition: v8-fast-api-calls.h:612
Definition: v8-fast-api-calls.h:567
Definition: v8-fast-api-calls.h:405
FastApiCallbackOptions * options_value
Definition: v8-fast-api-calls.h:429
AnyCType()
Definition: v8-fast-api-calls.h:406
const FastOneByteString * string_value
Definition: v8-fast-api-calls.h:428
Local< Array > sequence_value
Definition: v8-fast-api-calls.h:427
Local< Object > object_value
Definition: v8-fast-api-calls.h:426
double double_value
Definition: v8-fast-api-calls.h:424
void * pointer_value
Definition: v8-fast-api-calls.h:425
uint32_t uint32_value
Definition: v8-fast-api-calls.h:420
bool bool_value
Definition: v8-fast-api-calls.h:418
float float_value
Definition: v8-fast-api-calls.h:423
uint64_t uint64_value
Definition: v8-fast-api-calls.h:422
int32_t int32_value
Definition: v8-fast-api-calls.h:419
int64_t int64_value
Definition: v8-fast-api-calls.h:421
#define DEFINE_TYPE_INFO_TRAITS(CType, Enum)
Definition: v8-fast-api-calls.h:632
#define PRIMITIVE_C_TYPES(V)
Definition: v8-fast-api-calls.h:638
#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum)
Definition: v8-fast-api-calls.h:616
#define ALL_C_TYPES(V)
Definition: v8-fast-api-calls.h:650
#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG)
Definition: v8-fast-api-calls.h:712
#define V8_EXPORT
Definition: v8config.h:793
#define V8_DEPRECATED(message)
Definition: v8config.h:595
#define END_ALLOW_USE_DEPRECATED()
Definition: v8config.h:623
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:660
#define V8_TRIVIAL_ABI
Definition: v8config.h:743
#define START_ALLOW_USE_DEPRECATED()
Definition: v8config.h:622