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
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<void>& 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<void>& info);
314
337 Local<Name> property, const PropertyCallbackInfo<Value>& info);
338
339// TODO(ishell): Rename IndexedPropertyXxxCallbackV2 back to
340// IndexedPropertyXxxCallback once the old IndexedPropertyXxxCallback is
341// removed.
342
347 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
348
353 uint32_t index, Local<Value> value, const PropertyCallbackInfo<void>& info);
354
359 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
360
365 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
366
374 void (*)(const PropertyCallbackInfo<Array>& info);
375
380 Intercepted (*)(uint32_t index, const PropertyDescriptor& desc,
381 const PropertyCallbackInfo<void>& info);
382
387 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
388
393 uint32_t (*)(Local<Value> value, uint32_t start_index, uint32_t end_index,
394 uint32_t* out_length, const PropertyCallbackInfo<void>& info);
395
400using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
401 Local<Object> accessed_object,
402 Local<Value> data);
404enum class ConstructorBehavior { kThrow, kAllow };
405
513class V8_EXPORT FunctionTemplate : public Template {
514 public:
516 static Local<FunctionTemplate> New(
517 Isolate* isolate, FunctionCallback callback = nullptr,
518 Local<Value> data = Local<Value>(),
519 Local<Signature> signature = Local<Signature>(), int length = 0,
520 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
521 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
522 const CFunction* c_function = nullptr, uint16_t instance_type = 0,
523 uint16_t allowed_receiver_instance_type_range_start = 0,
524 uint16_t allowed_receiver_instance_type_range_end = 0);
525
527 static Local<FunctionTemplate> NewWithCFunctionOverloads(
528 Isolate* isolate, FunctionCallback callback = nullptr,
529 Local<Value> data = Local<Value>(),
530 Local<Signature> signature = Local<Signature>(), int length = 0,
531 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
532 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
533 const MemorySpan<const CFunction>& c_function_overloads = {});
534
538 static Local<FunctionTemplate> NewWithCache(
539 Isolate* isolate, FunctionCallback callback,
540 Local<Private> cache_property, Local<Value> data = Local<Value>(),
541 Local<Signature> signature = Local<Signature>(), int length = 0,
542 SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
543
545 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
546 Local<Context> context);
547
555 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
556
563 void SetCallHandler(
564 FunctionCallback callback, Local<Data> data = {},
565 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
566 const MemorySpan<const CFunction>& c_function_overloads = {});
567
569 void SetLength(int length);
570
572 Local<ObjectTemplate> InstanceTemplate();
573
579 void Inherit(Local<FunctionTemplate> parent);
580
585 Local<ObjectTemplate> PrototypeTemplate();
586
593 void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
594
600 void SetClassName(Local<String> name);
601
606 void SetInterfaceName(Local<String> name);
607
612 void SetExceptionContext(ExceptionContext context);
613
618 void SetAcceptAnyReceiver(bool value);
619
624 void ReadOnlyPrototype();
625
630 void RemovePrototype();
631
636 bool HasInstance(Local<Value> object);
637
645 bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
646
653 void SealAndPrepareForPromotionToReadOnly();
654
655 V8_INLINE static FunctionTemplate* Cast(Data* data);
656
657 private:
658 FunctionTemplate();
659
660 static void CheckCast(Data* that);
661 friend class Context;
662 friend class ObjectTemplate;
663};
664
669enum class PropertyHandlerFlags {
673 kNone = 0,
674
680 kNonMasking = 1,
681
686 kOnlyInterceptStrings = 1 << 1,
687
691 kHasNoSideEffect = 1 << 2,
692
701};
704 private:
705 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
707 return static_cast<PropertyHandlerFlags>(
708 static_cast<int>(flags) |
709 static_cast<int>(
711 }
712
713 public:
724 : getter(getter),
725 setter(setter),
726 query(query),
731 data(data),
732 flags(flags) {}
742 : getter(getter),
743 setter(setter),
744 query(query),
747 definer(nullptr),
748 descriptor(nullptr),
749 data(data),
750 flags(flags) {}
761 : getter(getter),
762 setter(setter),
763 query(nullptr),
768 data(data),
769 flags(flags) {}
780};
783 private:
784 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
786 return static_cast<PropertyHandlerFlags>(
787 static_cast<int>(flags) |
788 static_cast<int>(
790 }
791
792 public:
803 : getter(getter),
804 setter(setter),
805 query(query),
810 data(data),
811 flags(flags) {}
821 : getter(getter),
822 setter(setter),
823 query(query),
826 definer(nullptr),
827 descriptor(nullptr),
828 data(data),
829 flags(flags) {}
840 : getter(getter),
841 setter(setter),
842 query(nullptr),
847 data(data),
848 flags(flags) {}
861 : getter(getter),
862 setter(setter),
863 query(query),
869 data(data),
870 flags(flags) {}
882};
883
890class V8_EXPORT ObjectTemplate : public Template {
891 public:
893 static Local<ObjectTemplate> New(
894 Isolate* isolate,
896
903
915 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
916
927 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
928
935 void SetCallAsFunctionHandler(FunctionCallback callback,
936 Local<Value> data = Local<Value>());
937
946 void MarkAsUndetectable();
947
956 void SetAccessCheckCallback(AccessCheckCallback callback,
957 Local<Value> data = Local<Value>());
958
965 void SetAccessCheckCallbackAndHandler(
966 AccessCheckCallback callback,
967 const NamedPropertyHandlerConfiguration& named_handler,
968 const IndexedPropertyHandlerConfiguration& indexed_handler,
969 Local<Value> data = Local<Value>());
970
975 int InternalFieldCount() const;
976
981 void SetInternalFieldCount(int value);
982
986 bool IsImmutableProto() const;
987
992 void SetImmutableProto();
993
1003 void SetCodeLike();
1004 bool IsCodeLike() const;
1005
1012 void SealAndPrepareForPromotionToReadOnly();
1013
1014 V8_INLINE static ObjectTemplate* Cast(Data* data);
1015
1016 private:
1018
1019 static void CheckCast(Data* that);
1020 friend class FunctionTemplate;
1021};
1022
1026class V8_EXPORT DictionaryTemplate final : public Data {
1027 public:
1035 static Local<DictionaryTemplate> New(
1037
1048 Local<Context> context, MemorySpan<MaybeLocal<Value>> property_values);
1049
1050 V8_INLINE static DictionaryTemplate* Cast(Data* data);
1051
1052 private:
1053 static void CheckCast(Data* that);
1054
1056};
1057
1066class V8_EXPORT Signature : public Data {
1067 public:
1068 static Local<Signature> New(
1069 Isolate* isolate,
1071
1072 V8_INLINE static Signature* Cast(Data* data);
1073
1074 private:
1075 Signature();
1076
1077 static void CheckCast(Data* that);
1078};
1079
1080// --- Implementation ---
1082void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1083 PropertyAttribute attributes) {
1085 .ToLocalChecked(),
1086 value, attributes);
1087}
1090#ifdef V8_ENABLE_CHECKS
1091 CheckCast(data);
1092#endif
1093 return reinterpret_cast<FunctionTemplate*>(data);
1094}
1097#ifdef V8_ENABLE_CHECKS
1098 CheckCast(data);
1099#endif
1100 return reinterpret_cast<ObjectTemplate*>(data);
1101}
1104#ifdef V8_ENABLE_CHECKS
1105 CheckCast(data);
1106#endif
1107 return reinterpret_cast<DictionaryTemplate*>(data);
1108}
1111#ifdef V8_ENABLE_CHECKS
1112 CheckCast(data);
1113#endif
1114 return reinterpret_cast<Signature*>(data);
1115}
1116
1117} // namespace v8
1118
1119#endif // INCLUDE_V8_TEMPLATE_H_
Definition: v8-context.h:48
Definition: v8-data.h:18
Definition: v8-template.h:1025
static DictionaryTemplate * Cast(Data *data)
Definition: v8-template.h:1102
Definition: v8-template.h:512
static FunctionTemplate * Cast(Data *data)
Definition: v8-template.h:1088
Definition: v8-isolate.h:291
Definition: v8-local-handle.h:366
Definition: v8-local-handle.h:734
Definition: v8-memory-span.h:48
Definition: v8-template.h:889
static ObjectTemplate * Cast(Data *data)
Definition: v8-template.h:1095
Definition: v8-container.h:148
Definition: v8-template.h:1065
static Signature * Cast(Data *data)
Definition: v8-template.h:1109
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(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) NamedPropertySetterCallback
Definition: v8-template.h:217
SideEffectType
Definition: v8-object.h:227
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyDescriptorCallback
Definition: v8-template.h:336
Intercepted(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< void > &info) IndexedPropertySetterCallbackV2
Definition: v8-template.h:352
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:312
Intrinsic
Definition: v8-template.h:41
void(*)(const PropertyCallbackInfo< Array > &info) NamedPropertyEnumeratorCallback
Definition: v8-template.h:282
ConstructorBehavior
Definition: v8-template.h:403
void(*)(const FunctionCallbackInfo< Value > &info) FunctionCallback
Definition: v8-function-callback.h:354
Intercepted(*)(uint32_t index, const PropertyDescriptor &desc, const PropertyCallbackInfo< void > &info) IndexedPropertyDefinerCallbackV2
Definition: v8-template.h:380
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDeleterCallbackV2
Definition: v8-template.h:364
void(*)(const PropertyCallbackInfo< Array > &info) IndexedPropertyEnumeratorCallback
Definition: v8-template.h:373
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyDescriptorCallbackV2
Definition: v8-template.h:386
Intercepted
Definition: v8-template.h:141
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Integer > &info) NamedPropertyQueryCallback
Definition: v8-template.h:246
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) AccessorNameSetterCallback
Definition: v8-object.h:203
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:393
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyGetterCallbackV2
Definition: v8-template.h:346
bool(*)(Local< Context > accessing_context, Local< Object > accessed_object, Local< Value > data) AccessCheckCallback
Definition: v8-template.h:401
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDeleterCallback
Definition: v8-template.h:273
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Integer > &info) IndexedPropertyQueryCallbackV2
Definition: v8-template.h:358
PropertyHandlerFlags
Definition: v8-template.h:668
Definition: v8-template.h:781
PropertyHandlerFlags flags
Definition: v8-template.h:880
IndexedPropertyQueryCallbackV2 query
Definition: v8-template.h:873
IndexedPropertyIndexOfCallback index_of
Definition: v8-template.h:878
IndexedPropertyDeleterCallbackV2 deleter
Definition: v8-template.h:874
IndexedPropertyGetterCallbackV2 getter
Definition: v8-template.h:871
IndexedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:875
Local< Value > data
Definition: v8-template.h:879
IndexedPropertyDescriptorCallbackV2 descriptor
Definition: v8-template.h:877
IndexedPropertySetterCallbackV2 setter
Definition: v8-template.h:872
IndexedPropertyDefinerCallbackV2 definer
Definition: v8-template.h:876
Definition: v8-template.h:702
NamedPropertyDescriptorCallback descriptor
Definition: v8-template.h:776
Local< Value > data
Definition: v8-template.h:777
NamedPropertySetterCallback setter
Definition: v8-template.h:771
NamedPropertyDeleterCallback deleter
Definition: v8-template.h:773
NamedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:774
NamedPropertyQueryCallback query
Definition: v8-template.h:772
NamedPropertyGetterCallback getter
Definition: v8-template.h:770
NamedPropertyDefinerCallback definer
Definition: v8-template.h:775
PropertyHandlerFlags flags
Definition: v8-template.h:778
#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:854
#define V8_INLINE
Definition: v8config.h:508
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:678