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 <span>
10#include <string_view>
11
12#include "v8-data.h" // NOLINT(build/include_directory)
13#include "v8-exception.h" // NOLINT(build/include_directory)
14#include "v8-function-callback.h" // NOLINT(build/include_directory)
15#include "v8-local-handle.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 V8_DEPRECATE_SOON("Use the 2-parameter version without PropertyAttribute.")
61 void SetPrivate(Local<Private> name, Local<Data> value,
62 PropertyAttribute attributes);
63 V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value,
64 PropertyAttribute attributes = None);
65
80 void SetAccessorProperty(
81 Local<Name> name,
84 PropertyAttribute attribute = None);
85
106 void SetNativeDataProperty(
108 AccessorNameSetterCallback setter = nullptr,
109 Local<Data> data = Local<Data>(), PropertyAttribute attribute = None,
110 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
111 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
112
117 void SetLazyDataProperty(
119 Local<Data> data = Local<Data>(), PropertyAttribute attribute = None,
120 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
121 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
122
127 void SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
128 PropertyAttribute attribute = None);
129
130 private:
131 Template();
133 friend class ObjectTemplate;
134 friend class FunctionTemplate;
135};
136
144enum class Intercepted : uint32_t { kNo = 1, kYes = 0 };
145
190 Local<Name> property, const PropertyCallbackInfo<Value>& info);
191
219 Intercepted (*)(Local<Name> property, Local<Value> value,
221// TODO(https://crbug.com/348660658): deprecate and remove.
223 V8_DEPRECATE_SOON("Use NamedPropertySetterCallback instead.") =
225
253 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
254
280 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
281
289 void (*)(const PropertyCallbackInfo<Array>& info);
290
318 Intercepted (*)(Local<Name> property, const PropertyDescriptor& desc,
320// TODO(https://crbug.com/348660658): deprecate and remove.
322 V8_DEPRECATE_SOON("Use NamedPropertyDefinerCallback instead.") =
324
347 Local<Name> property, const PropertyCallbackInfo<Value>& info);
348
353 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
354
359 Intercepted (*)(uint32_t index, Local<Value> value,
361
366 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
367
372 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
373
381 void (*)(const PropertyCallbackInfo<Array>& info);
382
387 Intercepted (*)(uint32_t index, const PropertyDescriptor& desc,
389
394 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
395
400 uint32_t (*)(Local<Value> value, uint32_t start_index, uint32_t end_index,
401 uint32_t* out_length, const PropertyCallbackInfo<void>& info);
402
407 void (*)(const PropertyCallbackInfo<Value>& info);
408
413using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
414 Local<Object> accessed_object,
415 Local<Value> data);
417enum class ConstructorBehavior { kThrow, kAllow };
418
526class V8_EXPORT FunctionTemplate : public Template {
527 public:
529 static Local<FunctionTemplate> New(
530 Isolate* isolate, FunctionCallback callback = nullptr,
531 Local<Data> data = Local<Data>(),
532 Local<Signature> signature = Local<Signature>(), int length = 0,
533 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
534 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
535 const CFunction* c_function = nullptr, uint16_t instance_type = 0,
536 uint16_t allowed_receiver_instance_type_range_start = 0,
537 uint16_t allowed_receiver_instance_type_range_end = 0);
538
540 static Local<FunctionTemplate> NewWithCFunctionOverloads(
541 Isolate* isolate, FunctionCallback callback = nullptr,
542 Local<Data> data = Local<Data>(),
543 Local<Signature> signature = Local<Signature>(), int length = 0,
544 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
545 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
546 const std::span<const CFunction>& c_function_overloads = {});
547
551 static Local<FunctionTemplate> NewWithCache(
552 Isolate* isolate, FunctionCallback callback,
553 Local<Private> cache_property, Local<Data> data = Local<Data>(),
554 Local<Signature> signature = Local<Signature>(), int length = 0,
555 SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
556
558 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
559 Local<Context> context);
560
568 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
569
576 void SetCallHandler(
577 FunctionCallback callback, Local<Data> data = {},
578 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
579 const std::span<const CFunction>& c_function_overloads = {});
580
582 void SetLength(int length);
583
585 Local<ObjectTemplate> InstanceTemplate();
586
592 void Inherit(Local<FunctionTemplate> parent);
593
598 Local<ObjectTemplate> PrototypeTemplate();
599
606 void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
607
613 void SetClassName(Local<String> name);
614
619 void SetInterfaceName(Local<String> name);
620
625 void SetExceptionContext(ExceptionContext context);
626
631 void SetAcceptAnyReceiver(bool value);
632
637 void ReadOnlyPrototype();
638
643 void RemovePrototype();
644
649 bool HasInstance(Local<Value> object);
650
658 bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
659
666 void SealAndPrepareForPromotionToReadOnly();
667
668 V8_INLINE static FunctionTemplate* Cast(Data* data);
669
670 private:
671 FunctionTemplate();
672
673 static void CheckCast(Data* that);
674 friend class Context;
675 friend class ObjectTemplate;
676};
677
682enum class PropertyHandlerFlags {
686 kNone = 0,
687
693 kNonMasking = 1,
694
699 kOnlyInterceptStrings = 1 << 1,
700
704 kHasNoSideEffect = 1 << 2,
705
711 kHasDontDeleteProperty = 1 << 3,
712
721};
724 private:
725 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
726 PropertyHandlerFlags flags) {
727 return static_cast<PropertyHandlerFlags>(
728 static_cast<int>(flags) |
729 static_cast<int>(
730 PropertyHandlerFlags::kInternalNewCallbacksSignatures));
731 }
732
733 public:
742 Local<Data> data = Local<Data>(),
743 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
744 : getter(getter),
745 setter(setter),
746 query(query),
747 deleter(deleter),
748 enumerator(enumerator),
749 definer(definer),
750 descriptor(descriptor),
751 data(data),
752 flags(flags) {}
756 NamedPropertySetterCallback setter = nullptr, //
757 NamedPropertyQueryCallback query = nullptr, //
758 NamedPropertyDeleterCallback deleter = nullptr, //
759 NamedPropertyEnumeratorCallback enumerator = nullptr, //
760 Local<Data> data = Local<Data>(),
761 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
762 : getter(getter),
763 setter(setter),
764 query(query),
765 deleter(deleter),
766 enumerator(enumerator),
767 definer(nullptr),
768 descriptor(nullptr),
769 data(data),
770 flags(flags) {}
779 Local<Data> data = Local<Data>(),
780 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
781 : getter(getter),
782 setter(setter),
783 query(nullptr),
784 deleter(deleter),
785 enumerator(enumerator),
786 definer(definer),
787 descriptor(descriptor),
788 data(data),
789 flags(flags) {}
800};
803 private:
804 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
805 PropertyHandlerFlags flags) {
806 return static_cast<PropertyHandlerFlags>(
807 static_cast<int>(flags) |
808 static_cast<int>(
809 PropertyHandlerFlags::kInternalNewCallbacksSignatures));
810 }
811
812 public:
821 Local<Data> data = Local<Data>(),
822 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
823 : getter(getter),
824 setter(setter),
825 query(query),
826 deleter(deleter),
827 enumerator(enumerator),
828 definer(definer),
829 descriptor(descriptor),
830 data(data),
831 flags(flags) {}
834 IndexedPropertyGetterCallback getter = nullptr, //
835 IndexedPropertySetterCallback setter = nullptr, //
836 IndexedPropertyQueryCallback query = nullptr, //
837 IndexedPropertyDeleterCallback deleter = nullptr, //
838 IndexedPropertyEnumeratorCallback enumerator = nullptr, //
839 Local<Data> data = Local<Data>(), //
840 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
841 : getter(getter),
842 setter(setter),
843 query(query),
844 deleter(deleter),
845 enumerator(enumerator),
846 definer(nullptr),
847 descriptor(nullptr),
848 data(data),
849 flags(flags) {}
858 Local<Data> data = Local<Data>(),
859 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
860 : getter(getter),
861 setter(setter),
862 query(nullptr),
863 deleter(deleter),
864 enumerator(enumerator),
865 definer(definer),
866 descriptor(descriptor),
867 data(data),
868 flags(flags) {}
869
871 "Use IndexedPropertyHandlerConfiguration with iterable_to_list")
881 Local<Data> data = Local<Data>(),
883 : getter(getter),
884 setter(setter),
885 query(query),
886 deleter(deleter),
887 enumerator(enumerator),
888 definer(definer),
889 descriptor(descriptor),
890 index_of(index_of),
891 iterable_to_list(nullptr),
892 data(data),
893 flags(flags) {}
905 Local<Data> data = Local<Data>(),
906 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
907 : getter(getter),
908 setter(setter),
909 query(query),
910 deleter(deleter),
911 enumerator(enumerator),
912 definer(definer),
913 descriptor(descriptor),
914 index_of(index_of),
915 iterable_to_list(iterable_to_list),
916 data(data),
917 flags(flags) {}
927 IndexedPropertyIterableToListCallback iterable_to_list = nullptr;
930};
931
938class V8_EXPORT ObjectTemplate : public Template {
939 public:
941 static Local<ObjectTemplate> New(
942 Isolate* isolate,
944
951
963 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
964
975 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
976
983 void SetCallAsFunctionHandler(FunctionCallback callback,
984 Local<Data> data = Local<Data>());
985
994 void MarkAsUndetectable();
995
1004 void SetAccessCheckCallback(AccessCheckCallback callback,
1005 Local<Value> data = Local<Value>());
1006
1013 void SetAccessCheckCallbackAndHandler(
1014 AccessCheckCallback callback,
1015 const NamedPropertyHandlerConfiguration& named_handler,
1016 const IndexedPropertyHandlerConfiguration& indexed_handler,
1017 Local<Value> data = Local<Value>());
1018
1023 int InternalFieldCount() const;
1024
1029 void SetInternalFieldCount(int value);
1030
1034 bool IsImmutableProto() const;
1035
1040 void SetImmutableProto();
1041
1051 void SetCodeLike();
1052 bool IsCodeLike() const;
1053
1060 void SealAndPrepareForPromotionToReadOnly();
1061
1062 V8_INLINE static ObjectTemplate* Cast(Data* data);
1063
1064 private:
1066
1067 static void CheckCast(Data* that);
1068 friend class FunctionTemplate;
1069};
1070
1074class V8_EXPORT DictionaryTemplate final : public Data {
1075 public:
1083 static Local<DictionaryTemplate> New(Isolate* isolate,
1084 std::span<const std::string_view> names);
1085
1096 Local<Context> context, std::span<MaybeLocal<Value>> property_values);
1097
1098 V8_INLINE static DictionaryTemplate* Cast(Data* data);
1099
1100 private:
1101 static void CheckCast(Data* that);
1102
1104};
1105
1114class V8_EXPORT Signature : public Data {
1115 public:
1116 static Local<Signature> New(
1117 Isolate* isolate,
1119
1120 V8_INLINE static Signature* Cast(Data* data);
1121
1122 private:
1123 Signature();
1124
1125 static void CheckCast(Data* that);
1126};
1127
1128// --- Implementation ---
1130void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1131 PropertyAttribute attributes) {
1132 Set(String::NewFromUtf8(isolate, name, NewStringType::kInternalized)
1133 .ToLocalChecked(),
1134 value, attributes);
1135}
1137FunctionTemplate* FunctionTemplate::Cast(Data* data) {
1138#ifdef V8_ENABLE_CHECKS
1139 CheckCast(data);
1140#endif
1141 return reinterpret_cast<FunctionTemplate*>(data);
1142}
1144ObjectTemplate* ObjectTemplate::Cast(Data* data) {
1145#ifdef V8_ENABLE_CHECKS
1146 CheckCast(data);
1147#endif
1148 return reinterpret_cast<ObjectTemplate*>(data);
1149}
1151DictionaryTemplate* DictionaryTemplate::Cast(Data* data) {
1152#ifdef V8_ENABLE_CHECKS
1153 CheckCast(data);
1154#endif
1155 return reinterpret_cast<DictionaryTemplate*>(data);
1156}
1158Signature* Signature::Cast(Data* data) {
1159#ifdef V8_ENABLE_CHECKS
1160 CheckCast(data);
1161#endif
1162 return reinterpret_cast<Signature*>(data);
1163}
1164
1165} // namespace v8
1166
1167#endif // INCLUDE_V8_TEMPLATE_H_
Definition: v8-context.h:55
Definition: v8-data.h:18
Definition: v8-template.h:1073
Definition: v8-template.h:525
Definition: v8-isolate.h:305
Definition: v8-local-handle.h:356
Definition: v8-local-handle.h:724
Definition: v8-primitive.h:82
Definition: v8-template.h:937
Definition: v8-object.h:49
Definition: v8-function-callback.h:231
Definition: v8-object.h:109
Definition: v8-container.h:147
Definition: v8-template.h:1113
Definition: v8-template.h:49
Definition: libplatform.h:15
PropertyAttribute
Definition: v8-object.h:157
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) AccessorNameGetterCallback
Definition: v8-object.h:183
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< Boolean > &info) AccessorNameSetterCallback
Definition: v8-object.h:206
Intercepted(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< Boolean > &info) IndexedPropertySetterCallback
Definition: v8-template.h:359
SideEffectType
Definition: v8-object.h:234
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyDescriptorCallback
Definition: v8-template.h:346
void(*)(const PropertyCallbackInfo< Value > &info) IndexedPropertyIterableToListCallback
Definition: v8-template.h:406
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyGetterCallback
Definition: v8-template.h:189
Intercepted(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< Boolean > &info) NamedPropertySetterCallback
Definition: v8-template.h:219
Intercepted(*)(uint32_t index, const PropertyDescriptor &desc, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDefinerCallback
Definition: v8-template.h:387
Intrinsic
Definition: v8-template.h:41
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDeleterCallback
Definition: v8-template.h:371
void(*)(const PropertyCallbackInfo< Array > &info) NamedPropertyEnumeratorCallback
Definition: v8-template.h:288
ConstructorBehavior
Definition: v8-template.h:416
void(*)(const FunctionCallbackInfo< Value > &info) FunctionCallback
Definition: v8-function-callback.h:355
Intercepted(*)(Local< Name > property, const PropertyDescriptor &desc, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDefinerCallback
Definition: v8-template.h:318
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyGetterCallback
Definition: v8-template.h:352
NamedPropertyDefinerCallback NamedPropertyDefinerCallbackV2
Definition: v8-template.h:322
void(*)(const PropertyCallbackInfo< Array > &info) IndexedPropertyEnumeratorCallback
Definition: v8-template.h:380
Intercepted
Definition: v8-template.h:143
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Integer > &info) NamedPropertyQueryCallback
Definition: v8-template.h:252
uint32_t(*)(Local< Value > value, uint32_t start_index, uint32_t end_index, uint32_t *out_length, const PropertyCallbackInfo< void > &info) IndexedPropertyIndexOfCallback
Definition: v8-template.h:400
bool(*)(Local< Context > accessing_context, Local< Object > accessed_object, Local< Value > data) AccessCheckCallback
Definition: v8-template.h:414
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDeleterCallback
Definition: v8-template.h:279
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyDescriptorCallback
Definition: v8-template.h:393
PropertyHandlerFlags
Definition: v8-template.h:681
NamedPropertySetterCallback NamedPropertySetterCallbackV2
Definition: v8-template.h:223
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Integer > &info) IndexedPropertyQueryCallback
Definition: v8-template.h:365
Definition: v8-template.h:801
Definition: v8-template.h:722
#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:875
#define V8_INLINE
Definition: v8config.h:519
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:632
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:689