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 deprecated soon.
187//
188// Use `info.GetReturnValue().Set()` to set the return value of the
189// intercepted get request. If the property does not exist the callback should
190// not set the result and must not produce side effects.
192 void (*)(Local<Name> property, const PropertyCallbackInfo<Value>& info);
193
214 Intercepted (*)(Local<Name> property, Local<Value> value,
215 const PropertyCallbackInfo<void>& info);
216// This variant will be deprecated soon.
217//
218// Use `info.GetReturnValue()` to indicate whether the request was intercepted
219// or not. If the setter successfully intercepts the request, i.e., if the
220// request should not be further executed, call
221// `info.GetReturnValue().Set(value)`. If the setter did not intercept the
222// request, i.e., if the request should be handled as if no interceptor is
223// present, do not not call `Set()` and do not produce side effects.
225 void (*)(Local<Name> property, Local<Value> value,
226 const PropertyCallbackInfo<Value>& info);
227
255 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
256// This variant will be deprecated soon.
257//
258// Use `info.GetReturnValue().Set(value)` to set the property attributes. The
259// value is an integer encoding a `v8::PropertyAttribute`. If the property does
260// not exist the callback should not set the result and must not produce side
261// effects.
263 void (*)(Local<Name> property, const PropertyCallbackInfo<Integer>& info);
264
290 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
291// This variant will be deprecated soon.
292//
293// Use `info.GetReturnValue()` to indicate whether the request was intercepted
294// or not. If the deleter successfully intercepts the request, i.e., if the
295// request should not be further executed, call
296// `info.GetReturnValue().Set(value)` with a boolean `value`. The `value` is
297// used as the return value of `delete`. If the deleter does not intercept the
298// request then it should not set the result and must not produce side effects.
300 void (*)(Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
301
309 void (*)(const PropertyCallbackInfo<Array>& info);
310// This variant will be deprecated soon.
311// This is just a renaming of the typedef.
313
334 Intercepted (*)(Local<Name> property, const PropertyDescriptor& desc,
335 const PropertyCallbackInfo<void>& info);
336// This variant will be deprecated soon.
337//
338// Use `info.GetReturnValue()` to indicate whether the request was intercepted
339// or not. If the definer successfully intercepts the request, i.e., if the
340// request should not be further executed, call
341// `info.GetReturnValue().Set(value)`. If the definer did not intercept the
342// request, i.e., if the request should be handled as if no interceptor is
343// present, do not not call `Set()` and do not produce side effects.
345 void (*)(Local<Name> property, const PropertyDescriptor& desc,
346 const PropertyCallbackInfo<Value>& info);
347
373 Local<Name> property, const PropertyCallbackInfo<Value>& info);
374// This variant will be deprecated soon.
375//
376// Use `info.GetReturnValue().Set()` to set the return value of the
377// intercepted request. The return value must be an object that
378// can be converted to a PropertyDescriptor, e.g., a `v8::Value` returned from
379// `v8::Object::getOwnPropertyDescriptor`.
381 void (*)(Local<Name> property, const PropertyCallbackInfo<Value>& info);
382
383// TODO(ishell): Rename IndexedPropertyXxxCallbackV2 back to
384// IndexedPropertyXxxCallback once the old IndexedPropertyXxxCallback is
385// removed.
386
391 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
392// This variant will be deprecated soon.
394 void (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
395
400 uint32_t index, Local<Value> value, const PropertyCallbackInfo<void>& info);
401// This variant will be deprecated soon.
403 void (*)(uint32_t index, Local<Value> value,
404 const PropertyCallbackInfo<Value>& info);
405
410 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
411// This variant will be deprecated soon.
413 void (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
414
419 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
420// This variant will be deprecated soon.
422 void (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
423
431 void (*)(const PropertyCallbackInfo<Array>& info);
432
437 Intercepted (*)(uint32_t index, const PropertyDescriptor& desc,
438 const PropertyCallbackInfo<void>& info);
439// This variant will be deprecated soon.
441 void (*)(uint32_t index, const PropertyDescriptor& desc,
442 const PropertyCallbackInfo<Value>& info);
443
448 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
449// This variant will be deprecated soon.
451 void (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
452
457using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
458 Local<Object> accessed_object,
459 Local<Value> data);
461enum class ConstructorBehavior { kThrow, kAllow };
462
570class V8_EXPORT FunctionTemplate : public Template {
571 public:
573 static Local<FunctionTemplate> New(
574 Isolate* isolate, FunctionCallback callback = nullptr,
575 Local<Value> data = Local<Value>(),
576 Local<Signature> signature = Local<Signature>(), int length = 0,
577 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
578 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
579 const CFunction* c_function = nullptr, uint16_t instance_type = 0,
580 uint16_t allowed_receiver_instance_type_range_start = 0,
581 uint16_t allowed_receiver_instance_type_range_end = 0);
582
584 static Local<FunctionTemplate> NewWithCFunctionOverloads(
585 Isolate* isolate, FunctionCallback callback = nullptr,
586 Local<Value> data = Local<Value>(),
587 Local<Signature> signature = Local<Signature>(), int length = 0,
588 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
589 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
590 const MemorySpan<const CFunction>& c_function_overloads = {});
591
595 static Local<FunctionTemplate> NewWithCache(
596 Isolate* isolate, FunctionCallback callback,
597 Local<Private> cache_property, Local<Value> data = Local<Value>(),
598 Local<Signature> signature = Local<Signature>(), int length = 0,
599 SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
600
602 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
603 Local<Context> context);
604
612 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
613
620 void SetCallHandler(
621 FunctionCallback callback, Local<Value> data = Local<Value>(),
622 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
623 const MemorySpan<const CFunction>& c_function_overloads = {});
624
626 void SetLength(int length);
627
629 Local<ObjectTemplate> InstanceTemplate();
630
636 void Inherit(Local<FunctionTemplate> parent);
637
642 Local<ObjectTemplate> PrototypeTemplate();
643
650 void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
651
657 void SetClassName(Local<String> name);
658
663 void SetInterfaceName(Local<String> name);
664
669 void SetExceptionContext(ExceptionContext context);
670
675 void SetAcceptAnyReceiver(bool value);
676
681 void ReadOnlyPrototype();
682
687 void RemovePrototype();
688
693 bool HasInstance(Local<Value> object);
694
702 bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
703
710 void SealAndPrepareForPromotionToReadOnly();
711
712 V8_INLINE static FunctionTemplate* Cast(Data* data);
713
714 private:
715 FunctionTemplate();
716
717 static void CheckCast(Data* that);
718 friend class Context;
719 friend class ObjectTemplate;
720};
721
726enum class PropertyHandlerFlags {
730 kNone = 0,
731
737 kNonMasking = 1,
738
743 kOnlyInterceptStrings = 1 << 1,
744
748 kHasNoSideEffect = 1 << 2,
749
758};
761 private:
762 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
764 return static_cast<PropertyHandlerFlags>(
765 static_cast<int>(flags) |
766 static_cast<int>(
768 }
769
770 public:
781 : getter(getter),
782 setter(setter),
783 query(query),
788 data(data),
789 flags(flags) {}
799 : getter(getter),
800 setter(setter),
801 query(query),
804 definer(nullptr),
805 descriptor(nullptr),
806 data(data),
807 flags(flags) {}
818 : getter(getter),
819 setter(setter),
820 query(nullptr),
825 data(data),
826 flags(flags) {}
837};
840 private:
841 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
843 return static_cast<PropertyHandlerFlags>(
844 static_cast<int>(flags) |
845 static_cast<int>(
847 }
848
849 public:
860 : getter(getter),
861 setter(setter),
862 query(query),
867 data(data),
868 flags(flags) {}
878 : getter(getter),
879 setter(setter),
880 query(query),
883 definer(nullptr),
884 descriptor(nullptr),
885 data(data),
886 flags(flags) {}
897 : getter(getter),
898 setter(setter),
899 query(nullptr),
904 data(data),
905 flags(flags) {}
916};
917
924class V8_EXPORT ObjectTemplate : public Template {
925 public:
927 static Local<ObjectTemplate> New(
928 Isolate* isolate,
930
937
949 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
950
961 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
962
969 void SetCallAsFunctionHandler(FunctionCallback callback,
970 Local<Value> data = Local<Value>());
971
980 void MarkAsUndetectable();
981
990 void SetAccessCheckCallback(AccessCheckCallback callback,
991 Local<Value> data = Local<Value>());
992
999 void SetAccessCheckCallbackAndHandler(
1000 AccessCheckCallback callback,
1001 const NamedPropertyHandlerConfiguration& named_handler,
1002 const IndexedPropertyHandlerConfiguration& indexed_handler,
1003 Local<Value> data = Local<Value>());
1004
1009 int InternalFieldCount() const;
1010
1015 void SetInternalFieldCount(int value);
1016
1020 bool IsImmutableProto() const;
1021
1026 void SetImmutableProto();
1027
1037 void SetCodeLike();
1038 bool IsCodeLike() const;
1039
1040 V8_INLINE static ObjectTemplate* Cast(Data* data);
1041
1042 private:
1044
1045 static void CheckCast(Data* that);
1046 friend class FunctionTemplate;
1047};
1048
1052class V8_EXPORT DictionaryTemplate final {
1053 public:
1061 static Local<DictionaryTemplate> New(
1063
1074 Local<Context> context, MemorySpan<MaybeLocal<Value>> property_values);
1075
1076 V8_INLINE static DictionaryTemplate* Cast(Data* data);
1077
1078 private:
1079 static void CheckCast(Data* that);
1080
1082};
1083
1092class V8_EXPORT Signature : public Data {
1093 public:
1094 static Local<Signature> New(
1095 Isolate* isolate,
1097
1098 V8_INLINE static Signature* Cast(Data* data);
1099
1100 private:
1101 Signature();
1102
1103 static void CheckCast(Data* that);
1104};
1105
1106// --- Implementation ---
1108void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1109 PropertyAttribute attributes) {
1111 .ToLocalChecked(),
1112 value, attributes);
1113}
1116#ifdef V8_ENABLE_CHECKS
1117 CheckCast(data);
1118#endif
1119 return reinterpret_cast<FunctionTemplate*>(data);
1120}
1123#ifdef V8_ENABLE_CHECKS
1124 CheckCast(data);
1125#endif
1126 return reinterpret_cast<ObjectTemplate*>(data);
1127}
1130#ifdef V8_ENABLE_CHECKS
1131 CheckCast(data);
1132#endif
1133 return reinterpret_cast<DictionaryTemplate*>(data);
1134}
1137#ifdef V8_ENABLE_CHECKS
1138 CheckCast(data);
1139#endif
1140 return reinterpret_cast<Signature*>(data);
1141}
1142
1143} // namespace v8
1144
1145#endif // INCLUDE_V8_TEMPLATE_H_
Definition: v8-context.h:48
Definition: v8-data.h:18
Definition: v8-template.h:1051
static DictionaryTemplate * Cast(Data *data)
Definition: v8-template.h:1128
Definition: v8-template.h:569
static FunctionTemplate * Cast(Data *data)
Definition: v8-template.h:1114
Definition: v8-isolate.h:274
Definition: v8-local-handle.h:267
Definition: v8-local-handle.h:635
Definition: v8-memory-span.h:64
Definition: v8-template.h:923
static ObjectTemplate * Cast(Data *data)
Definition: v8-template.h:1121
Definition: v8-function-callback.h:187
Definition: v8-object.h:91
Definition: v8-container.h:148
Definition: v8-template.h:1091
static Signature * Cast(Data *data)
Definition: v8-template.h:1135
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:412
PropertyAttribute
Definition: v8-object.h:139
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) AccessorNameGetterCallback
Definition: v8-object.h:156
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) GenericNamedPropertyGetterCallback
Definition: v8-template.h:191
Intercepted(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) NamedPropertySetterCallback
Definition: v8-template.h:214
void(*)(Local< Name > property, const PropertyDescriptor &desc, const PropertyCallbackInfo< Value > &info) GenericNamedPropertyDefinerCallback
Definition: v8-template.h:345
void(*)(uint32_t index, const PropertyDescriptor &desc, const PropertyCallbackInfo< Value > &info) IndexedPropertyDefinerCallback
Definition: v8-template.h:441
void(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< Value > &info) IndexedPropertySetterCallback
Definition: v8-template.h:403
SideEffectType
Definition: v8-object.h:198
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyDescriptorCallback
Definition: v8-template.h:372
void(*)(uint32_t index, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDeleterCallback
Definition: v8-template.h:421
Intercepted(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< void > &info) IndexedPropertySetterCallbackV2
Definition: v8-template.h:399
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyGetterCallback
Definition: v8-template.h:184
NamedPropertyEnumeratorCallback GenericNamedPropertyEnumeratorCallback
Definition: v8-template.h:311
void(*)(Local< Name > property, const PropertyCallbackInfo< Integer > &info) GenericNamedPropertyQueryCallback
Definition: v8-template.h:262
Intercepted(*)(Local< Name > property, const PropertyDescriptor &desc, const PropertyCallbackInfo< void > &info) NamedPropertyDefinerCallback
Definition: v8-template.h:334
void(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyDescriptorCallback
Definition: v8-template.h:450
Intrinsic
Definition: v8-template.h:41
void(*)(const PropertyCallbackInfo< Array > &info) NamedPropertyEnumeratorCallback
Definition: v8-template.h:308
ConstructorBehavior
Definition: v8-template.h:460
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:437
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDeleterCallbackV2
Definition: v8-template.h:418
void(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyGetterCallback
Definition: v8-template.h:393
void(*)(const PropertyCallbackInfo< Array > &info) IndexedPropertyEnumeratorCallback
Definition: v8-template.h:430
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyDescriptorCallbackV2
Definition: v8-template.h:447
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Integer > &info) NamedPropertyQueryCallback
Definition: v8-template.h:254
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) AccessorNameSetterCallback
Definition: v8-object.h:160
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< Value > &info) GenericNamedPropertySetterCallback
Definition: v8-template.h:225
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyGetterCallbackV2
Definition: v8-template.h:390
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) GenericNamedPropertyDescriptorCallback
Definition: v8-template.h:380
bool(*)(Local< Context > accessing_context, Local< Object > accessed_object, Local< Value > data) AccessCheckCallback
Definition: v8-template.h:458
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDeleterCallback
Definition: v8-template.h:289
void(*)(Local< Name > property, const PropertyCallbackInfo< Boolean > &info) GenericNamedPropertyDeleterCallback
Definition: v8-template.h:299
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Integer > &info) IndexedPropertyQueryCallbackV2
Definition: v8-template.h:409
PropertyHandlerFlags
Definition: v8-template.h:725
Intercepted
Definition: v8-template.h:138
Definition: v8-template.h:838
PropertyHandlerFlags flags
Definition: v8-template.h:914
IndexedPropertyQueryCallbackV2 query
Definition: v8-template.h:908
IndexedPropertyDeleterCallbackV2 deleter
Definition: v8-template.h:909
IndexedPropertyGetterCallbackV2 getter
Definition: v8-template.h:906
IndexedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:910
Local< Value > data
Definition: v8-template.h:913
IndexedPropertyDescriptorCallbackV2 descriptor
Definition: v8-template.h:912
IndexedPropertySetterCallbackV2 setter
Definition: v8-template.h:907
IndexedPropertyDefinerCallbackV2 definer
Definition: v8-template.h:911
Definition: v8-template.h:759
NamedPropertyDescriptorCallback descriptor
Definition: v8-template.h:833
Local< Value > data
Definition: v8-template.h:834
NamedPropertySetterCallback setter
Definition: v8-template.h:828
NamedPropertyDeleterCallback deleter
Definition: v8-template.h:830
NamedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:831
NamedPropertyQueryCallback query
Definition: v8-template.h:829
NamedPropertyGetterCallback getter
Definition: v8-template.h:827
NamedPropertyDefinerCallback definer
Definition: v8-template.h:832
PropertyHandlerFlags flags
Definition: v8-template.h:835
#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:800
#define V8_INLINE
Definition: v8config.h:500
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:671