v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
js-array.h
Go to the documentation of this file.
1// Copyright 2017 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 V8_OBJECTS_JS_ARRAY_H_
6#define V8_OBJECTS_JS_ARRAY_H_
7
11
12// Has to be the last include (doesn't have include guards):
14
15namespace v8 {
16namespace internal {
17
18#include "torque-generated/src/objects/js-array-tq.inc"
19
20// The JSArray describes JavaScript Arrays
21// Such an array can be in one of two modes:
22// - fast, backing storage is a FixedArray and length <= elements.length();
23// Please note: push and pop can be used to grow and shrink the array.
24// - slow, backing storage is a HashTable with numbers as keys.
25class JSArray : public TorqueGeneratedJSArray<JSArray, JSObject> {
26 public:
27 // [length]: The length property.
30
31 // Acquire/release semantics on this field are explicitly forbidden to avoid
32 // confusion, since the default setter uses relaxed semantics. If
33 // acquire/release semantics ever become necessary, the default setter should
34 // be reverted to non-atomic behavior, and setters with explicit tags
35 // introduced and used when required.
37 AcquireLoadTag tag) const = delete;
40
41 // Overload the length setter to skip write barrier when the length
42 // is set to a smi. This matches the set function on FixedArray.
43 inline void set_length(Tagged<Smi> length);
44
45 static bool MayHaveReadOnlyLength(Tagged<Map> js_array_map);
48 uint32_t index);
49
50 // Initialize the array with the given capacity. The function may
51 // fail due to out-of-memory situations, but only if the requested
52 // capacity is non-zero.
54 int capacity, int length = 0);
55
56 // If the JSArray has fast elements, and new_length would result in
57 // normalization, returns true.
59 static inline bool SetLengthWouldNormalize(Heap* heap, uint32_t new_length);
60
61 // Initializes the array to a certain length.
63 uint32_t length);
64
65 // Set the content of the array to the content of storage.
66 static inline void SetContent(DirectHandle<JSArray> array,
68
69 // ES6 9.4.2.1
72 PropertyDescriptor* desc, Maybe<ShouldThrow> should_throw);
73
74 static bool AnythingToArrayLength(Isolate* isolate,
75 DirectHandle<Object> length_object,
76 uint32_t* output);
79 Maybe<ShouldThrow> should_throw);
80
81 // Support for Array.prototype.join().
82 // Writes a fixed array of strings and separators to a single destination
83 // string. This helpers assumes the fixed array encodes separators in two
84 // ways:
85 // 1) Explicitly with a smi, whos value represents the number of repeated
86 // separators.
87 // 2) Implicitly between two consecutive strings a single separator.
88 //
89 // In addition repeated strings are represented by a negative smi, indicating
90 // how many times the previously written string has to be repeated.
91 //
92 // Here are some input/output examples given the separator string is ',':
93 //
94 // [1, 'hello', 2, 'world', 1] => ',hello,,world,'
95 // ['hello', 'world'] => 'hello,world'
96 // ['hello', -2, 'world'] => 'hello,hello,hello,world'
97 //
98 // To avoid any allocations, this helper assumes the destination string is the
99 // exact length necessary to write the strings and separators from the fixed
100 // array.
101 // Since this is called via ExternalReferences, it uses raw Address values:
102 // - {raw_fixed_array} is a tagged FixedArray pointer.
103 // - {raw_separator} and {raw_dest} are tagged String pointers.
104 // - Returns a tagged String pointer.
106 Address raw_fixed_array,
107 intptr_t length,
108 Address raw_separator,
109 Address raw_dest);
110
111 // Checks whether the Array has the current realm's Array.prototype as its
112 // prototype. This function is best-effort and only gives a conservative
113 // approximation, erring on the side of false, in particular with respect
114 // to Proxies and objects with a hidden prototype.
115 inline bool HasArrayPrototype(Isolate* isolate);
116
117 // Dispatched behavior.
120
121 // Number of element slots to pre-allocate for an empty array.
122 static const int kPreallocatedArrayElements = 4;
123
124 static const int kLengthDescriptorIndex = 0;
125
126 // Max. number of elements being copied in Array builtins.
127 static const int kMaxCopyElements = 100;
128
129 // Valid array indices range from +0 <= i < 2^32 - 1 (kMaxUInt32).
130 static constexpr uint32_t kMaxArrayLength = JSObject::kMaxElementCount;
131 static constexpr uint32_t kMaxArrayIndex = JSObject::kMaxElementIndex;
132 static_assert(kMaxArrayLength == kMaxUInt32);
133 static_assert(kMaxArrayIndex == kMaxUInt32 - 1);
134
135 // This constant is somewhat arbitrary. Any large enough value would work.
136 static constexpr uint32_t kMaxFastArrayLength =
137 V8_LOWER_LIMITS_MODE_BOOL ? (8 * 1024 * 1024) : (32 * 1024 * 1024);
138 static_assert(kMaxFastArrayLength <= kMaxArrayLength);
140
141 // Min. stack size for detecting an Array.prototype.join() call cycle.
142 static const uint32_t kMinJoinStackSize = 2;
143
145 (kMaxRegularHeapObjectSize - sizeof(FixedArray) - kHeaderSize -
146 sizeof(AllocationMemento)) >>
148
150};
151
152// The JSArrayIterator describes JavaScript Array Iterators Objects, as
153// defined in ES section #sec-array-iterator-objects.
155 : public TorqueGeneratedJSArrayIterator<JSArrayIterator, JSObject> {
156 public:
159
160 // [kind]: the [[ArrayIterationKind]] inobject property.
161 inline IterationKind kind() const;
162 inline void set_kind(IterationKind kind);
163
164 private:
165 DECL_INT_ACCESSORS(raw_kind)
166
168};
169
170// Helper class for JSArrays that are template literal objects
172 : public TorqueGeneratedTemplateLiteralObject<TemplateLiteralObject,
173 JSArray> {
174 public:
175 private:
177};
178
179} // namespace internal
180} // namespace v8
181
183
184#endif // V8_OBJECTS_JS_ARRAY_H_
Builtins::Kind kind
Definition builtins.cc:40
bool SetLengthWouldNormalize(uint32_t new_length)
Definition objects.cc:4891
static bool AnythingToArrayLength(Isolate *isolate, DirectHandle< Object > length_object, uint32_t *output)
Definition objects.cc:3188
static constexpr uint32_t kMaxArrayIndex
Definition js-array.h:131
static V8_EXPORT_PRIVATE Maybe< bool > SetLength(DirectHandle< JSArray > array, uint32_t length)
Definition objects.cc:4812
void set_length(Tagged< Number > value, ReleaseStoreTag tag, WriteBarrierMode mode=UPDATE_WRITE_BARRIER)=delete
static const int kInitialMaxFastElementArray
Definition js-array.h:144
static V8_EXPORT_PRIVATE void Initialize(DirectHandle< JSArray > array, int capacity, int length=0)
Definition objects.cc:4804
static constexpr uint32_t kMaxArrayLength
Definition js-array.h:130
static const int kPreallocatedArrayElements
Definition js-array.h:122
static const uint32_t kMinJoinStackSize
Definition js-array.h:142
static bool MayHaveReadOnlyLength(Tagged< Map > js_array_map)
Definition objects.cc:4949
static bool WouldChangeReadOnlyLength(DirectHandle< JSArray > array, uint32_t index)
Definition objects.cc:4976
static Address ArrayJoinConcatToSequentialString(Isolate *isolate, Address raw_fixed_array, intptr_t length, Address raw_separator, Address raw_dest)
Definition objects.cc:4246
static void SetContent(DirectHandle< JSArray > array, DirectHandle< FixedArrayBase > storage)
static const int kLengthDescriptorIndex
Definition js-array.h:124
bool HasArrayPrototype(Isolate *isolate)
static bool HasReadOnlyLength(DirectHandle< JSArray > array)
Definition objects.cc:4962
static const int kMaxCopyElements
Definition js-array.h:127
static V8_WARN_UNUSED_RESULT Maybe< bool > ArraySetLength(Isolate *isolate, DirectHandle< JSArray > a, PropertyDescriptor *desc, Maybe< ShouldThrow > should_throw)
Definition objects.cc:3224
static constexpr uint32_t kMaxFastArrayLength
Definition js-array.h:136
static V8_WARN_UNUSED_RESULT Maybe< bool > DefineOwnProperty(Isolate *isolate, DirectHandle< JSArray > o, DirectHandle< Object > name, PropertyDescriptor *desc, Maybe< ShouldThrow > should_throw)
Definition objects.cc:3120
#define V8_LOWER_LIMITS_MODE_BOOL
Definition globals.h:129
constexpr int kMaxRegularHeapObjectSize
Definition globals.h:680
@ UPDATE_WRITE_BARRIER
Definition objects.h:55
constexpr int kDoubleSizeLog2
Definition globals.h:421
constexpr uint32_t kMaxUInt32
Definition globals.h:387
static constexpr int kMaxFixedArrayCapacity
Definition fixed-array.h:32
#define DECL_ACCESSORS(name,...)
#define DECL_VERIFIER(Name)
#define DECL_RELAXED_GETTER(name,...)
#define DECL_PRINTER(Name)
#define DECL_INT_ACCESSORS(name)
#define TQ_OBJECT_CONSTRUCTORS(Type)
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671