Loading...
Searching...
No Matches
v8-template.h
Go to the documentation of this file.
1// Copyright 2021 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_TEMPLATE_H_
6#define INCLUDE_V8_TEMPLATE_H_
7
8#include <cstddef>
9#include <string_view>
10
11#include "v8-data.h" // NOLINT(build/include_directory)
12#include "v8-exception.h" // NOLINT(build/include_directory)
13#include "v8-function-callback.h" // NOLINT(build/include_directory)
14#include "v8-local-handle.h" // NOLINT(build/include_directory)
15#include "v8-memory-span.h" // NOLINT(build/include_directory)
16#include "v8-object.h" // NOLINT(build/include_directory)
17#include "v8config.h" // NOLINT(build/include_directory)
18
19namespace v8 {
20
21class CFunction;
22class FunctionTemplate;
23class ObjectTemplate;
24class Signature;
25
26// --- Templates ---
27
28#define V8_INTRINSICS_LIST(F) \
29 F(ArrayProto_entries, array_entries_iterator) \
30 F(ArrayProto_forEach, array_for_each_iterator) \
31 F(ArrayProto_keys, array_keys_iterator) \
32 F(ArrayProto_values, array_values_iterator) \
33 F(ArrayPrototype, initial_array_prototype) \
34 F(AsyncIteratorPrototype, initial_async_iterator_prototype) \
35 F(ErrorPrototype, initial_error_prototype) \
36 F(IteratorPrototype, initial_iterator_prototype) \
37 F(MapIteratorPrototype, initial_map_iterator_prototype) \
38 F(ObjProto_valueOf, object_value_of_function) \
39 F(SetIteratorPrototype, initial_set_iterator_prototype)
40
42#define V8_DECL_INTRINSIC(name, iname) k##name,
44#undef V8_DECL_INTRINSIC
45};
46
50class V8_EXPORT Template : public Data {
51 public:
57 void Set(Local<Name> name, Local<Data> value,
58 PropertyAttribute attributes = None);
59 void SetPrivate(Local<Private> name, Local<Data> value,
60 PropertyAttribute attributes = None);
61 V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value,
62 PropertyAttribute attributes = None);
63
78 void SetAccessorProperty(
79 Local<Name> name,
82 PropertyAttribute attribute = None);
83
104 void SetNativeDataProperty(
106 AccessorNameSetterCallback setter = nullptr,
107 Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
108 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
109 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
110
115 void SetLazyDataProperty(
117 Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
118 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
119 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
120
125 void SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
126 PropertyAttribute attribute = None);
127
128 private:
129 Template();
131 friend class ObjectTemplate;
132 friend class FunctionTemplate;
133};
134
139enum class Intercepted : uint8_t { kNo = 0, kYes = 1 };
140
185 Local<Name> property, const PropertyCallbackInfo<Value>& info);
186// This variant will be deleted soon.
188 "Use NamedPropertyGetterCallback instead") =
189 void (*)(Local<Name> property, const PropertyCallbackInfo<Value>& info);
190
211 Intercepted (*)(Local<Name> property, Local<Value> value,
212 const PropertyCallbackInfo<void>& info);
213// This variant will be deleted soon.
215 "Use NamedPropertySetterCallback instead") =
216 void (*)(Local<Name> property, Local<Value> value,
217 const PropertyCallbackInfo<Value>& info);
218
246 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
247// This variant will be deleted soon.
249 "Use NamedPropertyQueryCallback instead") =
250 void (*)(Local<Name> property, const PropertyCallbackInfo<Integer>& info);
251
277 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
278// This variant will be deleted soon.
280 "Use NamedPropertyDeleterCallback instead") =
281 void (*)(Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
282
290 void (*)(const PropertyCallbackInfo<Array>& info);
291// This variant will be deleted soon.
292// This is just a renaming of the typedef.
294 "Use NamedPropertyEnumeratorCallback instead") =
296
317 Intercepted (*)(Local<Name> property, const PropertyDescriptor& desc,
318 const PropertyCallbackInfo<void>& info);
319// This variant will be deleted soon.
321 "Use NamedPropertyDefinerCallback instead") =
322 void (*)(Local<Name> property, const PropertyDescriptor& desc,
323 const PropertyCallbackInfo<Value>& info);
324
350 Local<Name> property, const PropertyCallbackInfo<Value>& info);
351// This variant will be deleted soon.
353 "Use NamedPropertyDescriptorCallback instead") =
354 void (*)(Local<Name> property, const PropertyCallbackInfo<Value>& info);
355
356// TODO(ishell): Rename IndexedPropertyXxxCallbackV2 back to
357// IndexedPropertyXxxCallback once the old IndexedPropertyXxxCallback is
358// removed.
359
364 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
365// This variant will be deleted soon.
367 "Use IndexedPropertyGetterCallbackV2 instead") =
368 void (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
369
374 uint32_t index, Local<Value> value, const PropertyCallbackInfo<void>& info);
375// This variant will be deleted soon.
377 "Use IndexedPropertySetterCallbackV2 instead") =
378 void (*)(uint32_t index, Local<Value> value,
379 const PropertyCallbackInfo<Value>& info);
380
385 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
386// This variant will be deleted soon.
388 "Use IndexedPropertyQueryCallbackV2 instead") =
389 void (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
390
395 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
396// This variant will be deleted soon.
398 "Use IndexedPropertyDeleterCallbackV2 instead") =
399 void (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
400
408 void (*)(const PropertyCallbackInfo<Array>& info);
409
414 Intercepted (*)(uint32_t index, const PropertyDescriptor& desc,
415 const PropertyCallbackInfo<void>& info);
416// This variant will be deleted soon.
418 "Use IndexedPropertyDefinerCallbackV2 instead") =
419 void (*)(uint32_t index, const PropertyDescriptor& desc,
420 const PropertyCallbackInfo<Value>& info);
421
426 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
427// This variant will be deleted soon.
429 "Use IndexedPropertyDescriptorCallbackV2 instead") =
430 void (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
431
436using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
437 Local<Object> accessed_object,
438 Local<Value> data);
440enum class ConstructorBehavior { kThrow, kAllow };
441
549class V8_EXPORT FunctionTemplate : public Template {
550 public:
552 static Local<FunctionTemplate> New(
553 Isolate* isolate, FunctionCallback callback = nullptr,
554 Local<Value> data = Local<Value>(),
555 Local<Signature> signature = Local<Signature>(), int length = 0,
556 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
557 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
558 const CFunction* c_function = nullptr, uint16_t instance_type = 0,
559 uint16_t allowed_receiver_instance_type_range_start = 0,
560 uint16_t allowed_receiver_instance_type_range_end = 0);
561
563 static Local<FunctionTemplate> NewWithCFunctionOverloads(
564 Isolate* isolate, FunctionCallback callback = nullptr,
565 Local<Value> data = Local<Value>(),
566 Local<Signature> signature = Local<Signature>(), int length = 0,
567 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
568 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
569 const MemorySpan<const CFunction>& c_function_overloads = {});
570
574 static Local<FunctionTemplate> NewWithCache(
575 Isolate* isolate, FunctionCallback callback,
576 Local<Private> cache_property, Local<Value> data = Local<Value>(),
577 Local<Signature> signature = Local<Signature>(), int length = 0,
578 SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
579
581 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
582 Local<Context> context);
583
591 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
592
599 void SetCallHandler(
600 FunctionCallback callback, Local<Value> data = Local<Value>(),
601 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
602 const MemorySpan<const CFunction>& c_function_overloads = {});
603
605 void SetLength(int length);
606
608 Local<ObjectTemplate> InstanceTemplate();
609
615 void Inherit(Local<FunctionTemplate> parent);
616
621 Local<ObjectTemplate> PrototypeTemplate();
622
629 void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
630
636 void SetClassName(Local<String> name);
637
642 void SetInterfaceName(Local<String> name);
643
648 void SetExceptionContext(ExceptionContext context);
649
654 void SetAcceptAnyReceiver(bool value);
655
660 void ReadOnlyPrototype();
661
666 void RemovePrototype();
667
672 bool HasInstance(Local<Value> object);
673
681 bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
682
689 void SealAndPrepareForPromotionToReadOnly();
690
691 V8_INLINE static FunctionTemplate* Cast(Data* data);
692
693 private:
694 FunctionTemplate();
695
696 static void CheckCast(Data* that);
697 friend class Context;
698 friend class ObjectTemplate;
699};
700
705enum class PropertyHandlerFlags {
709 kNone = 0,
710
716 kNonMasking = 1,
717
722 kOnlyInterceptStrings = 1 << 1,
723
727 kHasNoSideEffect = 1 << 2,
728
737};
740 private:
741 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
743 return static_cast<PropertyHandlerFlags>(
744 static_cast<int>(flags) |
745 static_cast<int>(
747 }
748
749 public:
760 : getter(getter),
761 setter(setter),
762 query(query),
767 data(data),
768 flags(flags) {}
778 : getter(getter),
779 setter(setter),
780 query(query),
783 definer(nullptr),
784 descriptor(nullptr),
785 data(data),
786 flags(flags) {}
797 : getter(getter),
798 setter(setter),
799 query(nullptr),
804 data(data),
805 flags(flags) {}
816};
819 private:
820 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
822 return static_cast<PropertyHandlerFlags>(
823 static_cast<int>(flags) |
824 static_cast<int>(
826 }
827
828 public:
839 : getter(getter),
840 setter(setter),
841 query(query),
846 data(data),
847 flags(flags) {}
857 : getter(getter),
858 setter(setter),
859 query(query),
862 definer(nullptr),
863 descriptor(nullptr),
864 data(data),
865 flags(flags) {}
876 : getter(getter),
877 setter(setter),
878 query(nullptr),
883 data(data),
884 flags(flags) {}
895};
896
903class V8_EXPORT ObjectTemplate : public Template {
904 public:
906 static Local<ObjectTemplate> New(
907 Isolate* isolate,
909
916
928 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
929
940 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
941
948 void SetCallAsFunctionHandler(FunctionCallback callback,
949 Local<Value> data = Local<Value>());
950
959 void MarkAsUndetectable();
960
969 void SetAccessCheckCallback(AccessCheckCallback callback,
970 Local<Value> data = Local<Value>());
971
978 void SetAccessCheckCallbackAndHandler(
979 AccessCheckCallback callback,
980 const NamedPropertyHandlerConfiguration& named_handler,
981 const IndexedPropertyHandlerConfiguration& indexed_handler,
982 Local<Value> data = Local<Value>());
983
988 int InternalFieldCount() const;
989
994 void SetInternalFieldCount(int value);
995
999 bool IsImmutableProto() const;
1000
1005 void SetImmutableProto();
1006
1016 void SetCodeLike();
1017 bool IsCodeLike() const;
1018
1025 void SealAndPrepareForPromotionToReadOnly();
1026
1027 V8_INLINE static ObjectTemplate* Cast(Data* data);
1028
1029 private:
1031
1032 static void CheckCast(Data* that);
1033 friend class FunctionTemplate;
1034};
1035
1039class V8_EXPORT DictionaryTemplate final : public Data {
1040 public:
1048 static Local<DictionaryTemplate> New(
1050
1061 Local<Context> context, MemorySpan<MaybeLocal<Value>> property_values);
1062
1063 V8_INLINE static DictionaryTemplate* Cast(Data* data);
1064
1065 private:
1066 static void CheckCast(Data* that);
1067
1069};
1070
1079class V8_EXPORT Signature : public Data {
1080 public:
1081 static Local<Signature> New(
1082 Isolate* isolate,
1084
1085 V8_INLINE static Signature* Cast(Data* data);
1086
1087 private:
1088 Signature();
1089
1090 static void CheckCast(Data* that);
1091};
1092
1093// --- Implementation ---
1095void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1096 PropertyAttribute attributes) {
1098 .ToLocalChecked(),
1099 value, attributes);
1100}
1103#ifdef V8_ENABLE_CHECKS
1104 CheckCast(data);
1105#endif
1106 return reinterpret_cast<FunctionTemplate*>(data);
1107}
1110#ifdef V8_ENABLE_CHECKS
1111 CheckCast(data);
1112#endif
1113 return reinterpret_cast<ObjectTemplate*>(data);
1114}
1117#ifdef V8_ENABLE_CHECKS
1118 CheckCast(data);
1119#endif
1120 return reinterpret_cast<DictionaryTemplate*>(data);
1121}
1124#ifdef V8_ENABLE_CHECKS
1125 CheckCast(data);
1126#endif
1127 return reinterpret_cast<Signature*>(data);
1128}
1129
1130} // namespace v8
1131
1132#endif // INCLUDE_V8_TEMPLATE_H_
Definition: v8-context.h:48
Definition: v8-data.h:18
Definition: v8-template.h:1038
static DictionaryTemplate * Cast(Data *data)
Definition: v8-template.h:1115
Definition: v8-template.h:548
static FunctionTemplate * Cast(Data *data)
Definition: v8-template.h:1101
Definition: v8-isolate.h:290
Definition: v8-local-handle.h:366
Definition: v8-local-handle.h:734
Definition: v8-memory-span.h:48
Definition: v8-template.h:902
static ObjectTemplate * Cast(Data *data)
Definition: v8-template.h:1108
Definition: v8-function-callback.h:187
Definition: v8-object.h:106
Definition: v8-container.h:148
Definition: v8-template.h:1078
static Signature * Cast(Data *data)
Definition: v8-template.h:1122
static MaybeLocal< String > NewFromUtf8(Isolate *isolate, const char *data, NewStringType type=NewStringType::kNormal, int length=-1)
Definition: v8-template.h:49
void Set(Local< Name > name, Local< Data > value, PropertyAttribute attributes=None)
Definition: libplatform.h:15
void(*)(uint32_t index, const PropertyCallbackInfo< Integer > &info) IndexedPropertyQueryCallback
Definition: v8-template.h:388
PropertyAttribute
Definition: v8-object.h:154
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) AccessorNameGetterCallback
Definition: v8-object.h:171
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) GenericNamedPropertyGetterCallback
Definition: v8-template.h:188
Intercepted(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) NamedPropertySetterCallback
Definition: v8-template.h:211
void(*)(Local< Name > property, const PropertyDescriptor &desc, const PropertyCallbackInfo< Value > &info) GenericNamedPropertyDefinerCallback
Definition: v8-template.h:322
void(*)(uint32_t index, const PropertyDescriptor &desc, const PropertyCallbackInfo< Value > &info) IndexedPropertyDefinerCallback
Definition: v8-template.h:419
void(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< Value > &info) IndexedPropertySetterCallback
Definition: v8-template.h:378
SideEffectType
Definition: v8-object.h:213
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyDescriptorCallback
Definition: v8-template.h:349
void(*)(uint32_t index, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDeleterCallback
Definition: v8-template.h:398
Intercepted(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< void > &info) IndexedPropertySetterCallbackV2
Definition: v8-template.h:373
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyGetterCallback
Definition: v8-template.h:184
NamedPropertyEnumeratorCallback GenericNamedPropertyEnumeratorCallback
Definition: v8-template.h:294
void(*)(Local< Name > property, const PropertyCallbackInfo< Integer > &info) GenericNamedPropertyQueryCallback
Definition: v8-template.h:249
Intercepted(*)(Local< Name > property, const PropertyDescriptor &desc, const PropertyCallbackInfo< void > &info) NamedPropertyDefinerCallback
Definition: v8-template.h:317
void(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyDescriptorCallback
Definition: v8-template.h:429
Intrinsic
Definition: v8-template.h:41
void(*)(const PropertyCallbackInfo< Array > &info) NamedPropertyEnumeratorCallback
Definition: v8-template.h:289
ConstructorBehavior
Definition: v8-template.h:439
void(*)(const FunctionCallbackInfo< Value > &info) FunctionCallback
Definition: v8-function-callback.h:314
Intercepted(*)(uint32_t index, const PropertyDescriptor &desc, const PropertyCallbackInfo< void > &info) IndexedPropertyDefinerCallbackV2
Definition: v8-template.h:414
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDeleterCallbackV2
Definition: v8-template.h:394
void(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyGetterCallback
Definition: v8-template.h:367
void(*)(const PropertyCallbackInfo< Array > &info) IndexedPropertyEnumeratorCallback
Definition: v8-template.h:407
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyDescriptorCallbackV2
Definition: v8-template.h:425
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Integer > &info) NamedPropertyQueryCallback
Definition: v8-template.h:245
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) AccessorNameSetterCallback
Definition: v8-object.h:175
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< Value > &info) GenericNamedPropertySetterCallback
Definition: v8-template.h:216
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyGetterCallbackV2
Definition: v8-template.h:363
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) GenericNamedPropertyDescriptorCallback
Definition: v8-template.h:353
bool(*)(Local< Context > accessing_context, Local< Object > accessed_object, Local< Value > data) AccessCheckCallback
Definition: v8-template.h:437
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDeleterCallback
Definition: v8-template.h:276
void(*)(Local< Name > property, const PropertyCallbackInfo< Boolean > &info) GenericNamedPropertyDeleterCallback
Definition: v8-template.h:280
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Integer > &info) IndexedPropertyQueryCallbackV2
Definition: v8-template.h:384
PropertyHandlerFlags
Definition: v8-template.h:704
Intercepted
Definition: v8-template.h:138
Definition: v8-template.h:817
PropertyHandlerFlags flags
Definition: v8-template.h:893
IndexedPropertyQueryCallbackV2 query
Definition: v8-template.h:887
IndexedPropertyDeleterCallbackV2 deleter
Definition: v8-template.h:888
IndexedPropertyGetterCallbackV2 getter
Definition: v8-template.h:885
IndexedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:889
Local< Value > data
Definition: v8-template.h:892
IndexedPropertyDescriptorCallbackV2 descriptor
Definition: v8-template.h:891
IndexedPropertySetterCallbackV2 setter
Definition: v8-template.h:886
IndexedPropertyDefinerCallbackV2 definer
Definition: v8-template.h:890
Definition: v8-template.h:738
NamedPropertyDescriptorCallback descriptor
Definition: v8-template.h:812
Local< Value > data
Definition: v8-template.h:813
NamedPropertySetterCallback setter
Definition: v8-template.h:807
NamedPropertyDeleterCallback deleter
Definition: v8-template.h:809
NamedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:810
NamedPropertyQueryCallback query
Definition: v8-template.h:808
NamedPropertyGetterCallback getter
Definition: v8-template.h:806
NamedPropertyDefinerCallback definer
Definition: v8-template.h:811
PropertyHandlerFlags flags
Definition: v8-template.h:814
#define V8_INTRINSICS_LIST(F)
Definition: v8-template.h:28
#define V8_DECL_INTRINSIC(name, iname)
Definition: v8-template.h:42
#define V8_EXPORT
Definition: v8config.h:855
#define V8_INLINE
Definition: v8config.h:508
#define V8_DEPRECATED(message)
Definition: v8config.h:614
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:679