1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
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/. */
7 /* JS Array interface. */
16 /* 2^32-2, inclusive */
17 const uint32_t MAX_ARRAY_INDEX
= 4294967294u;
21 js_IdIsIndex(jsid id
, uint32_t* indexp
)
23 if (JSID_IS_INT(id
)) {
24 int32_t i
= JSID_TO_INT(id
);
26 *indexp
= (uint32_t)i
;
30 if (MOZ_UNLIKELY(!JSID_IS_STRING(id
)))
33 return js::StringIsArrayIndex(JSID_TO_ATOM(id
), indexp
);
37 js_InitArrayClass(JSContext
* cx
, js::HandleObject obj
);
40 js_InitContextBusyArrayTable(JSContext
* cx
);
46 /* Create a dense array with no capacity allocated, length set to 0. */
47 extern ArrayObject
* JS_FASTCALL
48 NewDenseEmptyArray(JSContext
* cx
, JSObject
* proto
= nullptr,
49 NewObjectKind newKind
= GenericObject
);
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.
55 extern ArrayObject
* JS_FASTCALL
56 NewDenseUnallocatedArray(ExclusiveContext
* cx
, uint32_t length
, JSObject
* proto
= nullptr,
57 NewObjectKind newKind
= GenericObject
);
60 * Create a dense array with length and capacity == |length|, initialized length set to 0,
61 * but with only |EagerAllocationMaxLength| elements allocated.
63 extern ArrayObject
* JS_FASTCALL
64 NewDensePartlyAllocatedArray(ExclusiveContext
* cx
, uint32_t length
, JSObject
* proto
= nullptr,
65 NewObjectKind newKind
= GenericObject
);
67 /* Create a dense array with length and capacity == 'length', initialized length set to 0. */
68 extern ArrayObject
* JS_FASTCALL
69 NewDenseFullyAllocatedArray(ExclusiveContext
* cx
, uint32_t length
, JSObject
* proto
= nullptr,
70 NewObjectKind newKind
= GenericObject
);
72 enum AllocatingBehaviour
{
73 NewArray_Unallocating
,
74 NewArray_PartlyAllocating
,
75 NewArray_FullyAllocating
79 * Create a dense array with a set length, but only allocates space for the
80 * contents if the length is not excessive.
83 NewDenseArray(ExclusiveContext
* cx
, uint32_t length
, HandleTypeObject type
,
84 AllocatingBehaviour allocating
);
86 /* Create a dense array with a copy of the dense array elements in src. */
88 NewDenseCopiedArray(JSContext
* cx
, uint32_t length
, HandleArrayObject src
,
89 uint32_t elementOffset
, JSObject
* proto
= nullptr);
91 /* Create a dense array from the given array values, which must be rooted */
93 NewDenseCopiedArray(JSContext
* cx
, uint32_t length
, const Value
* values
, JSObject
* proto
= nullptr,
94 NewObjectKind newKind
= GenericObject
);
96 /* Create a dense array based on templateObject with the given length. */
98 NewDenseFullyAllocatedArrayWithTemplate(JSContext
* cx
, uint32_t length
, JSObject
* templateObject
);
100 /* Create a dense array with the same copy-on-write elements as another object. */
102 NewDenseCopyOnWriteArray(JSContext
* cx
, HandleArrayObject templateObject
, gc::InitialHeap heap
);
105 * Determines whether a write to the given element on |obj| should fail because
106 * |obj| is an Array with a non-writable length, and writing that element would
107 * increase the length of the array.
110 WouldDefinePastNonwritableLength(ExclusiveContext
* cx
,
111 HandleObject obj
, uint32_t index
, bool strict
,
115 * Canonicalize |vp| to a uint32_t value potentially suitable for use as an
119 CanonicalizeArrayLengthValue(JSContext
* cx
, HandleValue v
, uint32_t* canonicalized
);
122 GetLengthProperty(JSContext
* cx
, HandleObject obj
, uint32_t* lengthp
);
125 SetLengthProperty(JSContext
* cx
, HandleObject obj
, double length
);
128 ObjectMayHaveExtraIndexedProperties(JSObject
* obj
);
131 * Copy 'length' elements from aobj to vp.
133 * This function assumes 'length' is effectively the result of calling
134 * js_GetLengthProperty on aobj. vp must point to rooted memory.
137 GetElements(JSContext
* cx
, HandleObject aobj
, uint32_t length
, js::Value
* vp
);
139 /* Natives exposed for optimization by the interpreter and JITs. */
142 array_sort(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
145 array_push(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
148 array_pop(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
151 array_splice(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
154 array_splice_impl(JSContext
* cx
, unsigned argc
, js::Value
* vp
, bool pop
);
157 array_concat(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
159 template <bool Locale
>
161 ArrayJoin(JSContext
* cx
, HandleObject obj
, HandleLinearString sepstr
, uint32_t length
);
164 array_concat_dense(JSContext
* cx
, Handle
<ArrayObject
*> arr1
, Handle
<ArrayObject
*> arr2
,
165 Handle
<ArrayObject
*> result
);
168 array_join(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
171 array_join_impl(JSContext
* cx
, HandleValue array
, HandleString sep
);
174 ArrayShiftMoveElements(ArrayObject
* obj
);
177 array_shift(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
180 array_unshift(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
183 array_slice(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
186 * Append the given (non-hole) value to the end of an array. The array must be
187 * a newborn array -- that is, one which has not been exposed to script for
188 * arbitrary manipulation. (This method optimizes on the assumption that
189 * extending the array to accommodate the element will never make the array
190 * sparse, which requires that the array be completely filled.)
193 NewbornArrayPush(JSContext
* cx
, HandleObject obj
, const Value
& v
);
196 ArrayConstructorOneArg(JSContext
* cx
, HandleTypeObject type
, int32_t lengthInt
);
202 js_ArrayInfo(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
205 /* Array constructor native. Exposed only so the JIT can know its address. */
207 js_Array(JSContext
* cx
, unsigned argc
, js::Value
* vp
);
209 #endif /* jsarray_h */