Debian Codegraph @

js/src/builtin/Array.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /* JS Array interface. */
8
9 #ifndef builtin_Array_h
10 #define builtin_Array_h
11
12 #include "mozilla/Attributes.h"
13
14 #include "vm/JSObject.h"
15
16 namespace js {
17
18 enum class ArraySortResult : uint32_t;
19
20 namespace jit {
21 class TrampolineNativeFrameLayout;
22 }
23
24 class ArrayObject;
25
26 MOZ_ALWAYS_INLINE bool IdIsIndex(jsid id, uint32_t* indexp) {
27 if (id.isInt()) {
28 int32_t i = id.toInt();
29 MOZ_ASSERT(i >= 0);
30 *indexp = uint32_t(i);
31 return true;
32 }
33
34 if (MOZ_UNLIKELY(!id.isAtom())) {
35 return false;
36 }
37
38 JSAtom* atom = id.toAtom();
39 return atom->isIndex(indexp);
40 }
41
42 // The methods below only create dense boxed arrays.
43
44 // Create a dense array with no capacity allocated, length set to 0, in the
45 // normal (i.e. non-tenured) heap.
46 extern ArrayObject* NewDenseEmptyArray(JSContext* cx);
47
48 // Create a dense array with no capacity allocated, length set to 0, in the
49 // tenured heap.
50 extern ArrayObject* NewTenuredDenseEmptyArray(JSContext* cx);
51
52 // Create a dense array with a set length, but without allocating space for the
53 // contents. This is useful, e.g., when accepting length from the user.
54 extern ArrayObject* NewDenseUnallocatedArray(
55 JSContext* cx, uint32_t length, NewObjectKind newKind = GenericObject);
56
57 // Create a dense array with length and capacity == 'length', initialized length
58 // set to 0.
59 extern ArrayObject* NewDenseFullyAllocatedArray(
60 JSContext* cx, uint32_t length, NewObjectKind newKind = GenericObject,
61 gc::AllocSite* site = nullptr);
62
63 // Create a dense array with length == 'length', initialized length set to 0,
64 // and capacity == 'length' clamped to EagerAllocationMaxLength.
65 extern ArrayObject* NewDensePartlyAllocatedArray(
66 JSContext* cx, uint32_t length, NewObjectKind newKind = GenericObject,
67 gc::AllocSite* site = nullptr);
68
69 // Like NewDensePartlyAllocatedArray, but the array will have |proto| as
70 // prototype (or Array.prototype if |proto| is nullptr).
71 extern ArrayObject* NewDensePartlyAllocatedArrayWithProto(JSContext* cx,
72 uint32_t length,
73 HandleObject proto);
74
75 // Create a dense array from the given array values, which must be rooted.
76 extern ArrayObject* NewDenseCopiedArray(JSContext* cx, uint32_t length,
77 const Value* values,
78 NewObjectKind newKind = GenericObject);
79
80 // Create a dense array from the given (linear)string values, which must be
81 // rooted
82 extern ArrayObject* NewDenseCopiedArray(JSContext* cx, uint32_t length,
83 JSLinearString** values,
84 NewObjectKind newKind = GenericObject);
85
86 // Like NewDenseCopiedArray, but the array will have |proto| as prototype (or
87 // Array.prototype if |proto| is nullptr).
88 extern ArrayObject* NewDenseCopiedArrayWithProto(JSContext* cx, uint32_t length,
89 const Value* values,
90 HandleObject proto);
91
92 // Create a dense array with the given shape and length.
93 extern ArrayObject* NewDenseFullyAllocatedArrayWithShape(
94 JSContext* cx, uint32_t length, Handle<SharedShape*> shape);
95
96 extern ArrayObject* NewArrayWithShape(JSContext* cx, uint32_t length,
97 Handle<Shape*> shape);
98
99 extern bool ToLength(JSContext* cx, HandleValue v, uint64_t* out);
100
101 extern bool GetLengthProperty(JSContext* cx, HandleObject obj,
102 uint64_t* lengthp);
103
104 extern bool SetLengthProperty(JSContext* cx, HandleObject obj, uint32_t length);
105
106 /*
107 * Copy 'length' elements from aobj to vp.
108 *
109 * This function assumes 'length' is effectively the result of calling
110 * GetLengthProperty on aobj. vp must point to rooted memory.
111 */
112 extern bool GetElements(JSContext* cx, HandleObject aobj, uint32_t length,
113 js::Value* vp);
114
115 /* Natives exposed for optimization by the interpreter and JITs. */
116
117 extern bool array_includes(JSContext* cx, unsigned argc, js::Value* vp);
118 extern bool array_indexOf(JSContext* cx, unsigned argc, js::Value* vp);
119 extern bool array_lastIndexOf(JSContext* cx, unsigned argc, js::Value* vp);
120 extern bool array_pop(JSContext* cx, unsigned argc, js::Value* vp);
121 extern bool array_join(JSContext* cx, unsigned argc, js::Value* vp);
122 extern bool array_sort(JSContext* cx, unsigned argc, js::Value* vp);
123
124 extern void ArrayShiftMoveElements(ArrayObject* arr);
125
126 extern JSObject* ArraySliceDense(JSContext* cx, HandleObject obj, int32_t begin,
127 int32_t end, HandleObject result);
128
129 extern JSObject* ArgumentsSliceDense(JSContext* cx, HandleObject obj,
130 int32_t begin, int32_t end,
131 HandleObject result);
132
133 extern ArrayObject* NewArrayWithNullProto(JSContext* cx);
134
135 /*
136 * Append the given (non-hole) value to the end of an array. The array must be
137 * a newborn array -- that is, one which has not been exposed to script for
138 * arbitrary manipulation. (This method optimizes on the assumption that
139 * extending the array to accommodate the element will never make the array
140 * sparse, which requires that the array be completely filled.)
141 */
142 extern bool NewbornArrayPush(JSContext* cx, HandleObject obj, const Value& v);
143
144 extern ArrayObject* ArrayConstructorOneArg(JSContext* cx,
145 Handle<ArrayObject*> templateObject,
146 int32_t lengthInt,
147 gc::AllocSite* site);
148
149 #ifdef DEBUG
150 extern bool ArrayInfo(JSContext* cx, unsigned argc, Value* vp);
151 #endif
152
153 /* Array constructor native. Exposed only so the JIT can know its address. */
154 extern bool ArrayConstructor(JSContext* cx, unsigned argc, Value* vp);
155
156 // Like Array constructor, but doesn't perform GetPrototypeFromConstructor.
157 extern bool array_construct(JSContext* cx, unsigned argc, Value* vp);
158
159 extern JSString* ArrayToSource(JSContext* cx, HandleObject obj);
160
161 extern bool IsCrossRealmArrayConstructor(JSContext* cx, JSObject* obj,
162 bool* result);
163
164 extern bool ObjectMayHaveExtraIndexedOwnProperties(JSObject* obj);
165
166 extern bool ObjectMayHaveExtraIndexedProperties(JSObject* obj);
167
168 extern bool PrototypeMayHaveIndexedProperties(NativeObject* obj);
169
170 // JS::IsArray has multiple overloads, use js::IsArrayFromJit to disambiguate.
171 extern bool IsArrayFromJit(JSContext* cx, HandleObject obj, bool* isArray);
172
173 extern bool ArrayLengthGetter(JSContext* cx, HandleObject obj, HandleId id,
174 MutableHandleValue vp);
175
176 extern bool ArrayLengthSetter(JSContext* cx, HandleObject obj, HandleId id,
177 HandleValue v, ObjectOpResult& result);
178
179 extern ArraySortResult ArraySortFromJit(
180 JSContext* cx, jit::TrampolineNativeFrameLayout* frame);
181
182 bool IsArrayConstructor(const JSObject* obj);
183
184 bool intrinsic_CanOptimizeArraySpecies(JSContext* cx, unsigned argc, Value* vp);
185
186 } /* namespace js */
187
188 #endif /* builtin_Array_h */