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 "jsobj.h" |
48 |
|
49 |
JS_BEGIN_EXTERN_C |
50 |
|
51 |
#define ARRAY_CAPACITY_MIN 7 |
52 |
|
53 |
/* Generous sanity-bound on length (in elements) of array initialiser. */ |
54 |
#define ARRAY_INIT_LIMIT JS_BIT(24) |
55 |
|
56 |
extern JSBool |
57 |
js_IdIsIndex(jsval id, jsuint *indexp); |
58 |
|
59 |
extern JSClass js_ArrayClass, js_SlowArrayClass; |
60 |
|
61 |
#define OBJ_IS_DENSE_ARRAY(cx,obj) (OBJ_GET_CLASS(cx, obj) == &js_ArrayClass) |
62 |
|
63 |
#define OBJ_IS_ARRAY(cx,obj) (OBJ_IS_DENSE_ARRAY(cx, obj) || \ |
64 |
OBJ_GET_CLASS(cx, obj) == &js_SlowArrayClass) |
65 |
|
66 |
/* |
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). |
69 |
* |
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. |
73 |
* |
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 native object |
77 |
* (to wit, Array.prototype) in which to probe for cached methods. |
78 |
* |
79 |
* Note that setting aobj.__proto__ for a dense array aobj turns aobj into a |
80 |
* slow array, avoiding the neede to skip. |
81 |
* |
82 |
* Callers of js_GetProtoIfDenseArray must take care to use the original object |
83 |
* (obj) for the |this| value of a getter, setter, or method call (bug 476447). |
84 |
*/ |
85 |
static JS_INLINE JSObject * |
86 |
js_GetProtoIfDenseArray(JSContext *cx, JSObject *obj) |
87 |
{ |
88 |
return OBJ_IS_DENSE_ARRAY(cx, obj) ? OBJ_GET_PROTO(cx, obj) : obj; |
89 |
} |
90 |
|
91 |
extern JSObject * |
92 |
js_InitArrayClass(JSContext *cx, JSObject *obj); |
93 |
|
94 |
extern JSObject * |
95 |
js_NewArrayObject(JSContext *cx, jsuint length, jsval *vector, |
96 |
JSBool holey = JS_FALSE); |
97 |
|
98 |
/* Create an array object that starts out already made slow/sparse. */ |
99 |
extern JSObject * |
100 |
js_NewSlowArrayObject(JSContext *cx); |
101 |
|
102 |
extern JSBool |
103 |
js_MakeArraySlow(JSContext *cx, JSObject *obj); |
104 |
|
105 |
#define JSSLOT_ARRAY_LENGTH JSSLOT_PRIVATE |
106 |
#define JSSLOT_ARRAY_COUNT (JSSLOT_ARRAY_LENGTH + 1) |
107 |
#define JSSLOT_ARRAY_UNUSED (JSSLOT_ARRAY_COUNT + 1) |
108 |
|
109 |
static JS_INLINE uint32 |
110 |
js_DenseArrayCapacity(JSObject *obj) |
111 |
{ |
112 |
JS_ASSERT(OBJ_IS_DENSE_ARRAY(BOGUS_CX, obj)); |
113 |
return obj->dslots ? (uint32) obj->dslots[-1] : 0; |
114 |
} |
115 |
|
116 |
static JS_INLINE void |
117 |
js_SetDenseArrayCapacity(JSObject *obj, uint32 capacity) |
118 |
{ |
119 |
JS_ASSERT(OBJ_IS_DENSE_ARRAY(BOGUS_CX, obj)); |
120 |
JS_ASSERT(obj->dslots); |
121 |
obj->dslots[-1] = (jsval) capacity; |
122 |
} |
123 |
|
124 |
extern JSBool |
125 |
js_GetLengthProperty(JSContext *cx, JSObject *obj, jsuint *lengthp); |
126 |
|
127 |
extern JSBool |
128 |
js_SetLengthProperty(JSContext *cx, JSObject *obj, jsdouble length); |
129 |
|
130 |
extern JSBool |
131 |
js_HasLengthProperty(JSContext *cx, JSObject *obj, jsuint *lengthp); |
132 |
|
133 |
extern JSBool JS_FASTCALL |
134 |
js_IndexToId(JSContext *cx, jsuint index, jsid *idp); |
135 |
|
136 |
/* |
137 |
* Test whether an object is "array-like". Currently this means whether obj |
138 |
* is an Array or an arguments object. We would like an API, and probably a |
139 |
* way in the language, to bless other objects as array-like: having indexed |
140 |
* properties, and a 'length' property of uint32 value equal to one more than |
141 |
* the greatest index. |
142 |
*/ |
143 |
extern JSBool |
144 |
js_IsArrayLike(JSContext *cx, JSObject *obj, JSBool *answerp, jsuint *lengthp); |
145 |
|
146 |
/* |
147 |
* JS-specific merge sort function. |
148 |
*/ |
149 |
typedef JSBool (*JSComparator)(void *arg, const void *a, const void *b, |
150 |
int *result); |
151 |
/* |
152 |
* NB: vec is the array to be sorted, tmp is temporary space at least as big |
153 |
* as vec. Both should be GC-rooted if appropriate. |
154 |
* |
155 |
* The sorted result is in vec. vec may be in an inconsistent state if the |
156 |
* comparator function cmp returns an error inside a comparison, so remember |
157 |
* to check the return value of this function. |
158 |
*/ |
159 |
extern JS_REQUIRES_STACK JSBool |
160 |
js_MergeSort(void *vec, size_t nel, size_t elsize, JSComparator cmp, |
161 |
void *arg, void *tmp); |
162 |
|
163 |
#ifdef DEBUG_ARRAYS |
164 |
extern JSBool |
165 |
js_ArrayInfo(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); |
166 |
#endif |
167 |
|
168 |
extern JSBool JS_FASTCALL |
169 |
js_ArrayCompPush(JSContext *cx, JSObject *obj, jsval v); |
170 |
|
171 |
/* |
172 |
* Fast dense-array-to-buffer conversions. |
173 |
* |
174 |
* If the array is a dense array, fill [offset..offset+count] values |
175 |
* into destination, assuming that types are consistent. Return |
176 |
* JS_TRUE if successful, otherwise JS_FALSE -- note that the |
177 |
* destination buffer may be modified even if JS_FALSE is returned |
178 |
* (e.g. due to finding an inappropriate type later on in the array). |
179 |
* If JS_FALSE is returned, no error conditions or exceptions are set |
180 |
* on the context. |
181 |
* |
182 |
* For ArrayToJSUint8, ArrayToJSUint16, and ArrayToJSUint32, each element |
183 |
* in the array a) must be an integer; b) must be >= 0. Integers |
184 |
* are clamped to fit in the destination size. Only JSVAL_IS_INT values |
185 |
* are considered to be valid, so for JSUint32, the maximum value that |
186 |
* can be fast-converted is less than the full unsigned 32-bit range. |
187 |
* |
188 |
* For ArrayToJSInt8, ArrayToJSInt16, ArrayToJSInt32, each element in |
189 |
* the array must be an integer. Integers are clamped to fit in the |
190 |
* destination size. Only JSVAL_IS_INT values are considered to be |
191 |
* valid, so for JSInt32, the maximum value that can be |
192 |
* fast-converted is less than the full signed 32-bit range. |
193 |
* |
194 |
* For ArrayToJSDouble, each element in the array must be an |
195 |
* integer -or- a double (JSVAL_IS_NUMBER). |
196 |
*/ |
197 |
|
198 |
JS_FRIEND_API(JSBool) |
199 |
js_ArrayToJSUint8Buffer(JSContext *cx, JSObject *obj, jsuint offset, jsuint count, |
200 |
JSUint8 *dest); |
201 |
|
202 |
JS_FRIEND_API(JSBool) |
203 |
js_ArrayToJSUint16Buffer(JSContext *cx, JSObject *obj, jsuint offset, jsuint count, |
204 |
JSUint16 *dest); |
205 |
|
206 |
JS_FRIEND_API(JSBool) |
207 |
js_ArrayToJSUint32Buffer(JSContext *cx, JSObject *obj, jsuint offset, jsuint count, |
208 |
JSUint32 *dest); |
209 |
|
210 |
JS_FRIEND_API(JSBool) |
211 |
js_ArrayToJSInt8Buffer(JSContext *cx, JSObject *obj, jsuint offset, jsuint count, |
212 |
JSInt8 *dest); |
213 |
|
214 |
JS_FRIEND_API(JSBool) |
215 |
js_ArrayToJSInt16Buffer(JSContext *cx, JSObject *obj, jsuint offset, jsuint count, |
216 |
JSInt16 *dest); |
217 |
|
218 |
JS_FRIEND_API(JSBool) |
219 |
js_ArrayToJSInt32Buffer(JSContext *cx, JSObject *obj, jsuint offset, jsuint count, |
220 |
JSInt32 *dest); |
221 |
|
222 |
JS_FRIEND_API(JSBool) |
223 |
js_ArrayToJSDoubleBuffer(JSContext *cx, JSObject *obj, jsuint offset, jsuint count, |
224 |
jsdouble *dest); |
225 |
|
226 |
JSBool |
227 |
js_PrototypeHasIndexedProperties(JSContext *cx, JSObject *obj); |
228 |
|
229 |
/* |
230 |
* Utility to access the value from the id returned by array_lookupProperty. |
231 |
*/ |
232 |
JSBool |
233 |
js_GetDenseArrayElementValue(JSContext *cx, JSObject *obj, JSProperty *prop, |
234 |
jsval *vp); |
235 |
|
236 |
JS_END_EXTERN_C |
237 |
|
238 |
#endif /* jsarray_h___ */ |