Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsarray.h
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Mozilla Communicator client code, released
17  * March 31, 1998.
18  *
19  * The Initial Developer of the Original Code is
20  * Netscape Communications Corporation.
21  * Portions created by the Initial Developer are Copyright (C) 1998
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either of the GNU General Public License Version 2 or later (the "GPL"),
28  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39
40 #ifndef jsarray_h___
41 #define jsarray_h___
42 /*
43  * JS Array interface.
44  */
45 #include "jsprvtd.h"
46 #include "jspubtd.h"
47 #include "jsatom.h"
48 #include "jsobj.h"
49 #include "jsstr.h"
50
51 /* Small arrays are dense, no matter what. */
52 const uintN MIN_SPARSE_INDEX = 256;
53
54 inline JSObject::EnsureDenseResult
55 JSObject::ensureDenseArrayElements(JSContext *cx, uintN index, uintN extra)
56 {
57     JS_ASSERT(isDenseArray());
58     uintN currentCapacity = numSlots();
59
60     uintN requiredCapacity;
61     if (extra == 1) {
62         /* Optimize for the common case. */
63         if (index < currentCapacity)
64             return ED_OK;
65         requiredCapacity = index + 1;
66         if (requiredCapacity == 0) {
67             /* Overflow. */
68             return ED_SPARSE;
69         }
70     } else {
71         requiredCapacity = index + extra;
72         if (requiredCapacity < index) {
73             /* Overflow. */
74             return ED_SPARSE;
75         }
76         if (requiredCapacity <= currentCapacity)
77             return ED_OK;
78     }
79
80     /*
81      * We use the extra argument also as a hint about number of non-hole
82      * elements to be inserted.
83      */
84     if (requiredCapacity > MIN_SPARSE_INDEX &&
85         willBeSparseDenseArray(requiredCapacity, extra)) {
86         return ED_SPARSE;
87     }
88     return growSlots(cx, requiredCapacity) ? ED_OK : ED_FAILED;
89 }
90
91 extern bool
92 js_StringIsIndex(JSLinearString *str, jsuint *indexp);
93
94 inline JSBool
95 js_IdIsIndex(jsid id, jsuint *indexp)
96 {
97     if (JSID_IS_INT(id)) {
98         jsint i;
99         i = JSID_TO_INT(id);
100         if (i < 0)
101             return JS_FALSE;
102         *indexp = (jsuint)i;
103         return JS_TRUE;
104     }
105
106     if (JS_UNLIKELY(!JSID_IS_STRING(id)))
107         return JS_FALSE;
108
109     return js_StringIsIndex(JSID_TO_ATOM(id), indexp);
110 }
111
112 /* XML really wants to pretend jsvals are jsids. */
113 inline bool
114 js_IdValIsIndex(JSContext *cx, jsval id, jsuint *indexp, bool *isIndex)
115 {
116     if (JSVAL_IS_INT(id)) {
117         jsint i;
118         i = JSVAL_TO_INT(id);
119         if (i < 0) {
120             *isIndex = false;
121             return true;
122         }
123         *indexp = (jsuint)i;
124         *isIndex = true;
125         return true;
126     }
127
128     if (!JSVAL_IS_STRING(id)) {
129         *isIndex = false;
130         return true;
131     }
132
133     JSLinearString *str = JSVAL_TO_STRING(id)->ensureLinear(cx);
134     if (!str)
135         return false;
136
137     *isIndex = js_StringIsIndex(str, indexp);
138     return true;
139 }
140
141 extern js::Class js_ArrayClass, js_SlowArrayClass;
142
143 inline bool
144 JSObject::isDenseArray() const
145 {
146     return getClass() == &js_ArrayClass;
147 }
148
149 inline bool
150 JSObject::isSlowArray() const
151 {
152     return getClass() == &js_SlowArrayClass;
153 }
154
155 inline bool
156 JSObject::isArray() const
157 {
158     return isDenseArray() || isSlowArray();
159 }
160
161 /*
162  * Dense arrays are not native -- aobj->isNative() for a dense array aobj
163  * results in false, meaning aobj->map does not point to a js::Shape.
164  *
165  * But Array methods are called via aobj.sort(), e.g., and the interpreter and
166  * the trace recorder must consult the property cache in order to perform well.
167  * The cache works only for native objects.
168  *
169  * Therefore the interpreter (js_Interpret in JSOP_GETPROP and JSOP_CALLPROP)
170  * and js_GetPropertyHelper use this inline function to skip up one link in the
171  * prototype chain when obj is a dense array, in order to find a native object
172  * (to wit, Array.prototype) in which to probe for cached methods.
173  *
174  * Note that setting aobj.__proto__ for a dense array aobj turns aobj into a
175  * slow array, avoiding the neede to skip.
176  *
177  * Callers of js_GetProtoIfDenseArray must take care to use the original object
178  * (obj) for the |this| value of a getter, setter, or method call (bug 476447).
179  */
180 static JS_INLINE JSObject *
181 js_GetProtoIfDenseArray(JSObject *obj)
182 {
183     return obj->isDenseArray() ? obj->getProto() : obj;
184 }
185
186 extern JSObject *
187 js_InitArrayClass(JSContext *cx, JSObject *obj);
188
189 extern bool
190 js_InitContextBusyArrayTable(JSContext *cx);
191
192 namespace js
193 {
194
195 /* Create a dense array with no capacity allocated, length set to 0. */
196 extern JSObject * JS_FASTCALL
197 NewDenseEmptyArray(JSContext *cx, JSObject *proto=NULL);
198
199 /* Create a dense array with length and capacity == 'length'. */
200 extern JSObject * JS_FASTCALL
201 NewDenseAllocatedArray(JSContext *cx, uint length, JSObject *proto=NULL);
202
203 /*
204  * Create a dense array with a set length, but without allocating space for the
205  * contents. This is useful, e.g., when accepting length from the user.
206  */
207 extern JSObject * JS_FASTCALL
208 NewDenseUnallocatedArray(JSContext *cx, uint length, JSObject *proto=NULL);
209
210 /* Create a dense array with a copy of vp. */
211 extern JSObject *
212 NewDenseCopiedArray(JSContext *cx, uint length, Value *vp, JSObject *proto=NULL);
213
214 /* Create a sparse array. */
215 extern JSObject *
216 NewSlowEmptyArray(JSContext *cx);
217
218 }
219
220 extern JSBool
221 js_GetLengthProperty(JSContext *cx, JSObject *obj, jsuint *lengthp);
222
223 extern JSBool
224 js_SetLengthProperty(JSContext *cx, JSObject *obj, jsdouble length);
225
226 extern JSBool
227 js_HasLengthProperty(JSContext *cx, JSObject *obj, jsuint *lengthp);
228
229 extern JSBool JS_FASTCALL
230 js_IndexToId(JSContext *cx, jsuint index, jsid *idp);
231
232 namespace js {
233
234 /*
235  * This function assumes 'length' is effectively the result of calling
236  * js_GetLengthProperty on aobj.
237  */
238 extern bool
239 GetElements(JSContext *cx, JSObject *aobj, jsuint length, js::Value *vp);
240
241 }
242
243 /*
244  * JS-specific merge sort function.
245  */
246 typedef JSBool (*JSComparator)(void *arg, const void *a, const void *b,
247                                int *result);
248
249 enum JSMergeSortElemType {
250     JS_SORTING_VALUES,
251     JS_SORTING_GENERIC
252 };
253
254 /*
255  * NB: vec is the array to be sorted, tmp is temporary space at least as big
256  * as vec. Both should be GC-rooted if appropriate.
257  *
258  * isValue should true iff vec points to an array of js::Value
259  *
260  * The sorted result is in vec. vec may be in an inconsistent state if the
261  * comparator function cmp returns an error inside a comparison, so remember
262  * to check the return value of this function.
263  */
264 extern bool
265 js_MergeSort(void *vec, size_t nel, size_t elsize, JSComparator cmp,
266              void *arg, void *tmp, JSMergeSortElemType elemType);
267
268 /*
269  * The Array.prototype.sort fast-native entry point is exported for joined
270  * function optimization in js{interp,tracer}.cpp.
271  */
272 namespace js {
273 extern JSBool
274 array_sort(JSContext *cx, uintN argc, js::Value *vp);
275 }
276
277 #ifdef DEBUG
278 extern JSBool
279 js_ArrayInfo(JSContext *cx, uintN argc, jsval *vp);
280 #endif
281
282 extern JSBool
283 js_ArrayCompPush(JSContext *cx, JSObject *obj, const js::Value &vp);
284
285 /*
286  * Fast dense-array-to-buffer conversion for use by canvas.
287  *
288  * If the array is a dense array, fill [offset..offset+count] values into
289  * destination, assuming that types are consistent.  Return JS_TRUE if
290  * successful, otherwise JS_FALSE -- note that the destination buffer may be
291  * modified even if JS_FALSE is returned (e.g. due to finding an inappropriate
292  * type later on in the array).  If JS_FALSE is returned, no error conditions
293  * or exceptions are set on the context.
294  *
295  * This method succeeds if each element of the array is an integer or a double.
296  * Values outside the 0-255 range are clamped to that range.  Double values are
297  * converted to integers in this range by clamping and then rounding to
298  * nearest, ties to even.
299  */
300
301 JS_FRIEND_API(JSBool)
302 js_CoerceArrayToCanvasImageData(JSObject *obj, jsuint offset, jsuint count,
303                                 JSUint8 *dest);
304
305 JSBool
306 js_PrototypeHasIndexedProperties(JSContext *cx, JSObject *obj);
307
308 /*
309  * Utility to access the value from the id returned by array_lookupProperty.
310  */
311 JSBool
312 js_GetDenseArrayElementValue(JSContext *cx, JSObject *obj, jsid id,
313                              js::Value *vp);
314
315 /* Array constructor native. Exposed only so the JIT can know its address. */
316 JSBool
317 js_Array(JSContext *cx, uintN argc, js::Value *vp);
318
319 /*
320  * Makes a fast clone of a dense array as long as the array only contains
321  * primitive values.
322  *
323  * If the return value is JS_FALSE then clone will not be set.
324  *
325  * If the return value is JS_TRUE then clone will either be set to the address
326  * of a new JSObject or to NULL if the array was not dense or contained values
327  * that were not primitives.
328  */
329 JS_FRIEND_API(JSBool)
330 js_CloneDensePrimitiveArray(JSContext *cx, JSObject *obj, JSObject **clone);
331
332 /*
333  * Returns JS_TRUE if the given object is a dense array that contains only
334  * primitive values.
335  */
336 JS_FRIEND_API(JSBool)
337 js_IsDensePrimitiveArray(JSObject *obj);
338
339 extern JSBool JS_FASTCALL
340 js_EnsureDenseArrayCapacity(JSContext *cx, JSObject *obj, jsint i);
341
342 #endif /* jsarray_h___ */