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 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 AccessorNameSetterCallbackV2 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
142enum class Intercepted : uint32_t { kNo = 1, kYes = 0 };
143
188 Local<Name> property, const PropertyCallbackInfo<Value>& info);
189
217 Intercepted (*)(Local<Name> property, Local<Value> value,
218 const PropertyCallbackInfo<Boolean>& info);
219// TODO(https://crbug.com/348660658): deprecate and remove.
221 V8_DEPRECATE_SOON("Use NamedPropertySetterCallbackV2 instead.") =
222 Intercepted (*)(Local<Name> property, Local<Value> value,
223 const PropertyCallbackInfo<void>& info);
224
252 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
253
279 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
280
288 void (*)(const PropertyCallbackInfo<Array>& info);
289
317 Intercepted (*)(Local<Name> property, const PropertyDescriptor& desc,
319// TODO(https://crbug.com/348660658): deprecate and remove.
321 V8_DEPRECATE_SOON("Use NamedPropertyDefinerCallbackV2 instead.") =
322 Intercepted (*)(Local<Name> property, const PropertyDescriptor& desc,
323 const PropertyCallbackInfo<void>& info);
324
347 Local<Name> property, const PropertyCallbackInfo<Value>& info);
348
353 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
354// TODO(https://crbug.com/348660658): deprecate and remove.
356 V8_DEPRECATE_SOON("Use IndexedPropertyGetterCallback instead.") =
358
363 Intercepted (*)(uint32_t index, Local<Value> value,
365// TODO(https://crbug.com/348660658): deprecate and remove.
367 V8_DEPRECATE_SOON("Use IndexedPropertySetterCallback instead.") =
368 Intercepted (*)(uint32_t index, Local<Value> value,
369 const PropertyCallbackInfo<void>& info);
370
375 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
376// TODO(https://crbug.com/348660658): deprecate and remove.
378 V8_DEPRECATE_SOON("Use IndexedPropertyQueryCallback instead.") =
380
385 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
386// TODO(https://crbug.com/348660658): deprecate and remove.
388 V8_DEPRECATE_SOON("Use IndexedPropertyDeleterCallback instead.") =
390
398 void (*)(const PropertyCallbackInfo<Array>& info);
399
404 Intercepted (*)(uint32_t index, const PropertyDescriptor& desc,
406// TODO(https://crbug.com/348660658): deprecate and remove.
408 V8_DEPRECATE_SOON("Use IndexedPropertyDefinerCallback instead.") =
409 Intercepted (*)(uint32_t index, const PropertyDescriptor& desc,
410 const PropertyCallbackInfo<void>& info);
411
416 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
417// TODO(https://crbug.com/348660658): deprecate and remove.
419 V8_DEPRECATE_SOON("Use IndexedPropertyDescriptorCallback instead.") =
421
426 uint32_t (*)(Local<Value> value, uint32_t start_index, uint32_t end_index,
427 uint32_t* out_length, const PropertyCallbackInfo<void>& info);
428
433 void (*)(const PropertyCallbackInfo<Value>& info);
434
439using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
440 Local<Object> accessed_object,
441 Local<Value> data);
443enum class ConstructorBehavior { kThrow, kAllow };
444
552class V8_EXPORT FunctionTemplate : public Template {
553 public:
555 static Local<FunctionTemplate> New(
556 Isolate* isolate, FunctionCallback callback = nullptr,
557 Local<Value> data = Local<Value>(),
558 Local<Signature> signature = Local<Signature>(), int length = 0,
559 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
560 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
561 const CFunction* c_function = nullptr, uint16_t instance_type = 0,
562 uint16_t allowed_receiver_instance_type_range_start = 0,
563 uint16_t allowed_receiver_instance_type_range_end = 0);
564
566 static Local<FunctionTemplate> NewWithCFunctionOverloads(
567 Isolate* isolate, FunctionCallback callback = nullptr,
568 Local<Value> data = Local<Value>(),
569 Local<Signature> signature = Local<Signature>(), int length = 0,
570 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
571 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
572 const std::span<const CFunction>& c_function_overloads = {});
573
577 static Local<FunctionTemplate> NewWithCache(
578 Isolate* isolate, FunctionCallback callback,
579 Local<Private> cache_property, Local<Value> data = Local<Value>(),
580 Local<Signature> signature = Local<Signature>(), int length = 0,
581 SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
582
584 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
585 Local<Context> context);
586
594 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
595
602 void SetCallHandler(
603 FunctionCallback callback, Local<Data> data = {},
604 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
605 const std::span<const CFunction>& c_function_overloads = {});
606
608 void SetLength(int length);
609
611 Local<ObjectTemplate> InstanceTemplate();
612
618 void Inherit(Local<FunctionTemplate> parent);
619
624 Local<ObjectTemplate> PrototypeTemplate();
625
632 void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
633
639 void SetClassName(Local<String> name);
640
645 void SetInterfaceName(Local<String> name);
646
651 void SetExceptionContext(ExceptionContext context);
652
657 void SetAcceptAnyReceiver(bool value);
658
663 void ReadOnlyPrototype();
664
669 void RemovePrototype();
670
675 bool HasInstance(Local<Value> object);
676
684 bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
685
692 void SealAndPrepareForPromotionToReadOnly();
693
694 V8_INLINE static FunctionTemplate* Cast(Data* data);
695
696 private:
697 FunctionTemplate();
698
699 static void CheckCast(Data* that);
700 friend class Context;
701 friend class ObjectTemplate;
702};
703
708enum class PropertyHandlerFlags {
712 kNone = 0,
713
719 kNonMasking = 1,
720
725 kOnlyInterceptStrings = 1 << 1,
726
730 kHasNoSideEffect = 1 << 2,
731
737 kHasDontDeleteProperty = 1 << 3,
738
747};
750 private:
751 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
753 return static_cast<PropertyHandlerFlags>(
754 static_cast<int>(flags) |
755 static_cast<int>(
757 }
758
759 public:
770 : getter(getter),
771 setter(setter),
772 query(query),
777 data(data),
778 flags(flags) {}
788 : getter(getter),
789 setter(setter),
790 query(query),
793 definer(nullptr),
794 descriptor(nullptr),
795 data(data),
796 flags(flags) {}
807 : getter(getter),
808 setter(setter),
809 query(nullptr),
814 data(data),
815 flags(flags) {}
826};
829 private:
830 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
832 return static_cast<PropertyHandlerFlags>(
833 static_cast<int>(flags) |
834 static_cast<int>(
836 }
837
838 public:
849 : getter(getter),
850 setter(setter),
851 query(query),
856 data(data),
857 flags(flags) {}
867 : getter(getter),
868 setter(setter),
869 query(query),
872 definer(nullptr),
873 descriptor(nullptr),
874 data(data),
875 flags(flags) {}
886 : getter(getter),
887 setter(setter),
888 query(nullptr),
893 data(data),
894 flags(flags) {}
895
897 "Use IndexedPropertyHandlerConfiguration with iterable_to_list")
907 Local<Value> data = Local<Value>(),
909 : getter(getter),
910 setter(setter),
911 query(query),
917 iterable_to_list(nullptr),
918 data(data),
919 flags(flags) {}
933 : getter(getter),
934 setter(setter),
935 query(query),
942 data(data),
943 flags(flags) {}
956};
957
964class V8_EXPORT ObjectTemplate : public Template {
965 public:
967 static Local<ObjectTemplate> New(
968 Isolate* isolate,
970
977
989 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
990
1001 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
1002
1009 void SetCallAsFunctionHandler(FunctionCallback callback,
1010 Local<Value> data = Local<Value>());
1011
1020 void MarkAsUndetectable();
1021
1030 void SetAccessCheckCallback(AccessCheckCallback callback,
1031 Local<Value> data = Local<Value>());
1032
1039 void SetAccessCheckCallbackAndHandler(
1040 AccessCheckCallback callback,
1041 const NamedPropertyHandlerConfiguration& named_handler,
1042 const IndexedPropertyHandlerConfiguration& indexed_handler,
1043 Local<Value> data = Local<Value>());
1044
1049 int InternalFieldCount() const;
1050
1055 void SetInternalFieldCount(int value);
1056
1060 bool IsImmutableProto() const;
1061
1066 void SetImmutableProto();
1067
1077 void SetCodeLike();
1078 bool IsCodeLike() const;
1079
1086 void SealAndPrepareForPromotionToReadOnly();
1087
1088 V8_INLINE static ObjectTemplate* Cast(Data* data);
1089
1090 private:
1092
1093 static void CheckCast(Data* that);
1094 friend class FunctionTemplate;
1095};
1096
1100class V8_EXPORT DictionaryTemplate final : public Data {
1101 public:
1109 static Local<DictionaryTemplate> New(Isolate* isolate,
1110 std::span<const std::string_view> names);
1111
1122 Local<Context> context, std::span<MaybeLocal<Value>> property_values);
1123
1124 V8_INLINE static DictionaryTemplate* Cast(Data* data);
1125
1126 private:
1127 static void CheckCast(Data* that);
1128
1130};
1131
1140class V8_EXPORT Signature : public Data {
1141 public:
1142 static Local<Signature> New(
1143 Isolate* isolate,
1145
1146 V8_INLINE static Signature* Cast(Data* data);
1147
1148 private:
1149 Signature();
1150
1151 static void CheckCast(Data* that);
1152};
1153
1154// --- Implementation ---
1156void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1157 PropertyAttribute attributes) {
1159 .ToLocalChecked(),
1160 value, attributes);
1161}
1164#ifdef V8_ENABLE_CHECKS
1165 CheckCast(data);
1166#endif
1167 return reinterpret_cast<FunctionTemplate*>(data);
1168}
1171#ifdef V8_ENABLE_CHECKS
1172 CheckCast(data);
1173#endif
1174 return reinterpret_cast<ObjectTemplate*>(data);
1175}
1178#ifdef V8_ENABLE_CHECKS
1179 CheckCast(data);
1180#endif
1181 return reinterpret_cast<DictionaryTemplate*>(data);
1182}
1185#ifdef V8_ENABLE_CHECKS
1186 CheckCast(data);
1187#endif
1188 return reinterpret_cast<Signature*>(data);
1189}
1190
1191} // namespace v8
1192
1193#endif // INCLUDE_V8_TEMPLATE_H_
Definition: v8-context.h:53
Definition: v8-data.h:18
Definition: v8-template.h:1099
static DictionaryTemplate * Cast(Data *data)
Definition: v8-template.h:1176
Definition: v8-template.h:551
static FunctionTemplate * Cast(Data *data)
Definition: v8-template.h:1162
Definition: v8-isolate.h:292
Definition: v8-local-handle.h:366
Definition: v8-local-handle.h:734
Definition: v8-template.h:963
static ObjectTemplate * Cast(Data *data)
Definition: v8-template.h:1169
Definition: v8-function-callback.h:224
Definition: v8-object.h:106
Definition: v8-container.h:139
Definition: v8-template.h:1139
static Signature * Cast(Data *data)
Definition: v8-template.h:1183
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: v8-value.h:32
Definition: libplatform.h:15
PropertyAttribute
Definition: v8-object.h:154
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) AccessorNameGetterCallback
Definition: v8-object.h:180
Intercepted(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) NamedPropertySetterCallback
Definition: v8-template.h:222
Intercepted(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< Boolean > &info) IndexedPropertySetterCallback
Definition: v8-template.h:363
SideEffectType
Definition: v8-object.h:232
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyDescriptorCallback
Definition: v8-template.h:346
void(*)(const PropertyCallbackInfo< Value > &info) IndexedPropertyIterableToListCallback
Definition: v8-template.h:432
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyGetterCallback
Definition: v8-template.h:187
Intercepted(*)(Local< Name > property, const PropertyDescriptor &desc, const PropertyCallbackInfo< void > &info) NamedPropertyDefinerCallback
Definition: v8-template.h:322
IndexedPropertyDescriptorCallback IndexedPropertyDescriptorCallbackV2
Definition: v8-template.h:419
IndexedPropertyGetterCallback IndexedPropertyGetterCallbackV2
Definition: v8-template.h:356
Intercepted(*)(uint32_t index, const PropertyDescriptor &desc, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDefinerCallback
Definition: v8-template.h:404
Intrinsic
Definition: v8-template.h:41
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDeleterCallback
Definition: v8-template.h:384
void(*)(const PropertyCallbackInfo< Array > &info) NamedPropertyEnumeratorCallback
Definition: v8-template.h:287
IndexedPropertyDeleterCallback IndexedPropertyDeleterCallbackV2
Definition: v8-template.h:388
ConstructorBehavior
Definition: v8-template.h:442
void(*)(const FunctionCallbackInfo< Value > &info) FunctionCallback
Definition: v8-function-callback.h:351
Intercepted(*)(uint32_t index, const PropertyDescriptor &desc, const PropertyCallbackInfo< void > &info) IndexedPropertyDefinerCallbackV2
Definition: v8-template.h:409
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyGetterCallback
Definition: v8-template.h:352
Intercepted(*)(Local< Name > property, const PropertyDescriptor &desc, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDefinerCallbackV2
Definition: v8-template.h:317
void(*)(const PropertyCallbackInfo< Array > &info) IndexedPropertyEnumeratorCallback
Definition: v8-template.h:397
IndexedPropertyQueryCallback IndexedPropertyQueryCallbackV2
Definition: v8-template.h:378
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< Boolean > &info) AccessorNameSetterCallbackV2
Definition: v8-object.h:203
Intercepted
Definition: v8-template.h:141
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Integer > &info) NamedPropertyQueryCallback
Definition: v8-template.h:251
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:426
bool(*)(Local< Context > accessing_context, Local< Object > accessed_object, Local< Value > data) AccessCheckCallback
Definition: v8-template.h:440
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDeleterCallback
Definition: v8-template.h:278
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyDescriptorCallback
Definition: v8-template.h:415
Intercepted(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< Boolean > &info) NamedPropertySetterCallbackV2
Definition: v8-template.h:217
PropertyHandlerFlags
Definition: v8-template.h:707
Intercepted(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< void > &info) IndexedPropertySetterCallbackV2
Definition: v8-template.h:368
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Integer > &info) IndexedPropertyQueryCallback
Definition: v8-template.h:374
Definition: v8-template.h:827
IndexedPropertyGetterCallback getter
Definition: v8-template.h:944
IndexedPropertyDefinerCallback definer
Definition: v8-template.h:949
PropertyHandlerFlags flags
Definition: v8-template.h:954
IndexedPropertyDescriptorCallback descriptor
Definition: v8-template.h:950
IndexedPropertySetterCallback setter
Definition: v8-template.h:945
IndexedPropertyIndexOfCallback index_of
Definition: v8-template.h:951
IndexedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:948
IndexedPropertyQueryCallback query
Definition: v8-template.h:946
Local< Value > data
Definition: v8-template.h:953
IndexedPropertyDeleterCallback deleter
Definition: v8-template.h:947
IndexedPropertyIterableToListCallback iterable_to_list
Definition: v8-template.h:952
Definition: v8-template.h:748
NamedPropertyDescriptorCallback descriptor
Definition: v8-template.h:822
Local< Value > data
Definition: v8-template.h:823
NamedPropertyDeleterCallback deleter
Definition: v8-template.h:819
NamedPropertyDefinerCallbackV2 definer
Definition: v8-template.h:821
NamedPropertySetterCallbackV2 setter
Definition: v8-template.h:817
NamedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:820
NamedPropertyQueryCallback query
Definition: v8-template.h:818
NamedPropertyGetterCallback getter
Definition: v8-template.h:816
PropertyHandlerFlags flags
Definition: v8-template.h:824
#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:867
#define V8_INLINE
Definition: v8config.h:511
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:624
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:681