1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
16 * The Original Code is Mozilla Communicator client code, released
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.
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.
38 * ***** END LICENSE BLOCK ***** */
51 /* Small arrays are dense, no matter what. */
52 const uintN MIN_SPARSE_INDEX = 256;
54 inline JSObject::EnsureDenseResult
55 JSObject::ensureDenseArrayElements(JSContext *cx, uintN index, uintN extra)
57 JS_ASSERT(isDenseArray());
58 uintN currentCapacity = numSlots();
60 uintN requiredCapacity;
62 /* Optimize for the common case. */
63 if (index < currentCapacity)
65 requiredCapacity = index + 1;
66 if (requiredCapacity == 0) {
71 requiredCapacity = index + extra;
72 if (requiredCapacity < index) {
76 if (requiredCapacity <= currentCapacity)
81 * We use the extra argument also as a hint about number of non-hole
82 * elements to be inserted.
84 if (requiredCapacity > MIN_SPARSE_INDEX &&
85 willBeSparseDenseArray(requiredCapacity, extra)) {
88 return growSlots(cx, requiredCapacity) ? ED_OK : ED_FAILED;
92 js_StringIsIndex(JSLinearString *str, jsuint *indexp);
95 js_IdIsIndex(jsid id, jsuint *indexp)
97 if (JSID_IS_INT(id)) {
106 if (JS_UNLIKELY(!JSID_IS_STRING(id)))
109 return js_StringIsIndex(JSID_TO_ATOM(id), indexp);
112 /* XML really wants to pretend jsvals are jsids. */
114 js_IdValIsIndex(JSContext *cx, jsval id, jsuint *indexp, bool *isIndex)
116 if (JSVAL_IS_INT(id)) {
118 i = JSVAL_TO_INT(id);
128 if (!JSVAL_IS_STRING(id)) {
133 JSLinearString *str = JSVAL_TO_STRING(id)->ensureLinear(cx);
137 *isIndex = js_StringIsIndex(str, indexp);
141 extern js::Class js_ArrayClass, js_SlowArrayClass;
144 JSObject::isDenseArray() const
146 return getClass() == &js_ArrayClass;
150 JSObject::isSlowArray() const
152 return getClass() == &js_SlowArrayClass;
156 JSObject::isArray() const
158 return isDenseArray() || isSlowArray();
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.
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.
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.
174 * Note that setting aobj.__proto__ for a dense array aobj turns aobj into a
175 * slow array, avoiding the neede to skip.
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).
180 static JS_INLINE JSObject *
181 js_GetProtoIfDenseArray(JSObject *obj)
183 return obj->isDenseArray() ? obj->getProto() : obj;
187 js_InitArrayClass(JSContext *cx, JSObject *obj);
190 js_InitContextBusyArrayTable(JSContext *cx);
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);
199 /* Create a dense array with length and capacity == 'length'. */
200 extern JSObject * JS_FASTCALL
201 NewDenseAllocatedArray(JSContext *cx, uint length, JSObject *proto=NULL);
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.
207 extern JSObject * JS_FASTCALL
208 NewDenseUnallocatedArray(JSContext *cx, uint length, JSObject *proto=NULL);
210 /* Create a dense array with a copy of vp. */
212 NewDenseCopiedArray(JSContext *cx, uint length, Value *vp, JSObject *proto=NULL);
214 /* Create a sparse array. */
216 NewSlowEmptyArray(JSContext *cx);
221 js_GetLengthProperty(JSContext *cx, JSObject *obj, jsuint *lengthp);
224 js_SetLengthProperty(JSContext *cx, JSObject *obj, jsdouble length);
227 js_HasLengthProperty(JSContext *cx, JSObject *obj, jsuint *lengthp);
229 extern JSBool JS_FASTCALL
230 js_IndexToId(JSContext *cx, jsuint index, jsid *idp);
235 * This function assumes 'length' is effectively the result of calling
236 * js_GetLengthProperty on aobj.
239 GetElements(JSContext *cx, JSObject *aobj, jsuint length, js::Value *vp);
244 * JS-specific merge sort function.
246 typedef JSBool (*JSComparator)(void *arg, const void *a, const void *b,
249 enum JSMergeSortElemType {
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.
258 * isValue should true iff vec points to an array of js::Value
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.
265 js_MergeSort(void *vec, size_t nel, size_t elsize, JSComparator cmp,
266 void *arg, void *tmp, JSMergeSortElemType elemType);
269 * The Array.prototype.sort fast-native entry point is exported for joined
270 * function optimization in js{interp,tracer}.cpp.
274 array_sort(JSContext *cx, uintN argc, js::Value *vp);
279 js_ArrayInfo(JSContext *cx, uintN argc, jsval *vp);
283 js_ArrayCompPush(JSContext *cx, JSObject *obj, const js::Value &vp);
286 * Fast dense-array-to-buffer conversion for use by canvas.
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.
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.
301 JS_FRIEND_API(JSBool)
302 js_CoerceArrayToCanvasImageData(JSObject *obj, jsuint offset, jsuint count,
306 js_PrototypeHasIndexedProperties(JSContext *cx, JSObject *obj);
309 * Utility to access the value from the id returned by array_lookupProperty.
312 js_GetDenseArrayElementValue(JSContext *cx, JSObject *obj, jsid id,
315 /* Array constructor native. Exposed only so the JIT can know its address. */
317 js_Array(JSContext *cx, uintN argc, js::Value *vp);
320 * Makes a fast clone of a dense array as long as the array only contains
323 * If the return value is JS_FALSE then clone will not be set.
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.
329 JS_FRIEND_API(JSBool)
330 js_CloneDensePrimitiveArray(JSContext *cx, JSObject *obj, JSObject **clone);
333 * Returns JS_TRUE if the given object is a dense array that contains only
336 JS_FRIEND_API(JSBool)
337 js_IsDensePrimitiveArray(JSObject *obj);
339 extern JSBool JS_FASTCALL
340 js_EnsureDenseArrayCapacity(JSContext *cx, JSObject *obj, jsint i);
342 #endif /* jsarray_h___ */