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
207 Intercepted (*)(Local<Name> property, Local<Value> value,
208 const PropertyCallbackInfo<void>& info);
209
237 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
238
264 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
265
273 void (*)(const PropertyCallbackInfo<Array>& info);
274
295 Intercepted (*)(Local<Name> property, const PropertyDescriptor& desc,
296 const PropertyCallbackInfo<void>& info);
297
323 Local<Name> property, const PropertyCallbackInfo<Value>& info);
324
325// TODO(ishell): Rename IndexedPropertyXxxCallbackV2 back to
326// IndexedPropertyXxxCallback once the old IndexedPropertyXxxCallback is
327// removed.
328
333 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
334
339 uint32_t index, Local<Value> value, const PropertyCallbackInfo<void>& info);
340
345 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
346
351 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
352
360 void (*)(const PropertyCallbackInfo<Array>& info);
361
366 Intercepted (*)(uint32_t index, const PropertyDescriptor& desc,
367 const PropertyCallbackInfo<void>& info);
368
373 Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
374
379using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
380 Local<Object> accessed_object,
381 Local<Value> data);
383enum class ConstructorBehavior { kThrow, kAllow };
384
492class V8_EXPORT FunctionTemplate : public Template {
493 public:
495 static Local<FunctionTemplate> New(
496 Isolate* isolate, FunctionCallback callback = nullptr,
497 Local<Value> data = Local<Value>(),
498 Local<Signature> signature = Local<Signature>(), int length = 0,
499 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
500 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
501 const CFunction* c_function = nullptr, uint16_t instance_type = 0,
502 uint16_t allowed_receiver_instance_type_range_start = 0,
503 uint16_t allowed_receiver_instance_type_range_end = 0);
504
506 static Local<FunctionTemplate> NewWithCFunctionOverloads(
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 MemorySpan<const CFunction>& c_function_overloads = {});
513
517 static Local<FunctionTemplate> NewWithCache(
518 Isolate* isolate, FunctionCallback callback,
519 Local<Private> cache_property, Local<Value> data = Local<Value>(),
520 Local<Signature> signature = Local<Signature>(), int length = 0,
521 SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
522
524 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
525 Local<Context> context);
526
534 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
535
542 void SetCallHandler(
543 FunctionCallback callback, Local<Value> data = Local<Value>(),
544 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
545 const MemorySpan<const CFunction>& c_function_overloads = {});
546
548 void SetLength(int length);
549
551 Local<ObjectTemplate> InstanceTemplate();
552
558 void Inherit(Local<FunctionTemplate> parent);
559
564 Local<ObjectTemplate> PrototypeTemplate();
565
572 void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
573
579 void SetClassName(Local<String> name);
580
585 void SetInterfaceName(Local<String> name);
586
591 void SetExceptionContext(ExceptionContext context);
592
597 void SetAcceptAnyReceiver(bool value);
598
603 void ReadOnlyPrototype();
604
609 void RemovePrototype();
610
615 bool HasInstance(Local<Value> object);
616
624 bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
625
632 void SealAndPrepareForPromotionToReadOnly();
633
634 V8_INLINE static FunctionTemplate* Cast(Data* data);
635
636 private:
637 FunctionTemplate();
638
639 static void CheckCast(Data* that);
640 friend class Context;
641 friend class ObjectTemplate;
642};
643
648enum class PropertyHandlerFlags {
652 kNone = 0,
653
659 kNonMasking = 1,
660
665 kOnlyInterceptStrings = 1 << 1,
666
670 kHasNoSideEffect = 1 << 2,
671
680};
683 private:
684 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
686 return static_cast<PropertyHandlerFlags>(
687 static_cast<int>(flags) |
688 static_cast<int>(
690 }
691
692 public:
703 : getter(getter),
704 setter(setter),
705 query(query),
710 data(data),
711 flags(flags) {}
721 : getter(getter),
722 setter(setter),
723 query(query),
726 definer(nullptr),
727 descriptor(nullptr),
728 data(data),
729 flags(flags) {}
740 : getter(getter),
741 setter(setter),
742 query(nullptr),
747 data(data),
748 flags(flags) {}
759};
762 private:
763 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
765 return static_cast<PropertyHandlerFlags>(
766 static_cast<int>(flags) |
767 static_cast<int>(
769 }
770
771 public:
782 : getter(getter),
783 setter(setter),
784 query(query),
789 data(data),
790 flags(flags) {}
800 : getter(getter),
801 setter(setter),
802 query(query),
805 definer(nullptr),
806 descriptor(nullptr),
807 data(data),
808 flags(flags) {}
819 : getter(getter),
820 setter(setter),
821 query(nullptr),
826 data(data),
827 flags(flags) {}
838};
839
846class V8_EXPORT ObjectTemplate : public Template {
847 public:
849 static Local<ObjectTemplate> New(
850 Isolate* isolate,
852
859
871 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
872
883 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
884
891 void SetCallAsFunctionHandler(FunctionCallback callback,
892 Local<Value> data = Local<Value>());
893
902 void MarkAsUndetectable();
903
912 void SetAccessCheckCallback(AccessCheckCallback callback,
913 Local<Value> data = Local<Value>());
914
921 void SetAccessCheckCallbackAndHandler(
922 AccessCheckCallback callback,
923 const NamedPropertyHandlerConfiguration& named_handler,
924 const IndexedPropertyHandlerConfiguration& indexed_handler,
925 Local<Value> data = Local<Value>());
926
931 int InternalFieldCount() const;
932
937 void SetInternalFieldCount(int value);
938
942 bool IsImmutableProto() const;
943
948 void SetImmutableProto();
949
959 void SetCodeLike();
960 bool IsCodeLike() const;
961
968 void SealAndPrepareForPromotionToReadOnly();
969
970 V8_INLINE static ObjectTemplate* Cast(Data* data);
971
972 private:
974
975 static void CheckCast(Data* that);
976 friend class FunctionTemplate;
977};
978
982class V8_EXPORT DictionaryTemplate final : public Data {
983 public:
991 static Local<DictionaryTemplate> New(
993
1004 Local<Context> context, MemorySpan<MaybeLocal<Value>> property_values);
1005
1006 V8_INLINE static DictionaryTemplate* Cast(Data* data);
1007
1008 private:
1009 static void CheckCast(Data* that);
1010
1012};
1013
1022class V8_EXPORT Signature : public Data {
1023 public:
1024 static Local<Signature> New(
1025 Isolate* isolate,
1027
1028 V8_INLINE static Signature* Cast(Data* data);
1029
1030 private:
1031 Signature();
1032
1033 static void CheckCast(Data* that);
1034};
1035
1036// --- Implementation ---
1038void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1039 PropertyAttribute attributes) {
1041 .ToLocalChecked(),
1042 value, attributes);
1043}
1046#ifdef V8_ENABLE_CHECKS
1047 CheckCast(data);
1048#endif
1049 return reinterpret_cast<FunctionTemplate*>(data);
1050}
1053#ifdef V8_ENABLE_CHECKS
1054 CheckCast(data);
1055#endif
1056 return reinterpret_cast<ObjectTemplate*>(data);
1057}
1060#ifdef V8_ENABLE_CHECKS
1061 CheckCast(data);
1062#endif
1063 return reinterpret_cast<DictionaryTemplate*>(data);
1064}
1067#ifdef V8_ENABLE_CHECKS
1068 CheckCast(data);
1069#endif
1070 return reinterpret_cast<Signature*>(data);
1071}
1072
1073} // namespace v8
1074
1075#endif // INCLUDE_V8_TEMPLATE_H_
Definition: v8-context.h:48
Definition: v8-data.h:18
Definition: v8-template.h:981
static DictionaryTemplate * Cast(Data *data)
Definition: v8-template.h:1058
Definition: v8-template.h:491
static FunctionTemplate * Cast(Data *data)
Definition: v8-template.h:1044
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:845
static ObjectTemplate * Cast(Data *data)
Definition: v8-template.h:1051
Definition: v8-container.h:148
Definition: v8-template.h:1021
static Signature * Cast(Data *data)
Definition: v8-template.h:1065
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:171
Intercepted(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) NamedPropertySetterCallback
Definition: v8-template.h:207
SideEffectType
Definition: v8-object.h:199
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) NamedPropertyDescriptorCallback
Definition: v8-template.h:322
Intercepted(*)(uint32_t index, Local< Value > value, const PropertyCallbackInfo< void > &info) IndexedPropertySetterCallbackV2
Definition: v8-template.h:338
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:295
Intrinsic
Definition: v8-template.h:41
void(*)(const PropertyCallbackInfo< Array > &info) NamedPropertyEnumeratorCallback
Definition: v8-template.h:272
ConstructorBehavior
Definition: v8-template.h:382
void(*)(const FunctionCallbackInfo< Value > &info) FunctionCallback
Definition: v8-function-callback.h:299
Intercepted(*)(uint32_t index, const PropertyDescriptor &desc, const PropertyCallbackInfo< void > &info) IndexedPropertyDefinerCallbackV2
Definition: v8-template.h:366
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Boolean > &info) IndexedPropertyDeleterCallbackV2
Definition: v8-template.h:350
void(*)(const PropertyCallbackInfo< Array > &info) IndexedPropertyEnumeratorCallback
Definition: v8-template.h:359
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyDescriptorCallbackV2
Definition: v8-template.h:372
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Integer > &info) NamedPropertyQueryCallback
Definition: v8-template.h:236
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) AccessorNameSetterCallback
Definition: v8-object.h:175
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Value > &info) IndexedPropertyGetterCallbackV2
Definition: v8-template.h:332
bool(*)(Local< Context > accessing_context, Local< Object > accessed_object, Local< Value > data) AccessCheckCallback
Definition: v8-template.h:380
Intercepted(*)(Local< Name > property, const PropertyCallbackInfo< Boolean > &info) NamedPropertyDeleterCallback
Definition: v8-template.h:263
Intercepted(*)(uint32_t index, const PropertyCallbackInfo< Integer > &info) IndexedPropertyQueryCallbackV2
Definition: v8-template.h:344
PropertyHandlerFlags
Definition: v8-template.h:647
Intercepted
Definition: v8-template.h:138
Definition: v8-template.h:760
PropertyHandlerFlags flags
Definition: v8-template.h:836
IndexedPropertyQueryCallbackV2 query
Definition: v8-template.h:830
IndexedPropertyDeleterCallbackV2 deleter
Definition: v8-template.h:831
IndexedPropertyGetterCallbackV2 getter
Definition: v8-template.h:828
IndexedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:832
Local< Value > data
Definition: v8-template.h:835
IndexedPropertyDescriptorCallbackV2 descriptor
Definition: v8-template.h:834
IndexedPropertySetterCallbackV2 setter
Definition: v8-template.h:829
IndexedPropertyDefinerCallbackV2 definer
Definition: v8-template.h:833
Definition: v8-template.h:681
NamedPropertyDescriptorCallback descriptor
Definition: v8-template.h:755
Local< Value > data
Definition: v8-template.h:756
NamedPropertySetterCallback setter
Definition: v8-template.h:750
NamedPropertyDeleterCallback deleter
Definition: v8-template.h:752
NamedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:753
NamedPropertyQueryCallback query
Definition: v8-template.h:751
NamedPropertyGetterCallback getter
Definition: v8-template.h:749
NamedPropertyDefinerCallback definer
Definition: v8-template.h:754
PropertyHandlerFlags flags
Definition: v8-template.h:757
#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