v8 14.1.146 (node 25.0.0)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
v8-fast-api-calls.h
Go to the documentation of this file.
1// Copyright 2020 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef INCLUDE_V8_FAST_API_CALLS_H_
6#define INCLUDE_V8_FAST_API_CALLS_H_
7
8/**
9 * This file provides additional API on top of the default one for making
10 * API calls, which come from embedder C++ functions. The functions are being
11 * called directly from optimized code, doing all the necessary typechecks
12 * in the compiler itself, instead of on the embedder side. Hence the "fast"
13 * in the name. Example usage might look like:
14 *
15 * \code
16 * void FastMethod(int param, bool another_param);
17 *
18 * v8::FunctionTemplate::New(isolate, SlowCallback, data,
19 * signature, length, constructor_behavior
20 * side_effect_type,
21 * &v8::CFunction::Make(FastMethod));
22 * \endcode
23 *
24 * By design, fast calls are limited by the following requirements, which
25 * the embedder should enforce themselves:
26 * - they should not allocate on the JS heap;
27 * - they should not trigger JS execution.
28 * To enforce them, the embedder could use the existing
29 * v8::Isolate::DisallowJavascriptExecutionScope and a utility similar to
30 * Blink's NoAllocationScope:
31 * https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/platform/heap/thread_state_scopes.h;l=16
32 *
33 * Due to these limitations, it's not directly possible to report errors by
34 * throwing a JS exception or to otherwise do an allocation. There is an
35 * alternative way of creating fast calls that supports falling back to the
36 * slow call and then performing the necessary allocation. When one creates
37 * the fast method by using CFunction::MakeWithFallbackSupport instead of
38 * CFunction::Make, the fast callback gets as last parameter an output variable,
39 * through which it can request falling back to the slow call. So one might
40 * declare their method like:
41 *
42 * \code
43 * void FastMethodWithFallback(int param, FastApiCallbackOptions& options);
44 * \endcode
45 *
46 * If the callback wants to signal an error condition or to perform an
47 * allocation, it must set options.fallback to true and do an early return from
48 * the fast method. Then V8 checks the value of options.fallback and if it's
49 * true, falls back to executing the SlowCallback, which is capable of reporting
50 * the error (either by throwing a JS exception or logging to the console) or
51 * doing the allocation. It's the embedder's responsibility to ensure that the
52 * fast callback is idempotent up to the point where error and fallback
53 * conditions are checked, because otherwise executing the slow callback might
54 * produce visible side-effects twice.
55 *
56 * An example for custom embedder type support might employ a way to wrap/
57 * unwrap various C++ types in JSObject instances, e.g:
58 *
59 * \code
60 *
61 * // Helper method with a check for field count.
62 * template <typename T, int offset>
63 * inline T* GetInternalField(v8::Local<v8::Object> wrapper) {
64 * assert(offset < wrapper->InternalFieldCount());
65 * return reinterpret_cast<T*>(
66 * wrapper->GetAlignedPointerFromInternalField(offset));
67 * }
68 *
69 * class CustomEmbedderType {
70 * public:
71 * // Returns the raw C object from a wrapper JS object.
72 * static CustomEmbedderType* Unwrap(v8::Local<v8::Object> wrapper) {
73 * return GetInternalField<CustomEmbedderType,
74 * kV8EmbedderWrapperObjectIndex>(wrapper);
75 * }
76 * static void FastMethod(v8::Local<v8::Object> receiver_obj, int param) {
77 * CustomEmbedderType* receiver = static_cast<CustomEmbedderType*>(
78 * receiver_obj->GetAlignedPointerFromInternalField(
79 * kV8EmbedderWrapperObjectIndex));
80 *
81 * // Type checks are already done by the optimized code.
82 * // Then call some performance-critical method like:
83 * // receiver->Method(param);
84 * }
85 *
86 * static void SlowMethod(
87 * const v8::FunctionCallbackInfo<v8::Value>& info) {
88 * v8::Local<v8::Object> instance =
89 * v8::Local<v8::Object>::Cast(info.Holder());
90 * CustomEmbedderType* receiver = Unwrap(instance);
91 * // TODO: Do type checks and extract {param}.
92 * receiver->Method(param);
93 * }
94 * };
95 *
96 * // The following setup function can be templatized based on
97 * // the {embedder_object} argument.
98 * void SetupCustomEmbedderObject(v8::Isolate* isolate,
99 * v8::Local<v8::Context> context,
100 * CustomEmbedderType* embedder_object) {
101 * v8::CFunction c_func =
102 * MakeV8CFunction(CustomEmbedderType::FastMethod);
103 *
104 * Local<v8::FunctionTemplate> method_template =
105 * v8::FunctionTemplate::New(
106 * isolate, CustomEmbedderType::SlowMethod, v8::Local<v8::Value>(),
107 * v8::Local<v8::Signature>(), 1, v8::ConstructorBehavior::kAllow,
108 * v8::SideEffectType::kHasSideEffect, &c_func);
109 *
110 * v8::Local<v8::ObjectTemplate> object_template =
111 * v8::ObjectTemplate::New(isolate);
112 * object_template->SetInternalFieldCount(
113 * kV8EmbedderWrapperObjectIndex + 1);
114 * object_template->Set(isolate, "method", method_template);
115 *
116 * // Instantiate the wrapper JS object.
117 * v8::Local<v8::Object> object =
118 * object_template->NewInstance(context).ToLocalChecked();
119 * object->SetAlignedPointerInInternalField(
120 * kV8EmbedderWrapperObjectIndex,
121 * reinterpret_cast<void*>(embedder_object));
122 *
123 * // TODO: Expose {object} where it's necessary.
124 * }
125 * \endcode
126 *
127 * For instance if {object} is exposed via a global "obj" variable,
128 * one could write in JS:
129 * function hot_func() {
130 * obj.method(42);
131 * }
132 * and once {hot_func} gets optimized, CustomEmbedderType::FastMethod
133 * will be called instead of the slow version, with the following arguments:
134 * receiver := the {embedder_object} from above
135 * param := 42
136 *
137 * Currently supported return types:
138 * - void
139 * - bool
140 * - int32_t
141 * - uint32_t
142 * - float32_t
143 * - float64_t
144 * Currently supported argument types:
145 * - pointer to an embedder type
146 * - JavaScript array of primitive types
147 * - bool
148 * - int32_t
149 * - uint32_t
150 * - int64_t
151 * - uint64_t
152 * - float32_t
153 * - float64_t
154 *
155 * The 64-bit integer types currently have the IDL (unsigned) long long
156 * semantics: https://heycam.github.io/webidl/#abstract-opdef-converttoint
157 * In the future we'll extend the API to also provide conversions from/to
158 * BigInt to preserve full precision.
159 * The floating point types currently have the IDL (unrestricted) semantics,
160 * which is the only one used by WebGL. We plan to add support also for
161 * restricted floats/doubles, similarly to the BigInt conversion policies.
162 * We also differ from the specific NaN bit pattern that WebIDL prescribes
163 * (https://heycam.github.io/webidl/#es-unrestricted-float) in that Blink
164 * passes NaN values as-is, i.e. doesn't normalize them.
165 *
166 * To be supported types:
167 * - TypedArrays and ArrayBuffers
168 * - arrays of embedder types
169 *
170 *
171 * The API offers a limited support for function overloads:
172 *
173 * \code
174 * void FastMethod_2Args(int param, bool another_param);
175 * void FastMethod_3Args(int param, bool another_param, int third_param);
176 *
177 * v8::CFunction fast_method_2args_c_func =
178 * MakeV8CFunction(FastMethod_2Args);
179 * v8::CFunction fast_method_3args_c_func =
180 * MakeV8CFunction(FastMethod_3Args);
181 * const v8::CFunction fast_method_overloads[] = {fast_method_2args_c_func,
182 * fast_method_3args_c_func};
183 * Local<v8::FunctionTemplate> method_template =
184 * v8::FunctionTemplate::NewWithCFunctionOverloads(
185 * isolate, SlowCallback, data, signature, length,
186 * constructor_behavior, side_effect_type,
187 * {fast_method_overloads, 2});
188 * \endcode
189 *
190 * In this example a single FunctionTemplate is associated to multiple C++
191 * functions. The overload resolution is currently only based on the number of
192 * arguments passed in a call. For example, if this method_template is
193 * registered with a wrapper JS object as described above, a call with two
194 * arguments:
195 * obj.method(42, true);
196 * will result in a fast call to FastMethod_2Args, while a call with three or
197 * more arguments:
198 * obj.method(42, true, 11);
199 * will result in a fast call to FastMethod_3Args. Instead a call with less than
200 * two arguments, like:
201 * obj.method(42);
202 * would not result in a fast call but would fall back to executing the
203 * associated SlowCallback.
204 */
205
206#include <stddef.h>
207#include <stdint.h>
208
209#include <tuple>
210#include <type_traits>
211
212#include "v8-internal.h" // NOLINT(build/include_directory)
213#include "v8-local-handle.h" // NOLINT(build/include_directory)
214#include "v8-typed-array.h" // NOLINT(build/include_directory)
215#include "v8-value.h" // NOLINT(build/include_directory)
216#include "v8config.h" // NOLINT(build/include_directory)
217
218namespace v8 {
219
220class Isolate;
221
223 public:
224 enum class Type : uint8_t {
225 kVoid,
226 kBool,
227 kUint8,
228 kInt32,
229 kUint32,
230 kInt64,
231 kUint64,
232 kFloat32,
233 kFloat64,
234 kPointer,
235 kV8Value,
237 kApiObject, // This will be deprecated once all users have
238 // migrated from v8::ApiObject to v8::Local<v8::Value>.
239 kAny, // This is added to enable untyped representation of fast
240 // call arguments for test purposes. It can represent any of
241 // the other types stored in the same memory as a union
242 // (see AnyCType declared below). This allows for
243 // uniform passing of arguments w.r.t. their location
244 // (in a register or on the stack), independent of their
245 // actual type. It's currently used by the arm64 simulator
246 // and can be added to the other simulators as well when fast
247 // calls having both GP and FP params need to be supported.
248 };
249
250 // kCallbackOptionsType is not part of the Type enum
251 // because it is only used internally. Use value 255 that is larger
252 // than any valid Type enum.
253 static constexpr Type kCallbackOptionsType = Type(255);
254
255 enum class Flags : uint8_t {
256 kNone = 0,
257 kAllowSharedBit = 1 << 0, // Must be an ArrayBuffer or TypedArray
258 kEnforceRangeBit = 1 << 1, // T must be integral
259 kClampBit = 1 << 2, // T must be integral
260 kIsRestrictedBit = 1 << 3, // T must be float or double
261 };
262
263 explicit constexpr CTypeInfo(Type type, Flags flags = Flags::kNone)
264 : type_(type), flags_(flags) {}
265
266 typedef uint32_t Identifier;
267 explicit constexpr CTypeInfo(Identifier identifier)
268 : type_(static_cast<Type>((identifier >> 8) & 255)),
269 flags_(static_cast<Flags>(identifier & 255)) {}
270 constexpr Identifier GetId() const {
271 return static_cast<uint8_t>(type_) << 8 |
272 static_cast<uint8_t>(flags_);
273 }
274
275 constexpr Type GetType() const { return type_; }
276 constexpr Flags GetFlags() const { return flags_; }
277
278 static constexpr bool IsIntegralType(Type type) {
279 return type == Type::kUint8 || type == Type::kInt32 ||
280 type == Type::kUint32 || type == Type::kInt64 ||
281 type == Type::kUint64;
282 }
283
284 static constexpr bool IsFloatingPointType(Type type) {
285 return type == Type::kFloat32 || type == Type::kFloat64;
286 }
287
288 static constexpr bool IsPrimitive(Type type) {
289 return IsIntegralType(type) || IsFloatingPointType(type) ||
290 type == Type::kBool;
291 }
292
293 private:
294 Type type_;
295 Flags flags_;
296};
297
299 const char* data;
300 uint32_t length;
301};
302
304 public:
306 kNumber = 0, // Use numbers to represent 64 bit integers.
307 kBigInt = 1, // Use BigInts to represent 64 bit integers.
308 };
309
310 // Construct a struct to hold a CFunction's type information.
311 // |return_info| describes the function's return type.
312 // |arg_info| is an array of |arg_count| CTypeInfos describing the
313 // arguments. Only the last argument may be of the special type
314 // CTypeInfo::kCallbackOptionsType.
315 CFunctionInfo(const CTypeInfo& return_info, unsigned int arg_count,
316 const CTypeInfo* arg_info,
318
319 const CTypeInfo& ReturnInfo() const { return return_info_; }
320
321 // The argument count, not including the v8::FastApiCallbackOptions
322 // if present.
323 unsigned int ArgumentCount() const {
324 return HasOptions() ? arg_count_ - 1 : arg_count_;
325 }
326
328
329 // |index| must be less than ArgumentCount().
330 // Note: if the last argument passed on construction of CFunctionInfo
331 // has type CTypeInfo::kCallbackOptionsType, it is not included in
332 // ArgumentCount().
333 const CTypeInfo& ArgumentInfo(unsigned int index) const;
334
335 bool HasOptions() const {
336 // The options arg is always the last one.
337 return arg_count_ > 0 && arg_info_[arg_count_ - 1].GetType() ==
339 }
340
341 private:
342 const CTypeInfo return_info_;
343 const Int64Representation repr_;
344 const unsigned int arg_count_;
345 const CTypeInfo* arg_info_;
346};
347
349
350// Provided for testing.
353
354#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
355 // In this case, Local<T> is not trivially copyable and the implicit
356 // copy constructor and copy assignment for the union are deleted.
358 AnyCType& operator=(const AnyCType& other) {
360 return *this;
361 }
362#endif
363
365 int32_t int32_value;
366 uint32_t uint32_value;
367 int64_t int64_value;
368 uint64_t uint64_value;
376};
377
378static_assert(
379 sizeof(AnyCType) == 8,
380 "The union AnyCType should have size == 64 bits, as this is assumed "
381 "by EffectControlLinearizer.");
382
384 public:
385 constexpr CFunction() : address_(nullptr), type_info_(nullptr) {}
386
387 const CTypeInfo& ReturnInfo() const { return type_info_->ReturnInfo(); }
388
389 const CTypeInfo& ArgumentInfo(unsigned int index) const {
390 return type_info_->ArgumentInfo(index);
391 }
392
393 unsigned int ArgumentCount() const { return type_info_->ArgumentCount(); }
394
395 const void* GetAddress() const { return address_; }
397 return type_info_->GetInt64Representation();
398 }
399 const CFunctionInfo* GetTypeInfo() const { return type_info_; }
400
402
403 template <typename F>
404 static CFunction Make(F* func,
407 CFunction result = ArgUnwrap<F*>::Make(func, int64_rep);
409 return result;
410 }
411
412 // Provided for testing purposes.
413 template <typename R, typename... Args, typename R_Patch,
414 typename... Args_Patch>
415 static CFunction Make(R (*func)(Args...),
416 R_Patch (*patching_func)(Args_Patch...),
419 CFunction c_func = ArgUnwrap<R (*)(Args...)>::Make(func, int64_rep);
420 static_assert(
421 sizeof...(Args_Patch) == sizeof...(Args),
422 "The patching function must have the same number of arguments.");
423 c_func.address_ = reinterpret_cast<void*>(patching_func);
424 return c_func;
425 }
426
427 CFunction(const void* address, const CFunctionInfo* type_info);
428
429 private:
430 const void* address_;
431 const CFunctionInfo* type_info_;
432
433 template <typename F>
434 class ArgUnwrap {
435 static_assert(sizeof(F) != sizeof(F),
436 "CFunction must be created from a function pointer.");
437 };
438
439 template <typename R, typename... Args>
440 class ArgUnwrap<R (*)(Args...)> {
441 public:
442 static CFunction Make(R (*func)(Args...),
445 };
446};
447
448/**
449 * A struct which may be passed to a fast call callback, like so:
450 * \code
451 * void FastMethodWithOptions(int param, FastApiCallbackOptions& options);
452 * \endcode
453 */
455 /**
456 * Creates a new instance of FastApiCallbackOptions for testing purpose. The
457 * returned instance may be filled with mock data.
458 */
460 return {};
461 }
462
463 v8::Isolate* isolate = nullptr;
464
465 /**
466 * The `data` passed to the FunctionTemplate constructor, or `undefined`.
467 */
469};
470
471namespace internal {
472
473// Helper to count the number of occurances of `T` in `List`
474template <typename T, typename... List>
475struct count : std::integral_constant<int, 0> {};
476template <typename T, typename... Args>
477struct count<T, T, Args...>
478 : std::integral_constant<std::size_t, 1 + count<T, Args...>::value> {};
479template <typename T, typename U, typename... Args>
480struct count<T, U, Args...> : count<T, Args...> {};
481
482template <CFunctionInfo::Int64Representation Representation,
483 typename RetBuilder, typename... ArgBuilders>
485 static constexpr int kOptionsArgCount =
486 count<FastApiCallbackOptions&, ArgBuilders...>();
487 static constexpr int kReceiverCount = 1;
488
489 static_assert(kOptionsArgCount == 0 || kOptionsArgCount == 1,
490 "Only one options parameter is supported.");
491
492 static_assert(sizeof...(ArgBuilders) >= kOptionsArgCount + kReceiverCount,
493 "The receiver or the options argument is missing.");
494
495 public:
497 : CFunctionInfo(RetBuilder::Build(), sizeof...(ArgBuilders),
498 arg_info_storage_, Representation),
499 arg_info_storage_{ArgBuilders::Build()...} {
500 constexpr CTypeInfo::Type kReturnType = RetBuilder::Build().GetType();
501 static_assert(kReturnType == CTypeInfo::Type::kVoid ||
502 kReturnType == CTypeInfo::Type::kBool ||
503 kReturnType == CTypeInfo::Type::kInt32 ||
504 kReturnType == CTypeInfo::Type::kUint32 ||
505 kReturnType == CTypeInfo::Type::kInt64 ||
506 kReturnType == CTypeInfo::Type::kUint64 ||
507 kReturnType == CTypeInfo::Type::kFloat32 ||
508 kReturnType == CTypeInfo::Type::kFloat64 ||
509 kReturnType == CTypeInfo::Type::kPointer ||
510 kReturnType == CTypeInfo::Type::kAny,
511 "String and api object values are not currently "
512 "supported return types.");
513 }
514
515 private:
516 const CTypeInfo arg_info_storage_[sizeof...(ArgBuilders)];
517};
518
519template <typename T>
521 static_assert(sizeof(T) != sizeof(T), "This type is not supported");
522};
523
524#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum)
525 template <>
526 struct TypeInfoHelper<T> {
527 static constexpr CTypeInfo::Flags Flags() {
528 return CTypeInfo::Flags::kNone;
529 }
530
531 static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; }
532 };
533
534template <CTypeInfo::Type type>
536
537#define DEFINE_TYPE_INFO_TRAITS(CType, Enum)
538 template <>
539 struct CTypeInfoTraits<CTypeInfo::Type::Enum> {
540 using ctype = CType;
541 };
542
543#define PRIMITIVE_C_TYPES(V)
544 V(bool, kBool)
545 V(uint8_t, kUint8)
546 V(int32_t, kInt32)
547 V(uint32_t, kUint32)
548 V(int64_t, kInt64)
549 V(uint64_t, kUint64)
550 V(float, kFloat32)
551 V(double, kFloat64)
552 V(void*, kPointer)
553
554// Same as above, but includes deprecated types for compatibility.
555#define ALL_C_TYPES(V)
556 PRIMITIVE_C_TYPES(V)
557 V(void, kVoid)
558 V(v8::Local<v8::Value>, kV8Value)
559 V(v8::Local<v8::Object>, kV8Value)
560 V(v8::Local<v8::Array>, kV8Value)
561 V(AnyCType, kAny)
562
563// ApiObject was a temporary solution to wrap the pointer to the v8::Value.
564// Please use v8::Local<v8::Value> in new code for the arguments and
565// v8::Local<v8::Object> for the receiver, as ApiObject will be deprecated.
566
569
570#undef PRIMITIVE_C_TYPES
571#undef ALL_C_TYPES
572
573#undef TYPED_ARRAY_C_TYPES
574
575template <>
577 static constexpr CTypeInfo::Flags Flags() { return CTypeInfo::Flags::kNone; }
578
579 static constexpr CTypeInfo::Type Type() {
581 }
582};
583
584template <>
586 static constexpr CTypeInfo::Flags Flags() { return CTypeInfo::Flags::kNone; }
587
588 static constexpr CTypeInfo::Type Type() {
590 }
591};
592
593#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG)
594 static_assert(((COND) == 0) || (ASSERTION), MSG)
595
596} // namespace internal
597
598template <typename T, CTypeInfo::Flags... Flags>
600 public:
601 using BaseType = T;
602
603 static constexpr CTypeInfo Build() {
604 constexpr CTypeInfo::Flags kFlags =
605 MergeFlags(internal::TypeInfoHelper<T>::Flags(), Flags...);
606 constexpr CTypeInfo::Type kType = internal::TypeInfoHelper<T>::Type();
607
609 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kEnforceRangeBit),
611 "kEnforceRangeBit is only allowed for integral types.");
613 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kClampBit),
615 "kClampBit is only allowed for integral types.");
617 uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kIsRestrictedBit),
619 "kIsRestrictedBit is only allowed for floating point types.");
620
621 // Return the same type with the merged flags.
622 return CTypeInfo(internal::TypeInfoHelper<T>::Type(), kFlags);
623 }
624
625 private:
626 template <typename... Rest>
627 static constexpr CTypeInfo::Flags MergeFlags(CTypeInfo::Flags flags,
628 Rest... rest) {
629 return CTypeInfo::Flags(uint8_t(flags) | uint8_t(MergeFlags(rest...)));
630 }
631 static constexpr CTypeInfo::Flags MergeFlags() { return CTypeInfo::Flags(0); }
632};
633
634namespace internal {
635template <typename RetBuilder, typename... ArgBuilders>
636class CFunctionBuilderWithFunction {
637 public:
638 explicit constexpr CFunctionBuilderWithFunction(const void* fn) : fn_(fn) {}
639
640 template <CTypeInfo::Flags... Flags>
641 constexpr auto Ret() {
642 return CFunctionBuilderWithFunction<
643 CTypeInfoBuilder<typename RetBuilder::BaseType, Flags...>,
644 ArgBuilders...>(fn_);
645 }
646
647 template <unsigned int N, CTypeInfo::Flags... Flags>
648 constexpr auto Arg() {
649 // Return a copy of the builder with the Nth arg builder merged with
650 // template parameter pack Flags.
651 return ArgImpl<N, Flags...>(
652 std::make_index_sequence<sizeof...(ArgBuilders)>());
653 }
654
655 // Provided for testing purposes.
656 template <typename Ret, typename... Args>
657 auto Patch(Ret (*patching_func)(Args...)) {
658 static_assert(
659 sizeof...(Args) == sizeof...(ArgBuilders),
660 "The patching function must have the same number of arguments.");
661 fn_ = reinterpret_cast<void*>(patching_func);
662 return *this;
663 }
664
665 template <CFunctionInfo::Int64Representation Representation =
667 auto Build() {
668 static CFunctionInfoImpl<Representation, RetBuilder, ArgBuilders...>
669 instance;
670 return CFunction(fn_, &instance);
671 }
672
673 private:
674 template <bool Merge, unsigned int N, CTypeInfo::Flags... Flags>
675 struct GetArgBuilder;
676
677 // Returns the same ArgBuilder as the one at index N, including its flags.
678 // Flags in the template parameter pack are ignored.
679 template <unsigned int N, CTypeInfo::Flags... Flags>
680 struct GetArgBuilder<false, N, Flags...> {
681 using type = std::tuple_element_t<N, std::tuple<ArgBuilders...>>;
682 };
683
684 // Returns an ArgBuilder with the same base type as the one at index N,
685 // but merges the flags with the flags in the template parameter pack.
686 template <unsigned int N, CTypeInfo::Flags... Flags>
687 struct GetArgBuilder<true, N, Flags...> {
688 using type = CTypeInfoBuilder<
689 typename std::tuple_element_t<N, std::tuple<ArgBuilders...>>::BaseType,
690 std::tuple_element_t<N, std::tuple<ArgBuilders...>>::Build().GetFlags(),
691 Flags...>;
692 };
693
694 // Return a copy of the CFunctionBuilder, but merges the Flags on
695 // ArgBuilder index N with the new Flags passed in the template parameter
696 // pack.
697 template <unsigned int N, CTypeInfo::Flags... Flags, size_t... I>
698 constexpr auto ArgImpl(std::index_sequence<I...>) {
699 return CFunctionBuilderWithFunction<
700 RetBuilder, typename GetArgBuilder<N == I, I, Flags...>::type...>(fn_);
701 }
702
703 const void* fn_;
704};
705
706class CFunctionBuilder {
707 public:
708 constexpr CFunctionBuilder() {}
709
710 template <typename R, typename... Args>
711 constexpr auto Fn(R (*fn)(Args...)) {
712 return CFunctionBuilderWithFunction<CTypeInfoBuilder<R>,
713 CTypeInfoBuilder<Args>...>(
714 reinterpret_cast<const void*>(fn));
715 }
716};
717
718} // namespace internal
719
720// static
721template <typename R, typename... Args>
722CFunction CFunction::ArgUnwrap<R (*)(Args...)>::Make(
723 R (*func)(Args...), CFunctionInfo::Int64Representation int64_rep) {
725 return internal::CFunctionBuilder().Fn(func).Build();
726 }
727 return internal::CFunctionBuilder()
728 .Fn(func)
730}
731
732using CFunctionBuilder = internal::CFunctionBuilder;
733
734static constexpr CTypeInfo kTypeInfoInt32 = CTypeInfo(CTypeInfo::Type::kInt32);
735static constexpr CTypeInfo kTypeInfoFloat64 =
737
738/**
739 * Copies the contents of this JavaScript array to a C++ buffer with
740 * a given max_length. A CTypeInfo is passed as an argument,
741 * instructing different rules for conversion (e.g. restricted float/double).
742 * The element type T of the destination array must match the C type
743 * corresponding to the CTypeInfo (specified by CTypeInfoTraits).
744 * If the array length is larger than max_length or the array is of
745 * unsupported type, the operation will fail, returning false. Generally, an
746 * array which contains objects, undefined, null or anything not convertible
747 * to the requested destination type, is considered unsupported. The operation
748 * returns true on success. `type_info` will be used for conversions.
749 */
750template <CTypeInfo::Identifier type_info_id, typename T>
752 Local<Array> src, T* dst, uint32_t max_length);
753
754template <>
759
760template <>
765
766template <>
769 float>(Local<Array> src, float* dst,
771
772template <>
775 double>(Local<Array> src, double* dst,
777
778} // namespace v8
779
780#endif // INCLUDE_V8_FAST_API_CALLS_H_
constexpr CFunction()
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
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)
friend class Local
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
const FastOneByteString * string_value
FastApiCallbackOptions * options_value
Local< Object > object_value
Local< Array > sequence_value
#define PRIMITIVE_C_TYPES(V)
#define ALL_C_TYPES(V)
#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG)
#define V8_HAS_ATTRIBUTE_TRIVIAL_ABI
Definition v8config.h:814
#define V8_EXPORT
Definition v8config.h:860
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:684
#define V8_TRIVIAL_ABI
Definition v8config.h:813