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
704 V8_INLINE static FunctionTemplate* Cast(Data* data);
705
706 private:
707 FunctionTemplate();
708
709 static void CheckCast(Data* that);
710 friend class Context;
711 friend class ObjectTemplate;
712};
713
718enum class PropertyHandlerFlags {
722 kNone = 0,
723
729 kNonMasking = 1,
730
735 kOnlyInterceptStrings = 1 << 1,
736
740 kHasNoSideEffect = 1 << 2,
741
750};
753 private:
754 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
756 return static_cast<PropertyHandlerFlags>(
757 static_cast<int>(flags) |
758 static_cast<int>(
760 }
761
762 public:
773 : getter(getter),
774 setter(setter),
775 query(query),
780 data(data),
781 flags(flags) {}
791 : getter(getter),
792 setter(setter),
793 query(query),
796 definer(nullptr),
797 descriptor(nullptr),
798 data(data),
799 flags(flags) {}
810 : getter(getter),
811 setter(setter),
812 query(nullptr),
817 data(data),
818 flags(flags) {}
829};
832 private:
833 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
835 return static_cast<PropertyHandlerFlags>(
836 static_cast<int>(flags) |
837 static_cast<int>(
839 }
840
841 public:
852 : getter(getter),
853 setter(setter),
854 query(query),
859 data(data),
860 flags(flags) {}
870 : getter(getter),
871 setter(setter),
872 query(query),
875 definer(nullptr),
876 descriptor(nullptr),
877 data(data),
878 flags(flags) {}
889 : getter(getter),
890 setter(setter),
891 query(nullptr),
896 data(data),
897 flags(flags) {}
908};
909
916class V8_EXPORT ObjectTemplate : public Template {
917 public:
919 static Local<ObjectTemplate> New(
920 Isolate* isolate,
922
929
941 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
942
953 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
954
961 void SetCallAsFunctionHandler(FunctionCallback callback,
962 Local<Value> data = Local<Value>());
963
972 void MarkAsUndetectable();
973
982 void SetAccessCheckCallback(AccessCheckCallback callback,
983 Local<Value> data = Local<Value>());
984
991 void SetAccessCheckCallbackAndHandler(
992 AccessCheckCallback callback,
993 const NamedPropertyHandlerConfiguration& named_handler,
994 const IndexedPropertyHandlerConfiguration& indexed_handler,
995 Local<Value> data = Local<Value>());
996
1001 int InternalFieldCount() const;
1002
1007 void SetInternalFieldCount(int value);
1008
1012 bool IsImmutableProto() const;
1013
1018 void SetImmutableProto();
1019
1029 void SetCodeLike();
1030 bool IsCodeLike() const;
1031
1032 V8_INLINE static ObjectTemplate* Cast(Data* data);
1033
1034 private:
1036
1037 static void CheckCast(Data* that);
1038 friend class FunctionTemplate;
1039};
1040
1044class V8_EXPORT DictionaryTemplate final {
1045 public:
1053 static Local<DictionaryTemplate> New(
1055
1066 Local<Context> context, MemorySpan<MaybeLocal<Value>> property_values);
1067
1068 V8_INLINE static DictionaryTemplate* Cast(Data* data);
1069
1070 private:
1071 static void CheckCast(Data* that);
1072
1074};
1075
1084class V8_EXPORT Signature : public Data {
1085 public:
1086 static Local<Signature> New(
1087 Isolate* isolate,
1089
1090 V8_INLINE static Signature* Cast(Data* data);
1091
1092 private:
1093 Signature();
1094
1095 static void CheckCast(Data* that);
1096};
1097
1098// --- Implementation ---
1100void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1101 PropertyAttribute attributes) {
1103 .ToLocalChecked(),
1104 value, attributes);
1105}
1108#ifdef V8_ENABLE_CHECKS
1109 CheckCast(data);
1110#endif
1111 return reinterpret_cast<FunctionTemplate*>(data);
1112}
1115#ifdef V8_ENABLE_CHECKS
1116 CheckCast(data);
1117#endif
1118 return reinterpret_cast<ObjectTemplate*>(data);
1119}
1122#ifdef V8_ENABLE_CHECKS
1123 CheckCast(data);
1124#endif
1125 return reinterpret_cast<DictionaryTemplate*>(data);
1126}
1129#ifdef V8_ENABLE_CHECKS
1130 CheckCast(data);
1131#endif
1132 return reinterpret_cast<Signature*>(data);
1133}
1134
1135} // namespace v8
1136
1137#endif // INCLUDE_V8_TEMPLATE_H_
Definition: v8-context.h:48
Definition: v8-data.h:18
Definition: v8-template.h:1043
static DictionaryTemplate * Cast(Data *data)
Definition: v8-template.h:1120
Definition: v8-template.h:569
static FunctionTemplate * Cast(Data *data)
Definition: v8-template.h:1106
Definition: v8-isolate.h:212
Definition: v8-local-handle.h:266
Definition: v8-local-handle.h:632
Definition: v8-memory-span.h:63
Definition: v8-template.h:915
static ObjectTemplate * Cast(Data *data)
Definition: v8-template.h:1113
Definition: v8-function-callback.h:206
Definition: v8-object.h:91
Definition: v8-container.h:148
Definition: v8-template.h:1083
static Signature * Cast(Data *data)
Definition: v8-template.h:1127
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:333
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:717
Intercepted
Definition: v8-template.h:138
Definition: v8-template.h:830
PropertyHandlerFlags flags
Definition: v8-template.h:906
IndexedPropertyQueryCallbackV2 query
Definition: v8-template.h:900
IndexedPropertyDeleterCallbackV2 deleter
Definition: v8-template.h:901
IndexedPropertyGetterCallbackV2 getter
Definition: v8-template.h:898
IndexedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:902
Local< Value > data
Definition: v8-template.h:905
IndexedPropertyDescriptorCallbackV2 descriptor
Definition: v8-template.h:904
IndexedPropertySetterCallbackV2 setter
Definition: v8-template.h:899
IndexedPropertyDefinerCallbackV2 definer
Definition: v8-template.h:903
Definition: v8-template.h:751
NamedPropertyDescriptorCallback descriptor
Definition: v8-template.h:825
Local< Value > data
Definition: v8-template.h:826
NamedPropertySetterCallback setter
Definition: v8-template.h:820
NamedPropertyDeleterCallback deleter
Definition: v8-template.h:822
NamedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:823
NamedPropertyQueryCallback query
Definition: v8-template.h:821
NamedPropertyGetterCallback getter
Definition: v8-template.h:819
NamedPropertyDefinerCallback definer
Definition: v8-template.h:824
PropertyHandlerFlags flags
Definition: v8-template.h:827
#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:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:660