Bumping manifests a=b2g-bump
[gecko.git] / js / src / jsarray.h
blobe1314e8fd712cc6d4070309b77081cd898e40594
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. */
9 #ifndef jsarray_h
10 #define jsarray_h
12 #include "jsobj.h"
13 #include "jspubtd.h"
15 namespace js {
16 /* 2^32-2, inclusive */
17 const uint32_t MAX_ARRAY_INDEX = 4294967294u;
20 inline bool
21 js_IdIsIndex(jsid id, uint32_t* indexp)
23 if (JSID_IS_INT(id)) {
24 int32_t i = JSID_TO_INT(id);
25 MOZ_ASSERT(i >= 0);
26 *indexp = (uint32_t)i;
27 return true;
30 if (MOZ_UNLIKELY(!JSID_IS_STRING(id)))
31 return false;
33 return js::StringIsArrayIndex(JSID_TO_ATOM(id), indexp);
36 extern JSObject*
37 js_InitArrayClass(JSContext* cx, js::HandleObject obj);
39 extern bool
40 js_InitContextBusyArrayTable(JSContext* cx);
42 namespace js {
44 class ArrayObject;
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.
82 extern ArrayObject*
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. */
87 extern ArrayObject*
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 */
92 extern ArrayObject*
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. */
97 extern ArrayObject*
98 NewDenseFullyAllocatedArrayWithTemplate(JSContext* cx, uint32_t length, JSObject* templateObject);
100 /* Create a dense array with the same copy-on-write elements as another object. */
101 extern JSObject*
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.
109 extern bool
110 WouldDefinePastNonwritableLength(ExclusiveContext* cx,
111 HandleObject obj, uint32_t index, bool strict,
112 bool* definesPast);
115 * Canonicalize |vp| to a uint32_t value potentially suitable for use as an
116 * array length.
118 extern bool
119 CanonicalizeArrayLengthValue(JSContext* cx, HandleValue v, uint32_t* canonicalized);
121 extern bool
122 GetLengthProperty(JSContext* cx, HandleObject obj, uint32_t* lengthp);
124 extern bool
125 SetLengthProperty(JSContext* cx, HandleObject obj, double length);
127 extern bool
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.
136 extern bool
137 GetElements(JSContext* cx, HandleObject aobj, uint32_t length, js::Value* vp);
139 /* Natives exposed for optimization by the interpreter and JITs. */
141 extern bool
142 array_sort(JSContext* cx, unsigned argc, js::Value* vp);
144 extern bool
145 array_push(JSContext* cx, unsigned argc, js::Value* vp);
147 extern bool
148 array_pop(JSContext* cx, unsigned argc, js::Value* vp);
150 extern bool
151 array_splice(JSContext* cx, unsigned argc, js::Value* vp);
153 extern bool
154 array_splice_impl(JSContext* cx, unsigned argc, js::Value* vp, bool pop);
156 extern bool
157 array_concat(JSContext* cx, unsigned argc, js::Value* vp);
159 template <bool Locale>
160 JSString*
161 ArrayJoin(JSContext* cx, HandleObject obj, HandleLinearString sepstr, uint32_t length);
163 extern bool
164 array_concat_dense(JSContext* cx, Handle<ArrayObject*> arr1, Handle<ArrayObject*> arr2,
165 Handle<ArrayObject*> result);
167 bool
168 array_join(JSContext* cx, unsigned argc, js::Value* vp);
170 extern JSString*
171 array_join_impl(JSContext* cx, HandleValue array, HandleString sep);
173 extern void
174 ArrayShiftMoveElements(ArrayObject* obj);
176 extern bool
177 array_shift(JSContext* cx, unsigned argc, js::Value* vp);
179 extern bool
180 array_unshift(JSContext* cx, unsigned argc, js::Value* vp);
182 extern bool
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.)
192 extern bool
193 NewbornArrayPush(JSContext* cx, HandleObject obj, const Value& v);
195 extern ArrayObject*
196 ArrayConstructorOneArg(JSContext* cx, HandleTypeObject type, int32_t lengthInt);
198 } /* namespace js */
200 #ifdef DEBUG
201 extern bool
202 js_ArrayInfo(JSContext* cx, unsigned argc, js::Value* vp);
203 #endif
205 /* Array constructor native. Exposed only so the JIT can know its address. */
206 bool
207 js_Array(JSContext* cx, unsigned argc, js::Value* vp);
209 #endif /* jsarray_h */