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_
Definition: v8-container.h:25
Definition: v8-traced-handle.h:125
Definition: v8-context.h:48
Definition: v8-data.h:18
Definition: v8-template.h:471
Definition: v8-function.h:26
Definition: v8-isolate.h:210
Definition: v8-local-handle.h:258
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:338
Definition: v8-local-handle.h:616
Definition: v8-maybe.h:32
Definition: v8-primitive.h:79
Definition: v8-object.h:234
Maybe< PropertyAttribute > GetRealNamedPropertyAttributesInPrototypeChain(Local< Context > context, Local< Name > key)
Maybe< PropertyAttribute > GetPropertyAttributes(Local< Context > context, Local< Value > key)
void SetAlignedPointerInInternalField(int index, void *value)
Maybe< bool > CreateDataProperty(Local< Context > context, uint32_t index, Local< Value > value)
MaybeLocal< Value > GetOwnPropertyDescriptor(Local< Context > context, Local< Name > key)
static Object * Cast(Value *obj)
Definition: v8-object.h:815
bool HasNamedLookupInterceptor() const
Maybe< bool > HasRealNamedProperty(Local< Context > context, Local< Name > key)
Maybe< bool > Delete(Local< Context > context, uint32_t index)
MaybeLocal< Value > GetRealNamedProperty(Local< Context > context, Local< Name > key)
Maybe< bool > DefineProperty(Local< Context > context, Local< Name > key, PropertyDescriptor &descriptor)
Maybe< bool > Delete(Local< Context > context, Local< Value > key)
bool IsConstructor() const
bool IsUndetectable() const
MaybeLocal< Value > CallAsFunction(Local< Context > context, Local< Value > recv, int argc, Local< Value > argv[])
Local< Object > Clone()
Maybe< bool > DefineOwnProperty(Local< Context > context, Local< Name > key, Local< Value > value, PropertyAttribute attributes=None)
void * GetAlignedPointerFromInternalField(int index)
Definition: v8-object.h:787
void * GetAlignedPointerFromEmbedderDataInCreationContext(int index)
MaybeLocal< Value > GetRealNamedPropertyInPrototypeChain(Local< Context > context, Local< Name > key)
static int InternalFieldCount(const BasicTracedReference< Object > &object)
Definition: v8-object.h:476
Maybe< bool > Has(Local< Context > context, Local< Value > key)
bool IsCodeLike(Isolate *isolate) const
Maybe< bool > Set(Local< Context > context, Local< Value > key, Local< Value > value)
MaybeLocal< Value > CallAsConstructor(Local< Context > context, int argc, Local< Value > argv[])
static void * GetAlignedPointerFromInternalField(const PersistentBase< Object > &object, int index)
Definition: v8-object.h:506
Maybe< bool > CreateDataProperty(Local< Context > context, Local< Name > key, Local< Value > value)
Maybe< bool > HasRealIndexedProperty(Local< Context > context, uint32_t index)
Isolate * GetIsolate()
bool IsApiWrapper() const
MaybeLocal< Value > Get(Local< Context > context, uint32_t index)
Local< Data > GetInternalField(int index)
Definition: v8-object.h:741
Maybe< PropertyAttribute > GetRealNamedPropertyAttributes(Local< Context > context, Local< Name > key)
static Isolate * GetIsolate(const TracedReference< Object > &handle)
Definition: v8-object.h:690
Local< Context > GetCreationContextChecked()
MaybeLocal< Context > GetCreationContext()
MaybeLocal< Value > Get(Local< Context > context, Local< Value > key)
static Local< Object > New(Isolate *isolate)
int GetIdentityHash()
void SetInternalField(int index, Local< Data > data)
Maybe< bool > Set(Local< Context > context, uint32_t index, Local< Value > value)
Maybe< bool > HasOwnProperty(Local< Context > context, Local< Name > key)
Maybe< bool > Has(Local< Context > context, uint32_t index)
MaybeLocal< Array > PreviewEntries(bool *is_key_value)
static MaybeLocal< Context > GetCreationContext(const PersistentBase< Object > &object)
Definition: v8-object.h:622
bool IsCallable() const
void SetAlignedPointerInInternalFields(int argc, int indices[], void *values[])
Maybe< bool > HasRealNamedCallbackProperty(Local< Context > context, Local< Name > key)
static void * GetAlignedPointerFromInternalField(const BasicTracedReference< Object > &object, int index)
Definition: v8-object.h:513
Maybe< bool > HasOwnProperty(Local< Context > context, uint32_t index)
static Local< Object > New(Isolate *isolate, Local< Value > prototype_or_null, Local< Name > *names, Local< Value > *values, size_t length)
bool HasIndexedLookupInterceptor() const
Definition: v8-persistent-handle.h:91
Definition: v8-object.h:29
static Private * Cast(Data *data)
Definition: v8-object.h:808
Local< Value > Name() const
static Local< Private > ForApi(Isolate *isolate, Local< String > name)
static Local< Private > New(Isolate *isolate, Local< String > name=Local< String >())
Definition: v8-function-callback.h:174
Definition: v8-object.h:89
void set_enumerable(bool enumerable)
bool has_writable() const
bool has_value() const
Local< Value > get() const
Local< Value > set() const
PropertyDescriptor(Local< Value > get, Local< Value > set)
bool has_enumerable() const
PropertyDescriptor(Local< Value > value)
PrivateData * get_private() const
Definition: v8-object.h:125
void operator=(const PropertyDescriptor &)=delete
Local< Value > value() const
bool enumerable() const
PropertyDescriptor(Local< Value > value, bool writable)
void set_configurable(bool configurable)
PropertyDescriptor(const PropertyDescriptor &)=delete
bool has_configurable() const
bool configurable() const
Definition: v8-primitive.h:124
Definition: v8-traced-handle.h:177
Definition: v8-value.h:32
Definition: v8-internal.h:628
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1345
internal::Isolate * IsolateFromNeverReadOnlySpaceObject(Address obj)
uintptr_t Address
Definition: v8-internal.h:31
Definition: libplatform.h:15
IntegrityLevel
Definition: v8-object.h:229
PropertyAttribute
Definition: v8-object.h:137
@ DontEnum
Definition: v8-object.h:143
@ None
Definition: v8-object.h:139
@ DontDelete
Definition: v8-object.h:145
@ ReadOnly
Definition: v8-object.h:141
KeyCollectionMode
Definition: v8-object.h:212
void(*)(Local< Name > property, const PropertyCallbackInfo< Value > &info) AccessorNameGetterCallback
Definition: v8-object.h:156
SideEffectType
Definition: v8-object.h:199
AccessControl
Definition: v8-object.h:173
@ DEFAULT
Definition: v8-object.h:174
IndexFilter
Definition: v8-object.h:218
KeyConversionMode
Definition: v8-object.h:224
void(*)(Local< String > property, Local< Value > value, const PropertyCallbackInfo< void > &info) AccessorSetterCallback
Definition: v8-object.h:160
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) AccessorNameSetterCallback
Definition: v8-object.h:163
void(*)(Local< String > property, const PropertyCallbackInfo< Value > &info) AccessorGetterCallback
Definition: v8-object.h:154
PropertyFilter
Definition: v8-object.h:180
@ ONLY_CONFIGURABLE
Definition: v8-object.h:184
@ SKIP_SYMBOLS
Definition: v8-object.h:186
@ ONLY_WRITABLE
Definition: v8-object.h:182
@ ALL_PROPERTIES
Definition: v8-object.h:181
@ SKIP_STRINGS
Definition: v8-object.h:185
@ ONLY_ENUMERABLE
Definition: v8-object.h:183
#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