v8 10.2.154 (node 18.16.0)
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
178 PROHIBITS_OVERWRITING = 1 << 2
180
190 SKIP_SYMBOLS = 16
192
203enum class SideEffectType {
207};
208
217
223
229
234
238class V8_EXPORT Object : public Value {
239 public:
245 Local<Value> key, Local<Value> value);
246
248 Local<Value> value);
249
250 // Implements CreateDataProperty (ECMA-262, 7.3.4).
251 //
252 // Defines a configurable, writable, enumerable property with the given value
253 // on the object unless the property already exists and is not configurable
254 // or the object is not extensible.
255 //
256 // Returns true on success.
258 Local<Name> key,
259 Local<Value> value);
261 uint32_t index,
262 Local<Value> value);
263
264 // Implements DefineOwnProperty.
265 //
266 // In general, CreateDataProperty will be faster, however, does not allow
267 // for specifying attributes.
268 //
269 // Returns true on success.
271 Local<Context> context, Local<Name> key, Local<Value> value,
272 PropertyAttribute attributes = None);
273
274 // Implements Object.DefineProperty(O, P, Attributes), see Ecma-262 19.1.2.4.
275 //
276 // The defineProperty function is used to add an own property or
277 // update the attributes of an existing own property of an object.
278 //
279 // Both data and accessor descriptors can be used.
280 //
281 // In general, CreateDataProperty is faster, however, does not allow
282 // for specifying attributes or an accessor descriptor.
283 //
284 // The PropertyDescriptor can change when redefining a property.
285 //
286 // Returns true on success.
288 Local<Context> context, Local<Name> key, PropertyDescriptor& descriptor);
289
291 Local<Value> key);
292
294 uint32_t index);
295
302 Local<Context> context, Local<Value> key);
303
308 Local<Context> context, Local<Name> key);
309
326 Local<Value> key);
327
329 Local<Value> key);
330
332
334 uint32_t index);
335
340 Local<Context> context, Local<Name> name,
342 AccessorNameSetterCallback setter = nullptr,
344 AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
345 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
346 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
347
350 PropertyAttribute attribute = None,
351 AccessControl settings = DEFAULT);
352
358 Local<Context> context, Local<Name> name,
360 AccessorNameSetterCallback setter = nullptr,
361 Local<Value> data = Local<Value>(), PropertyAttribute attributes = None,
362 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
363 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
364
374 Local<Context> context, Local<Name> name,
376 PropertyAttribute attributes = None,
377 SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
378 SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
379
388 Local<Value> value);
391
399 Local<Context> context);
401 Local<Context> context, KeyCollectionMode mode,
402 PropertyFilter property_filter, IndexFilter index_filter,
403 KeyConversionMode key_conversion = KeyConversionMode::kKeepNumbers);
404
411 Local<Context> context);
412
420 Local<Context> context, PropertyFilter filter,
421 KeyConversionMode key_conversion = KeyConversionMode::kKeepNumbers);
422
429
436 Local<Value> prototype);
437
443
450 Local<Context> context);
451
456
461
464
467 const PersistentBase<Object>& object) {
468 return object.val_->InternalFieldCount();
469 }
470
473 const BasicTracedReference<Object>& object) {
474 return object->InternalFieldCount();
475 }
476
478 V8_INLINE Local<Value> GetInternalField(int index);
479
481 void SetInternalField(int index, Local<Value> value);
482
488 V8_INLINE void* GetAlignedPointerFromInternalField(int index);
489
492 const PersistentBase<Object>& object, int index) {
493 return object.val_->GetAlignedPointerFromInternalField(index);
494 }
495
498 const BasicTracedReference<Object>& object, int index) {
499 return object->GetAlignedPointerFromInternalField(index);
500 }
501
507 void SetAlignedPointerInInternalField(int index, void* value);
508 void SetAlignedPointerInInternalFields(int argc, int indices[],
509 void* values[]);
510
517 Local<Name> key);
519 uint32_t index);
534 Local<Name> key);
536 Local<Context> context, uint32_t index);
538 Local<Context> context, Local<Name> key);
539
545 Local<Context> context, Local<Name> key);
546
554 Local<Name> key);
555
562 Local<Context> context, Local<Name> key);
563
570 Local<Context> context, Local<Name> key);
571
574
577
586
591 // TODO(dcarney): take an isolate and optionally bail out?
593
597 V8_DEPRECATED("Use MaybeLocal<Context> GetCreationContext()")
598 Local<Context> CreationContext();
599 MaybeLocal<Context> GetCreationContext();
600
604 Local<Context> GetCreationContextChecked();
605
608 "Use MaybeLocal<Context> GetCreationContext(const "
609 "PersistentBase<Object>& object)")
610 static Local<Context> CreationContext(const PersistentBase<Object>& object);
611 V8_INLINE static MaybeLocal<Context> GetCreationContext(
612 const PersistentBase<Object>& object) {
613 return object.val_->GetCreationContext();
614 }
615
621 bool IsCallable() const;
622
626 bool IsConstructor() const;
627
635 bool IsApiWrapper() const;
636
642 bool IsUndetectable() const;
643
649 Local<Value> recv,
650 int argc,
651 Local<Value> argv[]);
652
659 Local<Context> context, int argc, Local<Value> argv[]);
660
665
676
677 static Local<Object> New(Isolate* isolate);
678
687 static Local<Object> New(Isolate* isolate, Local<Value> prototype_or_null,
688 Local<Name>* names, Local<Value>* values,
689 size_t length);
690
691 V8_INLINE static Object* Cast(Value* obj);
692
701 bool IsCodeLike(Isolate* isolate) const;
702
703 private:
704 Object();
705 static void CheckCast(Value* obj);
706 Local<Value> SlowGetInternalField(int index);
707 void* SlowGetAlignedPointerFromInternalField(int index);
708};
709
710// --- Implementation ---
711
713#ifndef V8_ENABLE_CHECKS
714 using A = internal::Address;
715 using I = internal::Internals;
716 A obj = *reinterpret_cast<A*>(this);
717 // Fast path: If the object is a plain JSObject, which is the common case, we
718 // know where to find the internal fields and can return the value directly.
719 int instance_type = I::GetInstanceType(obj);
720 if (v8::internal::CanHaveInternalField(instance_type)) {
721 int offset = I::kJSObjectHeaderSize + (I::kEmbedderDataSlotSize * index);
722 A value = I::ReadRawField<A>(obj, offset);
723#ifdef V8_COMPRESS_POINTERS
724 // We read the full pointer value and then decompress it in order to avoid
725 // dealing with potential endiannes issues.
726 value = I::DecompressTaggedAnyField(obj, static_cast<uint32_t>(value));
727#endif
728 internal::Isolate* isolate =
730 A* result = HandleScope::CreateHandle(isolate, value);
731 return Local<Value>(reinterpret_cast<Value*>(result));
732 }
733#endif
734 return SlowGetInternalField(index);
735}
736
738#if !defined(V8_ENABLE_CHECKS)
739 using A = internal::Address;
740 using I = internal::Internals;
741 A obj = *reinterpret_cast<A*>(this);
742 // Fast path: If the object is a plain JSObject, which is the common case, we
743 // know where to find the internal fields and can return the value directly.
744 auto instance_type = I::GetInstanceType(obj);
745 if (v8::internal::CanHaveInternalField(instance_type)) {
746 int offset = I::kJSObjectHeaderSize + (I::kEmbedderDataSlotSize * index);
747#ifdef V8_SANDBOXED_EXTERNAL_POINTERS
748 offset += I::kEmbedderDataSlotRawPayloadOffset;
749#endif
750 internal::Isolate* isolate = I::GetIsolateForSandbox(obj);
751 A value = I::ReadExternalPointerField(
752 isolate, obj, offset, internal::kEmbedderDataSlotPayloadTag);
753 return reinterpret_cast<void*>(value);
754 }
755#endif
756 return SlowGetAlignedPointerFromInternalField(index);
757}
758
760#ifdef V8_ENABLE_CHECKS
761 CheckCast(data);
762#endif
763 return reinterpret_cast<Private*>(data);
764}
765
767#ifdef V8_ENABLE_CHECKS
768 CheckCast(value);
769#endif
770 return static_cast<Object*>(value);
771}
772
773} // namespace v8
774
775#endif // INCLUDE_V8_OBJECT_H_
static internal::Address * CreateHandle(internal::Isolate *isolate, internal::Address value)
bool HasNamedLookupInterceptor() const
static V8_INLINE int InternalFieldCount(const PersistentBase< Object > &object)
Definition v8-object.h:466
V8_WARN_UNUSED_RESULT MaybeLocal< Array > GetPropertyNames(Local< Context > context, KeyCollectionMode mode, PropertyFilter property_filter, IndexFilter index_filter, KeyConversionMode key_conversion=KeyConversionMode::kKeepNumbers)
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)
V8_WARN_UNUSED_RESULT MaybeLocal< Array > GetOwnPropertyNames(Local< Context > context)
static V8_INLINE int InternalFieldCount(const BasicTracedReference< Object > &object)
Definition v8-object.h:472
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)
V8_WARN_UNUSED_RESULT Maybe< bool > SetLazyDataProperty(Local< Context > context, Local< Name > name, AccessorNameGetterCallback getter, Local< Value > data=Local< Value >(), PropertyAttribute attributes=None, SideEffectType getter_side_effect_type=SideEffectType::kHasSideEffect, SideEffectType setter_side_effect_type=SideEffectType::kHasSideEffect)
Maybe< bool > SetIntegrityLevel(Local< Context > context, IntegrityLevel level)
V8_WARN_UNUSED_RESULT Maybe< bool > SetNativeDataProperty(Local< Context > context, Local< Name > name, AccessorNameGetterCallback getter, AccessorNameSetterCallback setter=nullptr, Local< Value > data=Local< Value >(), PropertyAttribute attributes=None, SideEffectType getter_side_effect_type=SideEffectType::kHasSideEffect, SideEffectType setter_side_effect_type=SideEffectType::kHasSideEffect)
static V8_INLINE void * GetAlignedPointerFromInternalField(const BasicTracedReference< Object > &object, int index)
Definition v8-object.h:497
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:737
bool IsApiWrapper() const
void SetAccessorProperty(Local< Name > name, Local< Function > getter, Local< Function > setter=Local< Function >(), PropertyAttribute attribute=None, AccessControl settings=DEFAULT)
Local< String > GetConstructorName()
V8_WARN_UNUSED_RESULT MaybeLocal< Array > GetOwnPropertyNames(Local< Context > context, PropertyFilter filter, KeyConversionMode key_conversion=KeyConversionMode::kKeepNumbers)
Maybe< bool > DeletePrivate(Local< Context > context, Local< Private > key)
V8_WARN_UNUSED_RESULT Maybe< PropertyAttribute > GetRealNamedPropertyAttributes(Local< Context > context, Local< Name > key)
Maybe< bool > HasPrivate(Local< Context > context, Local< Private > key)
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
V8_WARN_UNUSED_RESULT Maybe< bool > SetPrototype(Local< Context > context, Local< Value > prototype)
V8_WARN_UNUSED_RESULT Maybe< bool > SetAccessor(Local< Context > context, Local< Name > name, AccessorNameGetterCallback getter, AccessorNameSetterCallback setter=nullptr, MaybeLocal< Value > data=MaybeLocal< Value >(), AccessControl settings=DEFAULT, PropertyAttribute attribute=None, SideEffectType getter_side_effect_type=SideEffectType::kHasSideEffect, SideEffectType setter_side_effect_type=SideEffectType::kHasSideEffect)
V8_WARN_UNUSED_RESULT MaybeLocal< String > ObjectProtoToString(Local< Context > context)
bool IsConstructor() const
static V8_INLINE Object * Cast(Value *obj)
Definition v8-object.h:766
void SetInternalField(int index, Local< Value > value)
Local< Value > GetPrototype()
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)
V8_WARN_UNUSED_RESULT MaybeLocal< Array > GetPropertyNames(Local< Context > context)
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[])
Maybe< bool > SetPrivate(Local< Context > context, Local< Private > key, Local< Value > value)
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:491
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)
Local< Object > FindInstanceInPrototypeChain(Local< FunctionTemplate > tmpl)
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)
MaybeLocal< Value > GetPrivate(Local< Context > context, Local< Private > 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)
V8_INLINE Local< Value > GetInternalField(int index)
Definition v8-object.h:712
int InternalFieldCount() const
Local< Value > Name() const
static Local< Private > ForApi(Isolate *isolate, Local< String > name)
static V8_INLINE Private * Cast(Data *data)
Definition v8-object.h:759
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
V8_EXPORT bool CanHaveInternalField(int instance_type)
V8_EXPORT internal::Isolate * IsolateFromNeverReadOnlySpaceObject(Address obj)
uintptr_t Address
Definition v8-internal.h:29
@ kEmbedderDataSlotPayloadTag
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:174
@ ALL_CAN_READ
Definition v8-object.h:176
@ PROHIBITS_OVERWRITING
Definition v8-object.h:178
@ ALL_CAN_WRITE
Definition v8-object.h:177
@ DEFAULT
Definition v8-object.h:175
SideEffectType
Definition v8-object.h:203
PropertyFilter
Definition v8-object.h:184
@ SKIP_SYMBOLS
Definition v8-object.h:190
@ ALL_PROPERTIES
Definition v8-object.h:185
@ ONLY_CONFIGURABLE
Definition v8-object.h:188
@ ONLY_WRITABLE
Definition v8-object.h:186
@ ONLY_ENUMERABLE
Definition v8-object.h:187
@ SKIP_STRINGS
Definition v8-object.h:189
void(*)(Local< Name > property, Local< Value > value, const PropertyCallbackInfo< void > &info) AccessorNameSetterCallback
Definition v8-object.h:163
IntegrityLevel
Definition v8-object.h:233
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:222
KeyCollectionMode
Definition v8-object.h:216
KeyConversionMode
Definition v8-object.h:228
void(*)(Local< String > property, const PropertyCallbackInfo< Value > &info) AccessorGetterCallback
Definition v8-object.h:154
#define V8_EXPORT
Definition v8config.h:578
#define V8_INLINE
Definition v8config.h:425
#define V8_DEPRECATED(message)
Definition v8config.h:462
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:499