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
393using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
394 Local<Object> accessed_object,
395 Local<Value> data);
397enum class ConstructorBehavior { kThrow, kAllow };
398
506class V8_EXPORT FunctionTemplate : public Template {
507 public:
509 static Local<FunctionTemplate> New(
510 Isolate* isolate, FunctionCallback callback = nullptr,
511 Local<Value> data = Local<Value>(),
512 Local<Signature> signature = Local<Signature>(), int length = 0,
513 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
514 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
515 const CFunction* c_function = nullptr, uint16_t instance_type = 0,
516 uint16_t allowed_receiver_instance_type_range_start = 0,
517 uint16_t allowed_receiver_instance_type_range_end = 0);
518
520 static Local<FunctionTemplate> NewWithCFunctionOverloads(
521 Isolate* isolate, FunctionCallback callback = nullptr,
522 Local<Value> data = Local<Value>(),
523 Local<Signature> signature = Local<Signature>(), int length = 0,
524 ConstructorBehavior behavior = ConstructorBehavior::kAllow,
525 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
526 const MemorySpan<const CFunction>& c_function_overloads = {});
527
531 static Local<FunctionTemplate> NewWithCache(
532 Isolate* isolate, FunctionCallback callback,
533 Local<Private> cache_property, Local<Value> data = Local<Value>(),
534 Local<Signature> signature = Local<Signature>(), int length = 0,
535 SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
536
538 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
539 Local<Context> context);
540
548 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
549
556 void SetCallHandler(
557 FunctionCallback callback, Local<Data> data = {},
558 SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
559 const MemorySpan<const CFunction>& c_function_overloads = {});
560
562 void SetLength(int length);
563
565 Local<ObjectTemplate> InstanceTemplate();
566
572 void Inherit(Local<FunctionTemplate> parent);
573
578 Local<ObjectTemplate> PrototypeTemplate();
579
586 void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
587
593 void SetClassName(Local<String> name);
594
599 void SetInterfaceName(Local<String> name);
600
605 void SetExceptionContext(ExceptionContext context);
606
611 void SetAcceptAnyReceiver(bool value);
612
617 void ReadOnlyPrototype();
618
623 void RemovePrototype();
624
629 bool HasInstance(Local<Value> object);
630
638 bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
639
646 void SealAndPrepareForPromotionToReadOnly();
647
648 V8_INLINE static FunctionTemplate* Cast(Data* data);
649
650 private:
651 FunctionTemplate();
652
653 static void CheckCast(Data* that);
654 friend class Context;
655 friend class ObjectTemplate;
656};
657
662enum class PropertyHandlerFlags {
666 kNone = 0,
667
673 kNonMasking = 1,
674
679 kOnlyInterceptStrings = 1 << 1,
680
684 kHasNoSideEffect = 1 << 2,
685
694};
697 private:
698 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
700 return static_cast<PropertyHandlerFlags>(
701 static_cast<int>(flags) |
702 static_cast<int>(
704 }
705
706 public:
717 : getter(getter),
718 setter(setter),
719 query(query),
724 data(data),
725 flags(flags) {}
735 : getter(getter),
736 setter(setter),
737 query(query),
740 definer(nullptr),
741 descriptor(nullptr),
742 data(data),
743 flags(flags) {}
754 : getter(getter),
755 setter(setter),
756 query(nullptr),
761 data(data),
762 flags(flags) {}
773};
776 private:
777 static constexpr PropertyHandlerFlags WithNewSignatureFlag(
779 return static_cast<PropertyHandlerFlags>(
780 static_cast<int>(flags) |
781 static_cast<int>(
783 }
784
785 public:
796 : getter(getter),
797 setter(setter),
798 query(query),
803 data(data),
804 flags(flags) {}
814 : getter(getter),
815 setter(setter),
816 query(query),
819 definer(nullptr),
820 descriptor(nullptr),
821 data(data),
822 flags(flags) {}
833 : getter(getter),
834 setter(setter),
835 query(nullptr),
840 data(data),
841 flags(flags) {}
852};
853
860class V8_EXPORT ObjectTemplate : public Template {
861 public:
863 static Local<ObjectTemplate> New(
864 Isolate* isolate,
866
873
885 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
886
897 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
898
905 void SetCallAsFunctionHandler(FunctionCallback callback,
906 Local<Value> data = Local<Value>());
907
916 void MarkAsUndetectable();
917
926 void SetAccessCheckCallback(AccessCheckCallback callback,
927 Local<Value> data = Local<Value>());
928
935 void SetAccessCheckCallbackAndHandler(
936 AccessCheckCallback callback,
937 const NamedPropertyHandlerConfiguration& named_handler,
938 const IndexedPropertyHandlerConfiguration& indexed_handler,
939 Local<Value> data = Local<Value>());
940
945 int InternalFieldCount() const;
946
951 void SetInternalFieldCount(int value);
952
956 bool IsImmutableProto() const;
957
962 void SetImmutableProto();
963
973 void SetCodeLike();
974 bool IsCodeLike() const;
975
982 void SealAndPrepareForPromotionToReadOnly();
983
984 V8_INLINE static ObjectTemplate* Cast(Data* data);
985
986 private:
988
989 static void CheckCast(Data* that);
990 friend class FunctionTemplate;
991};
992
996class V8_EXPORT DictionaryTemplate final : public Data {
997 public:
1005 static Local<DictionaryTemplate> New(
1007
1018 Local<Context> context, MemorySpan<MaybeLocal<Value>> property_values);
1019
1020 V8_INLINE static DictionaryTemplate* Cast(Data* data);
1021
1022 private:
1023 static void CheckCast(Data* that);
1024
1026};
1027
1036class V8_EXPORT Signature : public Data {
1037 public:
1038 static Local<Signature> New(
1039 Isolate* isolate,
1041
1042 V8_INLINE static Signature* Cast(Data* data);
1043
1044 private:
1045 Signature();
1046
1047 static void CheckCast(Data* that);
1048};
1049
1050// --- Implementation ---
1052void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1053 PropertyAttribute attributes) {
1055 .ToLocalChecked(),
1056 value, attributes);
1057}
1060#ifdef V8_ENABLE_CHECKS
1061 CheckCast(data);
1062#endif
1063 return reinterpret_cast<FunctionTemplate*>(data);
1064}
1067#ifdef V8_ENABLE_CHECKS
1068 CheckCast(data);
1069#endif
1070 return reinterpret_cast<ObjectTemplate*>(data);
1071}
1074#ifdef V8_ENABLE_CHECKS
1075 CheckCast(data);
1076#endif
1077 return reinterpret_cast<DictionaryTemplate*>(data);
1078}
1081#ifdef V8_ENABLE_CHECKS
1082 CheckCast(data);
1083#endif
1084 return reinterpret_cast<Signature*>(data);
1085}
1086
1087} // namespace v8
1088
1089#endif // INCLUDE_V8_TEMPLATE_H_
Definition: v8-context.h:48
Definition: v8-data.h:18
Definition: v8-template.h:995
static DictionaryTemplate * Cast(Data *data)
Definition: v8-template.h:1072
Definition: v8-template.h:505
static FunctionTemplate * Cast(Data *data)
Definition: v8-template.h:1058
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:859
static ObjectTemplate * Cast(Data *data)
Definition: v8-template.h:1065
Definition: v8-container.h:148
Definition: v8-template.h:1035
static Signature * Cast(Data *data)
Definition: v8-template.h:1079
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:396
void(*)(const FunctionCallbackInfo< Value > &info) FunctionCallback
Definition: v8-function-callback.h:352
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
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:394
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:661
Definition: v8-template.h:774
PropertyHandlerFlags flags
Definition: v8-template.h:850
IndexedPropertyQueryCallbackV2 query
Definition: v8-template.h:844
IndexedPropertyDeleterCallbackV2 deleter
Definition: v8-template.h:845
IndexedPropertyGetterCallbackV2 getter
Definition: v8-template.h:842
IndexedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:846
Local< Value > data
Definition: v8-template.h:849
IndexedPropertyDescriptorCallbackV2 descriptor
Definition: v8-template.h:848
IndexedPropertySetterCallbackV2 setter
Definition: v8-template.h:843
IndexedPropertyDefinerCallbackV2 definer
Definition: v8-template.h:847
Definition: v8-template.h:695
NamedPropertyDescriptorCallback descriptor
Definition: v8-template.h:769
Local< Value > data
Definition: v8-template.h:770
NamedPropertySetterCallback setter
Definition: v8-template.h:764
NamedPropertyDeleterCallback deleter
Definition: v8-template.h:766
NamedPropertyEnumeratorCallback enumerator
Definition: v8-template.h:767
NamedPropertyQueryCallback query
Definition: v8-template.h:765
NamedPropertyGetterCallback getter
Definition: v8-template.h:763
NamedPropertyDefinerCallback definer
Definition: v8-template.h:768
PropertyHandlerFlags flags
Definition: v8-template.h:771
#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