|
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/. */ |
|
6 |
|
7 /* JS Array interface. */ |
|
8 |
|
9 #ifndef jsarray_h |
|
10 #define jsarray_h |
|
11 |
|
12 #include "jsobj.h" |
|
13 #include "jspubtd.h" |
|
14 |
|
15 namespace js { |
|
16 /* 2^32-2, inclusive */ |
|
17 const uint32_t MAX_ARRAY_INDEX = 4294967294u; |
|
18 } |
|
19 |
|
20 inline bool |
|
21 js_IdIsIndex(jsid id, uint32_t *indexp) |
|
22 { |
|
23 if (JSID_IS_INT(id)) { |
|
24 int32_t i = JSID_TO_INT(id); |
|
25 JS_ASSERT(i >= 0); |
|
26 *indexp = (uint32_t)i; |
|
27 return true; |
|
28 } |
|
29 |
|
30 if (MOZ_UNLIKELY(!JSID_IS_STRING(id))) |
|
31 return false; |
|
32 |
|
33 return js::StringIsArrayIndex(JSID_TO_ATOM(id), indexp); |
|
34 } |
|
35 |
|
36 extern JSObject * |
|
37 js_InitArrayClass(JSContext *cx, js::HandleObject obj); |
|
38 |
|
39 extern bool |
|
40 js_InitContextBusyArrayTable(JSContext *cx); |
|
41 |
|
42 namespace js { |
|
43 |
|
44 class ArrayObject; |
|
45 |
|
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); |
|
50 |
|
51 /* Create a dense array with length and capacity == 'length', initialized length set to 0. */ |
|
52 extern ArrayObject * JS_FASTCALL |
|
53 NewDenseAllocatedArray(ExclusiveContext *cx, uint32_t length, JSObject *proto = nullptr, |
|
54 NewObjectKind newKind = GenericObject); |
|
55 |
|
56 /* |
|
57 * Create a dense array with a set length, but without allocating space for the |
|
58 * contents. This is useful, e.g., when accepting length from the user. |
|
59 */ |
|
60 extern ArrayObject * JS_FASTCALL |
|
61 NewDenseUnallocatedArray(ExclusiveContext *cx, uint32_t length, JSObject *proto = nullptr, |
|
62 NewObjectKind newKind = GenericObject); |
|
63 |
|
64 /* Create a dense array with a copy of the dense array elements in src. */ |
|
65 extern ArrayObject * |
|
66 NewDenseCopiedArray(JSContext *cx, uint32_t length, HandleObject src, uint32_t elementOffset, JSObject *proto = nullptr); |
|
67 |
|
68 /* Create a dense array from the given array values, which must be rooted */ |
|
69 extern ArrayObject * |
|
70 NewDenseCopiedArray(JSContext *cx, uint32_t length, const Value *values, JSObject *proto = nullptr, |
|
71 NewObjectKind newKind = GenericObject); |
|
72 |
|
73 /* Create a dense array based on templateObject with the given length. */ |
|
74 extern ArrayObject * |
|
75 NewDenseAllocatedArrayWithTemplate(JSContext *cx, uint32_t length, JSObject *templateObject); |
|
76 |
|
77 /* |
|
78 * Determines whether a write to the given element on |obj| should fail because |
|
79 * |obj| is an Array with a non-writable length, and writing that element would |
|
80 * increase the length of the array. |
|
81 */ |
|
82 extern bool |
|
83 WouldDefinePastNonwritableLength(ThreadSafeContext *cx, |
|
84 HandleObject obj, uint32_t index, bool strict, |
|
85 bool *definesPast); |
|
86 |
|
87 /* |
|
88 * Canonicalize |vp| to a uint32_t value potentially suitable for use as an |
|
89 * array length. |
|
90 * |
|
91 * For parallel execution we can only canonicalize non-object values. |
|
92 */ |
|
93 template <ExecutionMode mode> |
|
94 extern bool |
|
95 CanonicalizeArrayLengthValue(typename ExecutionModeTraits<mode>::ContextType cx, |
|
96 HandleValue v, uint32_t *canonicalized); |
|
97 |
|
98 extern bool |
|
99 GetLengthProperty(JSContext *cx, HandleObject obj, uint32_t *lengthp); |
|
100 |
|
101 extern bool |
|
102 SetLengthProperty(JSContext *cx, HandleObject obj, double length); |
|
103 |
|
104 extern bool |
|
105 ObjectMayHaveExtraIndexedProperties(JSObject *obj); |
|
106 |
|
107 /* |
|
108 * Copy 'length' elements from aobj to vp. |
|
109 * |
|
110 * This function assumes 'length' is effectively the result of calling |
|
111 * js_GetLengthProperty on aobj. vp must point to rooted memory. |
|
112 */ |
|
113 extern bool |
|
114 GetElements(JSContext *cx, HandleObject aobj, uint32_t length, js::Value *vp); |
|
115 |
|
116 /* Natives exposed for optimization by the interpreter and JITs. */ |
|
117 |
|
118 extern bool |
|
119 array_sort(JSContext *cx, unsigned argc, js::Value *vp); |
|
120 |
|
121 extern bool |
|
122 array_push(JSContext *cx, unsigned argc, js::Value *vp); |
|
123 |
|
124 extern bool |
|
125 array_pop(JSContext *cx, unsigned argc, js::Value *vp); |
|
126 |
|
127 extern bool |
|
128 array_splice(JSContext *cx, unsigned argc, js::Value *vp); |
|
129 |
|
130 extern bool |
|
131 array_splice_impl(JSContext *cx, unsigned argc, js::Value *vp, bool pop); |
|
132 |
|
133 extern bool |
|
134 array_concat(JSContext *cx, unsigned argc, js::Value *vp); |
|
135 |
|
136 extern bool |
|
137 array_concat_dense(JSContext *cx, Handle<ArrayObject*> arr1, Handle<ArrayObject*> arr2, |
|
138 Handle<ArrayObject*> result); |
|
139 |
|
140 extern void |
|
141 ArrayShiftMoveElements(JSObject *obj); |
|
142 |
|
143 extern bool |
|
144 array_shift(JSContext *cx, unsigned argc, js::Value *vp); |
|
145 |
|
146 /* |
|
147 * Append the given (non-hole) value to the end of an array. The array must be |
|
148 * a newborn array -- that is, one which has not been exposed to script for |
|
149 * arbitrary manipulation. (This method optimizes on the assumption that |
|
150 * extending the array to accommodate the element will never make the array |
|
151 * sparse, which requires that the array be completely filled.) |
|
152 */ |
|
153 extern bool |
|
154 NewbornArrayPush(JSContext *cx, HandleObject obj, const Value &v); |
|
155 |
|
156 } /* namespace js */ |
|
157 |
|
158 #ifdef DEBUG |
|
159 extern bool |
|
160 js_ArrayInfo(JSContext *cx, unsigned argc, js::Value *vp); |
|
161 #endif |
|
162 |
|
163 /* Array constructor native. Exposed only so the JIT can know its address. */ |
|
164 bool |
|
165 js_Array(JSContext *cx, unsigned argc, js::Value *vp); |
|
166 |
|
167 #endif /* jsarray_h */ |