219#ifndef INCLUDE_V8_FAST_API_CALLS_H_
220#define INCLUDE_V8_FAST_API_CALLS_H_
226#include <type_traits>
270 static constexpr Type kCallbackOptionsType =
Type(255);
273 "There is no special support in V8 anymore, there is no need to"
282 kAllowSharedBit = 1 << 0,
283 kEnforceRangeBit = 1 << 1,
285 kIsRestrictedBit = 1 << 3,
289 : type_(type), sequence_type_(
SequenceType::kScalar), flags_(flags) {}
294 : type_(type), sequence_type_(sequence_type), flags_(flags) {}
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_);
309 constexpr
SequenceType GetSequenceType()
const {
return sequence_type_; }
313 return type == Type::kUint8 || type == Type::kInt32 ||
314 type == Type::kUint32 || type == Type::kInt64 ||
315 type == Type::kUint64;
319 return type == Type::kFloat32 || type == Type::kFloat64;
323 return IsIntegralType(type) || IsFloatingPointType(type) ||
329 SequenceType sequence_type_;
360 return HasOptions() ? arg_count_ - 1 : arg_count_;
373 return arg_count_ > 0 && arg_info_[arg_count_ - 1].GetType() ==
374 CTypeInfo::kCallbackOptionsType;
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>
442 CFunctionInfo::Int64Representation::kNumber) {
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...),
454 CFunctionInfo::Int64Representation::kNumber) {
455 CFunction c_func = ArgUnwrap<R (*)(Args...)>::Make(func, int64_rep);
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...),
480 CFunctionInfo::Int64Representation::kNumber);
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)
645 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kEnforceRangeBit),
646 CTypeInfo::IsIntegralType(kType),
647 "kEnforceRangeBit is only allowed for integral types.");
649 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kClampBit),
650 CTypeInfo::IsIntegralType(kType),
651 "kClampBit is only allowed for integral types.");
653 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kIsRestrictedBit),
654 CTypeInfo::IsFloatingPointType(kType),
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>
677 constexpr auto Ret() {
680 ArgBuilders...>(fn_);
684 constexpr auto Arg() {
687 return ArgImpl<N, Flags...>(
688 std::make_index_sequence<
sizeof...(ArgBuilders)>());
692 template <
typename Ret,
typename... Args>
693 auto Patch(
Ret (*patching_func)(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...>) {
738 return CFunctionBuilderWithFunction<
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();
766 return internal::CFunctionBuilder()
768 .template Build<CFunctionInfo::Int64Representation::kBigInt>();
774static constexpr CTypeInfo kTypeInfoFloat64 =
789template <CTypeInfo::Identifier type_info_
id,
typename T>
791 Local<Array> src, T* dst, uint32_t max_length);
795TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<int32_t>::Build().GetId(),
796 int32_t>(Local<Array> src, int32_t* dst,
797 uint32_t max_length);
801TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<uint32_t>::Build().GetId(),
802 uint32_t>(Local<Array> src, uint32_t* dst,
803 uint32_t max_length);
807TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<float>::Build().GetId(),
808 float>(Local<Array> src,
float* dst,
809 uint32_t max_length);
813TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<double>::Build().GetId(),
814 double>(Local<Array> src,
double* dst,
815 uint32_t max_length);
Definition: v8-fast-api-calls.h:339
Int64Representation GetInt64Representation() const
Definition: v8-fast-api-calls.h:363
Int64Representation
Definition: v8-fast-api-calls.h:341
const CTypeInfo & ReturnInfo() const
Definition: v8-fast-api-calls.h:355
const CTypeInfo & ArgumentInfo(unsigned int index) const
bool HasOptions() const
Definition: v8-fast-api-calls.h:371
unsigned int ArgumentCount() const
Definition: v8-fast-api-calls.h:359
CFunctionInfo(const CTypeInfo &return_info, unsigned int arg_count, const CTypeInfo *arg_info, Int64Representation repr=Int64Representation::kNumber)
Definition: v8-fast-api-calls.h:419
const CTypeInfo & ReturnInfo() const
Definition: v8-fast-api-calls.h:423
constexpr CFunction()
Definition: v8-fast-api-calls.h:421
const CTypeInfo & ArgumentInfo(unsigned int index) const
Definition: v8-fast-api-calls.h:425
unsigned int ArgumentCount() const
Definition: v8-fast-api-calls.h:429
CFunctionInfo::Int64Representation GetInt64Representation() const
Definition: v8-fast-api-calls.h:432
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:451
const void * GetAddress() const
Definition: v8-fast-api-calls.h:431
OverloadResolution
Definition: v8-fast-api-calls.h:437
const CFunctionInfo * GetTypeInfo() const
Definition: v8-fast-api-calls.h:435
static CFunction Make(F *func, CFunctionInfo::Int64Representation int64_rep=CFunctionInfo::Int64Representation::kNumber)
Definition: v8-fast-api-calls.h:440
Definition: v8-fast-api-calls.h:632
static constexpr CTypeInfo Build()
Definition: v8-fast-api-calls.h:636
T BaseType
Definition: v8-fast-api-calls.h:634
Definition: v8-fast-api-calls.h:239
Flags
Definition: v8-fast-api-calls.h:280
constexpr CTypeInfo(Type type, Flags flags=Flags::kNone)
Definition: v8-fast-api-calls.h:288
static constexpr bool IsFloatingPointType(Type type)
Definition: v8-fast-api-calls.h:318
static constexpr bool IsPrimitive(Type type)
Definition: v8-fast-api-calls.h:322
static constexpr Type kCallbackOptionsType
Definition: v8-fast-api-calls.h:270
static constexpr bool IsIntegralType(Type type)
Definition: v8-fast-api-calls.h:312
SequenceType
Definition: v8-fast-api-calls.h:274
constexpr Identifier GetId() const
Definition: v8-fast-api-calls.h:301
constexpr Type GetType() const
Definition: v8-fast-api-calls.h:307
constexpr Flags GetFlags() const
Definition: v8-fast-api-calls.h:310
Type
Definition: v8-fast-api-calls.h:241
uint32_t Identifier
Definition: v8-fast-api-calls.h:296
constexpr CTypeInfo(Identifier identifier)
Definition: v8-fast-api-calls.h:297
Definition: v8-isolate.h:273
Definition: v8-local-handle.h:266
Definition: v8-fast-api-calls.h:669
auto Build()
Definition: v8-fast-api-calls.h:700
constexpr auto Ret()
Definition: v8-fast-api-calls.h:674
constexpr auto Arg()
Definition: v8-fast-api-calls.h:681
auto Patch(Ret(*patching_func)(Args...))
Definition: v8-fast-api-calls.h:690
Definition: v8-fast-api-calls.h:742
constexpr CFunctionBuilder()
Definition: v8-fast-api-calls.h:744
constexpr auto Fn(R(*fn)(Args...))
Definition: v8-fast-api-calls.h:747
Definition: v8-fast-api-calls.h:520
constexpr CFunctionInfoImpl()
Definition: v8-fast-api-calls.h:532
Definition: libplatform.h:15
bool TryToCopyAndConvertArrayToCppBuffer(Local< Array > src, T *dst, uint32_t max_length)
internal::CFunctionBuilder CFunctionBuilder
Definition: v8-fast-api-calls.h:768
Definition: v8-fast-api-calls.h:490
v8::Isolate * isolate
Definition: v8-fast-api-calls.h:499
v8::Local< v8::Value > data
Definition: v8-fast-api-calls.h:504
static FastApiCallbackOptions CreateForTesting(Isolate *isolate)
Definition: v8-fast-api-calls.h:495
Definition: v8-fast-api-calls.h:334
const char * data
Definition: v8-fast-api-calls.h:335
uint32_t length
Definition: v8-fast-api-calls.h:336
Definition: v8-fast-api-calls.h:571
Definition: v8-fast-api-calls.h:556
Definition: v8-fast-api-calls.h:511
Definition: v8-fast-api-calls.h:387
FastApiCallbackOptions * options_value
Definition: v8-fast-api-calls.h:411
AnyCType()
Definition: v8-fast-api-calls.h:388
const FastOneByteString * string_value
Definition: v8-fast-api-calls.h:410
Local< Array > sequence_value
Definition: v8-fast-api-calls.h:409
Local< Object > object_value
Definition: v8-fast-api-calls.h:408
double double_value
Definition: v8-fast-api-calls.h:406
void * pointer_value
Definition: v8-fast-api-calls.h:407
uint32_t uint32_value
Definition: v8-fast-api-calls.h:402
bool bool_value
Definition: v8-fast-api-calls.h:400
float float_value
Definition: v8-fast-api-calls.h:405
uint64_t uint64_value
Definition: v8-fast-api-calls.h:404
int32_t int32_value
Definition: v8-fast-api-calls.h:401
int64_t int64_value
Definition: v8-fast-api-calls.h:403
#define DEFINE_TYPE_INFO_TRAITS(CType, Enum)
Definition: v8-fast-api-calls.h:573
#define PRIMITIVE_C_TYPES(V)
Definition: v8-fast-api-calls.h:579
#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum)
Definition: v8-fast-api-calls.h:560
#define ALL_C_TYPES(V)
Definition: v8-fast-api-calls.h:591
#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG)
Definition: v8-fast-api-calls.h:626
#define V8_EXPORT
Definition: v8config.h:793
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:603
#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