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
214 Intercepted (*)(Local<Name> property, Local<Value> value,
215 const PropertyCallbackInfo<void>& info);
216
244 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
245
271 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
272
280 void (*)(const PropertyCallbackInfo<Array>& info);
281
309 Intercepted (*)(Local<Name> property, const PropertyDescriptor& desc,
310 const PropertyCallbackInfo<void>& info);
311
334 Local<Name> property, const PropertyCallbackInfo<Value>& info);
335
336// TODO(ishell): Rename IndexedPropertyXxxCallbackV2 back to
337// IndexedPropertyXxxCallback once the old IndexedPropertyXxxCallback is
338// removed.
339
344 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
345
350 uint32_t index, Local<Value> value, const PropertyCallbackInfo<void>& 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<void>& info);
379
384 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
385
390using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
391 Local<Object> accessed_object,
392 Local<Value> data);
394enum class ConstructorBehavior { kThrow, kAllow };
395
503class V8_EXPORT FunctionTemplate : public Template {
504 public:
506 static Local<FunctionTemplate> New(
507 Isolate* isolate, FunctionCallback callback = nullptr,
508 Local<Value> data = Local<Value>(),
509 Local<Signature> signature = Local<Signature>(), int length = 0,
510 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
511 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
512 const CFunction* c_function = nullptr, uint16_t instance_type = 0,
513 uint16_t allowed_receiver_instance_type_range_start = 0,
514 uint16_t allowed_receiver_instance_type_range_end = 0);
515
517 static Local<FunctionTemplate> NewWithCFunctionOverloads(
518 Isolate* isolate, FunctionCallback callback = nullptr,
519 Local<Value> data = Local<Value>(),
520 Local<Signature> signature = Local<Signature>(), int length = 0,
521 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
522 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
523 const MemorySpan<const CFunction>& c_function_overloads = {});
524
528 static Local<FunctionTemplate> NewWithCache(
529 Isolate* isolate, FunctionCallback callback,
530 Local<Private> cache_property, Local<Value> data = Local<Value>(),
531 Local<Signature> signature = Local<Signature>(), int length = 0,
532 SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
533
535 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
536 Local<Context> context);
537
545 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
546
553 void SetCallHandler(
554 FunctionCallback callback, Local<Data> data = {},
555 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
556 const MemorySpan<const CFunction>& c_function_overloads = {});
557
559 void SetLength(int length);
560
562 Local<ObjectTemplate> InstanceTemplate();
563
569 void Inherit(Local<FunctionTemplate> parent);
570
575 Local<ObjectTemplate> PrototypeTemplate();
576
583 void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
584
590 void SetClassName(Local<String> name);
591
596 void SetInterfaceName(Local<String> name);
597
602 void SetExceptionContext(ExceptionContext context);
603
608 void SetAcceptAnyReceiver(bool value);
609
614 void ReadOnlyPrototype();
615
620 void RemovePrototype();
621
626 bool HasInstance(Local<Value> object);
627
635 bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
636
643 void SealAndPrepareForPromotionToReadOnly();
644
645 V8_INLINE static FunctionTemplate* Cast(Data* data);
646
647 private:
648 FunctionTemplate();
649
650 static void CheckCast(Data* that);
651 friend class Context;
652 friend class ObjectTemplate;
653};
654
659enum class PropertyHandlerFlags {
663 kNone = 0,
664
670 kNonMasking = 1,
671
676 kOnlyInterceptStrings = 1 << 1,
677
681 kHasNoSideEffect = 1 << 2,
682
691};
694 private:
695 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
697 return static_cast<PropertyHandlerFlags>(
698 static_cast<int>(flags) |
699 static_cast<int>(
701 }
702
703 public:
714 : getter(getter),
715 setter(setter),
716 query(query),
721 data(data),
722 flags(flags) {}
732 : getter(getter),
733 setter(setter),
734 query(query),
737 definer(nullptr),
738 descriptor(nullptr),
739 data(data),
740 flags(flags) {}
751 : getter(getter),
752 setter(setter),
753 query(nullptr),
758 data(data),
759 flags(flags) {}
770};
773 private:
774 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
776 return static_cast<PropertyHandlerFlags>(
777 static_cast<int>(flags) |
778 static_cast<int>(
780 }
781
782 public:
793 : getter(getter),
794 setter(setter),
795 query(query),
800 data(data),
801 flags(flags) {}
811 : getter(getter),
812 setter(setter),
813 query(query),
816 definer(nullptr),
817 descriptor(nullptr),
818 data(data),
819 flags(flags) {}
830 : getter(getter),
831 setter(setter),
832 query(nullptr),
837 data(data),
838 flags(flags) {}
849};
850
857class V8_EXPORT ObjectTemplate : public Template {
858 public:
860 static Local<ObjectTemplate> New(
861 Isolate* isolate,
863
870
882 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
883
894 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
895
902 void SetCallAsFunctionHandler(FunctionCallback callback,
903 Local<Value> data = Local<Value>());
904
913 void MarkAsUndetectable();
914
923 void SetAccessCheckCallback(AccessCheckCallback callback,
924 Local<Value> data = Local<Value>());
925
932 void SetAccessCheckCallbackAndHandler(
933 AccessCheckCallback callback,
934 const NamedPropertyHandlerConfiguration& named_handler,
935 const IndexedPropertyHandlerConfiguration& indexed_handler,
936 Local<Value> data = Local<Value>());
937
942 int InternalFieldCount() const;
943
948 void SetInternalFieldCount(int value);
949
953 bool IsImmutableProto() const;
954
959 void SetImmutableProto();
960
970 void SetCodeLike();
971 bool IsCodeLike() const;
972
979 void SealAndPrepareForPromotionToReadOnly();
980
981 V8_INLINE static ObjectTemplate* Cast(Data* data);
982
983 private:
985
986 static void CheckCast(Data* that);
987 friend class FunctionTemplate;
988};
989
993class V8_EXPORT DictionaryTemplate final : public Data {
994 public:
1002 static Local<DictionaryTemplate> New(
1004
1015 Local<Context> context, MemorySpan<MaybeLocal<Value>> property_values);
1016
1017 V8_INLINE static DictionaryTemplate* Cast(Data* data);
1018
1019 private:
1020 static void CheckCast(Data* that);
1021
1023};
1024
1033class V8_EXPORT Signature : public Data {
1034 public:
1035 static Local<Signature> New(
1036 Isolate* isolate,
1038
1039 V8_INLINE static Signature* Cast(Data* data);
1040
1041 private:
1042 Signature();
1043
1044 static void CheckCast(Data* that);
1045};
1046
1047// --- Implementation ---
1049void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1050 PropertyAttribute attributes) {
1052 .ToLocalChecked(),
1053 value, attributes);
1054}
1057#ifdef V8_ENABLE_CHECKS
1058 CheckCast(data);
1059#endif
1060 return reinterpret_cast<FunctionTemplate*>(data);
1061}
1064#ifdef V8_ENABLE_CHECKS
1065 CheckCast(data);
1066#endif
1067 return reinterpret_cast<ObjectTemplate*>(data);
1068}
1071#ifdef V8_ENABLE_CHECKS
1072 CheckCast(data);
1073#endif
1074 return reinterpret_cast<DictionaryTemplate*>(data);
1075}
1078#ifdef V8_ENABLE_CHECKS
1079 CheckCast(data);
1080#endif
1081 return reinterpret_cast<Signature*>(data);
1082}
1083
1084} // namespace v8
1085
1086#endif // INCLUDE_V8_TEMPLATE_H_
Definition: v8-context.h:48
Definition: v8-data.h:18
Definition: v8-template.h:992
static DictionaryTemplate * Cast(Data *data)
Definition: v8-template.h:1069
Definition: v8-template.h:502
static FunctionTemplate * Cast(Data *data)
Definition: v8-template.h:1055
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:856
static ObjectTemplate * Cast(Data *data)
Definition: v8-template.h:1062
Definition: v8-container.h:148
Definition: v8-template.h:1032
static Signature * Cast(Data *data)
Definition: v8-template.h:1076
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:214
SideEffectType
Definition: v8-object.h:227
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyDescriptorCallback
Definition: v8-template.h:333
Intercepted(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< void > &info) IndexedPropertySetterCallbackV2
Definition: v8-template.h:349
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyGetterCallback
Definition: v8-template.h:184
Intercepted(*)(Local< Name > property, const PropertyDescriptor &desc, const PropertyCallbackInfo< void > &info) NamedPropertyDefinerCallback
Definition: v8-template.h:309
Intrinsic
Definition: v8-template.h:41
void(*)(const PropertyCallbackInfo< Array > &info) NamedPropertyEnumeratorCallback
Definition: v8-template.h:279
ConstructorBehavior
Definition: v8-template.h:393
void(*)(const FunctionCallbackInfo< Value > &info) FunctionCallback
Definition: v8-function-callback.h:309
Intercepted(*)(uint32_t index, const PropertyDescriptor &desc, const PropertyCallbackInfo< void > &info) IndexedPropertyDefinerCallbackV2
Definition: v8-template.h:377
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDeleterCallbackV2
Definition: v8-template.h:361
void(*)(const PropertyCallbackInfo< Array > &info) IndexedPropertyEnumeratorCallback
Definition: v8-template.h:370
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyDescriptorCallbackV2
Definition: v8-template.h:383
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Integer > &info) NamedPropertyQueryCallback
Definition: v8-template.h:243
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) AccessorNameSetterCallback
Definition: v8-object.h:203
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyGetterCallbackV2
Definition: v8-template.h:343
bool(*)(Local< Context > accessing_context, Local< Object > accessed_object, Local< Value > data) AccessCheckCallback
Definition: v8-template.h:391
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDeleterCallback
Definition: v8-template.h:270
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Integer > &info) IndexedPropertyQueryCallbackV2
Definition: v8-template.h:355
PropertyHandlerFlags
Definition: v8-template.h:658
Intercepted
Definition: v8-template.h:138
Definition: v8-template.h:771
PropertyHandlerFlags flags
Definition: v8-template.h:847
IndexedPropertyQueryCallbackV2 query
Definition: v8-template.h:841
IndexedPropertyDeleterCallbackV2 deleter
Definition: v8-template.h:842
IndexedPropertyGetterCallbackV2 getter
Definition: v8-template.h:839
IndexedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:843
Local< Value > data
Definition: v8-template.h:846
IndexedPropertyDescriptorCallbackV2 descriptor
Definition: v8-template.h:845
IndexedPropertySetterCallbackV2 setter
Definition: v8-template.h:840
IndexedPropertyDefinerCallbackV2 definer
Definition: v8-template.h:844
Definition: v8-template.h:692
NamedPropertyDescriptorCallback descriptor
Definition: v8-template.h:766
Local< Value > data
Definition: v8-template.h:767
NamedPropertySetterCallback setter
Definition: v8-template.h:761
NamedPropertyDeleterCallback deleter
Definition: v8-template.h:763
NamedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:764
NamedPropertyQueryCallback query
Definition: v8-template.h:762
NamedPropertyGetterCallback getter
Definition: v8-template.h:760
NamedPropertyDefinerCallback definer
Definition: v8-template.h:765
PropertyHandlerFlags flags
Definition: v8-template.h:768
#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:855
#define V8_INLINE
Definition: v8config.h:508
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:679