1 |
siliconforks |
332 |
/* -*- 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 |
siliconforks |
460 |
#include "jsobj.h" |
48 |
siliconforks |
332 |
|
49 |
|
|
JS_BEGIN_EXTERN_C |
50 |
|
|
|
51 |
siliconforks |
460 |
#define ARRAY_CAPACITY_MIN 7 |
52 |
|
|
|
53 |
siliconforks |
332 |
extern JSBool |
54 |
|
|
js_IdIsIndex(jsval id, jsuint *indexp); |
55 |
|
|
|
56 |
|
|
extern JSClass js_ArrayClass, js_SlowArrayClass; |
57 |
|
|
|
58 |
siliconforks |
507 |
static JS_INLINE JSBool |
59 |
|
|
js_IsDenseArray(JSObject *obj) |
60 |
|
|
{ |
61 |
|
|
return STOBJ_GET_CLASS(obj) == &js_ArrayClass; |
62 |
|
|
} |
63 |
siliconforks |
332 |
|
64 |
siliconforks |
507 |
#define OBJ_IS_DENSE_ARRAY(cx, obj) js_IsDenseArray(obj) |
65 |
|
|
|
66 |
siliconforks |
332 |
#define OBJ_IS_ARRAY(cx,obj) (OBJ_IS_DENSE_ARRAY(cx, obj) || \ |
67 |
|
|
OBJ_GET_CLASS(cx, obj) == &js_SlowArrayClass) |
68 |
|
|
|
69 |
siliconforks |
460 |
/* |
70 |
|
|
* Dense arrays are not native (OBJ_IS_NATIVE(cx, aobj) for a dense array aobj |
71 |
|
|
* results in false, meaning aobj->map does not point to a JSScope). |
72 |
|
|
* |
73 |
|
|
* But Array methods are called via aobj.sort(), e.g., and the interpreter and |
74 |
|
|
* the trace recorder must consult the property cache in order to perform well. |
75 |
|
|
* The cache works only for native objects. |
76 |
|
|
* |
77 |
|
|
* Therefore the interpreter (js_Interpret in JSOP_GETPROP and JSOP_CALLPROP) |
78 |
|
|
* and js_GetPropertyHelper use this inline function to skip up one link in the |
79 |
|
|
* prototype chain when obj is a dense array, in order to find a native object |
80 |
|
|
* (to wit, Array.prototype) in which to probe for cached methods. |
81 |
|
|
* |
82 |
|
|
* Note that setting aobj.__proto__ for a dense array aobj turns aobj into a |
83 |
|
|
* slow array, avoiding the neede to skip. |
84 |
|
|
* |
85 |
|
|
* Callers of js_GetProtoIfDenseArray must take care to use the original object |
86 |
|
|
* (obj) for the |this| value of a getter, setter, or method call (bug 476447). |
87 |
|
|
*/ |
88 |
siliconforks |
507 |
#ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */ |
89 |
siliconforks |
460 |
static JS_INLINE JSObject * |
90 |
|
|
js_GetProtoIfDenseArray(JSContext *cx, JSObject *obj) |
91 |
|
|
{ |
92 |
|
|
return OBJ_IS_DENSE_ARRAY(cx, obj) ? OBJ_GET_PROTO(cx, obj) : obj; |
93 |
|
|
} |
94 |
siliconforks |
507 |
#endif |
95 |
siliconforks |
460 |
|
96 |
siliconforks |
332 |
extern JSObject * |
97 |
|
|
js_InitArrayClass(JSContext *cx, JSObject *obj); |
98 |
|
|
|
99 |
siliconforks |
507 |
#ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */ |
100 |
|
|
extern bool |
101 |
|
|
#else |
102 |
|
|
extern JSBool |
103 |
|
|
#endif |
104 |
|
|
js_InitContextBusyArrayTable(JSContext *cx); |
105 |
|
|
|
106 |
|
|
#ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */ |
107 |
siliconforks |
332 |
extern JSObject * |
108 |
|
|
js_NewArrayObject(JSContext *cx, jsuint length, jsval *vector, |
109 |
|
|
JSBool holey = JS_FALSE); |
110 |
siliconforks |
507 |
#else |
111 |
|
|
extern JSObject * |
112 |
|
|
js_NewArrayObject(JSContext *cx, jsuint length, jsval *vector, |
113 |
|
|
JSBool holey); |
114 |
|
|
#endif |
115 |
siliconforks |
332 |
|
116 |
|
|
/* Create an array object that starts out already made slow/sparse. */ |
117 |
|
|
extern JSObject * |
118 |
|
|
js_NewSlowArrayObject(JSContext *cx); |
119 |
|
|
|
120 |
|
|
extern JSBool |
121 |
|
|
js_MakeArraySlow(JSContext *cx, JSObject *obj); |
122 |
|
|
|
123 |
|
|
#define JSSLOT_ARRAY_LENGTH JSSLOT_PRIVATE |
124 |
|
|
#define JSSLOT_ARRAY_COUNT (JSSLOT_ARRAY_LENGTH + 1) |
125 |
siliconforks |
460 |
#define JSSLOT_ARRAY_UNUSED (JSSLOT_ARRAY_COUNT + 1) |
126 |
siliconforks |
332 |
|
127 |
siliconforks |
460 |
static JS_INLINE uint32 |
128 |
|
|
js_DenseArrayCapacity(JSObject *obj) |
129 |
|
|
{ |
130 |
siliconforks |
507 |
JS_ASSERT(js_IsDenseArray(obj)); |
131 |
siliconforks |
460 |
return obj->dslots ? (uint32) obj->dslots[-1] : 0; |
132 |
|
|
} |
133 |
siliconforks |
332 |
|
134 |
siliconforks |
460 |
static JS_INLINE void |
135 |
|
|
js_SetDenseArrayCapacity(JSObject *obj, uint32 capacity) |
136 |
|
|
{ |
137 |
siliconforks |
507 |
JS_ASSERT(js_IsDenseArray(obj)); |
138 |
siliconforks |
460 |
JS_ASSERT(obj->dslots); |
139 |
|
|
obj->dslots[-1] = (jsval) capacity; |
140 |
|
|
} |
141 |
siliconforks |
332 |
|
142 |
|
|
extern JSBool |
143 |
|
|
js_GetLengthProperty(JSContext *cx, JSObject *obj, jsuint *lengthp); |
144 |
|
|
|
145 |
|
|
extern JSBool |
146 |
siliconforks |
460 |
js_SetLengthProperty(JSContext *cx, JSObject *obj, jsdouble length); |
147 |
siliconforks |
332 |
|
148 |
|
|
extern JSBool |
149 |
|
|
js_HasLengthProperty(JSContext *cx, JSObject *obj, jsuint *lengthp); |
150 |
|
|
|
151 |
|
|
extern JSBool JS_FASTCALL |
152 |
|
|
js_IndexToId(JSContext *cx, jsuint index, jsid *idp); |
153 |
|
|
|
154 |
|
|
/* |
155 |
|
|
* Test whether an object is "array-like". Currently this means whether obj |
156 |
|
|
* is an Array or an arguments object. We would like an API, and probably a |
157 |
|
|
* way in the language, to bless other objects as array-like: having indexed |
158 |
|
|
* properties, and a 'length' property of uint32 value equal to one more than |
159 |
|
|
* the greatest index. |
160 |
|
|
*/ |
161 |
|
|
extern JSBool |
162 |
|
|
js_IsArrayLike(JSContext *cx, JSObject *obj, JSBool *answerp, jsuint *lengthp); |
163 |
|
|
|
164 |
|
|
/* |
165 |
|
|
* JS-specific merge sort function. |
166 |
|
|
*/ |
167 |
|
|
typedef JSBool (*JSComparator)(void *arg, const void *a, const void *b, |
168 |
|
|
int *result); |
169 |
|
|
/* |
170 |
|
|
* NB: vec is the array to be sorted, tmp is temporary space at least as big |
171 |
|
|
* as vec. Both should be GC-rooted if appropriate. |
172 |
|
|
* |
173 |
|
|
* The sorted result is in vec. vec may be in an inconsistent state if the |
174 |
|
|
* comparator function cmp returns an error inside a comparison, so remember |
175 |
|
|
* to check the return value of this function. |
176 |
|
|
*/ |
177 |
siliconforks |
507 |
extern JSBool |
178 |
siliconforks |
332 |
js_MergeSort(void *vec, size_t nel, size_t elsize, JSComparator cmp, |
179 |
|
|
void *arg, void *tmp); |
180 |
|
|
|
181 |
|
|
#ifdef DEBUG_ARRAYS |
182 |
|
|
extern JSBool |
183 |
|
|
js_ArrayInfo(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); |
184 |
|
|
#endif |
185 |
|
|
|
186 |
siliconforks |
460 |
extern JSBool JS_FASTCALL |
187 |
|
|
js_ArrayCompPush(JSContext *cx, JSObject *obj, jsval v); |
188 |
|
|
|
189 |
siliconforks |
332 |
/* |
190 |
siliconforks |
507 |
* Fast dense-array-to-buffer conversion for use by canvas. |
191 |
siliconforks |
332 |
* |
192 |
siliconforks |
507 |
* If the array is a dense array, fill [offset..offset+count] values into |
193 |
|
|
* destination, assuming that types are consistent. Return JS_TRUE if |
194 |
|
|
* successful, otherwise JS_FALSE -- note that the destination buffer may be |
195 |
|
|
* modified even if JS_FALSE is returned (e.g. due to finding an inappropriate |
196 |
|
|
* type later on in the array). If JS_FALSE is returned, no error conditions |
197 |
|
|
* or exceptions are set on the context. |
198 |
siliconforks |
332 |
* |
199 |
siliconforks |
507 |
* This method succeeds if each element of the array is an integer or a double. |
200 |
|
|
* Values outside the 0-255 range are clamped to that range. Double values are |
201 |
|
|
* converted to integers in this range by clamping and then rounding to |
202 |
|
|
* nearest, ties to even. |
203 |
siliconforks |
332 |
*/ |
204 |
|
|
|
205 |
|
|
JS_FRIEND_API(JSBool) |
206 |
siliconforks |
507 |
js_CoerceArrayToCanvasImageData(JSObject *obj, jsuint offset, jsuint count, |
207 |
|
|
JSUint8 *dest); |
208 |
siliconforks |
332 |
|
209 |
siliconforks |
460 |
JSBool |
210 |
|
|
js_PrototypeHasIndexedProperties(JSContext *cx, JSObject *obj); |
211 |
|
|
|
212 |
|
|
/* |
213 |
|
|
* Utility to access the value from the id returned by array_lookupProperty. |
214 |
|
|
*/ |
215 |
|
|
JSBool |
216 |
|
|
js_GetDenseArrayElementValue(JSContext *cx, JSObject *obj, JSProperty *prop, |
217 |
|
|
jsval *vp); |
218 |
|
|
|
219 |
siliconforks |
507 |
/* Array constructor native. Exposed only so the JIT can know its address. */ |
220 |
|
|
JSBool |
221 |
|
|
js_Array(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval); |
222 |
|
|
|
223 |
|
|
/* |
224 |
|
|
* Friend api function that allows direct creation of an array object with a |
225 |
|
|
* given capacity. Non-null return value means allocation of the internal |
226 |
|
|
* buffer for a capacity of at least |capacity| succeeded. A pointer to the |
227 |
|
|
* first element of this internal buffer is returned in the |vector| out |
228 |
|
|
* parameter. The caller promises to fill in the first |capacity| values |
229 |
|
|
* starting from that pointer immediately after this function returns and |
230 |
|
|
* without triggering GC (so this method is allowed to leave those |
231 |
|
|
* uninitialized) and to set them to non-JSVAL_HOLE values, so that the |
232 |
|
|
* resulting array has length and count both equal to |capacity|. |
233 |
|
|
*/ |
234 |
|
|
JS_FRIEND_API(JSObject *) |
235 |
|
|
js_NewArrayObjectWithCapacity(JSContext *cx, jsuint capacity, jsval **vector); |
236 |
|
|
|
237 |
siliconforks |
332 |
JS_END_EXTERN_C |
238 |
|
|
|
239 |
|
|
#endif /* jsarray_h___ */ |