v8 12.4.254 (node 22.4.1)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
v8-object.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_OBJECT_H_
6#define INCLUDE_V8_OBJECT_H_
7
8#include "v8-local-handle.h" // NOLINT(build/include_directory)
9#include "v8-maybe.h" // NOLINT(build/include_directory)
10#include "v8-persistent-handle.h" // NOLINT(build/include_directory)
11#include "v8-primitive.h" // NOLINT(build/include_directory)
12#include "v8-traced-handle.h" // NOLINT(build/include_directory)
13#include "v8-value.h" // NOLINT(build/include_directory)
14#include "v8config.h" // NOLINT(build/include_directory)
15
16namespace v8 {
17
18class Array;
19class Function;
20class FunctionTemplate;
21template <typename T>
22class PropertyCallbackInfo;
23
29class V8_EXPORT Private : public Data {
30 public:
35
39 static Local<Private> New(Isolate* isolate,
41
52
53 V8_INLINE static Private* Cast(Data* data);
54
55 private:
56 Private();
57
58 static void CheckCast(Data* that);
59};
60
90 public:
91 // GenericDescriptor
93
94 // DataDescriptor
96
97 // DataDescriptor with writable property
98 PropertyDescriptor(Local<Value> value, bool writable);
99
100 // AccessorDescriptor
102
104
106 bool has_value() const;
107
109 bool has_get() const;
111 bool has_set() const;
112
113 void set_enumerable(bool enumerable);
114 bool enumerable() const;
115 bool has_enumerable() const;
116
117 void set_configurable(bool configurable);
118 bool configurable() const;
119 bool has_configurable() const;
120
121 bool writable() const;
122 bool has_writable() const;
123
124 struct PrivateData;
125 PrivateData* get_private() const { return private_; }
126
128 void operator=(const PropertyDescriptor&) = delete;
129
130 private:
131 PrivateData* private_;
132};
133
139 None = 0,
141 ReadOnly = 1 << 0,
143 DontEnum = 1 << 1,
145 DontDelete = 1 << 2
147
154 void (*)(Local<String> property, const PropertyCallbackInfo<Value>& info);
156 void (*)(Local<Name> property, const PropertyCallbackInfo<Value>& info);
157
158using AccessorSetterCallback = void (*)(Local<String> property,
159 Local<Value> value,
160 const PropertyCallbackInfo<void>& info);
162 void (*)(Local<Name> property, Local<Value> value,
163 const PropertyCallbackInfo<void>& info);
164
175};
176
186 SKIP_SYMBOLS = 16
188
199enum class SideEffectType {
203};
204
213
219
225
230
234class V8_EXPORT Object : public Value {
235 public:
241 Local<Value> key, Local<Value> value);
242
244 Local<Value> value);
245
257 Local<Name> key,
258 Local<Value> value);
260 uint32_t index,
261 Local<Value> value);
262
273 Local<Context> context, Local<Name> key, Local<Value> value,
274 PropertyAttribute attributes = None);
275
293 Local<Context> context, Local<Name> key, PropertyDescriptor& descriptor);
294
296 Local<Value> key);
297
299 uint32_t index);
300
307 Local<Context> context, Local<Value> key);
308
314 Local<Context> context, Local<Name> key);
315
332 Local<Value> key);
333
335 Local<Value> key);
336
338
340 uint32_t index);
341
342 V8_DEPRECATE_SOON("Use SetNativeDataProperty instead")
343 V8_WARN_UNUSED_RESULT Maybe<bool> SetAccessor(
344 Local<Context> context, Local<Name> name,
346 AccessorNameSetterCallback setter = nullptr,
347 MaybeLocal<Value> data = MaybeLocal<Value>(),
348 AccessControl deprecated_settings = DEFAULT,
349 PropertyAttribute attribute = None,
350 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
351 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
352
353 void SetAccessorProperty(Local<Name> name, Local<Function> getter,
354 Local<Function> setter = Local<Function>(),
355 PropertyAttribute attributes = None);
356
361 V8_WARN_UNUSED_RESULT Maybe<bool> SetNativeDataProperty(
362 Local<Context> context, Local<Name> name,
364 AccessorNameSetterCallback setter = nullptr,
365 Local<Value> data = Local<Value>(), PropertyAttribute attributes = None,
366 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
367 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
368
377 V8_WARN_UNUSED_RESULT Maybe<bool> SetLazyDataProperty(
378 Local<Context> context, Local<Name> name,
380 PropertyAttribute attributes = None,
381 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
382 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
383
390 Maybe<bool> HasPrivate(Local<Context> context, Local<Private> key);
391 Maybe<bool> SetPrivate(Local<Context> context, Local<Private> key,
392 Local<Value> value);
393 Maybe<bool> DeletePrivate(Local<Context> context, Local<Private> key);
394 MaybeLocal<Value> GetPrivate(Local<Context> context, Local<Private> key);
395
403 Local<Context> context);
405 Local<Context> context, KeyCollectionMode mode,
406 PropertyFilter property_filter, IndexFilter index_filter,
407 KeyConversionMode key_conversion = KeyConversionMode::kKeepNumbers);
408
415 Local<Context> context);
416
424 Local<Context> context, PropertyFilter filter,
425 KeyConversionMode key_conversion = KeyConversionMode::kKeepNumbers);
426
432 Local<Value> GetPrototype();
433
439 V8_WARN_UNUSED_RESULT Maybe<bool> SetPrototype(Local<Context> context,
440 Local<Value> prototype);
441
446 Local<Object> FindInstanceInPrototypeChain(Local<FunctionTemplate> tmpl);
447
454 Local<Context> context);
455
459 Local<String> GetConstructorName();
460
464 Maybe<bool> SetIntegrityLevel(Local<Context> context, IntegrityLevel level);
465
467 int InternalFieldCount() const;
468
470 V8_INLINE static int InternalFieldCount(
471 const PersistentBase<Object>& object) {
472 return object.template value<Object>()->InternalFieldCount();
473 }
474
477 const BasicTracedReference<Object>& object) {
478 return object.template value<Object>()->InternalFieldCount();
479 }
480
491 V8_INLINE Local<Data> GetInternalField(int index);
492
494 void SetInternalField(int index, Local<Data> data);
495
501 V8_INLINE void* GetAlignedPointerFromInternalField(int index);
502 V8_INLINE void* GetAlignedPointerFromInternalField(v8::Isolate* isolate,
503 int index);
504
507 const PersistentBase<Object>& object, int index) {
508 return object.template value<Object>()->GetAlignedPointerFromInternalField(
509 index);
510 }
511
514 const BasicTracedReference<Object>& object, int index) {
515 return object.template value<Object>()->GetAlignedPointerFromInternalField(
516 index);
517 }
518
524 void SetAlignedPointerInInternalField(int index, void* value);
525 void SetAlignedPointerInInternalFields(int argc, int indices[],
526 void* values[]);
527
534 Local<Name> key);
536 uint32_t index);
551 Local<Name> key);
553 Local<Context> context, uint32_t index);
555 Local<Context> context, Local<Name> key);
556
562 Local<Context> context, Local<Name> key);
563
571 Local<Name> key);
572
579 Local<Context> context, Local<Name> key);
580
587 Local<Context> context, Local<Name> key);
588
591
594
603
608 // TODO(dcarney): take an isolate and optionally bail out?
610
615
620
623 const PersistentBase<Object>& object) {
624 return object.template value<Object>()->GetCreationContext();
625 }
626
639
645 bool IsCallable() const;
646
650 bool IsConstructor() const;
651
659 bool IsApiWrapper() const;
660
666 bool IsUndetectable() const;
667
673 Local<Value> recv,
674 int argc,
675 Local<Value> argv[]);
676
683 Local<Context> context, int argc, Local<Value> argv[]);
684
689
691 return handle.template value<Object>()->GetIsolate();
692 }
693
704
705 static Local<Object> New(Isolate* isolate);
706
715 static Local<Object> New(Isolate* isolate, Local<Value> prototype_or_null,
716 Local<Name>* names, Local<Value>* values,
717 size_t length);
718
719 V8_INLINE static Object* Cast(Value* obj);
720
729 bool IsCodeLike(Isolate* isolate) const;
730
731 private:
732 Object();
733 static void CheckCast(Value* obj);
734 Local<Data> SlowGetInternalField(int index);
735 void* SlowGetAlignedPointerFromInternalField(int index);
736 void* SlowGetAlignedPointerFromInternalField(v8::Isolate* isolate, int index);
737};
738
739// --- Implementation ---
740
742#ifndef V8_ENABLE_CHECKS
743 using A = internal::Address;
744 using I = internal::Internals;
746 // Fast path: If the object is a plain JSObject, which is the common case, we
747 // know where to find the internal fields and can return the value directly.
748 int instance_type = I::GetInstanceType(obj);
749 if (I::CanHaveInternalField(instance_type)) {
750 int offset = I::kJSObjectHeaderSize + (I::kEmbedderDataSlotSize * index);
751 A value = I::ReadRawField<A>(obj, offset);
752#ifdef V8_COMPRESS_POINTERS
753 // We read the full pointer value and then decompress it in order to avoid
754 // dealing with potential endiannes issues.
755 value = I::DecompressTaggedField(obj, static_cast<uint32_t>(value));
756#endif
757
758 auto isolate = reinterpret_cast<v8::Isolate*>(
760 return Local<Data>::New(isolate, value);
761 }
762#endif
763 return SlowGetInternalField(index);
764}
765
767 int index) {
768#if !defined(V8_ENABLE_CHECKS)
769 using A = internal::Address;
770 using I = internal::Internals;
772 // Fast path: If the object is a plain JSObject, which is the common case, we
773 // know where to find the internal fields and can return the value directly.
774 auto instance_type = I::GetInstanceType(obj);
775 if (V8_LIKELY(I::CanHaveInternalField(instance_type))) {
776 int offset = I::kJSObjectHeaderSize + (I::kEmbedderDataSlotSize * index) +
777 I::kEmbedderDataSlotExternalPointerOffset;
778 A value =
779 I::ReadExternalPointerField<internal::kEmbedderDataSlotPayloadTag>(
780 isolate, obj, offset);
781 return reinterpret_cast<void*>(value);
782 }
783#endif
784 return SlowGetAlignedPointerFromInternalField(isolate, index);
785}
786
788#if !defined(V8_ENABLE_CHECKS)
789 using A = internal::Address;
790 using I = internal::Internals;
792 // Fast path: If the object is a plain JSObject, which is the common case, we
793 // know where to find the internal fields and can return the value directly.
794 auto instance_type = I::GetInstanceType(obj);
795 if (V8_LIKELY(I::CanHaveInternalField(instance_type))) {
796 int offset = I::kJSObjectHeaderSize + (I::kEmbedderDataSlotSize * index) +
797 I::kEmbedderDataSlotExternalPointerOffset;
798 Isolate* isolate = I::GetIsolateForSandbox(obj);
799 A value =
800 I::ReadExternalPointerField<internal::kEmbedderDataSlotPayloadTag>(
801 isolate, obj, offset);
802 return reinterpret_cast<void*>(value);
803 }
804#endif
805 return SlowGetAlignedPointerFromInternalField(index);
806}
807
809#ifdef V8_ENABLE_CHECKS
810 CheckCast(data);
811#endif
812 return reinterpret_cast<Private*>(data);
813}
814
816#ifdef V8_ENABLE_CHECKS
817 CheckCast(value);
818#endif
819 return static_cast<Object*>(value);
820}
821
822} // namespace v8
823
824#endif // INCLUDE_V8_OBJECT_H_
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
bool HasNamedLookupInterceptor() const
V8_WARN_UNUSED_RESULT Maybe< bool > HasOwnProperty(Local< Context > context, uint32_t index)
bool HasIndexedLookupInterceptor() const
bool IsCallable() const
V8_WARN_UNUSED_RESULT Maybe< PropertyAttribute > GetPropertyAttributes(Local< Context > context, Local< Value > key)
V8_WARN_UNUSED_RESULT Maybe< bool > Has(Local< Context > context, Local< Value > key)
static V8_INLINE int InternalFieldCount(const BasicTracedReference< Object > &object)
Definition v8-object.h:476
V8_WARN_UNUSED_RESULT MaybeLocal< Value > CallAsConstructor(Local< Context > context, int argc, Local< Value > argv[])
V8_WARN_UNUSED_RESULT Maybe< bool > HasRealNamedProperty(Local< Context > context, Local< Name > key)
Local< Object > Clone()
int GetIdentityHash()
Isolate * GetIsolate()
V8_WARN_UNUSED_RESULT Maybe< bool > Delete(Local< Context > context, Local< Value > key)
Local< Context > GetCreationContextChecked()
void * GetAlignedPointerFromEmbedderDataInCreationContext(int index)
static V8_INLINE void * GetAlignedPointerFromInternalField(const BasicTracedReference< Object > &object, int index)
Definition v8-object.h:513
V8_WARN_UNUSED_RESULT Maybe< bool > DefineOwnProperty(Local< Context > context, Local< Name > key, Local< Value > value, PropertyAttribute attributes=None)
V8_INLINE void * GetAlignedPointerFromInternalField(int index)
Definition v8-object.h:787
V8_INLINE Local< Data > GetInternalField(int index)
Definition v8-object.h:741
bool IsApiWrapper() const
MaybeLocal< Context > GetCreationContext()
V8_WARN_UNUSED_RESULT Maybe< PropertyAttribute > GetRealNamedPropertyAttributes(Local< Context > context, Local< Name > key)
void SetInternalField(int index, Local< Data > data)
V8_WARN_UNUSED_RESULT Maybe< bool > CreateDataProperty(Local< Context > context, uint32_t index, Local< Value > value)
MaybeLocal< Array > PreviewEntries(bool *is_key_value)
bool IsCodeLike(Isolate *isolate) const
static V8_INLINE MaybeLocal< Context > GetCreationContext(const PersistentBase< Object > &object)
Definition v8-object.h:622
bool IsConstructor() const
static V8_INLINE Object * Cast(Value *obj)
Definition v8-object.h:815
V8_WARN_UNUSED_RESULT Maybe< bool > HasRealIndexedProperty(Local< Context > context, uint32_t index)
void SetAlignedPointerInInternalField(int index, void *value)
V8_WARN_UNUSED_RESULT MaybeLocal< Value > Get(Local< Context > context, Local< Value > key)
void SetAlignedPointerInInternalFields(int argc, int indices[], void *values[])
V8_WARN_UNUSED_RESULT MaybeLocal< Value > CallAsFunction(Local< Context > context, Local< Value > recv, int argc, Local< Value > argv[])
V8_WARN_UNUSED_RESULT MaybeLocal< Value > GetOwnPropertyDescriptor(Local< Context > context, Local< Name > key)
static V8_INLINE void * GetAlignedPointerFromInternalField(const PersistentBase< Object > &object, int index)
Definition v8-object.h:506
V8_WARN_UNUSED_RESULT Maybe< bool > Set(Local< Context > context, uint32_t index, Local< Value > value)
V8_WARN_UNUSED_RESULT Maybe< bool > HasOwnProperty(Local< Context > context, Local< Name > key)
V8_WARN_UNUSED_RESULT Maybe< bool > Delete(Local< Context > context, uint32_t index)
V8_WARN_UNUSED_RESULT Maybe< bool > CreateDataProperty(Local< Context > context, Local< Name > key, Local< Value > value)
V8_WARN_UNUSED_RESULT MaybeLocal< Value > Get(Local< Context > context, uint32_t index)
V8_WARN_UNUSED_RESULT Maybe< bool > HasRealNamedCallbackProperty(Local< Context > context, Local< Name > key)
static V8_INLINE Isolate * GetIsolate(const TracedReference< Object > &handle)
Definition v8-object.h:690
static Local< Object > New(Isolate *isolate)
static Local< Object > New(Isolate *isolate, Local< Value > prototype_or_null, Local< Name > *names, Local< Value > *values, size_t length)
V8_WARN_UNUSED_RESULT Maybe< bool > DefineProperty(Local< Context > context, Local< Name > key, PropertyDescriptor &descriptor)
bool IsUndetectable() const
V8_WARN_UNUSED_RESULT Maybe< PropertyAttribute > GetRealNamedPropertyAttributesInPrototypeChain(Local< Context > context, Local< Name > key)
V8_WARN_UNUSED_RESULT Maybe< bool > Has(Local< Context > context, uint32_t index)
V8_WARN_UNUSED_RESULT MaybeLocal< Value > GetRealNamedProperty(Local< Context > context, Local< Name > key)
V8_WARN_UNUSED_RESULT Maybe< bool > Set(Local< Context > context, Local< Value > key, Local< Value > value)
V8_WARN_UNUSED_RESULT MaybeLocal< Value > GetRealNamedPropertyInPrototypeChain(Local< Context > context, Local< Name > key)
Local< Value > Name() const
static Local< Private > ForApi(Isolate *isolate, Local< String > name)
static V8_INLINE Private * Cast(Data *data)
Definition v8-object.h:808
static Local< Private > New(Isolate *isolate, Local< String > name=Local< String >())
PrivateData * get_private() const
Definition v8-object.h:125
bool has_configurable() const
PropertyDescriptor(const PropertyDescriptor &)=delete
bool has_enumerable() const
Local< Value > set() const
void set_configurable(bool configurable)
bool has_value() const
void operator=(const PropertyDescriptor &)=delete
PropertyDescriptor(Local< Value > get, Local< Value > set)
bool has_writable() const
bool enumerable() const
PropertyDescriptor(Local< Value > value)
Local< Value > value() const
void set_enumerable(bool enumerable)
Local< Value > get() const
PropertyDescriptor(Local< Value > value, bool writable)
bool configurable() const
static V8_INLINE Address ValueAsAddress(const T *value)
V8_EXPORT internal::Isolate * IsolateFromNeverReadOnlySpaceObject(Address obj)
uintptr_t Address
Definition v8-internal.h:31
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) AccessorNameGetterCallback
Definition v8-object.h:156
void(*)(Local< String > property, Local< Value > value, const PropertyCallbackInfo< void > &info) AccessorSetterCallback
Definition v8-object.h:160
AccessControl
Definition v8-object.h:173
@ DEFAULT
Definition v8-object.h:174
SideEffectType
Definition v8-object.h:199
PropertyFilter
Definition v8-object.h:180
@ SKIP_SYMBOLS
Definition v8-object.h:186
@ ALL_PROPERTIES
Definition v8-object.h:181
@ ONLY_CONFIGURABLE
Definition v8-object.h:184
@ ONLY_WRITABLE
Definition v8-object.h:182
@ ONLY_ENUMERABLE
Definition v8-object.h:183
@ SKIP_STRINGS
Definition v8-object.h:185
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) AccessorNameSetterCallback
Definition v8-object.h:163
IntegrityLevel
Definition v8-object.h:229
PropertyAttribute
Definition v8-object.h:137
@ DontDelete
Definition v8-object.h:145
@ DontEnum
Definition v8-object.h:143
@ None
Definition v8-object.h:139
@ ReadOnly
Definition v8-object.h:141
IndexFilter
Definition v8-object.h:218
KeyCollectionMode
Definition v8-object.h:212
KeyConversionMode
Definition v8-object.h:224
void(*)(Local< String > property, const PropertyCallbackInfo< Value > &info) AccessorGetterCallback
Definition v8-object.h:154
#define V8_EXPORT
Definition v8config.h:753
#define V8_INLINE
Definition v8config.h:477
#define V8_DEPRECATE_SOON(message)
Definition v8config.h:571
#define V8_LIKELY(condition)
Definition v8config.h:618
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:628