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 "jscntxt.h"
46 : #include "jsprvtd.h"
47 : #include "jspubtd.h"
48 : #include "jsatom.h"
49 : #include "jsobj.h"
50 :
51 : /* Small arrays are dense, no matter what. */
52 : const unsigned MIN_SPARSE_INDEX = 256;
53 :
54 : namespace js {
55 : /* 2^32-2, inclusive */
56 : const uint32_t MAX_ARRAY_INDEX = 4294967294u;
57 : }
58 :
59 : inline JSBool
60 68326293 : js_IdIsIndex(jsid id, uint32_t *indexp)
61 : {
62 68326293 : if (JSID_IS_INT(id)) {
63 27044103 : int32_t i = JSID_TO_INT(id);
64 27044103 : if (i < 0)
65 2253 : return JS_FALSE;
66 27041850 : *indexp = (uint32_t)i;
67 27041850 : return JS_TRUE;
68 : }
69 :
70 41282190 : if (JS_UNLIKELY(!JSID_IS_STRING(id)))
71 353 : return JS_FALSE;
72 :
73 41281837 : return js::StringIsArrayIndex(JSID_TO_ATOM(id), indexp);
74 : }
75 :
76 : /*
77 : * Dense arrays are not native -- aobj->isNative() for a dense array aobj
78 : * results in false, meaning aobj->map does not point to a js::Shape.
79 : *
80 : * But Array methods are called via aobj.sort(), e.g., and the interpreter and
81 : * the trace recorder must consult the property cache in order to perform well.
82 : * The cache works only for native objects.
83 : *
84 : * Therefore the interpreter (js_Interpret in JSOP_GETPROP and JSOP_CALLPROP)
85 : * and js_GetPropertyHelper use this inline function to skip up one link in the
86 : * prototype chain when obj is a dense array, in order to find a native object
87 : * (to wit, Array.prototype) in which to probe for cached methods.
88 : *
89 : * Note that setting aobj.__proto__ for a dense array aobj turns aobj into a
90 : * slow array, avoiding the neede to skip.
91 : *
92 : * Callers of js_GetProtoIfDenseArray must take care to use the original object
93 : * (obj) for the |this| value of a getter, setter, or method call (bug 476447).
94 : */
95 : inline JSObject *
96 : js_GetProtoIfDenseArray(JSObject *obj);
97 :
98 : extern JSObject *
99 : js_InitArrayClass(JSContext *cx, JSObject *obj);
100 :
101 : extern bool
102 : js_InitContextBusyArrayTable(JSContext *cx);
103 :
104 : namespace js {
105 :
106 : /* Create a dense array with no capacity allocated, length set to 0. */
107 : extern JSObject * JS_FASTCALL
108 : NewDenseEmptyArray(JSContext *cx, JSObject *proto=NULL);
109 :
110 : /* Create a dense array with length and capacity == 'length', initialized length set to 0. */
111 : extern JSObject * JS_FASTCALL
112 : NewDenseAllocatedArray(JSContext *cx, uint32_t length, JSObject *proto=NULL);
113 :
114 : /*
115 : * Create a dense array with length, capacity and initialized length == 'length', and filled with holes.
116 : * This is a kludge, as the tracer doesn't yet track/update initialized length when initializing
117 : * array elements.
118 : */
119 : extern JSObject * JS_FASTCALL
120 : NewDenseAllocatedEmptyArray(JSContext *cx, uint32_t length, JSObject *proto=NULL);
121 :
122 : /*
123 : * Create a dense array with a set length, but without allocating space for the
124 : * contents. This is useful, e.g., when accepting length from the user.
125 : */
126 : extern JSObject * JS_FASTCALL
127 : NewDenseUnallocatedArray(JSContext *cx, uint32_t length, JSObject *proto=NULL);
128 :
129 : /* Create a dense array with a copy of vp. */
130 : extern JSObject *
131 : NewDenseCopiedArray(JSContext *cx, uint32_t length, const Value *vp, JSObject *proto = NULL);
132 :
133 : /* Create a sparse array. */
134 : extern JSObject *
135 : NewSlowEmptyArray(JSContext *cx);
136 :
137 : } /* namespace js */
138 :
139 : extern JSBool
140 : js_GetLengthProperty(JSContext *cx, JSObject *obj, uint32_t *lengthp);
141 :
142 : extern JSBool
143 : js_SetLengthProperty(JSContext *cx, JSObject *obj, double length);
144 :
145 : namespace js {
146 :
147 : extern JSBool
148 : array_defineElement(JSContext *cx, JSObject *obj, uint32_t index, const Value *value,
149 : PropertyOp getter, StrictPropertyOp setter, unsigned attrs);
150 :
151 : extern JSBool
152 : array_deleteElement(JSContext *cx, JSObject *obj, uint32_t index, Value *rval, JSBool strict);
153 :
154 : /*
155 : * Copy 'length' elements from aobj to vp.
156 : *
157 : * This function assumes 'length' is effectively the result of calling
158 : * js_GetLengthProperty on aobj.
159 : */
160 : extern bool
161 : GetElements(JSContext *cx, JSObject *aobj, uint32_t length, js::Value *vp);
162 :
163 : /* Natives exposed for optimization by the interpreter and JITs. */
164 :
165 : extern JSBool
166 : array_sort(JSContext *cx, unsigned argc, js::Value *vp);
167 :
168 : extern JSBool
169 : array_push(JSContext *cx, unsigned argc, js::Value *vp);
170 :
171 : extern JSBool
172 : array_pop(JSContext *cx, unsigned argc, js::Value *vp);
173 :
174 : extern JSBool
175 : array_concat(JSContext *cx, unsigned argc, js::Value *vp);
176 :
177 : extern JSBool
178 : array_shift(JSContext *cx, unsigned argc, js::Value *vp);
179 :
180 : } /* namespace js */
181 :
182 : #ifdef DEBUG
183 : extern JSBool
184 : js_ArrayInfo(JSContext *cx, unsigned argc, jsval *vp);
185 : #endif
186 :
187 : /*
188 : * Append the given (non-hole) value to the end of an array. The array must be
189 : * a newborn array -- that is, one which has not been exposed to script for
190 : * arbitrary manipulation. (This method optimizes on the assumption that
191 : * extending the array to accommodate the element will never make the array
192 : * sparse, which requires that the array be completely filled.)
193 : */
194 : extern JSBool
195 : js_NewbornArrayPush(JSContext *cx, JSObject *obj, const js::Value &v);
196 :
197 : JSBool
198 : js_PrototypeHasIndexedProperties(JSContext *cx, JSObject *obj);
199 :
200 : /*
201 : * Utility to access the value from the id returned by array_lookupProperty.
202 : */
203 : JSBool
204 : js_GetDenseArrayElementValue(JSContext *cx, JSObject *obj, jsid id,
205 : js::Value *vp);
206 :
207 : /* Array constructor native. Exposed only so the JIT can know its address. */
208 : JSBool
209 : js_Array(JSContext *cx, unsigned argc, js::Value *vp);
210 :
211 : #endif /* jsarray_h___ */
|