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<Data> data = Local<Data>(), 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<Data> data = Local<Data>(), 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
247 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
248
274 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
275
283 void (*)(const PropertyCallbackInfo<Array>& info);
284
312 Intercepted (*)(Local<Name> property, const PropertyDescriptor& desc,
313 const PropertyCallbackInfo<Boolean>& info);
314
337 Local<Name> property, const PropertyCallbackInfo<Value>& info);
338
343 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
344
349 Intercepted (*)(uint32_t index, Local<Value> value,
350 const PropertyCallbackInfo<Boolean>& info);
351
356 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
357
362 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
363
371 void (*)(const PropertyCallbackInfo<Array>& info);
372
377 Intercepted (*)(uint32_t index, const PropertyDescriptor& desc,
378 const PropertyCallbackInfo<Boolean>& info);
379
384 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
385
390 uint32_t (*)(Local<Value> value, uint32_t start_index, uint32_t end_index,
391 uint32_t* out_length, const PropertyCallbackInfo<void>& info);
392
397 void (*)(const PropertyCallbackInfo<Value>& info);
398
403using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
404 Local<Object> accessed_object,
405 Local<Value> data);
407enum class ConstructorBehavior { kThrow, kAllow };
408
516class V8_EXPORT FunctionTemplate : public Template {
517 public:
519 static Local<FunctionTemplate> New(
520 Isolate* isolate, FunctionCallback callback = nullptr,
521 Local<Data> data = Local<Data>(),
522 Local<Signature> signature = Local<Signature>(), int length = 0,
523 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
524 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
525 const CFunction* c_function = nullptr, uint16_t instance_type = 0,
526 uint16_t allowed_receiver_instance_type_range_start = 0,
527 uint16_t allowed_receiver_instance_type_range_end = 0);
528
530 static Local<FunctionTemplate> NewWithCFunctionOverloads(
531 Isolate* isolate, FunctionCallback callback = nullptr,
532 Local<Data> data = Local<Data>(),
533 Local<Signature> signature = Local<Signature>(), int length = 0,
534 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
535 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
536 const std::span<const CFunction>& c_function_overloads = {});
537
541 static Local<FunctionTemplate> NewWithCache(
542 Isolate* isolate, FunctionCallback callback,
543 Local<Private> cache_property, Local<Data> data = Local<Data>(),
544 Local<Signature> signature = Local<Signature>(), int length = 0,
545 SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
546
548 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
549 Local<Context> context);
550
558 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
559
566 void SetCallHandler(
567 FunctionCallback callback, Local<Data> data = {},
568 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
569 const std::span<const CFunction>& c_function_overloads = {});
570
572 void SetLength(int length);
573
575 Local<ObjectTemplate> InstanceTemplate();
576
582 void Inherit(Local<FunctionTemplate> parent);
583
588 Local<ObjectTemplate> PrototypeTemplate();
589
596 void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
597
603 void SetClassName(Local<String> name);
604
609 void SetInterfaceName(Local<String> name);
610
615 void SetExceptionContext(ExceptionContext context);
616
621 void SetAcceptAnyReceiver(bool value);
622
627 void ReadOnlyPrototype();
628
633 void RemovePrototype();
634
639 bool HasInstance(Local<Value> object);
640
648 bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
649
656 void SealAndPrepareForPromotionToReadOnly();
657
658 V8_INLINE static FunctionTemplate* Cast(Data* data);
659
660 private:
661 FunctionTemplate();
662
663 static void CheckCast(Data* that);
664 friend class Context;
665 friend class ObjectTemplate;
666};
667
672enum class PropertyHandlerFlags {
676 kNone = 0,
677
683 kNonMasking = 1,
684
689 kOnlyInterceptStrings = 1 << 1,
690
694 kHasNoSideEffect = 1 << 2,
695
701 kHasDontDeleteProperty = 1 << 3,
702
711};
714 private:
715 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
717 return static_cast<PropertyHandlerFlags>(
718 static_cast<int>(flags) |
719 static_cast<int>(
721 }
722
723 public:
734 : getter(getter),
735 setter(setter),
736 query(query),
741 data(data),
742 flags(flags) {}
752 : getter(getter),
753 setter(setter),
754 query(query),
757 definer(nullptr),
758 descriptor(nullptr),
759 data(data),
760 flags(flags) {}
771 : getter(getter),
772 setter(setter),
773 query(nullptr),
778 data(data),
779 flags(flags) {}
790};
793 private:
794 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
796 return static_cast<PropertyHandlerFlags>(
797 static_cast<int>(flags) |
798 static_cast<int>(
800 }
801
802 public:
813 : getter(getter),
814 setter(setter),
815 query(query),
820 data(data),
821 flags(flags) {}
831 : getter(getter),
832 setter(setter),
833 query(query),
836 definer(nullptr),
837 descriptor(nullptr),
838 data(data),
839 flags(flags) {}
850 : getter(getter),
851 setter(setter),
852 query(nullptr),
857 data(data),
858 flags(flags) {}
859
861 "Use IndexedPropertyHandlerConfiguration with iterable_to_list")
871 Local<Data> data = Local<Data>(),
873 : getter(getter),
874 setter(setter),
875 query(query),
881 iterable_to_list(nullptr),
882 data(data),
883 flags(flags) {}
897 : getter(getter),
898 setter(setter),
899 query(query),
906 data(data),
907 flags(flags) {}
920};
921
928class V8_EXPORT ObjectTemplate : public Template {
929 public:
931 static Local<ObjectTemplate> New(
932 Isolate* isolate,
934
941
953 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
954
965 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
966
973 void SetCallAsFunctionHandler(FunctionCallback callback,
974 Local<Data> data = Local<Data>());
975
984 void MarkAsUndetectable();
985
994 void SetAccessCheckCallback(AccessCheckCallback callback,
995 Local<Value> data = Local<Value>());
996
1003 void SetAccessCheckCallbackAndHandler(
1004 AccessCheckCallback callback,
1005 const NamedPropertyHandlerConfiguration& named_handler,
1006 const IndexedPropertyHandlerConfiguration& indexed_handler,
1007 Local<Value> data = Local<Value>());
1008
1013 int InternalFieldCount() const;
1014
1019 void SetInternalFieldCount(int value);
1020
1024 bool IsImmutableProto() const;
1025
1030 void SetImmutableProto();
1031
1041 void SetCodeLike();
1042 bool IsCodeLike() const;
1043
1050 void SealAndPrepareForPromotionToReadOnly();
1051
1052 V8_INLINE static ObjectTemplate* Cast(Data* data);
1053
1054 private:
1056
1057 static void CheckCast(Data* that);
1058 friend class FunctionTemplate;
1059};
1060
1064class V8_EXPORT DictionaryTemplate final : public Data {
1065 public:
1073 static Local<DictionaryTemplate> New(Isolate* isolate,
1074 std::span<const std::string_view> names);
1075
1086 Local<Context> context, std::span<MaybeLocal<Value>> property_values);
1087
1088 V8_INLINE static DictionaryTemplate* Cast(Data* data);
1089
1090 private:
1091 static void CheckCast(Data* that);
1092
1094};
1095
1104class V8_EXPORT Signature : public Data {
1105 public:
1106 static Local<Signature> New(
1107 Isolate* isolate,
1109
1110 V8_INLINE static Signature* Cast(Data* data);
1111
1112 private:
1113 Signature();
1114
1115 static void CheckCast(Data* that);
1116};
1117
1118// --- Implementation ---
1120void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1121 PropertyAttribute attributes) {
1123 .ToLocalChecked(),
1124 value, attributes);
1125}
1128#ifdef V8_ENABLE_CHECKS
1129 CheckCast(data);
1130#endif
1131 return reinterpret_cast<FunctionTemplate*>(data);
1132}
1135#ifdef V8_ENABLE_CHECKS
1136 CheckCast(data);
1137#endif
1138 return reinterpret_cast<ObjectTemplate*>(data);
1139}
1142#ifdef V8_ENABLE_CHECKS
1143 CheckCast(data);
1144#endif
1145 return reinterpret_cast<DictionaryTemplate*>(data);
1146}
1149#ifdef V8_ENABLE_CHECKS
1150 CheckCast(data);
1151#endif
1152 return reinterpret_cast<Signature*>(data);
1153}
1154
1155} // namespace v8
1156
1157#endif // INCLUDE_V8_TEMPLATE_H_
Definition: v8-context.h:55
Definition: v8-data.h:18
Definition: v8-template.h:1063
static DictionaryTemplate * Cast(Data *data)
Definition: v8-template.h:1140
Definition: v8-template.h:515
static FunctionTemplate * Cast(Data *data)
Definition: v8-template.h:1126
Definition: v8-isolate.h:305
Definition: v8-local-handle.h:359
Definition: v8-local-handle.h:730
Definition: v8-template.h:927
static ObjectTemplate * Cast(Data *data)
Definition: v8-template.h:1133
Definition: v8-container.h:147
Definition: v8-template.h:1103
static Signature * Cast(Data *data)
Definition: v8-template.h:1147
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
PropertyAttribute
Definition: v8-object.h:154
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) AccessorNameGetterCallback
Definition: v8-object.h:180
Intercepted(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< Boolean > &info) IndexedPropertySetterCallback
Definition: v8-template.h:349
SideEffectType
Definition: v8-object.h:227
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyDescriptorCallback
Definition: v8-template.h:336
void(*)(const PropertyCallbackInfo< Value > &info) IndexedPropertyIterableToListCallback
Definition: v8-template.h:396
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyGetterCallback
Definition: v8-template.h:187
Intercepted(*)(uint32_t index, const PropertyDescriptor &desc, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDefinerCallback
Definition: v8-template.h:377
Intrinsic
Definition: v8-template.h:41
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDeleterCallback
Definition: v8-template.h:361
void(*)(const PropertyCallbackInfo< Array > &info) NamedPropertyEnumeratorCallback
Definition: v8-template.h:282
ConstructorBehavior
Definition: v8-template.h:406
void(*)(const FunctionCallbackInfo< Value > &info) FunctionCallback
Definition: v8-function-callback.h:354
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyGetterCallback
Definition: v8-template.h:342
Intercepted(*)(Local< Name > property, const PropertyDescriptor &desc, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDefinerCallbackV2
Definition: v8-template.h:312
void(*)(const PropertyCallbackInfo< Array > &info) IndexedPropertyEnumeratorCallback
Definition: v8-template.h:370
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:246
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:390
bool(*)(Local< Context > accessing_context, Local< Object > accessed_object, Local< Value > data) AccessCheckCallback
Definition: v8-template.h:404
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDeleterCallback
Definition: v8-template.h:273
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyDescriptorCallback
Definition: v8-template.h:383
Intercepted(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< Boolean > &info) NamedPropertySetterCallbackV2
Definition: v8-template.h:217
PropertyHandlerFlags
Definition: v8-template.h:671
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Integer > &info) IndexedPropertyQueryCallback
Definition: v8-template.h:355
Definition: v8-template.h:791
IndexedPropertyGetterCallback getter
Definition: v8-template.h:908
Local< Data > data
Definition: v8-template.h:917
IndexedPropertyDefinerCallback definer
Definition: v8-template.h:913
PropertyHandlerFlags flags
Definition: v8-template.h:918
IndexedPropertyDescriptorCallback descriptor
Definition: v8-template.h:914
IndexedPropertySetterCallback setter
Definition: v8-template.h:909
IndexedPropertyIndexOfCallback index_of
Definition: v8-template.h:915
IndexedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:912
IndexedPropertyQueryCallback query
Definition: v8-template.h:910
IndexedPropertyDeleterCallback deleter
Definition: v8-template.h:911
IndexedPropertyIterableToListCallback iterable_to_list
Definition: v8-template.h:916
Definition: v8-template.h:712
NamedPropertyDescriptorCallback descriptor
Definition: v8-template.h:786
NamedPropertyDeleterCallback deleter
Definition: v8-template.h:783
NamedPropertyDefinerCallbackV2 definer
Definition: v8-template.h:785
NamedPropertySetterCallbackV2 setter
Definition: v8-template.h:781
NamedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:784
NamedPropertyQueryCallback query
Definition: v8-template.h:782
NamedPropertyGetterCallback getter
Definition: v8-template.h:780
Local< Data > data
Definition: v8-template.h:787
PropertyHandlerFlags flags
Definition: v8-template.h:788
#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