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 #define ARRAY_CAPACITY_MIN 7
53 /* Generous sanity-bound on length (in elements) of array initialiser. */
54 #define ARRAY_INIT_LIMIT JS_BIT(24)
57 js_IdIsIndex(jsval id
, jsuint
*indexp
);
59 extern JSClass js_ArrayClass
, js_SlowArrayClass
;
61 #define OBJ_IS_DENSE_ARRAY(cx,obj) (OBJ_GET_CLASS(cx, obj) == &js_ArrayClass)
63 #define OBJ_IS_ARRAY(cx,obj) (OBJ_IS_DENSE_ARRAY(cx, obj) || \
64 OBJ_GET_CLASS(cx, obj) == &js_SlowArrayClass)
67 * Dense arrays are not native (OBJ_IS_NATIVE(cx, aobj) for a dense array aobj
68 * results in false, meaning aobj->map does not point to a JSScope).
70 * But Array methods are called via aobj.sort(), e.g., and the interpreter and
71 * the trace recorder must consult the property cache in order to perform well.
72 * The cache works only for native objects.
74 * Therefore the interpreter (js_Interpret in JSOP_GETPROP and JSOP_CALLPROP)
75 * and js_GetPropertyHelper use this inline function to skip up one link in the
76 * prototype chain when obj is a dense array, in order to find a likely-native
77 * object (to wit, Array.prototype) in which to probe for cached methods.
79 * Callers of js_GetProtoIfDenseArray must take care to use the original object
80 * (obj) for the |this| value of a getter, setter, or method call (bug 476447).
82 static JS_INLINE JSObject
*
83 js_GetProtoIfDenseArray(JSContext
*cx
, JSObject
*obj
)
85 return OBJ_IS_DENSE_ARRAY(cx
, obj
) ? OBJ_GET_PROTO(cx
, obj
) : obj
;
89 js_InitArrayClass(JSContext
*cx
, JSObject
*obj
);
92 js_NewArrayObject(JSContext
*cx
, jsuint length
, jsval
*vector
,
93 JSBool holey
= JS_FALSE
);
95 /* Create an array object that starts out already made slow/sparse. */
97 js_NewSlowArrayObject(JSContext
*cx
);
100 js_MakeArraySlow(JSContext
*cx
, JSObject
*obj
);
102 #define JSSLOT_ARRAY_LENGTH JSSLOT_PRIVATE
103 #define JSSLOT_ARRAY_COUNT (JSSLOT_ARRAY_LENGTH + 1)
104 #define JSSLOT_ARRAY_LOOKUP_HOLDER (JSSLOT_ARRAY_COUNT + 1)
106 static JS_INLINE uint32
107 js_DenseArrayCapacity(JSObject
*obj
)
109 JS_ASSERT(OBJ_IS_DENSE_ARRAY(BOGUS_CX
, obj
));
110 return obj
->dslots
? (uint32
) obj
->dslots
[-1] : 0;
113 static JS_INLINE
void
114 js_SetDenseArrayCapacity(JSObject
*obj
, uint32 capacity
)
116 JS_ASSERT(OBJ_IS_DENSE_ARRAY(BOGUS_CX
, obj
));
117 JS_ASSERT(obj
->dslots
);
118 obj
->dslots
[-1] = (jsval
) capacity
;
122 js_GetLengthProperty(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
125 js_SetLengthProperty(JSContext
*cx
, JSObject
*obj
, jsdouble length
);
128 js_HasLengthProperty(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
130 extern JSBool JS_FASTCALL
131 js_IndexToId(JSContext
*cx
, jsuint index
, jsid
*idp
);
134 * Test whether an object is "array-like". Currently this means whether obj
135 * is an Array or an arguments object. We would like an API, and probably a
136 * way in the language, to bless other objects as array-like: having indexed
137 * properties, and a 'length' property of uint32 value equal to one more than
138 * the greatest index.
141 js_IsArrayLike(JSContext
*cx
, JSObject
*obj
, JSBool
*answerp
, jsuint
*lengthp
);
144 * JS-specific merge sort function.
146 typedef JSBool (*JSComparator
)(void *arg
, const void *a
, const void *b
,
149 * NB: vec is the array to be sorted, tmp is temporary space at least as big
150 * as vec. Both should be GC-rooted if appropriate.
152 * The sorted result is in vec. vec may be in an inconsistent state if the
153 * comparator function cmp returns an error inside a comparison, so remember
154 * to check the return value of this function.
156 extern JS_REQUIRES_STACK JSBool
157 js_MergeSort(void *vec
, size_t nel
, size_t elsize
, JSComparator cmp
,
158 void *arg
, void *tmp
);
162 js_ArrayInfo(JSContext
*cx
, JSObject
*obj
, uintN argc
, jsval
*argv
, jsval
*rval
);
165 extern JSBool JS_FASTCALL
166 js_ArrayCompPush(JSContext
*cx
, JSObject
*obj
, jsval v
);
169 * Fast dense-array-to-buffer conversions.
171 * If the array is a dense array, fill [offset..offset+count] values
172 * into destination, assuming that types are consistent. Return
173 * JS_TRUE if successful, otherwise JS_FALSE -- note that the
174 * destination buffer may be modified even if JS_FALSE is returned
175 * (e.g. due to finding an inappropriate type later on in the array).
176 * If JS_FALSE is returned, no error conditions or exceptions are set
179 * For ArrayToJSUint8, ArrayToJSUint16, and ArrayToJSUint32, each element
180 * in the array a) must be an integer; b) must be >= 0. Integers
181 * are clamped to fit in the destination size. Only JSVAL_IS_INT values
182 * are considered to be valid, so for JSUint32, the maximum value that
183 * can be fast-converted is less than the full unsigned 32-bit range.
185 * For ArrayToJSInt8, ArrayToJSInt16, ArrayToJSInt32, each element in
186 * the array must be an integer. Integers are clamped to fit in the
187 * destination size. Only JSVAL_IS_INT values are considered to be
188 * valid, so for JSInt32, the maximum value that can be
189 * fast-converted is less than the full signed 32-bit range.
191 * For ArrayToJSDouble, each element in the array must be an
192 * integer -or- a double (JSVAL_IS_NUMBER).
195 JS_FRIEND_API(JSBool
)
196 js_ArrayToJSUint8Buffer(JSContext
*cx
, JSObject
*obj
, jsuint offset
, jsuint count
,
199 JS_FRIEND_API(JSBool
)
200 js_ArrayToJSUint16Buffer(JSContext
*cx
, JSObject
*obj
, jsuint offset
, jsuint count
,
203 JS_FRIEND_API(JSBool
)
204 js_ArrayToJSUint32Buffer(JSContext
*cx
, JSObject
*obj
, jsuint offset
, jsuint count
,
207 JS_FRIEND_API(JSBool
)
208 js_ArrayToJSInt8Buffer(JSContext
*cx
, JSObject
*obj
, jsuint offset
, jsuint count
,
211 JS_FRIEND_API(JSBool
)
212 js_ArrayToJSInt16Buffer(JSContext
*cx
, JSObject
*obj
, jsuint offset
, jsuint count
,
215 JS_FRIEND_API(JSBool
)
216 js_ArrayToJSInt32Buffer(JSContext
*cx
, JSObject
*obj
, jsuint offset
, jsuint count
,
219 JS_FRIEND_API(JSBool
)
220 js_ArrayToJSDoubleBuffer(JSContext
*cx
, JSObject
*obj
, jsuint offset
, jsuint count
,
224 js_PrototypeHasIndexedProperties(JSContext
*cx
, JSObject
*obj
);
227 * Utility to access the value from the id returned by array_lookupProperty.
230 js_GetDenseArrayElementValue(JSObject
*obj
, JSProperty
*prop
);
234 #endif /* jsarray_h___ */