5#ifndef INCLUDE_V8_FAST_API_CALLS_H_
6#define INCLUDE_V8_FAST_API_CALLS_H_
210#include <type_traits>
254 static constexpr Type kCallbackOptionsType =
Type(255);
257 "There is no special support in V8 anymore, there is no need to"
266 kAllowSharedBit = 1 << 0,
267 kEnforceRangeBit = 1 << 1,
269 kIsRestrictedBit = 1 << 3,
273 : type_(type), sequence_type_(
SequenceType::kScalar), flags_(flags) {}
278 : type_(type), sequence_type_(sequence_type), flags_(flags) {}
284 static_cast<
Flags>(identifier & 255)) {}
286 return static_cast<uint8_t
>(type_) << 16 |
287 static_cast<uint8_t
>(sequence_type_) << 8 |
288 static_cast<uint8_t
>(flags_);
292 V8_DEPRECATED(
"Use the constant SequenceType::kScalar instead")
293 constexpr
SequenceType GetSequenceType()
const {
return sequence_type_; }
297 return type == Type::kUint8 || type == Type::kInt32 ||
298 type == Type::kUint32 || type == Type::kInt64 ||
299 type == Type::kUint64;
303 return type == Type::kFloat32 || type == Type::kFloat64;
307 return IsIntegralType(type) || IsFloatingPointType(type) ||
313 SequenceType sequence_type_;
344 return HasOptions() ? arg_count_ - 1 : arg_count_;
357 return arg_count_ > 0 && arg_info_[arg_count_ - 1].GetType() ==
358 CTypeInfo::kCallbackOptionsType;
363 const Int64Representation repr_;
364 const unsigned int arg_count_;
368struct FastApiCallbackOptions;
374#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
378 AnyCType& operator=(
const AnyCType& other) {
379 int64_value = other.int64_value;
400 "The union AnyCType should have size == 64 bits, as this is assumed "
401 "by EffectControlLinearizer.");
405 constexpr CFunction() : address_(nullptr), type_info_(nullptr) {}
410 return type_info_->ArgumentInfo(index);
417 return type_info_->GetInt64Representation();
423 template <
typename F>
426 CFunctionInfo::Int64Representation::kNumber) {
427 CFunction result = ArgUnwrap<F*>::Make(func, int64_rep);
433 template <
typename R,
typename... Args,
typename R_Patch,
434 typename... Args_Patch>
436 R_Patch (*patching_func)(Args_Patch...),
438 CFunctionInfo::Int64Representation::kNumber) {
439 CFunction c_func = ArgUnwrap<R (*)(Args...)>::Make(func, int64_rep);
441 sizeof...(Args_Patch) ==
sizeof...(Args),
442 "The patching function must have the same number of arguments.");
443 c_func.address_ =
reinterpret_cast<void*
>(patching_func);
450 const void* address_;
453 template <
typename F>
455 static_assert(
sizeof(F) !=
sizeof(F),
456 "CFunction must be created from a function pointer.");
459 template <
typename R,
typename... Args>
460 class ArgUnwrap<R (*)(Args...)> {
462 static CFunction Make(R (*func)(Args...),
464 CFunctionInfo::Int64Representation::kNumber);
494template <
typename T,
typename... List>
495struct count : std::integral_constant<int, 0> {};
496template <
typename T,
typename... Args>
498 : std::integral_constant<std::size_t, 1 + count<T, Args...>::value> {};
499template <
typename T,
typename U,
typename... Args>
503 typename RetBuilder,
typename... ArgBuilders>
505 static constexpr int kOptionsArgCount =
507 static constexpr int kReceiverCount = 1;
509 static_assert(kOptionsArgCount == 0 || kOptionsArgCount == 1,
510 "Only one options parameter is supported.");
512 static_assert(
sizeof...(ArgBuilders) >= kOptionsArgCount + kReceiverCount,
513 "The receiver or the options argument is missing.");
518 arg_info_storage_, Representation),
519 arg_info_storage_{ArgBuilders::Build()...} {
531 "String and api object values are not currently "
532 "supported return types.");
536 const CTypeInfo arg_info_storage_[
sizeof...(ArgBuilders)];
541 static_assert(
sizeof(T) !=
sizeof(T),
"This type is not supported");
544#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum) \
546 struct TypeInfoHelper<T> { \
547 static constexpr CTypeInfo::Flags Flags() { \
548 return CTypeInfo::Flags::kNone; \
551 static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \
554template <CTypeInfo::Type type>
557#define DEFINE_TYPE_INFO_TRAITS(CType, Enum) \
559 struct CTypeInfoTraits<CTypeInfo::Type::Enum> { \
560 using ctype = CType; \
563#define PRIMITIVE_C_TYPES(V) \
567 V(uint32_t, kUint32) \
569 V(uint64_t, kUint64) \
571 V(double, kFloat64) \
575#define ALL_C_TYPES(V) \
576 PRIMITIVE_C_TYPES(V) \
578 V(v8::Local<v8::Value>, kV8Value) \
579 V(v8::Local<v8::Object>, kV8Value) \
580 V(v8::Local<v8::Array>, kV8Value) \
590#undef PRIMITIVE_C_TYPES
593#undef TYPED_ARRAY_C_TYPES
613#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG) \
614 static_assert(((COND) == 0) || (ASSERTION), MSG)
629 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kEnforceRangeBit),
630 CTypeInfo::IsIntegralType(kType),
631 "kEnforceRangeBit is only allowed for integral types.");
633 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kClampBit),
634 CTypeInfo::IsIntegralType(kType),
635 "kClampBit is only allowed for integral types.");
637 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kIsRestrictedBit),
638 CTypeInfo::IsFloatingPointType(kType),
639 "kIsRestrictedBit is only allowed for floating point types.");
646 template <
typename... Rest>
651 static constexpr CTypeInfo::Flags MergeFlags() {
return CTypeInfo::Flags(0); }
655template <
typename RetBuilder,
typename... ArgBuilders>
661 constexpr auto Ret() {
664 ArgBuilders...>(fn_);
668 constexpr auto Arg() {
671 return ArgImpl<N, Flags...>(
672 std::make_index_sequence<
sizeof...(ArgBuilders)>());
676 template <
typename Ret,
typename... Args>
677 auto Patch(
Ret (*patching_func)(Args...)) {
679 sizeof...(Args) ==
sizeof...(ArgBuilders),
680 "The patching function must have the same number of arguments.");
681 fn_ =
reinterpret_cast<void*
>(patching_func);
695 struct GetArgBuilder;
700 struct GetArgBuilder<false, N, Flags...> {
701 using type = std::tuple_element_t<N, std::tuple<ArgBuilders...>>;
707 struct GetArgBuilder<true, N, Flags...> {
709 typename std::tuple_element_t<N, std::tuple<ArgBuilders...>>::BaseType,
710 std::tuple_element_t<N, std::tuple<ArgBuilders...>>
::Build().GetFlags(),
718 constexpr auto ArgImpl(std::index_sequence<I...>) {
719 return CFunctionBuilderWithFunction<
720 RetBuilder,
typename GetArgBuilder<N == I, I, Flags...>::type...>(fn_);
730 template <
typename R,
typename... Args>
731 constexpr auto Fn(R (*fn)(Args...)) {
734 reinterpret_cast<const void*
>(fn));
741template <
typename R,
typename... Args>
742CFunction CFunction::ArgUnwrap<R (*)(Args...)>::Make(
745 return internal::CFunctionBuilder().Fn(func).
Build();
747 return internal::CFunctionBuilder()
749 .template Build<CFunctionInfo::Int64Representation::kBigInt>();
755static constexpr CTypeInfo kTypeInfoFloat64 =
770template <CTypeInfo::Identifier type_info_
id,
typename T>
772 Local<Array> src, T* dst, uint32_t max_length);
776TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<int32_t>::Build().GetId(),
777 int32_t>(Local<Array> src, int32_t* dst,
778 uint32_t max_length);
782TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<uint32_t>::Build().GetId(),
783 uint32_t>(Local<Array> src, uint32_t* dst,
784 uint32_t max_length);
788TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<float>::Build().GetId(),
789 float>(Local<Array> src,
float* dst,
790 uint32_t max_length);
794TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<double>::Build().GetId(),
795 double>(Local<Array> src,
double* dst,
796 uint32_t max_length);
Definition: v8-fast-api-calls.h:323
Int64Representation GetInt64Representation() const
Definition: v8-fast-api-calls.h:347
Int64Representation
Definition: v8-fast-api-calls.h:325
const CTypeInfo & ReturnInfo() const
Definition: v8-fast-api-calls.h:339
const CTypeInfo & ArgumentInfo(unsigned int index) const
bool HasOptions() const
Definition: v8-fast-api-calls.h:355
unsigned int ArgumentCount() const
Definition: v8-fast-api-calls.h:343
CFunctionInfo(const CTypeInfo &return_info, unsigned int arg_count, const CTypeInfo *arg_info, Int64Representation repr=Int64Representation::kNumber)
Definition: v8-fast-api-calls.h:403
const CTypeInfo & ReturnInfo() const
Definition: v8-fast-api-calls.h:407
constexpr CFunction()
Definition: v8-fast-api-calls.h:405
const CTypeInfo & ArgumentInfo(unsigned int index) const
Definition: v8-fast-api-calls.h:409
unsigned int ArgumentCount() const
Definition: v8-fast-api-calls.h:413
CFunctionInfo::Int64Representation GetInt64Representation() const
Definition: v8-fast-api-calls.h:416
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:435
const void * GetAddress() const
Definition: v8-fast-api-calls.h:415
OverloadResolution
Definition: v8-fast-api-calls.h:421
const CFunctionInfo * GetTypeInfo() const
Definition: v8-fast-api-calls.h:419
static CFunction Make(F *func, CFunctionInfo::Int64Representation int64_rep=CFunctionInfo::Int64Representation::kNumber)
Definition: v8-fast-api-calls.h:424
Definition: v8-fast-api-calls.h:616
static constexpr CTypeInfo Build()
Definition: v8-fast-api-calls.h:620
T BaseType
Definition: v8-fast-api-calls.h:618
Definition: v8-fast-api-calls.h:223
Flags
Definition: v8-fast-api-calls.h:264
constexpr CTypeInfo(Type type, Flags flags=Flags::kNone)
Definition: v8-fast-api-calls.h:272
static constexpr bool IsFloatingPointType(Type type)
Definition: v8-fast-api-calls.h:302
static constexpr bool IsPrimitive(Type type)
Definition: v8-fast-api-calls.h:306
static constexpr Type kCallbackOptionsType
Definition: v8-fast-api-calls.h:254
static constexpr bool IsIntegralType(Type type)
Definition: v8-fast-api-calls.h:296
SequenceType
Definition: v8-fast-api-calls.h:258
constexpr Identifier GetId() const
Definition: v8-fast-api-calls.h:285
constexpr Type GetType() const
Definition: v8-fast-api-calls.h:291
constexpr Flags GetFlags() const
Definition: v8-fast-api-calls.h:294
Type
Definition: v8-fast-api-calls.h:225
uint32_t Identifier
Definition: v8-fast-api-calls.h:280
constexpr CTypeInfo(Identifier identifier)
Definition: v8-fast-api-calls.h:281
Definition: v8-isolate.h:290
Definition: v8-local-handle.h:366
Definition: v8-fast-api-calls.h:653
auto Build()
Definition: v8-fast-api-calls.h:684
constexpr auto Ret()
Definition: v8-fast-api-calls.h:658
constexpr auto Arg()
Definition: v8-fast-api-calls.h:665
auto Patch(Ret(*patching_func)(Args...))
Definition: v8-fast-api-calls.h:674
Definition: v8-fast-api-calls.h:723
constexpr CFunctionBuilder()
Definition: v8-fast-api-calls.h:725
constexpr auto Fn(R(*fn)(Args...))
Definition: v8-fast-api-calls.h:728
Definition: v8-fast-api-calls.h:504
constexpr CFunctionInfoImpl()
Definition: v8-fast-api-calls.h:516
Definition: libplatform.h:15
bool TryToCopyAndConvertArrayToCppBuffer(Local< Array > src, T *dst, uint32_t max_length)
internal::CFunctionBuilder CFunctionBuilder
Definition: v8-fast-api-calls.h:749
Definition: v8-fast-api-calls.h:474
v8::Isolate * isolate
Definition: v8-fast-api-calls.h:483
v8::Local< v8::Value > data
Definition: v8-fast-api-calls.h:488
static FastApiCallbackOptions CreateForTesting(Isolate *isolate)
Definition: v8-fast-api-calls.h:479
Definition: v8-fast-api-calls.h:318
const char * data
Definition: v8-fast-api-calls.h:319
uint32_t length
Definition: v8-fast-api-calls.h:320
Definition: v8-fast-api-calls.h:555
Definition: v8-fast-api-calls.h:540
Definition: v8-fast-api-calls.h:495
Definition: v8-fast-api-calls.h:371
FastApiCallbackOptions * options_value
Definition: v8-fast-api-calls.h:395
AnyCType()
Definition: v8-fast-api-calls.h:372
const FastOneByteString * string_value
Definition: v8-fast-api-calls.h:394
Local< Array > sequence_value
Definition: v8-fast-api-calls.h:393
Local< Object > object_value
Definition: v8-fast-api-calls.h:392
double double_value
Definition: v8-fast-api-calls.h:390
void * pointer_value
Definition: v8-fast-api-calls.h:391
uint32_t uint32_value
Definition: v8-fast-api-calls.h:386
bool bool_value
Definition: v8-fast-api-calls.h:384
float float_value
Definition: v8-fast-api-calls.h:389
uint64_t uint64_value
Definition: v8-fast-api-calls.h:388
int32_t int32_value
Definition: v8-fast-api-calls.h:385
int64_t int64_value
Definition: v8-fast-api-calls.h:387
#define DEFINE_TYPE_INFO_TRAITS(CType, Enum)
Definition: v8-fast-api-calls.h:557
#define PRIMITIVE_C_TYPES(V)
Definition: v8-fast-api-calls.h:563
#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum)
Definition: v8-fast-api-calls.h:544
#define ALL_C_TYPES(V)
Definition: v8-fast-api-calls.h:575
#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG)
Definition: v8-fast-api-calls.h:610
#define V8_EXPORT
Definition: v8config.h:860
#define V8_DEPRECATED(message)
Definition: v8config.h:619
#define END_ALLOW_USE_DEPRECATED()
Definition: v8config.h:647
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:684
#define V8_TRIVIAL_ABI
Definition: v8config.h:813
#define START_ALLOW_USE_DEPRECATED()
Definition: v8config.h:646