2001-09-20 00:02:59 +00:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2006-10-05 23:28:51 +00:00
|
|
|
* vim: set sw=4 ts=8 et tw=78:
|
1998-03-28 02:44:41 +00:00
|
|
|
*
|
2003-11-15 00:11:16 +00:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-03-28 02:44:41 +00:00
|
|
|
*
|
2003-11-15 00:11:16 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-03-28 02:44:41 +00:00
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
1999-09-28 23:12:09 +00:00
|
|
|
*
|
2003-11-15 00:11:16 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-09-28 23:12:09 +00:00
|
|
|
*
|
2001-01-04 10:13:18 +00:00
|
|
|
* Contributor(s):
|
1999-09-28 23:12:09 +00:00
|
|
|
*
|
2003-11-15 00:11:16 +00:00
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* JS array class.
|
|
|
|
*/
|
1998-06-09 23:04:48 +00:00
|
|
|
#include "jsstddef.h"
|
1998-03-28 02:44:41 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-10-14 10:22:38 +00:00
|
|
|
#include "jstypes.h"
|
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
1998-03-28 02:44:41 +00:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsarray.h"
|
|
|
|
#include "jsatom.h"
|
2005-04-17 18:31:59 +00:00
|
|
|
#include "jsbool.h"
|
1998-03-28 02:44:41 +00:00
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "jsconfig.h"
|
|
|
|
#include "jsfun.h"
|
|
|
|
#include "jsgc.h"
|
|
|
|
#include "jsinterp.h"
|
|
|
|
#include "jslock.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsstr.h"
|
|
|
|
|
1998-07-31 00:07:22 +00:00
|
|
|
/* 2^32 - 1 as a number and a string */
|
|
|
|
#define MAXINDEX 4294967295u
|
|
|
|
#define MAXSTR "4294967295"
|
1998-06-09 16:47:00 +00:00
|
|
|
|
1998-07-31 00:07:22 +00:00
|
|
|
/*
|
2004-10-05 10:19:07 +00:00
|
|
|
* Determine if the id represents an array index or an XML property index.
|
1998-07-31 00:07:22 +00:00
|
|
|
*
|
1998-06-09 16:47:00 +00:00
|
|
|
* An id is an array index according to ECMA by (15.4):
|
|
|
|
*
|
|
|
|
* "Array objects give special treatment to a certain class of property names.
|
|
|
|
* A property name P (in the form of a string value) is an array index if and
|
1998-07-31 00:07:22 +00:00
|
|
|
* only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal
|
1998-06-09 16:47:00 +00:00
|
|
|
* to 2^32-1."
|
1998-07-31 00:07:22 +00:00
|
|
|
*
|
1998-06-09 16:47:00 +00:00
|
|
|
* In our implementation, it would be sufficient to check for JSVAL_IS_INT(id)
|
1998-07-31 00:07:22 +00:00
|
|
|
* except that by using signed 32-bit integers we miss the top half of the
|
1998-06-09 16:47:00 +00:00
|
|
|
* valid range. This function checks the string representation itself; note
|
1998-07-31 00:07:22 +00:00
|
|
|
* that calling a standard conversion routine might allow strings such as
|
1998-06-09 16:47:00 +00:00
|
|
|
* "08" or "4.0" as array indices, which they are not.
|
|
|
|
*/
|
2004-10-05 10:19:07 +00:00
|
|
|
JSBool
|
|
|
|
js_IdIsIndex(jsval id, jsuint *indexp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
1998-04-24 00:31:11 +00:00
|
|
|
JSString *str;
|
1998-07-31 00:07:22 +00:00
|
|
|
jschar *cp;
|
1998-04-24 00:31:11 +00:00
|
|
|
|
|
|
|
if (JSVAL_IS_INT(id)) {
|
2003-06-12 00:26:40 +00:00
|
|
|
jsint i;
|
|
|
|
i = JSVAL_TO_INT(id);
|
|
|
|
if (i < 0)
|
|
|
|
return JS_FALSE;
|
|
|
|
*indexp = (jsuint)i;
|
|
|
|
return JS_TRUE;
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
|
|
|
|
2004-10-05 10:19:07 +00:00
|
|
|
/* NB: id should be a string, but jsxml.c may call us with an object id. */
|
|
|
|
if (!JSVAL_IS_STRING(id))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
str = JSVAL_TO_STRING(id);
|
2001-10-25 00:26:38 +00:00
|
|
|
cp = JSSTRING_CHARS(str);
|
|
|
|
if (JS7_ISDEC(*cp) && JSSTRING_LENGTH(str) < sizeof(MAXSTR)) {
|
2003-06-12 00:26:40 +00:00
|
|
|
jsuint index = JS7_UNDEC(*cp++);
|
|
|
|
jsuint oldIndex = 0;
|
|
|
|
jsuint c = 0;
|
|
|
|
if (index != 0) {
|
|
|
|
while (JS7_ISDEC(*cp)) {
|
|
|
|
oldIndex = index;
|
|
|
|
c = JS7_UNDEC(*cp);
|
|
|
|
index = 10*index + c;
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
2004-10-05 10:19:07 +00:00
|
|
|
|
|
|
|
/* Ensure that all characters were consumed and we didn't overflow. */
|
2003-06-12 00:26:40 +00:00
|
|
|
if (*cp == 0 &&
|
|
|
|
(oldIndex < (MAXINDEX / 10) ||
|
|
|
|
(oldIndex == (MAXINDEX / 10) && c < (MAXINDEX % 10))))
|
|
|
|
{
|
|
|
|
*indexp = index;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
1998-06-09 16:47:00 +00:00
|
|
|
}
|
1998-07-31 00:07:22 +00:00
|
|
|
return JS_FALSE;
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
ValueIsLength(JSContext *cx, jsval v, jsuint *lengthp)
|
|
|
|
{
|
|
|
|
jsint i;
|
2001-10-26 02:35:01 +00:00
|
|
|
jsdouble d;
|
1998-07-31 00:07:22 +00:00
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
if (JSVAL_IS_INT(v)) {
|
2001-10-26 02:35:01 +00:00
|
|
|
i = JSVAL_TO_INT(v);
|
1999-09-21 18:58:51 +00:00
|
|
|
if (i < 0) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
2001-10-26 02:35:01 +00:00
|
|
|
JSMSG_BAD_ARRAY_LENGTH);
|
1999-09-21 18:58:51 +00:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2001-10-26 02:35:01 +00:00
|
|
|
*lengthp = (jsuint) i;
|
|
|
|
return JS_TRUE;
|
1998-07-31 00:07:22 +00:00
|
|
|
}
|
2004-10-05 10:19:07 +00:00
|
|
|
|
2001-10-26 02:35:01 +00:00
|
|
|
if (!js_ValueToNumber(cx, v, &d)) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
2002-09-20 18:45:05 +00:00
|
|
|
JSMSG_BAD_ARRAY_LENGTH);
|
|
|
|
return JS_FALSE;
|
2001-10-26 02:35:01 +00:00
|
|
|
}
|
|
|
|
if (!js_DoubleToECMAUint32(cx, d, (uint32 *)lengthp)) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
2002-09-20 18:45:05 +00:00
|
|
|
JSMSG_BAD_ARRAY_LENGTH);
|
|
|
|
return JS_FALSE;
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
2001-10-26 02:35:01 +00:00
|
|
|
if (JSDOUBLE_IS_NaN(d) || d != *lengthp) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_ARRAY_LENGTH);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_GetLengthProperty(JSContext *cx, JSObject *obj, jsuint *lengthp)
|
|
|
|
{
|
2006-09-19 06:48:25 +00:00
|
|
|
JSTempValueRooter tvr;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsid id;
|
2006-09-19 06:48:25 +00:00
|
|
|
JSBool ok;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsint i;
|
|
|
|
|
2006-09-19 06:48:25 +00:00
|
|
|
JS_PUSH_SINGLE_TEMP_ROOT(cx, JSVAL_NULL, &tvr);
|
2004-10-05 10:19:07 +00:00
|
|
|
id = ATOM_TO_JSID(cx->runtime->atomState.lengthAtom);
|
2006-09-19 06:48:25 +00:00
|
|
|
ok = OBJ_GET_PROPERTY(cx, obj, id, &tvr.u.value);
|
|
|
|
if (ok) {
|
|
|
|
/*
|
|
|
|
* Short-circuit, because js_ValueToECMAUint32 fails when called
|
|
|
|
* during init time.
|
|
|
|
*/
|
|
|
|
if (JSVAL_IS_INT(tvr.u.value)) {
|
|
|
|
i = JSVAL_TO_INT(tvr.u.value);
|
|
|
|
*lengthp = (jsuint)i; /* jsuint cast does ToUint32 */
|
|
|
|
} else {
|
|
|
|
ok = js_ValueToECMAUint32(cx, tvr.u.value, (uint32 *)lengthp);
|
|
|
|
}
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
2006-09-19 06:48:25 +00:00
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
|
|
|
return ok;
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2005-10-02 06:27:07 +00:00
|
|
|
IndexToValue(JSContext *cx, jsuint index, jsval *vp)
|
1998-04-24 00:31:11 +00:00
|
|
|
{
|
2005-10-02 06:27:07 +00:00
|
|
|
if (index <= JSVAL_INT_MAX) {
|
|
|
|
*vp = INT_TO_JSVAL(index);
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_TRUE;
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
2005-10-02 06:27:07 +00:00
|
|
|
return js_NewDoubleValue(cx, (jsdouble)index, vp);
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2006-08-12 08:41:54 +00:00
|
|
|
BigIndexToId(JSContext *cx, JSObject *obj, jsuint index, JSBool createAtom,
|
|
|
|
jsid *idp)
|
1998-04-24 00:31:11 +00:00
|
|
|
{
|
2006-08-12 08:41:54 +00:00
|
|
|
jschar buf[10], *start;
|
|
|
|
JSClass *clasp;
|
1998-04-24 00:31:11 +00:00
|
|
|
JSAtom *atom;
|
2006-08-12 08:41:54 +00:00
|
|
|
JS_STATIC_ASSERT((jsuint)-1 == 4294967295U);
|
2006-08-08 20:25:30 +00:00
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
JS_ASSERT(index > JSVAL_INT_MAX);
|
|
|
|
|
|
|
|
start = JS_ARRAY_END(buf);
|
|
|
|
do {
|
|
|
|
--start;
|
|
|
|
*start = (jschar)('0' + index % 10);
|
|
|
|
index /= 10;
|
|
|
|
} while (index != 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip the atomization if the class is known to store atoms corresponding
|
|
|
|
* to big indexes together with elements. In such case we know that the
|
|
|
|
* array does not have an element at the given index if its atom does not
|
|
|
|
* exist.
|
|
|
|
*/
|
|
|
|
if (!createAtom &&
|
|
|
|
((clasp = OBJ_GET_CLASS(cx, obj)) == &js_ArrayClass ||
|
|
|
|
clasp == &js_ArgumentsClass ||
|
|
|
|
clasp == &js_ObjectClass)) {
|
|
|
|
atom = js_GetExistingStringAtom(cx, start, JS_ARRAY_END(buf) - start);
|
|
|
|
if (!atom) {
|
|
|
|
*idp = JSVAL_VOID;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
} else {
|
2006-08-12 08:41:54 +00:00
|
|
|
atom = js_AtomizeChars(cx, start, JS_ARRAY_END(buf) - start, 0);
|
2003-06-12 00:26:40 +00:00
|
|
|
if (!atom)
|
|
|
|
return JS_FALSE;
|
2006-08-08 23:52:08 +00:00
|
|
|
}
|
2006-08-12 08:41:54 +00:00
|
|
|
|
|
|
|
*idp = ATOM_TO_JSID(atom);
|
1998-03-28 02:44:41 +00:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
/*
|
|
|
|
* If the property at the given index exists, get its value into location
|
|
|
|
* pointed by vp and set *hole to false. Otherwise set *hole to true and *vp
|
|
|
|
* to JSVAL_VOID. This function assumes that the location pointed by vp is
|
|
|
|
* properly rooted and can be used as GC-protected storage for temporaries.
|
|
|
|
*/
|
2005-07-13 16:38:46 +00:00
|
|
|
static JSBool
|
2006-08-12 08:41:54 +00:00
|
|
|
GetArrayElement(JSContext *cx, JSObject *obj, jsuint index, JSBool *hole,
|
|
|
|
jsval *vp)
|
2005-07-13 16:38:46 +00:00
|
|
|
{
|
2006-08-12 08:41:54 +00:00
|
|
|
jsid id;
|
2005-07-13 16:38:46 +00:00
|
|
|
JSObject *obj2;
|
|
|
|
JSProperty *prop;
|
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
if (index <= JSVAL_INT_MAX) {
|
|
|
|
id = INT_TO_JSID(index);
|
|
|
|
} else {
|
|
|
|
if (!BigIndexToId(cx, obj, index, JS_FALSE, &id))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (id == JSVAL_VOID) {
|
|
|
|
*hole = JS_TRUE;
|
|
|
|
*vp = JSVAL_VOID;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-13 16:38:46 +00:00
|
|
|
if (!OBJ_LOOKUP_PROPERTY(cx, obj, id, &obj2, &prop))
|
|
|
|
return JS_FALSE;
|
2006-08-12 08:41:54 +00:00
|
|
|
if (!prop) {
|
|
|
|
*hole = JS_TRUE;
|
|
|
|
*vp = JSVAL_VOID;
|
|
|
|
} else {
|
2005-07-13 16:38:46 +00:00
|
|
|
OBJ_DROP_PROPERTY(cx, obj2, prop);
|
2006-08-12 08:41:54 +00:00
|
|
|
if (!OBJ_GET_PROPERTY(cx, obj, id, vp))
|
|
|
|
return JS_FALSE;
|
|
|
|
*hole = JS_FALSE;
|
|
|
|
}
|
2005-07-13 16:38:46 +00:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
/*
|
|
|
|
* Set the value of the property at the given index to v assuming v is rooted.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
SetArrayElement(JSContext *cx, JSObject *obj, jsuint index, jsval v)
|
|
|
|
{
|
|
|
|
jsid id;
|
|
|
|
|
|
|
|
if (index <= JSVAL_INT_MAX) {
|
|
|
|
id = INT_TO_JSID(index);
|
|
|
|
} else {
|
|
|
|
if (!BigIndexToId(cx, obj, index, JS_TRUE, &id))
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_ASSERT(id != JSVAL_VOID);
|
|
|
|
}
|
|
|
|
return OBJ_SET_PROPERTY(cx, obj, id, &v);
|
|
|
|
}
|
2005-10-02 06:27:07 +00:00
|
|
|
|
|
|
|
static JSBool
|
2006-08-12 08:41:54 +00:00
|
|
|
DeleteArrayElement(JSContext *cx, JSObject *obj, jsuint index)
|
2005-10-02 06:27:07 +00:00
|
|
|
{
|
2006-08-12 08:41:54 +00:00
|
|
|
jsid id;
|
|
|
|
jsval junk;
|
2005-10-02 06:27:07 +00:00
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
if (index <= JSVAL_INT_MAX) {
|
|
|
|
id = INT_TO_JSID(index);
|
|
|
|
} else {
|
|
|
|
if (!BigIndexToId(cx, obj, index, JS_FALSE, &id))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (id == JSVAL_VOID)
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
return OBJ_DELETE_PROPERTY(cx, obj, id, &junk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When hole is true, delete the property at the given index. Otherwise set
|
|
|
|
* its value to v assuming v is rooted.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
SetOrDeleteArrayElement(JSContext *cx, JSObject *obj, jsuint index,
|
|
|
|
JSBool hole, jsval v)
|
|
|
|
{
|
|
|
|
if (hole) {
|
|
|
|
JS_ASSERT(v == JSVAL_VOID);
|
|
|
|
return DeleteArrayElement(cx, obj, index);
|
|
|
|
} else {
|
|
|
|
return SetArrayElement(cx, obj, index, v);
|
|
|
|
}
|
2006-08-08 20:25:30 +00:00
|
|
|
}
|
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
JSBool
|
|
|
|
js_SetLengthProperty(JSContext *cx, JSObject *obj, jsuint length)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
|
|
|
jsval v;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsid id;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!IndexToValue(cx, length, &v))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2004-10-05 10:19:07 +00:00
|
|
|
id = ATOM_TO_JSID(cx->runtime->atomState.lengthAtom);
|
1998-04-24 00:31:11 +00:00
|
|
|
return OBJ_SET_PROPERTY(cx, obj, id, &v);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
JSBool
|
|
|
|
js_HasLengthProperty(JSContext *cx, JSObject *obj, jsuint *lengthp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
1998-04-24 00:31:11 +00:00
|
|
|
JSErrorReporter older;
|
2006-09-19 06:48:25 +00:00
|
|
|
JSTempValueRooter tvr;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsid id;
|
|
|
|
JSBool ok;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
older = JS_SetErrorReporter(cx, NULL);
|
2006-09-19 06:48:25 +00:00
|
|
|
JS_PUSH_SINGLE_TEMP_ROOT(cx, JSVAL_NULL, &tvr);
|
2004-10-05 10:19:07 +00:00
|
|
|
id = ATOM_TO_JSID(cx->runtime->atomState.lengthAtom);
|
2006-09-19 06:48:25 +00:00
|
|
|
ok = OBJ_GET_PROPERTY(cx, obj, id, &tvr.u.value);
|
1998-04-24 00:31:11 +00:00
|
|
|
JS_SetErrorReporter(cx, older);
|
2006-09-19 06:48:25 +00:00
|
|
|
if (ok)
|
|
|
|
ok = ValueIsLength(cx, tvr.u.value, lengthp);
|
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
|
|
|
return ok;
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
|
|
|
|
2006-04-27 00:39:43 +00:00
|
|
|
JSBool
|
|
|
|
js_IsArrayLike(JSContext *cx, JSObject *obj, JSBool *answerp, jsuint *lengthp)
|
|
|
|
{
|
|
|
|
JSClass *clasp;
|
2006-06-19 23:38:02 +00:00
|
|
|
|
2006-04-27 00:39:43 +00:00
|
|
|
clasp = OBJ_GET_CLASS(cx, obj);
|
|
|
|
*answerp = (clasp == &js_ArgumentsClass || clasp == &js_ArrayClass);
|
|
|
|
if (!*answerp) {
|
|
|
|
*lengthp = 0;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
return js_GetLengthProperty(cx, obj, lengthp);
|
|
|
|
}
|
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
/*
|
2007-08-02 04:33:52 +00:00
|
|
|
* The 'length' property of all native Array instances is a shared permanent
|
|
|
|
* property of Array.prototype, so it appears to be a direct property of each
|
|
|
|
* array instance delegating to that Array.prototype. It accesses the private
|
|
|
|
* slot reserved by js_ArrayClass.
|
2007-08-02 20:36:53 +00:00
|
|
|
*
|
|
|
|
* Since SpiderMonkey supports cross-class prototype-based delegation, we have
|
|
|
|
* to be careful about the length getter and setter being called on an object
|
|
|
|
* not of Array class. For the getter, we search obj's prototype chain for the
|
|
|
|
* array that caused this getter to be invoked. In the setter case to overcome
|
|
|
|
* the JSPROP_SHARED attribute, we must define a shadowing length property.
|
1998-04-24 00:31:11 +00:00
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
array_length_getter(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
2007-08-02 20:36:53 +00:00
|
|
|
do {
|
|
|
|
if (OBJ_GET_CLASS(cx, obj) == &js_ArrayClass) {
|
|
|
|
*vp = STOBJ_GET_SLOT(obj, JSSLOT_ARRAY_LENGTH);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while ((obj = OBJ_GET_PROTO(cx, obj)) != NULL);
|
2007-08-02 04:33:52 +00:00
|
|
|
return JS_TRUE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
array_length_setter(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
2006-07-01 19:58:44 +00:00
|
|
|
jsuint newlen, oldlen, gap, index;
|
1998-03-28 02:44:41 +00:00
|
|
|
jsval junk;
|
2006-07-01 19:58:44 +00:00
|
|
|
JSObject *iter;
|
|
|
|
JSTempValueRooter tvr;
|
|
|
|
JSBool ok;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2007-08-02 20:36:53 +00:00
|
|
|
if (OBJ_GET_CLASS(cx, obj) != &js_ArrayClass) {
|
|
|
|
jsid lengthId = ATOM_TO_JSID(cx->runtime->atomState.lengthAtom);
|
|
|
|
|
|
|
|
return OBJ_DEFINE_PROPERTY(cx, obj, lengthId, *vp, NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE, NULL);
|
|
|
|
}
|
|
|
|
|
1999-09-21 18:58:51 +00:00
|
|
|
if (!ValueIsLength(cx, *vp, &newlen))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &oldlen))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2006-07-01 19:58:44 +00:00
|
|
|
if (oldlen > newlen) {
|
|
|
|
if (oldlen - newlen < (1 << 24)) {
|
|
|
|
do {
|
|
|
|
--oldlen;
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
|
|
|
!DeleteArrayElement(cx, obj, oldlen)) {
|
2006-07-01 19:58:44 +00:00
|
|
|
return JS_FALSE;
|
2006-12-24 11:31:37 +00:00
|
|
|
}
|
2006-07-01 19:58:44 +00:00
|
|
|
} while (oldlen != newlen);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We are going to remove a lot of indexes in a presumably sparse
|
|
|
|
* array. So instead of looping through indexes between newlen and
|
|
|
|
* oldlen, we iterate through all properties and remove those that
|
2007-08-02 04:33:52 +00:00
|
|
|
* correspond to indexes in the half-open range [newlen, oldlen).
|
2006-07-01 19:58:44 +00:00
|
|
|
* See bug 322135.
|
|
|
|
*/
|
|
|
|
iter = JS_NewPropertyIterator(cx, obj);
|
|
|
|
if (!iter)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* Protect iter against GC in OBJ_DELETE_PROPERTY. */
|
2006-10-27 19:38:33 +00:00
|
|
|
JS_PUSH_TEMP_ROOT_OBJECT(cx, iter, &tvr);
|
2006-07-01 19:58:44 +00:00
|
|
|
gap = oldlen - newlen;
|
|
|
|
for (;;) {
|
2006-12-24 11:31:37 +00:00
|
|
|
ok = (JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) &&
|
2007-08-02 04:33:52 +00:00
|
|
|
JS_NextProperty(cx, iter, &id));
|
2006-07-01 19:58:44 +00:00
|
|
|
if (!ok)
|
|
|
|
break;
|
2007-08-02 04:33:52 +00:00
|
|
|
if (id == JSVAL_VOID)
|
2006-07-01 19:58:44 +00:00
|
|
|
break;
|
2007-08-02 04:33:52 +00:00
|
|
|
if (js_IdIsIndex(id, &index) && index - newlen < gap) {
|
|
|
|
ok = OBJ_DELETE_PROPERTY(cx, obj, id, &junk);
|
2006-07-01 19:58:44 +00:00
|
|
|
if (!ok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
|
|
|
if (!ok)
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2007-08-02 04:33:52 +00:00
|
|
|
if (!IndexToValue(cx, newlen, vp))
|
|
|
|
return JS_FALSE;
|
|
|
|
STOBJ_SET_SLOT(obj, JSSLOT_ARRAY_LENGTH, *vp);
|
|
|
|
return JS_TRUE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
array_addProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
1998-04-24 00:31:11 +00:00
|
|
|
jsuint index, length;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2004-10-05 10:19:07 +00:00
|
|
|
if (!js_IdIsIndex(id, &index))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_TRUE;
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &length))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-04-24 00:31:11 +00:00
|
|
|
if (index >= length) {
|
2003-06-12 00:26:40 +00:00
|
|
|
length = index + 1;
|
|
|
|
return js_SetLengthProperty(cx, obj, length);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
1998-04-24 00:31:11 +00:00
|
|
|
return JS_TRUE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
array_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp)
|
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
return js_TryValueOf(cx, obj, type, vp);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSClass js_ArrayClass = {
|
|
|
|
"Array",
|
2007-08-02 04:33:52 +00:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_CACHED_PROTO(JSProto_Array),
|
1998-04-24 00:31:11 +00:00
|
|
|
array_addProperty, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
|
1999-11-11 21:52:35 +00:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, array_convert, JS_FinalizeStub,
|
|
|
|
JSCLASS_NO_OPTIONAL_MEMBERS
|
1998-03-28 02:44:41 +00:00
|
|
|
};
|
|
|
|
|
2006-01-11 05:18:30 +00:00
|
|
|
enum ArrayToStringOp {
|
2005-11-20 01:12:58 +00:00
|
|
|
TO_STRING,
|
|
|
|
TO_LOCALE_STRING,
|
|
|
|
TO_SOURCE
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When op is TO_STRING or TO_LOCALE_STRING sep indicates a separator to use
|
|
|
|
* or "," when sep is NULL.
|
|
|
|
* When op is TO_SOURCE sep must be NULL.
|
|
|
|
*/
|
1998-03-28 02:44:41 +00:00
|
|
|
static JSBool
|
2006-01-11 05:18:30 +00:00
|
|
|
array_join_sub(JSContext *cx, JSObject *obj, enum ArrayToStringOp op,
|
2007-08-02 06:05:44 +00:00
|
|
|
JSString *sep, jsval *rval)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2006-08-12 08:41:54 +00:00
|
|
|
JSBool ok, hole;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsuint length, index;
|
|
|
|
jschar *chars, *ochars;
|
2005-11-20 01:12:58 +00:00
|
|
|
size_t nchars, growth, seplen, tmplen, extratail;
|
1998-03-28 02:44:41 +00:00
|
|
|
const jschar *sepstr;
|
|
|
|
JSString *str;
|
1998-10-14 10:22:38 +00:00
|
|
|
JSHashEntry *he;
|
2006-09-19 06:48:25 +00:00
|
|
|
JSAtom *atom;
|
2004-12-06 23:17:19 +00:00
|
|
|
int stackDummy;
|
|
|
|
|
|
|
|
if (!JS_CHECK_STACK_SIZE(cx, stackDummy)) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_OVER_RECURSED);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
ok = js_GetLengthProperty(cx, obj, &length);
|
|
|
|
if (!ok)
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-04-24 00:31:11 +00:00
|
|
|
|
2001-11-07 00:15:44 +00:00
|
|
|
he = js_EnterSharpObject(cx, obj, NULL, &chars);
|
|
|
|
if (!he)
|
|
|
|
return JS_FALSE;
|
2005-12-07 22:42:06 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
growth = (size_t) -1;
|
|
|
|
#endif
|
|
|
|
|
2005-11-20 01:12:58 +00:00
|
|
|
if (op == TO_SOURCE) {
|
2003-06-12 00:26:40 +00:00
|
|
|
if (IS_SHARP(he)) {
|
1998-04-24 00:31:11 +00:00
|
|
|
#if JS_HAS_SHARP_VARS
|
2003-06-12 00:26:40 +00:00
|
|
|
nchars = js_strlen(chars);
|
1998-03-28 02:44:41 +00:00
|
|
|
#else
|
2003-06-12 00:26:40 +00:00
|
|
|
chars[0] = '[';
|
|
|
|
chars[1] = ']';
|
|
|
|
chars[2] = 0;
|
|
|
|
nchars = 2;
|
1998-03-28 02:44:41 +00:00
|
|
|
#endif
|
2003-06-12 00:26:40 +00:00
|
|
|
goto make_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-11-20 01:12:58 +00:00
|
|
|
* Always allocate 2 extra chars for closing ']' and terminating 0
|
|
|
|
* and then preallocate 1 + extratail to include starting '['.
|
2003-06-12 00:26:40 +00:00
|
|
|
*/
|
2005-11-20 01:12:58 +00:00
|
|
|
extratail = 2;
|
|
|
|
growth = (1 + extratail) * sizeof(jschar);
|
2003-06-12 00:26:40 +00:00
|
|
|
if (!chars) {
|
|
|
|
nchars = 0;
|
|
|
|
chars = (jschar *) malloc(growth);
|
|
|
|
if (!chars)
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
MAKE_SHARP(he);
|
|
|
|
nchars = js_strlen(chars);
|
2006-02-27 17:32:22 +00:00
|
|
|
growth += nchars * sizeof(jschar);
|
|
|
|
chars = (jschar *)realloc((ochars = chars), growth);
|
2003-06-12 00:26:40 +00:00
|
|
|
if (!chars) {
|
|
|
|
free(ochars);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chars[nchars++] = '[';
|
2005-11-20 01:12:58 +00:00
|
|
|
JS_ASSERT(sep == NULL);
|
|
|
|
sepstr = NULL; /* indicates to use ", " as separator */
|
|
|
|
seplen = 2;
|
1998-03-28 02:44:41 +00:00
|
|
|
} else {
|
2001-11-07 00:15:44 +00:00
|
|
|
/*
|
|
|
|
* Free any sharp variable definition in chars. Normally, we would
|
|
|
|
* MAKE_SHARP(he) so that only the first sharp variable annotation is
|
|
|
|
* a definition, and all the rest are references, but in the current
|
2005-11-20 01:12:58 +00:00
|
|
|
* case of (op != TO_SOURCE), we don't need chars at all.
|
2001-11-07 00:15:44 +00:00
|
|
|
*/
|
|
|
|
if (chars)
|
|
|
|
JS_free(cx, chars);
|
|
|
|
chars = NULL;
|
|
|
|
nchars = 0;
|
2005-11-20 01:12:58 +00:00
|
|
|
extratail = 1; /* allocate extra char for terminating 0 */
|
2001-11-07 00:15:44 +00:00
|
|
|
|
|
|
|
/* Return the empty string on a cycle as well as on empty join. */
|
|
|
|
if (IS_BUSY(he) || length == 0) {
|
|
|
|
js_LeaveSharpObject(cx, NULL);
|
2007-08-02 06:05:44 +00:00
|
|
|
*rval = JS_GetEmptyStringValue(cx);
|
2003-06-12 00:26:40 +00:00
|
|
|
return ok;
|
|
|
|
}
|
2001-11-07 00:15:44 +00:00
|
|
|
|
|
|
|
/* Flag he as BUSY so we can distinguish a cycle from a join-point. */
|
|
|
|
MAKE_BUSY(he);
|
2005-11-20 01:12:58 +00:00
|
|
|
|
|
|
|
if (sep) {
|
2007-08-16 06:23:06 +00:00
|
|
|
JSSTRING_CHARS_AND_LENGTH(sep, sepstr, seplen);
|
2005-11-20 01:12:58 +00:00
|
|
|
} else {
|
|
|
|
sepstr = NULL; /* indicates to use "," as separator */
|
|
|
|
seplen = 1;
|
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2007-08-02 06:05:44 +00:00
|
|
|
/* Use rval to locally root each element value as we loop and convert. */
|
1998-03-28 02:44:41 +00:00
|
|
|
for (index = 0; index < length; index++) {
|
2006-12-24 11:31:37 +00:00
|
|
|
ok = (JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) &&
|
2007-08-02 06:05:44 +00:00
|
|
|
GetArrayElement(cx, obj, index, &hole, rval));
|
2003-06-12 00:26:40 +00:00
|
|
|
if (!ok)
|
|
|
|
goto done;
|
2006-08-12 08:41:54 +00:00
|
|
|
if (hole ||
|
2007-08-02 04:33:52 +00:00
|
|
|
(op != TO_SOURCE &&
|
2007-08-02 06:05:44 +00:00
|
|
|
(JSVAL_IS_VOID(*rval) || JSVAL_IS_NULL(*rval)))) {
|
2003-06-12 00:26:40 +00:00
|
|
|
str = cx->runtime->emptyString;
|
2000-08-09 21:46:03 +00:00
|
|
|
} else {
|
2005-11-20 01:12:58 +00:00
|
|
|
if (op == TO_LOCALE_STRING) {
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *robj;
|
|
|
|
|
2006-09-19 06:48:25 +00:00
|
|
|
atom = cx->runtime->atomState.toLocaleStringAtom;
|
2007-08-02 06:05:44 +00:00
|
|
|
ok = js_ValueToObject(cx, *rval, &robj);
|
2007-08-02 04:33:52 +00:00
|
|
|
if (ok) {
|
2007-08-02 06:05:44 +00:00
|
|
|
/* Re-use *rval to protect robj temporarily. */
|
|
|
|
*rval = OBJECT_TO_JSVAL(robj);
|
|
|
|
ok = js_TryMethod(cx, robj, atom, 0, NULL, rval);
|
2007-08-02 04:33:52 +00:00
|
|
|
}
|
2006-09-19 06:48:25 +00:00
|
|
|
if (!ok)
|
|
|
|
goto done;
|
2007-08-02 06:05:44 +00:00
|
|
|
str = js_ValueToString(cx, *rval);
|
2005-11-20 01:12:58 +00:00
|
|
|
} else if (op == TO_STRING) {
|
2007-08-02 06:05:44 +00:00
|
|
|
str = js_ValueToString(cx, *rval);
|
2001-11-07 00:15:44 +00:00
|
|
|
} else {
|
2005-11-20 01:12:58 +00:00
|
|
|
JS_ASSERT(op == TO_SOURCE);
|
2007-08-02 06:05:44 +00:00
|
|
|
str = js_ValueToSource(cx, *rval);
|
2001-11-07 00:15:44 +00:00
|
|
|
}
|
2003-06-12 00:26:40 +00:00
|
|
|
if (!str) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
/*
|
|
|
|
* Do not append separator after the last element unless it is a hole
|
|
|
|
* and we are in toSource. In that case we append single ",".
|
|
|
|
*/
|
2005-11-20 01:12:58 +00:00
|
|
|
if (index + 1 == length)
|
2006-08-12 08:41:54 +00:00
|
|
|
seplen = (hole && op == TO_SOURCE) ? 1 : 0;
|
2005-11-20 01:12:58 +00:00
|
|
|
|
|
|
|
/* Allocate 1 at end for closing bracket and zero. */
|
2006-06-15 03:26:21 +00:00
|
|
|
tmplen = JSSTRING_LENGTH(str);
|
|
|
|
growth = nchars + tmplen + seplen + extratail;
|
|
|
|
if (nchars > growth || tmplen > growth ||
|
|
|
|
growth > (size_t)-1 / sizeof(jschar)) {
|
|
|
|
if (chars) {
|
|
|
|
free(chars);
|
|
|
|
chars = NULL;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
growth *= sizeof(jschar);
|
2006-12-24 11:31:37 +00:00
|
|
|
JS_COUNT_OPERATION(cx, JSOW_ALLOCATION);
|
2003-06-12 00:26:40 +00:00
|
|
|
if (!chars) {
|
|
|
|
chars = (jschar *) malloc(growth);
|
|
|
|
if (!chars)
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
chars = (jschar *) realloc((ochars = chars), growth);
|
|
|
|
if (!chars) {
|
|
|
|
free(ochars);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-25 00:26:38 +00:00
|
|
|
js_strncpy(&chars[nchars], JSSTRING_CHARS(str), tmplen);
|
|
|
|
nchars += tmplen;
|
2005-11-20 01:12:58 +00:00
|
|
|
|
|
|
|
if (seplen) {
|
|
|
|
if (sepstr) {
|
|
|
|
js_strncpy(&chars[nchars], sepstr, seplen);
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(seplen == 1 || seplen == 2);
|
|
|
|
chars[nchars] = ',';
|
|
|
|
if (seplen == 2)
|
|
|
|
chars[nchars + 1] = ' ';
|
|
|
|
}
|
|
|
|
nchars += seplen;
|
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
done:
|
2005-11-20 01:12:58 +00:00
|
|
|
if (op == TO_SOURCE) {
|
|
|
|
if (chars)
|
2003-06-12 00:26:40 +00:00
|
|
|
chars[nchars++] = ']';
|
2001-11-07 00:15:44 +00:00
|
|
|
} else {
|
|
|
|
CLEAR_BUSY(he);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2001-11-07 00:15:44 +00:00
|
|
|
js_LeaveSharpObject(cx, NULL);
|
1998-03-28 02:44:41 +00:00
|
|
|
if (!ok) {
|
2003-06-12 00:26:40 +00:00
|
|
|
if (chars)
|
|
|
|
free(chars);
|
|
|
|
return ok;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
make_string:
|
|
|
|
if (!chars) {
|
2003-06-12 00:26:40 +00:00
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
chars[nchars] = 0;
|
2005-12-07 22:42:06 +00:00
|
|
|
JS_ASSERT(growth == (size_t)-1 || (nchars + 1) * sizeof(jschar) == growth);
|
2007-08-16 06:23:06 +00:00
|
|
|
str = js_NewString(cx, chars, nchars);
|
1998-03-28 02:44:41 +00:00
|
|
|
if (!str) {
|
2003-06-12 00:26:40 +00:00
|
|
|
free(chars);
|
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2007-08-02 06:05:44 +00:00
|
|
|
*rval = STRING_TO_JSVAL(str);
|
1998-03-28 02:44:41 +00:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_toSource(JSContext *cx, uintN argc, jsval *vp)
|
1998-04-24 00:31:11 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
obj = JSVAL_TO_OBJECT(vp[1]);
|
|
|
|
if (!JS_InstanceOf(cx, obj, &js_ArrayClass, vp + 2))
|
2007-07-10 05:07:20 +00:00
|
|
|
return JS_FALSE;
|
2007-08-02 06:05:44 +00:00
|
|
|
return array_join_sub(cx, obj, TO_SOURCE, NULL, vp);
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_toString(JSContext *cx, uintN argc, jsval *vp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
obj = JSVAL_TO_OBJECT(vp[1]);
|
|
|
|
if (!JS_InstanceOf(cx, obj, &js_ArrayClass, vp + 2))
|
2007-07-10 05:07:20 +00:00
|
|
|
return JS_FALSE;
|
2007-08-02 06:05:44 +00:00
|
|
|
return array_join_sub(cx, obj, TO_STRING, NULL, vp);
|
2000-08-09 21:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_toLocaleString(JSContext *cx, uintN argc, jsval *vp)
|
2000-08-09 21:46:03 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
obj = JSVAL_TO_OBJECT(vp[1]);
|
|
|
|
if (!JS_InstanceOf(cx, obj, &js_ArrayClass, vp + 2))
|
2007-07-10 05:07:20 +00:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2000-08-09 21:46:03 +00:00
|
|
|
/*
|
2001-03-10 00:20:42 +00:00
|
|
|
* Passing comma here as the separator. Need a way to get a
|
2001-01-04 10:13:18 +00:00
|
|
|
* locale-specific version.
|
2000-08-09 21:46:03 +00:00
|
|
|
*/
|
2007-08-02 06:05:44 +00:00
|
|
|
return array_join_sub(cx, obj, TO_LOCALE_STRING, NULL, vp);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2002-03-14 00:10:31 +00:00
|
|
|
static JSBool
|
2006-08-12 08:41:54 +00:00
|
|
|
InitArrayElements(JSContext *cx, JSObject *obj, jsuint start, jsuint end,
|
|
|
|
jsval *vector)
|
2002-03-14 00:10:31 +00:00
|
|
|
{
|
2006-08-12 08:41:54 +00:00
|
|
|
while (start != end) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
|
|
|
!SetArrayElement(cx, obj, start++, *vector++)) {
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2006-12-24 11:31:37 +00:00
|
|
|
}
|
2002-03-14 00:10:31 +00:00
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
static JSBool
|
1998-04-24 00:31:11 +00:00
|
|
|
InitArrayObject(JSContext *cx, JSObject *obj, jsuint length, jsval *vector)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
1998-04-24 00:31:11 +00:00
|
|
|
jsval v;
|
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
JS_ASSERT(OBJ_GET_CLASS(cx, obj) == &js_ArrayClass);
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!IndexToValue(cx, length, &v))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2007-08-02 04:33:52 +00:00
|
|
|
STOBJ_SET_SLOT(obj, JSSLOT_ARRAY_LENGTH, v);
|
|
|
|
return !vector || InitArrayElements(cx, obj, 0, length, vector);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2003-06-12 00:26:40 +00:00
|
|
|
/*
|
|
|
|
* Perl-inspired join, reverse, and sort.
|
|
|
|
*/
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_join(JSContext *cx, uintN argc, jsval *vp)
|
2003-06-12 00:26:40 +00:00
|
|
|
{
|
|
|
|
JSString *str;
|
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
if (JSVAL_IS_VOID(vp[2])) {
|
2005-11-20 01:12:58 +00:00
|
|
|
str = NULL;
|
|
|
|
} else {
|
2007-08-02 04:33:52 +00:00
|
|
|
str = js_ValueToString(cx, vp[2]);
|
2005-11-20 01:12:58 +00:00
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2007-08-02 04:33:52 +00:00
|
|
|
vp[2] = STRING_TO_JSVAL(str);
|
2005-11-20 01:12:58 +00:00
|
|
|
}
|
2007-08-02 06:05:44 +00:00
|
|
|
return array_join_sub(cx, JS_THIS_OBJECT(cx, vp), TO_STRING, str, vp);
|
2003-06-12 00:26:40 +00:00
|
|
|
}
|
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_reverse(JSContext *cx, uintN argc, jsval *vp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *obj;
|
|
|
|
jsval *argv, *tmproot, *tmproot2;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsuint len, half, i;
|
2006-08-12 08:41:54 +00:00
|
|
|
JSBool hole, hole2;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &len))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2005-10-10 23:33:16 +00:00
|
|
|
/*
|
|
|
|
* Use argv[argc] and argv[argc + 1] as local roots to hold temporarily
|
|
|
|
* array elements for GC-safe swap.
|
|
|
|
*/
|
2007-08-02 04:33:52 +00:00
|
|
|
argv = JS_ARGV(cx, vp);
|
2005-10-10 23:33:16 +00:00
|
|
|
tmproot = argv + argc;
|
|
|
|
tmproot2 = argv + argc + 1;
|
1998-04-24 00:31:11 +00:00
|
|
|
half = len / 2;
|
|
|
|
for (i = 0; i < half; i++) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
|
|
|
!GetArrayElement(cx, obj, i, &hole, tmproot) ||
|
2006-08-12 08:41:54 +00:00
|
|
|
!GetArrayElement(cx, obj, len - i - 1, &hole2, tmproot2) ||
|
|
|
|
!SetOrDeleteArrayElement(cx, obj, len - i - 1, hole, *tmproot) ||
|
|
|
|
!SetOrDeleteArrayElement(cx, obj, i, hole2, *tmproot2)) {
|
|
|
|
return JS_FALSE;
|
2005-07-13 16:38:46 +00:00
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2007-08-02 04:33:52 +00:00
|
|
|
*vp = OBJECT_TO_JSVAL(obj);
|
2006-08-12 08:41:54 +00:00
|
|
|
return JS_TRUE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
typedef struct MSortArgs {
|
1998-03-28 02:44:41 +00:00
|
|
|
size_t elsize;
|
|
|
|
JSComparator cmp;
|
|
|
|
void *arg;
|
2003-02-27 01:43:44 +00:00
|
|
|
JSBool fastcopy;
|
2006-10-05 23:28:51 +00:00
|
|
|
} MSortArgs;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2005-11-16 10:02:57 +00:00
|
|
|
static JSBool
|
|
|
|
sort_compare(void *arg, const void *a, const void *b, int *result);
|
2001-01-04 10:13:18 +00:00
|
|
|
|
2002-05-27 05:53:57 +00:00
|
|
|
static int
|
2005-11-16 10:02:57 +00:00
|
|
|
sort_compare_strings(void *arg, const void *a, const void *b, int *result);
|
2002-05-27 05:53:57 +00:00
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
/* Helper function for js_MergeSort. */
|
2005-11-16 10:02:57 +00:00
|
|
|
static JSBool
|
2006-10-05 23:28:51 +00:00
|
|
|
MergeArrays(MSortArgs *msa, void *src, void *dest, size_t run1, size_t run2)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2006-10-05 23:28:51 +00:00
|
|
|
void *arg, *a, *b, *c;
|
|
|
|
size_t elsize, runtotal;
|
|
|
|
int cmp_result;
|
2001-01-04 10:13:18 +00:00
|
|
|
JSComparator cmp;
|
2003-02-27 01:43:44 +00:00
|
|
|
JSBool fastcopy;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
runtotal = run1 + run2;
|
|
|
|
|
|
|
|
elsize = msa->elsize;
|
|
|
|
cmp = msa->cmp;
|
|
|
|
arg = msa->arg;
|
|
|
|
fastcopy = msa->fastcopy;
|
2001-01-04 10:13:18 +00:00
|
|
|
|
2005-11-16 10:02:57 +00:00
|
|
|
#define CALL_CMP(a, b) \
|
|
|
|
if (!cmp(arg, (a), (b), &cmp_result)) return JS_FALSE;
|
2001-01-04 10:13:18 +00:00
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
/* Copy runs already in sorted order. */
|
|
|
|
b = (char *)src + run1 * elsize;
|
|
|
|
a = (char *)b - elsize;
|
|
|
|
CALL_CMP(a, b);
|
|
|
|
if (cmp_result <= 0) {
|
|
|
|
memcpy(dest, src, runtotal * elsize);
|
|
|
|
return JS_TRUE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2001-01-04 10:13:18 +00:00
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
#define COPY_ONE(p,q,n) \
|
|
|
|
(fastcopy ? (void)(*(jsval*)(p) = *(jsval*)(q)) : (void)memcpy(p, q, n))
|
|
|
|
|
|
|
|
a = src;
|
|
|
|
c = dest;
|
|
|
|
for (; runtotal != 0; runtotal--) {
|
|
|
|
JSBool from_a = run2 == 0;
|
|
|
|
if (!from_a && run1 != 0) {
|
|
|
|
CALL_CMP(a,b);
|
|
|
|
from_a = cmp_result <= 0;
|
2005-11-16 10:02:57 +00:00
|
|
|
}
|
2002-03-13 01:50:13 +00:00
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
if (from_a) {
|
|
|
|
COPY_ONE(c, a, elsize);
|
|
|
|
run1--;
|
|
|
|
a = (char *)a + elsize;
|
|
|
|
} else {
|
|
|
|
COPY_ONE(c, b, elsize);
|
|
|
|
run2--;
|
|
|
|
b = (char *)b + elsize;
|
|
|
|
}
|
|
|
|
c = (char *)c + elsize;
|
2002-03-13 01:50:13 +00:00
|
|
|
}
|
2006-10-05 23:28:51 +00:00
|
|
|
#undef COPY_ONE
|
2005-11-16 10:02:57 +00:00
|
|
|
#undef CALL_CMP
|
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
return JS_TRUE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
/*
|
|
|
|
* This sort is stable, i.e. sequence of equal elements is preserved.
|
|
|
|
* See also bug #224128.
|
|
|
|
*/
|
2005-11-16 10:02:57 +00:00
|
|
|
JSBool
|
2006-10-05 23:28:51 +00:00
|
|
|
js_MergeSort(void *src, size_t nel, size_t elsize,
|
|
|
|
JSComparator cmp, void *arg, void *tmp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2006-10-05 23:28:51 +00:00
|
|
|
void *swap, *vec1, *vec2;
|
|
|
|
MSortArgs msa;
|
|
|
|
size_t i, j, lo, hi, run;
|
|
|
|
JSBool fastcopy;
|
|
|
|
int cmp_result;
|
|
|
|
|
|
|
|
fastcopy = (cmp == sort_compare || cmp == sort_compare_strings);
|
|
|
|
#define COPY_ONE(p,q,n) \
|
|
|
|
(fastcopy ? (void)(*(jsval*)(p) = *(jsval*)(q)) : (void)memcpy(p, q, n))
|
|
|
|
#define CALL_CMP(a, b) \
|
|
|
|
if (!cmp(arg, (a), (b), &cmp_result)) return JS_FALSE;
|
|
|
|
#define INS_SORT_INT 4
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Apply insertion sort to small chunks to reduce the number of merge
|
|
|
|
* passes needed.
|
|
|
|
*/
|
|
|
|
for (lo = 0; lo < nel; lo += INS_SORT_INT) {
|
|
|
|
hi = lo + INS_SORT_INT;
|
|
|
|
if (hi >= nel)
|
|
|
|
hi = nel;
|
|
|
|
for (i = lo + 1; i < hi; i++) {
|
|
|
|
vec1 = (char *)src + i * elsize;
|
|
|
|
vec2 = (char *)vec1 - elsize;
|
|
|
|
for (j = i; j > lo; j--) {
|
|
|
|
CALL_CMP(vec2, vec1);
|
|
|
|
/* "<=" instead of "<" insures the sort is stable */
|
|
|
|
if (cmp_result <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Swap elements, using "tmp" as tmp storage */
|
|
|
|
COPY_ONE(tmp, vec2, elsize);
|
|
|
|
COPY_ONE(vec2, vec1, elsize);
|
|
|
|
COPY_ONE(vec1, tmp, elsize);
|
|
|
|
vec1 = vec2;
|
|
|
|
vec2 = (char *)vec1 - elsize;
|
|
|
|
}
|
|
|
|
}
|
2005-11-16 10:02:57 +00:00
|
|
|
}
|
2006-10-05 23:28:51 +00:00
|
|
|
#undef CALL_CMP
|
|
|
|
#undef COPY_ONE
|
|
|
|
|
|
|
|
msa.elsize = elsize;
|
|
|
|
msa.cmp = cmp;
|
|
|
|
msa.arg = arg;
|
|
|
|
msa.fastcopy = fastcopy;
|
|
|
|
|
|
|
|
vec1 = src;
|
|
|
|
vec2 = tmp;
|
|
|
|
for (run = INS_SORT_INT; run < nel; run *= 2) {
|
|
|
|
for (lo = 0; lo < nel; lo += 2 * run) {
|
|
|
|
hi = lo + run;
|
|
|
|
if (hi >= nel) {
|
|
|
|
memcpy((char *)vec2 + lo * elsize, (char *)vec1 + lo * elsize,
|
|
|
|
(nel - lo) * elsize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!MergeArrays(&msa, (char *)vec1 + lo * elsize,
|
|
|
|
(char *)vec2 + lo * elsize, run,
|
|
|
|
hi + run > nel ? nel - hi : run)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
swap = vec1;
|
|
|
|
vec1 = vec2;
|
|
|
|
vec2 = swap;
|
2005-11-16 10:02:57 +00:00
|
|
|
}
|
2006-10-05 23:28:51 +00:00
|
|
|
if (src != vec1)
|
|
|
|
memcpy(src, tmp, nel * elsize);
|
2005-11-16 10:02:57 +00:00
|
|
|
|
|
|
|
return JS_TRUE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct CompareArgs {
|
2005-09-02 17:49:05 +00:00
|
|
|
JSContext *context;
|
|
|
|
jsval fval;
|
|
|
|
jsval *localroot; /* need one local root, for sort_compare */
|
1998-03-28 02:44:41 +00:00
|
|
|
} CompareArgs;
|
|
|
|
|
2005-11-16 10:02:57 +00:00
|
|
|
static JSBool
|
|
|
|
sort_compare(void *arg, const void *a, const void *b, int *result)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
1998-04-24 00:31:11 +00:00
|
|
|
jsval av = *(const jsval *)a, bv = *(const jsval *)b;
|
2000-02-04 02:01:49 +00:00
|
|
|
CompareArgs *ca = (CompareArgs *) arg;
|
1998-03-28 02:44:41 +00:00
|
|
|
JSContext *cx = ca->context;
|
2006-03-15 20:49:09 +00:00
|
|
|
jsval fval;
|
1998-03-28 02:44:41 +00:00
|
|
|
JSBool ok;
|
|
|
|
|
2006-03-15 20:49:09 +00:00
|
|
|
/**
|
|
|
|
* array_sort deals with holes and undefs on its own and they should not
|
|
|
|
* come here.
|
2005-06-08 16:38:38 +00:00
|
|
|
*/
|
2006-03-15 20:49:09 +00:00
|
|
|
JS_ASSERT(av != JSVAL_VOID);
|
|
|
|
JS_ASSERT(bv != JSVAL_VOID);
|
2005-06-08 16:38:38 +00:00
|
|
|
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2006-03-15 20:49:09 +00:00
|
|
|
*result = 0;
|
2005-11-16 10:02:57 +00:00
|
|
|
ok = JS_TRUE;
|
2006-03-15 20:49:09 +00:00
|
|
|
fval = ca->fval;
|
|
|
|
if (fval == JSVAL_NULL) {
|
2003-06-12 00:26:40 +00:00
|
|
|
JSString *astr, *bstr;
|
|
|
|
|
2006-03-15 20:49:09 +00:00
|
|
|
if (av != bv) {
|
2005-09-02 17:49:05 +00:00
|
|
|
/*
|
|
|
|
* Set our local root to astr in case the second js_ValueToString
|
|
|
|
* displaces the newborn root in cx, and the GC nests under that
|
|
|
|
* call. Don't bother guarding the local root store with an astr
|
|
|
|
* non-null test. If we tag null as a string, the GC will untag,
|
|
|
|
* null-test, and avoid dereferencing null.
|
|
|
|
*/
|
|
|
|
astr = js_ValueToString(cx, av);
|
|
|
|
*ca->localroot = STRING_TO_JSVAL(astr);
|
|
|
|
if (astr && (bstr = js_ValueToString(cx, bv)))
|
2006-03-15 20:49:09 +00:00
|
|
|
*result = js_CompareStrings(astr, bstr);
|
2005-09-02 17:49:05 +00:00
|
|
|
else
|
2005-11-16 10:02:57 +00:00
|
|
|
ok = JS_FALSE;
|
2003-06-12 00:26:40 +00:00
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
} else {
|
2006-03-15 20:49:09 +00:00
|
|
|
jsdouble cmp;
|
2006-09-19 06:48:25 +00:00
|
|
|
jsval argv[2];
|
2006-03-15 20:49:09 +00:00
|
|
|
|
2003-06-12 00:26:40 +00:00
|
|
|
argv[0] = av;
|
|
|
|
argv[1] = bv;
|
|
|
|
ok = js_InternalCall(cx,
|
|
|
|
OBJ_GET_PARENT(cx, JSVAL_TO_OBJECT(fval)),
|
2006-09-19 06:48:25 +00:00
|
|
|
fval, 2, argv, ca->localroot);
|
2003-06-12 00:26:40 +00:00
|
|
|
if (ok) {
|
2006-09-19 06:48:25 +00:00
|
|
|
ok = js_ValueToNumber(cx, *ca->localroot, &cmp);
|
2005-09-02 17:49:05 +00:00
|
|
|
|
2003-06-12 00:26:40 +00:00
|
|
|
/* Clamp cmp to -1, 0, 1. */
|
2005-09-02 17:49:05 +00:00
|
|
|
if (ok) {
|
|
|
|
if (JSDOUBLE_IS_NaN(cmp)) {
|
|
|
|
/*
|
|
|
|
* XXX report some kind of error here? ECMA talks about
|
|
|
|
* 'consistent compare functions' that don't return NaN,
|
|
|
|
* but is silent about what the result should be. So we
|
|
|
|
* currently ignore it.
|
|
|
|
*/
|
|
|
|
} else if (cmp != 0) {
|
2006-03-15 20:49:09 +00:00
|
|
|
*result = cmp > 0 ? 1 : -1;
|
2005-09-02 17:49:05 +00:00
|
|
|
}
|
2003-06-12 00:26:40 +00:00
|
|
|
}
|
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2005-11-16 10:02:57 +00:00
|
|
|
return ok;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2002-05-27 05:53:57 +00:00
|
|
|
static int
|
2005-11-16 10:02:57 +00:00
|
|
|
sort_compare_strings(void *arg, const void *a, const void *b, int *result)
|
2002-05-27 05:53:57 +00:00
|
|
|
{
|
|
|
|
jsval av = *(const jsval *)a, bv = *(const jsval *)b;
|
|
|
|
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT((JSContext *)arg, JSOW_JUMP))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2005-11-16 10:02:57 +00:00
|
|
|
*result = (int) js_CompareStrings(JSVAL_TO_STRING(av), JSVAL_TO_STRING(bv));
|
|
|
|
return JS_TRUE;
|
2002-05-27 05:53:57 +00:00
|
|
|
}
|
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
/*
|
|
|
|
* The array_sort function below assumes JSVAL_NULL is zero in order to
|
|
|
|
* perform initialization using memset. Other parts of SpiderMonkey likewise
|
|
|
|
* "know" that JSVAL_NULL is zero; this static assertion covers all cases.
|
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT(JSVAL_NULL == 0);
|
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_sort(JSContext *cx, uintN argc, jsval *vp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
jsval *argv, fval, *vec, *mergesort_tmp;
|
|
|
|
JSObject *obj;
|
1998-03-28 02:44:41 +00:00
|
|
|
CompareArgs ca;
|
2006-03-15 20:49:09 +00:00
|
|
|
jsuint len, newlen, i, undefs;
|
2006-08-12 08:41:54 +00:00
|
|
|
JSTempValueRooter tvr;
|
|
|
|
JSBool hole, ok;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2002-05-27 05:53:57 +00:00
|
|
|
/*
|
|
|
|
* Optimize the default compare function case if all of obj's elements
|
|
|
|
* have values of type string.
|
|
|
|
*/
|
|
|
|
JSBool all_strings;
|
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
argv = JS_ARGV(cx, vp);
|
1998-03-28 02:44:41 +00:00
|
|
|
if (argc > 0) {
|
2003-06-12 00:26:40 +00:00
|
|
|
if (JSVAL_IS_PRIMITIVE(argv[0])) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_SORT_ARG);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
fval = argv[0];
|
2002-05-27 05:53:57 +00:00
|
|
|
all_strings = JS_FALSE; /* non-default compare function */
|
1998-03-28 02:44:41 +00:00
|
|
|
} else {
|
2003-06-12 00:26:40 +00:00
|
|
|
fval = JSVAL_NULL;
|
2002-05-27 05:53:57 +00:00
|
|
|
all_strings = JS_TRUE; /* check for all string values */
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &len))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2002-11-07 10:51:23 +00:00
|
|
|
if (len == 0) {
|
2007-08-02 04:33:52 +00:00
|
|
|
*vp = OBJECT_TO_JSVAL(obj);
|
2000-06-03 19:00:28 +00:00
|
|
|
return JS_TRUE;
|
2002-11-07 10:51:23 +00:00
|
|
|
}
|
2002-07-31 21:42:12 +00:00
|
|
|
|
2005-10-09 06:09:21 +00:00
|
|
|
/*
|
2006-10-05 23:28:51 +00:00
|
|
|
* We need a temporary array of 2 * len jsvals to hold the array elements.
|
2005-10-12 07:39:45 +00:00
|
|
|
* Check that its size does not overflow size_t, which would allow for
|
|
|
|
* indexing beyond the end of the malloc'd vector.
|
2005-10-09 06:09:21 +00:00
|
|
|
*/
|
2006-10-05 23:28:51 +00:00
|
|
|
if (len > (size_t)-1 / (2 * sizeof(jsval))) {
|
2002-07-31 21:42:12 +00:00
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2005-10-12 07:39:45 +00:00
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
/*
|
|
|
|
* Allocate 2 * len instead of len, to reserve space for the mergesort
|
|
|
|
* algorithm.
|
|
|
|
*/
|
|
|
|
vec = (jsval *) JS_malloc(cx, 2 * (size_t)len * sizeof(jsval));
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!vec)
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2006-03-17 22:09:46 +00:00
|
|
|
/*
|
|
|
|
* Initialize vec as a root. We will clear elements of vec one by
|
2006-08-12 08:41:54 +00:00
|
|
|
* one while increasing tvr.count when we know that the property at
|
2006-03-17 22:09:46 +00:00
|
|
|
* the corresponding index exists and its value must be rooted.
|
|
|
|
*
|
2006-07-01 19:52:32 +00:00
|
|
|
* In this way when sorting a huge mostly sparse array we will not
|
2006-03-17 22:09:46 +00:00
|
|
|
* access the tail of vec corresponding to properties that do not
|
2006-08-12 08:41:54 +00:00
|
|
|
* exist, allowing OS to avoiding committing RAM. See bug 330812.
|
|
|
|
*
|
|
|
|
* After this point control must flow through label out: to exit.
|
2006-03-17 22:09:46 +00:00
|
|
|
*/
|
2006-08-12 08:41:54 +00:00
|
|
|
JS_PUSH_TEMP_ROOT(cx, 0, vec, &tvr);
|
2004-11-30 17:52:29 +00:00
|
|
|
|
2006-03-15 20:49:09 +00:00
|
|
|
/*
|
2006-03-16 08:04:28 +00:00
|
|
|
* By ECMA 262, 15.4.4.11, a property that does not exist (which we
|
|
|
|
* call a "hole") is always greater than an existing property with
|
|
|
|
* value undefined and that is always greater than any other property.
|
|
|
|
* Thus to sort holes and undefs we simply count them, sort the rest
|
|
|
|
* of elements, append undefs after them and then make holes after
|
|
|
|
* undefs.
|
2006-03-15 20:49:09 +00:00
|
|
|
*/
|
|
|
|
undefs = 0;
|
2005-10-12 07:39:45 +00:00
|
|
|
newlen = 0;
|
1998-04-24 00:31:11 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2006-12-24 11:31:37 +00:00
|
|
|
ok = JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP);
|
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
|
2006-08-08 23:52:08 +00:00
|
|
|
/* Clear vec[newlen] before including it in the rooted set. */
|
|
|
|
vec[newlen] = JSVAL_NULL;
|
2006-08-12 08:41:54 +00:00
|
|
|
tvr.count = newlen + 1;
|
|
|
|
ok = GetArrayElement(cx, obj, i, &hole, &vec[newlen]);
|
2006-08-08 23:52:08 +00:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
if (hole)
|
|
|
|
continue;
|
|
|
|
|
2006-03-15 20:49:09 +00:00
|
|
|
if (vec[newlen] == JSVAL_VOID) {
|
|
|
|
++undefs;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-05-27 05:53:57 +00:00
|
|
|
/* We know JSVAL_IS_STRING yields 0 or 1, so avoid a branch via &=. */
|
2006-03-15 20:49:09 +00:00
|
|
|
all_strings &= JSVAL_IS_STRING(vec[newlen]);
|
|
|
|
|
|
|
|
++newlen;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2006-10-05 23:28:51 +00:00
|
|
|
/*
|
|
|
|
* The first newlen elements of vec are copied from the array
|
|
|
|
* object (above).
|
|
|
|
*
|
|
|
|
* Of the remaining 2*len-newlen positions, newlen are used as GC
|
|
|
|
* rooted temp space for mergesort, and the last (2*len-2*newlen)
|
|
|
|
* positions are unused.
|
|
|
|
*
|
|
|
|
* Here we clear the tmp-values before GC-rooting the array.
|
|
|
|
* We assume JSVAL_NULL==0 to optimize initialization using memset.
|
|
|
|
*/
|
|
|
|
mergesort_tmp = vec + newlen;
|
|
|
|
memset(mergesort_tmp, 0, newlen * sizeof(jsval));
|
2006-11-16 14:35:28 +00:00
|
|
|
tvr.count = newlen * 2;
|
2006-10-05 23:28:51 +00:00
|
|
|
|
|
|
|
/* Here len == 2 * (newlen + undefs + number_of_holes). */
|
2006-12-24 11:31:37 +00:00
|
|
|
if (all_strings) {
|
|
|
|
ok = js_MergeSort(vec, (size_t) newlen, sizeof(jsval),
|
|
|
|
sort_compare_strings, cx, mergesort_tmp);
|
|
|
|
} else {
|
|
|
|
ca.context = cx;
|
|
|
|
ca.fval = fval;
|
|
|
|
ca.localroot = argv + argc; /* local GC root for temporary string */
|
|
|
|
ok = js_MergeSort(vec, (size_t) newlen, sizeof(jsval),
|
|
|
|
sort_compare, &ca, mergesort_tmp);
|
|
|
|
}
|
2006-07-01 19:52:32 +00:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
2005-11-16 10:02:57 +00:00
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
ok = InitArrayElements(cx, obj, 0, newlen, vec);
|
|
|
|
if (!ok)
|
|
|
|
goto out;
|
1999-04-27 15:18:57 +00:00
|
|
|
|
2006-07-01 19:52:32 +00:00
|
|
|
out:
|
2006-08-12 08:41:54 +00:00
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
2006-07-01 19:52:32 +00:00
|
|
|
JS_free(cx, vec);
|
|
|
|
if (!ok)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
/* Set undefs that sorted after the rest of elements. */
|
|
|
|
while (undefs != 0) {
|
|
|
|
--undefs;
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
|
|
|
!SetArrayElement(cx, obj, newlen++, JSVAL_VOID)) {
|
2006-08-12 08:41:54 +00:00
|
|
|
return JS_FALSE;
|
2006-12-24 11:31:37 +00:00
|
|
|
}
|
2006-08-12 08:41:54 +00:00
|
|
|
}
|
|
|
|
|
2006-07-01 19:52:32 +00:00
|
|
|
/* Re-create any holes that sorted to the end of the array. */
|
|
|
|
while (len > newlen) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
|
|
|
!DeleteArrayElement(cx, obj, --len)) {
|
2006-07-01 19:52:32 +00:00
|
|
|
return JS_FALSE;
|
2006-12-24 11:31:37 +00:00
|
|
|
}
|
2006-07-01 19:52:32 +00:00
|
|
|
}
|
2007-08-02 04:33:52 +00:00
|
|
|
*vp = OBJECT_TO_JSVAL(obj);
|
2006-07-01 19:52:32 +00:00
|
|
|
return JS_TRUE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-06-12 00:26:40 +00:00
|
|
|
* Perl-inspired push, pop, shift, unshift, and splice methods.
|
1998-03-28 02:44:41 +00:00
|
|
|
*/
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
slow_array_push(JSContext *cx, JSObject *obj, uintN argc, jsval *vp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2006-08-12 08:41:54 +00:00
|
|
|
jsuint length, newlength;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &length))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2006-08-12 08:41:54 +00:00
|
|
|
newlength = length + argc;
|
2007-08-02 04:33:52 +00:00
|
|
|
if (!InitArrayElements(cx, obj, length, newlength, vp + 2))
|
2006-08-12 08:41:54 +00:00
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2006-04-26 21:33:01 +00:00
|
|
|
/* Per ECMA-262, return the new array length. */
|
2007-08-02 04:33:52 +00:00
|
|
|
if (!IndexToValue(cx, newlength, vp))
|
2006-04-26 21:33:01 +00:00
|
|
|
return JS_FALSE;
|
2006-08-12 08:41:54 +00:00
|
|
|
return js_SetLengthProperty(cx, obj, newlength);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_push(JSContext *cx, uintN argc, jsval *vp)
|
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
jsval v;
|
|
|
|
|
|
|
|
/* Insist on one argument and obj of the expected class. */
|
|
|
|
obj = JSVAL_TO_OBJECT(vp[1]);
|
|
|
|
if (argc != 1 || OBJ_GET_CLASS(cx, obj) != &js_ArrayClass)
|
|
|
|
return slow_array_push(cx, obj, argc, vp);
|
|
|
|
|
|
|
|
/* Beware 'length' int to double jsval overflow. */
|
|
|
|
v = STOBJ_GET_SLOT(obj, JSSLOT_ARRAY_LENGTH);
|
|
|
|
if (!(v & JSVAL_INT) || v == INT_TO_JSVAL(JSVAL_INT_MAX))
|
|
|
|
return slow_array_push(cx, obj, argc, vp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, we can optimize to define a new property identified by the
|
|
|
|
* current value of 'length'. We know no such property exists,
|
|
|
|
* because if one did, then js_ArrayClass.addProperty would have
|
|
|
|
* increased the value of 'length' to one greater than this index
|
|
|
|
* (proof by contradiction).
|
|
|
|
*/
|
|
|
|
if (!js_DefineNativeProperty(cx, obj, INT_JSVAL_TO_JSID(v), vp[2],
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE, 0, 0,
|
|
|
|
NULL)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
v += 2;
|
|
|
|
JS_ASSERT(STOBJ_GET_SLOT(obj, JSSLOT_ARRAY_LENGTH) == v);
|
|
|
|
*vp = v;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
array_pop(JSContext *cx, uintN argc, jsval *vp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *obj;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsuint index;
|
2006-08-12 08:41:54 +00:00
|
|
|
JSBool hole;
|
1998-04-24 00:31:11 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &index))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2007-08-02 18:40:47 +00:00
|
|
|
if (index == 0) {
|
|
|
|
*vp = JSVAL_VOID;
|
|
|
|
} else {
|
2003-06-12 00:26:40 +00:00
|
|
|
index--;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
/* Get the to-be-deleted property's value into vp. */
|
|
|
|
if (!GetArrayElement(cx, obj, index, &hole, vp))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2006-08-12 08:41:54 +00:00
|
|
|
if (!hole && !DeleteArrayElement(cx, obj, index))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
1998-04-24 00:31:11 +00:00
|
|
|
return js_SetLengthProperty(cx, obj, index);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_shift(JSContext *cx, uintN argc, jsval *vp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *obj;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsuint length, i;
|
2006-08-12 08:41:54 +00:00
|
|
|
JSBool hole;
|
1998-04-24 00:31:11 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &length))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2006-08-12 08:41:54 +00:00
|
|
|
if (length == 0) {
|
2007-08-02 04:33:52 +00:00
|
|
|
*vp = JSVAL_VOID;
|
2006-08-12 08:41:54 +00:00
|
|
|
} else {
|
2003-06-12 00:26:40 +00:00
|
|
|
length--;
|
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
/* Get the to-be-deleted property's value into vp ASAP. */
|
|
|
|
if (!GetArrayElement(cx, obj, 0, &hole, vp))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/*
|
2007-08-02 04:33:52 +00:00
|
|
|
* Slide down the array above the first element, using our stack-local
|
|
|
|
* GC root at vp[2].
|
2003-06-12 00:26:40 +00:00
|
|
|
*/
|
2006-08-12 08:41:54 +00:00
|
|
|
for (i = 0; i != length; i++) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
2007-08-02 04:33:52 +00:00
|
|
|
!GetArrayElement(cx, obj, i + 1, &hole, &vp[2]) ||
|
|
|
|
!SetOrDeleteArrayElement(cx, obj, i, hole, vp[2])) {
|
2006-08-12 08:41:54 +00:00
|
|
|
return JS_FALSE;
|
2006-12-24 11:31:37 +00:00
|
|
|
}
|
2003-06-12 00:26:40 +00:00
|
|
|
}
|
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
/* Delete the only or last element when it exist. */
|
|
|
|
if (!hole && !DeleteArrayElement(cx, obj, length))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
1998-04-24 00:31:11 +00:00
|
|
|
return js_SetLengthProperty(cx, obj, length);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_unshift(JSContext *cx, uintN argc, jsval *vp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *obj;
|
|
|
|
jsval *argv, *localroot;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsuint length, last;
|
2006-08-12 08:41:54 +00:00
|
|
|
JSBool hole;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &length))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
if (argc > 0) {
|
2003-06-12 00:26:40 +00:00
|
|
|
/* Slide up the array to make room for argc at the bottom. */
|
2007-08-02 04:33:52 +00:00
|
|
|
argv = JS_ARGV(cx, vp);
|
2003-06-12 00:26:40 +00:00
|
|
|
if (length > 0) {
|
|
|
|
last = length;
|
2007-08-02 04:33:52 +00:00
|
|
|
localroot = argv + argc;
|
2006-08-12 08:41:54 +00:00
|
|
|
do {
|
|
|
|
--last;
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
2007-08-02 04:33:52 +00:00
|
|
|
!GetArrayElement(cx, obj, last, &hole, localroot) ||
|
|
|
|
!SetOrDeleteArrayElement(cx, obj, last + argc, hole,
|
|
|
|
*localroot)) {
|
2006-08-08 23:52:08 +00:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2006-08-12 08:41:54 +00:00
|
|
|
} while (last != 0);
|
2003-06-12 00:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy from argv to the bottom of the array. */
|
2006-08-12 08:41:54 +00:00
|
|
|
if (!InitArrayElements(cx, obj, 0, argc, argv))
|
|
|
|
return JS_FALSE;
|
2003-06-12 00:26:40 +00:00
|
|
|
|
|
|
|
length += argc;
|
|
|
|
if (!js_SetLengthProperty(cx, obj, length))
|
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2006-08-12 08:41:54 +00:00
|
|
|
|
|
|
|
/* Follow Perl by returning the new array length. */
|
2007-08-02 04:33:52 +00:00
|
|
|
return IndexToValue(cx, length, vp);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_splice(JSContext *cx, uintN argc, jsval *vp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2007-08-02 06:05:44 +00:00
|
|
|
jsval *argv, *localroot;
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *obj;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsuint length, begin, end, count, delta, last;
|
1998-03-28 02:44:41 +00:00
|
|
|
jsdouble d;
|
2006-08-12 08:41:54 +00:00
|
|
|
JSBool hole;
|
1998-03-28 02:44:41 +00:00
|
|
|
JSObject *obj2;
|
|
|
|
|
2005-11-01 01:13:36 +00:00
|
|
|
/*
|
2007-08-02 06:05:44 +00:00
|
|
|
* Nothing to do if no args. Otherwise point localroot at our single
|
|
|
|
* stack-local root and get length.
|
2005-11-01 01:13:36 +00:00
|
|
|
*/
|
1998-03-28 02:44:41 +00:00
|
|
|
if (argc == 0)
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_TRUE;
|
2007-08-02 04:33:52 +00:00
|
|
|
argv = JS_ARGV(cx, vp);
|
2007-08-02 06:05:44 +00:00
|
|
|
localroot = argv + argc;
|
2007-08-02 04:33:52 +00:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &length))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
/* Convert the first argument into a starting index. */
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_ValueToNumber(cx, *argv, &d))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-04-24 00:31:11 +00:00
|
|
|
d = js_DoubleToInteger(d);
|
|
|
|
if (d < 0) {
|
2003-06-12 00:26:40 +00:00
|
|
|
d += length;
|
|
|
|
if (d < 0)
|
|
|
|
d = 0;
|
1998-04-24 00:31:11 +00:00
|
|
|
} else if (d > length) {
|
2003-06-12 00:26:40 +00:00
|
|
|
d = length;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
1998-04-24 00:31:11 +00:00
|
|
|
begin = (jsuint)d; /* d has been clamped to uint32 */
|
1998-03-28 02:44:41 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
/* Convert the second argument from a count into a fencepost index. */
|
|
|
|
delta = length - begin;
|
|
|
|
if (argc == 0) {
|
2003-06-12 00:26:40 +00:00
|
|
|
count = delta;
|
|
|
|
end = length;
|
1998-03-28 02:44:41 +00:00
|
|
|
} else {
|
2003-06-12 00:26:40 +00:00
|
|
|
if (!js_ValueToNumber(cx, *argv, &d))
|
|
|
|
return JS_FALSE;
|
|
|
|
d = js_DoubleToInteger(d);
|
|
|
|
if (d < 0)
|
|
|
|
d = 0;
|
|
|
|
else if (d > delta)
|
|
|
|
d = delta;
|
|
|
|
count = (jsuint)d;
|
|
|
|
end = begin + count;
|
|
|
|
argc--;
|
|
|
|
argv++;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2005-07-13 16:38:46 +00:00
|
|
|
|
2006-04-28 00:20:44 +00:00
|
|
|
/*
|
|
|
|
* Create a new array value to return. Our ECMA v2 proposal specs
|
|
|
|
* that splice always returns an array value, even when given no
|
|
|
|
* arguments. We think this is best because it eliminates the need
|
|
|
|
* for callers to do an extra test to handle the empty splice case.
|
|
|
|
*/
|
|
|
|
obj2 = js_NewArrayObject(cx, 0, NULL);
|
|
|
|
if (!obj2)
|
|
|
|
return JS_FALSE;
|
2007-08-02 06:05:44 +00:00
|
|
|
*vp = OBJECT_TO_JSVAL(obj2);
|
2006-04-26 21:33:01 +00:00
|
|
|
|
2006-04-28 00:20:44 +00:00
|
|
|
/* If there are elements to remove, put them into the return value. */
|
|
|
|
if (count > 0) {
|
|
|
|
for (last = begin; last < end; last++) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
2007-08-02 06:05:44 +00:00
|
|
|
!GetArrayElement(cx, obj, last, &hole, localroot)) {
|
2006-04-28 00:20:44 +00:00
|
|
|
return JS_FALSE;
|
2006-12-24 11:31:37 +00:00
|
|
|
}
|
2006-07-01 19:29:34 +00:00
|
|
|
|
2007-08-02 06:05:44 +00:00
|
|
|
/* Copy *localroot to new array unless it's a hole. */
|
|
|
|
if (!hole && !SetArrayElement(cx, obj2, last - begin, *localroot))
|
2006-04-26 21:33:01 +00:00
|
|
|
return JS_FALSE;
|
2003-06-12 00:26:40 +00:00
|
|
|
}
|
2006-04-28 00:20:44 +00:00
|
|
|
|
|
|
|
if (!js_SetLengthProperty(cx, obj2, end - begin))
|
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the direction (up or down) to copy and make way for argv. */
|
1998-04-24 00:31:11 +00:00
|
|
|
if (argc > count) {
|
2003-06-12 00:26:40 +00:00
|
|
|
delta = (jsuint)argc - count;
|
|
|
|
last = length;
|
|
|
|
/* (uint) end could be 0, so can't use vanilla >= test */
|
|
|
|
while (last-- > end) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
2007-08-02 06:05:44 +00:00
|
|
|
!GetArrayElement(cx, obj, last, &hole, localroot) ||
|
2007-08-02 04:33:52 +00:00
|
|
|
!SetOrDeleteArrayElement(cx, obj, last + delta, hole,
|
2007-08-02 06:05:44 +00:00
|
|
|
*localroot)) {
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2005-07-13 16:38:46 +00:00
|
|
|
}
|
2003-06-12 00:26:40 +00:00
|
|
|
}
|
|
|
|
length += delta;
|
1998-04-24 00:31:11 +00:00
|
|
|
} else if (argc < count) {
|
2003-06-12 00:26:40 +00:00
|
|
|
delta = count - (jsuint)argc;
|
|
|
|
for (last = end; last < length; last++) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
2007-08-02 06:05:44 +00:00
|
|
|
!GetArrayElement(cx, obj, last, &hole, localroot) ||
|
2007-08-02 04:33:52 +00:00
|
|
|
!SetOrDeleteArrayElement(cx, obj, last - delta, hole,
|
2007-08-02 06:05:44 +00:00
|
|
|
*localroot)) {
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2005-07-13 16:38:46 +00:00
|
|
|
}
|
2003-06-12 00:26:40 +00:00
|
|
|
}
|
|
|
|
length -= delta;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy from argv into the hole to complete the splice. */
|
2006-08-12 08:41:54 +00:00
|
|
|
if (!InitArrayElements(cx, obj, begin, begin + argc, argv))
|
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
/* Update length in case we deleted elements from the end. */
|
1998-04-24 00:31:11 +00:00
|
|
|
return js_SetLengthProperty(cx, obj, length);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Python-esque sequence operations.
|
|
|
|
*/
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_concat(JSContext *cx, uintN argc, jsval *vp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2007-08-02 06:05:44 +00:00
|
|
|
jsval *argv, *localroot, v;
|
1998-03-28 02:44:41 +00:00
|
|
|
JSObject *nobj, *aobj;
|
2002-09-20 18:45:05 +00:00
|
|
|
jsuint length, alength, slot;
|
1998-03-28 02:44:41 +00:00
|
|
|
uintN i;
|
2006-08-12 08:41:54 +00:00
|
|
|
JSBool hole;
|
2002-09-20 18:45:05 +00:00
|
|
|
|
2005-11-01 01:13:36 +00:00
|
|
|
/* Hoist the explicit local root address computation. */
|
2007-08-02 04:33:52 +00:00
|
|
|
argv = JS_ARGV(cx, vp);
|
2007-08-02 06:05:44 +00:00
|
|
|
localroot = argv + argc;
|
2005-11-01 01:13:36 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
/* Treat our |this| object as the first argument; see ECMA 15.4.4.4. */
|
2002-09-20 18:45:05 +00:00
|
|
|
--argv;
|
2007-08-02 04:33:52 +00:00
|
|
|
JS_ASSERT(JS_THIS_OBJECT(cx, vp) == JSVAL_TO_OBJECT(argv[0]));
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
/* Create a new Array object and root it using *vp. */
|
1998-04-24 00:31:11 +00:00
|
|
|
nobj = js_NewArrayObject(cx, 0, NULL);
|
1998-03-28 02:44:41 +00:00
|
|
|
if (!nobj)
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2007-08-02 06:05:44 +00:00
|
|
|
*vp = OBJECT_TO_JSVAL(nobj);
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2002-09-20 18:45:05 +00:00
|
|
|
/* Loop over [0, argc] to concat args into nobj, expanding all Arrays. */
|
|
|
|
length = 0;
|
|
|
|
for (i = 0; i <= argc; i++) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP))
|
|
|
|
return JS_FALSE;
|
2003-06-12 00:26:40 +00:00
|
|
|
v = argv[i];
|
|
|
|
if (JSVAL_IS_OBJECT(v)) {
|
|
|
|
aobj = JSVAL_TO_OBJECT(v);
|
|
|
|
if (aobj && OBJ_GET_CLASS(cx, aobj) == &js_ArrayClass) {
|
2002-09-20 18:45:05 +00:00
|
|
|
if (!OBJ_GET_PROPERTY(cx, aobj,
|
2004-10-05 10:19:07 +00:00
|
|
|
ATOM_TO_JSID(cx->runtime->atomState
|
|
|
|
.lengthAtom),
|
2007-08-02 06:05:44 +00:00
|
|
|
localroot)) {
|
2002-09-20 18:45:05 +00:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-08-02 06:05:44 +00:00
|
|
|
if (!ValueIsLength(cx, *localroot, &alength))
|
2002-09-20 18:45:05 +00:00
|
|
|
return JS_FALSE;
|
2003-06-12 00:26:40 +00:00
|
|
|
for (slot = 0; slot < alength; slot++) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
2007-08-02 06:05:44 +00:00
|
|
|
!GetArrayElement(cx, aobj, slot, &hole, localroot)) {
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2006-12-24 11:31:37 +00:00
|
|
|
}
|
2006-07-01 19:29:34 +00:00
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
/*
|
|
|
|
* Per ECMA 262, 15.4.4.4, step 9, ignore non-existent
|
|
|
|
* properties.
|
|
|
|
*/
|
2007-08-02 04:33:52 +00:00
|
|
|
if (!hole &&
|
2007-08-02 06:05:44 +00:00
|
|
|
!SetArrayElement(cx, nobj, length + slot, *localroot)) {
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2007-08-02 04:33:52 +00:00
|
|
|
}
|
2003-06-12 00:26:40 +00:00
|
|
|
}
|
|
|
|
length += alength;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-12 08:41:54 +00:00
|
|
|
if (!SetArrayElement(cx, nobj, length, v))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
|
|
|
length++;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2005-07-05 21:25:12 +00:00
|
|
|
return js_SetLengthProperty(cx, nobj, length);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_slice(JSContext *cx, uintN argc, jsval *vp)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2007-08-02 06:05:44 +00:00
|
|
|
jsval *argv, *localroot;
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *nobj, *obj;
|
1998-04-24 00:31:11 +00:00
|
|
|
jsuint length, begin, end, slot;
|
1998-03-28 02:44:41 +00:00
|
|
|
jsdouble d;
|
2006-08-12 08:41:54 +00:00
|
|
|
JSBool hole;
|
2005-11-01 01:13:36 +00:00
|
|
|
|
|
|
|
/* Hoist the explicit local root address computation. */
|
2007-08-02 04:33:52 +00:00
|
|
|
argv = JS_ARGV(cx, vp);
|
2007-08-02 06:05:44 +00:00
|
|
|
localroot = argv + argc;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
/* Create a new Array object and root it using *vp. */
|
1998-04-24 00:31:11 +00:00
|
|
|
nobj = js_NewArrayObject(cx, 0, NULL);
|
1998-03-28 02:44:41 +00:00
|
|
|
if (!nobj)
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
2007-08-02 06:05:44 +00:00
|
|
|
*vp = OBJECT_TO_JSVAL(nobj);
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
1998-04-24 00:31:11 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &length))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
begin = 0;
|
|
|
|
end = length;
|
|
|
|
|
|
|
|
if (argc > 0) {
|
2003-06-12 00:26:40 +00:00
|
|
|
if (!js_ValueToNumber(cx, argv[0], &d))
|
|
|
|
return JS_FALSE;
|
|
|
|
d = js_DoubleToInteger(d);
|
|
|
|
if (d < 0) {
|
|
|
|
d += length;
|
|
|
|
if (d < 0)
|
|
|
|
d = 0;
|
|
|
|
} else if (d > length) {
|
|
|
|
d = length;
|
|
|
|
}
|
|
|
|
begin = (jsuint)d;
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
if (!js_ValueToNumber(cx, argv[1], &d))
|
|
|
|
return JS_FALSE;
|
|
|
|
d = js_DoubleToInteger(d);
|
|
|
|
if (d < 0) {
|
|
|
|
d += length;
|
|
|
|
if (d < 0)
|
|
|
|
d = 0;
|
|
|
|
} else if (d > length) {
|
|
|
|
d = length;
|
|
|
|
}
|
|
|
|
end = (jsuint)d;
|
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2005-07-18 18:44:22 +00:00
|
|
|
if (begin > end)
|
|
|
|
begin = end;
|
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
for (slot = begin; slot < end; slot++) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
2007-08-02 06:05:44 +00:00
|
|
|
!GetArrayElement(cx, obj, slot, &hole, localroot)) {
|
2005-07-13 16:38:46 +00:00
|
|
|
return JS_FALSE;
|
2006-12-24 11:31:37 +00:00
|
|
|
}
|
2007-08-02 06:05:44 +00:00
|
|
|
if (!hole && !SetArrayElement(cx, nobj, slot - begin, *localroot))
|
2003-06-12 00:26:40 +00:00
|
|
|
return JS_FALSE;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2005-07-13 16:38:46 +00:00
|
|
|
return js_SetLengthProperty(cx, nobj, end - begin);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2005-04-17 18:31:59 +00:00
|
|
|
#if JS_HAS_ARRAY_EXTRAS
|
2005-10-02 06:27:07 +00:00
|
|
|
|
2005-04-17 18:31:59 +00:00
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_indexOfHelper(JSContext *cx, JSBool isLast, uintN argc, jsval *vp)
|
2005-04-17 18:31:59 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *obj;
|
2005-10-02 06:27:07 +00:00
|
|
|
jsuint length, i, stop;
|
|
|
|
jsint direction;
|
2006-08-12 08:41:54 +00:00
|
|
|
JSBool hole;
|
2005-05-04 02:43:13 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
obj = JSVAL_TO_OBJECT(vp[1]);
|
2005-05-04 02:43:13 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &length))
|
2005-04-17 18:31:59 +00:00
|
|
|
return JS_FALSE;
|
2005-10-02 06:27:07 +00:00
|
|
|
if (length == 0)
|
|
|
|
goto not_found;
|
|
|
|
|
|
|
|
if (argc <= 1) {
|
|
|
|
i = isLast ? length - 1 : 0;
|
|
|
|
} else {
|
|
|
|
jsdouble start;
|
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
if (!js_ValueToNumber(cx, vp[3], &start))
|
2005-05-04 02:43:13 +00:00
|
|
|
return JS_FALSE;
|
|
|
|
start = js_DoubleToInteger(start);
|
|
|
|
if (start < 0) {
|
|
|
|
start += length;
|
2006-12-18 06:43:02 +00:00
|
|
|
if (start < 0) {
|
|
|
|
if (isLast)
|
|
|
|
goto not_found;
|
|
|
|
i = 0;
|
|
|
|
} else {
|
|
|
|
i = (jsuint)start;
|
|
|
|
}
|
2005-10-02 06:27:07 +00:00
|
|
|
} else if (start >= length) {
|
2006-12-18 06:43:02 +00:00
|
|
|
if (!isLast)
|
|
|
|
goto not_found;
|
2005-10-02 06:27:07 +00:00
|
|
|
i = length - 1;
|
|
|
|
} else {
|
|
|
|
i = (jsuint)start;
|
2005-05-04 02:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-02 06:27:07 +00:00
|
|
|
|
|
|
|
if (isLast) {
|
|
|
|
stop = 0;
|
|
|
|
direction = -1;
|
|
|
|
} else {
|
|
|
|
stop = length - 1;
|
|
|
|
direction = 1;
|
|
|
|
}
|
|
|
|
|
2005-10-09 06:09:21 +00:00
|
|
|
for (;;) {
|
2006-12-24 11:31:37 +00:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) ||
|
2007-08-02 04:33:52 +00:00
|
|
|
!GetArrayElement(cx, obj, (jsuint)i, &hole, vp)) {
|
2005-04-17 18:31:59 +00:00
|
|
|
return JS_FALSE;
|
2006-12-24 11:31:37 +00:00
|
|
|
}
|
2007-08-02 04:33:52 +00:00
|
|
|
if (!hole && js_StrictlyEqual(*vp, vp[2]))
|
|
|
|
return js_NewNumberValue(cx, i, vp);
|
2005-10-02 06:27:07 +00:00
|
|
|
if (i == stop)
|
|
|
|
goto not_found;
|
2005-10-09 06:09:21 +00:00
|
|
|
i += direction;
|
2005-04-17 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
2005-10-02 06:27:07 +00:00
|
|
|
not_found:
|
2007-08-02 04:33:52 +00:00
|
|
|
*vp = INT_TO_JSVAL(-1);
|
2005-05-04 02:43:13 +00:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2005-10-02 06:27:07 +00:00
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_indexOf(JSContext *cx, uintN argc, jsval *vp)
|
2005-10-02 06:27:07 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
return array_indexOfHelper(cx, JS_FALSE, argc, vp);
|
2005-10-02 06:27:07 +00:00
|
|
|
}
|
|
|
|
|
2005-05-04 02:43:13 +00:00
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_lastIndexOf(JSContext *cx, uintN argc, jsval *vp)
|
2005-05-04 02:43:13 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
return array_indexOfHelper(cx, JS_TRUE, argc, vp);
|
2005-04-17 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 01:47:58 +00:00
|
|
|
/* Order is important; extras that take a predicate funarg must follow MAP. */
|
2005-04-17 18:31:59 +00:00
|
|
|
typedef enum ArrayExtraMode {
|
|
|
|
FOREACH,
|
2007-01-10 01:47:58 +00:00
|
|
|
REDUCE,
|
|
|
|
REDUCE_RIGHT,
|
2005-04-17 18:31:59 +00:00
|
|
|
MAP,
|
|
|
|
FILTER,
|
|
|
|
SOME,
|
|
|
|
EVERY
|
|
|
|
} ArrayExtraMode;
|
|
|
|
|
2007-01-10 01:47:58 +00:00
|
|
|
#define REDUCE_MODE(mode) ((mode) == REDUCE || (mode) == REDUCE_RIGHT)
|
|
|
|
|
2005-04-17 18:31:59 +00:00
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_extra(JSContext *cx, ArrayExtraMode mode, uintN argc, jsval *vp)
|
2005-04-17 18:31:59 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
JSObject *obj;
|
2007-09-17 19:28:14 +00:00
|
|
|
jsuint length, newlen;
|
2007-09-18 07:34:54 +00:00
|
|
|
jsval *argv, *elemroot, *invokevp, *sp;
|
|
|
|
JSBool ok, cond, hole;
|
2005-10-25 08:00:30 +00:00
|
|
|
JSObject *callable, *thisp, *newarr;
|
2007-01-10 01:47:58 +00:00
|
|
|
jsint start, end, step, i;
|
2005-04-17 18:31:59 +00:00
|
|
|
void *mark;
|
2005-11-01 01:13:36 +00:00
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
obj = JSVAL_TO_OBJECT(vp[1]);
|
2007-09-18 07:34:54 +00:00
|
|
|
if (!js_GetLengthProperty(cx, obj, &length))
|
2005-04-17 18:31:59 +00:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2005-05-04 02:43:13 +00:00
|
|
|
/*
|
2005-10-25 08:00:30 +00:00
|
|
|
* First, get or compute our callee, so that we error out consistently
|
|
|
|
* when passed a non-callable object.
|
2005-05-04 02:43:13 +00:00
|
|
|
*/
|
2007-09-18 07:34:54 +00:00
|
|
|
argv = vp + 2;
|
2006-09-18 11:04:30 +00:00
|
|
|
callable = js_ValueToCallableObject(cx, &argv[0], JSV2F_SEARCH_STACK);
|
2005-10-25 08:00:30 +00:00
|
|
|
if (!callable)
|
|
|
|
return JS_FALSE;
|
2005-04-17 18:31:59 +00:00
|
|
|
|
2005-05-04 02:43:13 +00:00
|
|
|
/*
|
|
|
|
* Set our initial return condition, used for zero-length array cases
|
|
|
|
* (and pre-size our map return to match our known length, for all cases).
|
|
|
|
*/
|
2005-11-01 01:13:36 +00:00
|
|
|
#ifdef __GNUC__ /* quell GCC overwarning */
|
|
|
|
newlen = 0;
|
|
|
|
newarr = NULL;
|
|
|
|
#endif
|
2007-01-10 01:47:58 +00:00
|
|
|
start = 0, end = length, step = 1;
|
2007-08-02 04:33:52 +00:00
|
|
|
|
2005-05-04 02:43:13 +00:00
|
|
|
switch (mode) {
|
2007-01-10 01:47:58 +00:00
|
|
|
case REDUCE_RIGHT:
|
|
|
|
start = length - 1, end = -1, step = -1;
|
|
|
|
/* FALL THROUGH */
|
|
|
|
case REDUCE:
|
|
|
|
if (length == 0 && argc == 1) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_EMPTY_ARRAY_REDUCE);
|
2007-09-18 07:34:54 +00:00
|
|
|
return JS_FALSE;
|
2007-01-10 01:47:58 +00:00
|
|
|
}
|
|
|
|
if (argc >= 2) {
|
2007-09-18 07:34:54 +00:00
|
|
|
*vp = argv[1];
|
2007-01-10 01:47:58 +00:00
|
|
|
} else {
|
2007-06-28 00:04:49 +00:00
|
|
|
do {
|
2007-09-18 07:34:54 +00:00
|
|
|
if (!GetArrayElement(cx, obj, start, &hole, vp))
|
|
|
|
return JS_FALSE;
|
2007-06-28 00:04:49 +00:00
|
|
|
start += step;
|
|
|
|
} while (hole && start != end);
|
|
|
|
|
|
|
|
if (hole && start == end) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_EMPTY_ARRAY_REDUCE);
|
2007-09-18 07:34:54 +00:00
|
|
|
return JS_FALSE;
|
2007-06-28 00:04:49 +00:00
|
|
|
}
|
2007-01-10 01:47:58 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-05-04 02:43:13 +00:00
|
|
|
case MAP:
|
|
|
|
case FILTER:
|
|
|
|
newlen = (mode == MAP) ? length : 0;
|
|
|
|
newarr = js_NewArrayObject(cx, newlen, NULL);
|
2007-09-18 07:34:54 +00:00
|
|
|
if (!newarr)
|
|
|
|
return JS_FALSE;
|
|
|
|
*vp = OBJECT_TO_JSVAL(newarr);
|
2005-05-04 02:43:13 +00:00
|
|
|
break;
|
|
|
|
case SOME:
|
2007-09-18 07:34:54 +00:00
|
|
|
*vp = JSVAL_FALSE;
|
2005-05-04 02:43:13 +00:00
|
|
|
break;
|
2005-08-19 01:48:48 +00:00
|
|
|
case EVERY:
|
2007-09-18 07:34:54 +00:00
|
|
|
*vp = JSVAL_TRUE;
|
2005-08-19 01:48:48 +00:00
|
|
|
break;
|
2005-05-04 02:43:13 +00:00
|
|
|
case FOREACH:
|
2007-09-18 07:34:54 +00:00
|
|
|
*vp = JSVAL_VOID;
|
2005-05-04 02:43:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 0)
|
2007-09-18 07:34:54 +00:00
|
|
|
return JS_TRUE;
|
2005-10-02 06:27:07 +00:00
|
|
|
|
2007-01-10 01:47:58 +00:00
|
|
|
if (argc > 1 && !REDUCE_MODE(mode)) {
|
2007-09-18 07:34:54 +00:00
|
|
|
if (!js_ValueToObject(cx, argv[1], &thisp))
|
|
|
|
return JS_FALSE;
|
2005-04-17 18:31:59 +00:00
|
|
|
argv[1] = OBJECT_TO_JSVAL(thisp);
|
|
|
|
} else {
|
2006-05-04 06:46:27 +00:00
|
|
|
thisp = NULL;
|
2005-04-17 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 01:47:58 +00:00
|
|
|
/*
|
2007-09-18 07:34:54 +00:00
|
|
|
* For all but REDUCE, we call with 3 args (value, index, array). REDUCE
|
|
|
|
* requires 4 args (accum, value, index, array).
|
2007-01-10 01:47:58 +00:00
|
|
|
*/
|
|
|
|
argc = 3 + REDUCE_MODE(mode);
|
2007-09-18 07:34:54 +00:00
|
|
|
elemroot = js_AllocStack(cx, 1 + 2 + argc, &mark);
|
|
|
|
if (!elemroot)
|
|
|
|
return JS_FALSE;
|
2005-04-17 18:31:59 +00:00
|
|
|
|
2007-09-18 07:34:54 +00:00
|
|
|
/* From this point the control must flow through out:. */
|
|
|
|
ok = JS_TRUE;
|
|
|
|
invokevp = elemroot + 1;
|
2005-04-17 18:31:59 +00:00
|
|
|
|
2007-01-10 01:47:58 +00:00
|
|
|
for (i = start; i != end; i += step) {
|
2007-09-18 07:34:54 +00:00
|
|
|
ok = JS_CHECK_OPERATION_LIMIT(cx, JSOW_JUMP) &&
|
|
|
|
GetArrayElement(cx, obj, i, &hole, elemroot);
|
2005-04-17 18:31:59 +00:00
|
|
|
if (!ok)
|
2007-09-18 07:34:54 +00:00
|
|
|
goto out;
|
2006-08-12 08:41:54 +00:00
|
|
|
if (hole)
|
2005-10-02 06:27:07 +00:00
|
|
|
continue;
|
2005-04-17 18:31:59 +00:00
|
|
|
|
2006-05-04 06:46:27 +00:00
|
|
|
/*
|
|
|
|
* Push callable and 'this', then args. We must do this for every
|
2007-09-18 07:34:54 +00:00
|
|
|
* iteration around the loop since js_Invoke uses spbase[0] for return
|
|
|
|
* value storage, while some native functions use spbase[1] for local
|
2007-08-02 04:33:52 +00:00
|
|
|
* rooting.
|
2006-05-04 06:46:27 +00:00
|
|
|
*/
|
2007-09-18 07:34:54 +00:00
|
|
|
sp = invokevp;
|
2005-10-25 08:00:30 +00:00
|
|
|
*sp++ = OBJECT_TO_JSVAL(callable);
|
2005-04-17 18:31:59 +00:00
|
|
|
*sp++ = OBJECT_TO_JSVAL(thisp);
|
2007-01-10 01:47:58 +00:00
|
|
|
if (REDUCE_MODE(mode))
|
2007-09-18 07:34:54 +00:00
|
|
|
*sp++ = *vp;
|
|
|
|
*sp++ = *elemroot;
|
2005-04-17 18:31:59 +00:00
|
|
|
*sp++ = INT_TO_JSVAL(i);
|
2005-05-04 02:43:13 +00:00
|
|
|
*sp++ = OBJECT_TO_JSVAL(obj);
|
2005-04-17 18:31:59 +00:00
|
|
|
|
|
|
|
/* Do the call. */
|
2007-09-18 07:34:54 +00:00
|
|
|
ok = js_Invoke(cx, argc, invokevp, JSINVOKE_INTERNAL);
|
2005-04-17 18:31:59 +00:00
|
|
|
if (!ok)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (mode > MAP) {
|
2007-09-18 07:34:54 +00:00
|
|
|
if (*invokevp == JSVAL_NULL) {
|
2006-04-27 00:39:43 +00:00
|
|
|
cond = JS_FALSE;
|
2007-09-18 07:34:54 +00:00
|
|
|
} else if (JSVAL_IS_BOOLEAN(*invokevp)) {
|
|
|
|
cond = JSVAL_TO_BOOLEAN(*invokevp);
|
2005-04-17 18:31:59 +00:00
|
|
|
} else {
|
2007-09-18 07:34:54 +00:00
|
|
|
ok = js_ValueToBoolean(cx, *invokevp, &cond);
|
2005-04-17 18:31:59 +00:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case FOREACH:
|
|
|
|
break;
|
2007-01-10 01:47:58 +00:00
|
|
|
case REDUCE:
|
|
|
|
case REDUCE_RIGHT:
|
2007-09-18 07:34:54 +00:00
|
|
|
*vp = *invokevp;
|
2007-01-10 01:47:58 +00:00
|
|
|
break;
|
2005-04-17 18:31:59 +00:00
|
|
|
case MAP:
|
2007-09-18 07:34:54 +00:00
|
|
|
ok = SetArrayElement(cx, newarr, i, *invokevp);
|
2005-04-17 18:31:59 +00:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
case FILTER:
|
2006-04-27 00:39:43 +00:00
|
|
|
if (!cond)
|
2005-04-17 18:31:59 +00:00
|
|
|
break;
|
2007-09-18 07:34:54 +00:00
|
|
|
/* The filter passed *elemroot, so push it onto our result. */
|
|
|
|
ok = SetArrayElement(cx, newarr, newlen++, *elemroot);
|
2005-04-17 18:31:59 +00:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
case SOME:
|
2006-04-27 00:39:43 +00:00
|
|
|
if (cond) {
|
2007-09-18 07:34:54 +00:00
|
|
|
*vp = JSVAL_TRUE;
|
2005-04-17 18:31:59 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EVERY:
|
2006-04-27 00:39:43 +00:00
|
|
|
if (!cond) {
|
2007-09-18 07:34:54 +00:00
|
|
|
*vp = JSVAL_FALSE;
|
2005-04-17 18:31:59 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
out:
|
2005-04-17 18:31:59 +00:00
|
|
|
js_FreeStack(cx, mark);
|
|
|
|
if (ok && mode == FILTER)
|
|
|
|
ok = js_SetLengthProperty(cx, newarr, newlen);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_forEach(JSContext *cx, uintN argc, jsval *vp)
|
2005-04-17 18:31:59 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
return array_extra(cx, FOREACH, argc, vp);
|
2005-04-17 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_map(JSContext *cx, uintN argc, jsval *vp)
|
2005-04-17 18:31:59 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
return array_extra(cx, MAP, argc, vp);
|
2005-04-17 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 01:47:58 +00:00
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_reduce(JSContext *cx, uintN argc, jsval *vp)
|
2007-01-10 01:47:58 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
return array_extra(cx, REDUCE, argc, vp);
|
2007-01-10 01:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_reduceRight(JSContext *cx, uintN argc, jsval *vp)
|
2007-01-10 01:47:58 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
return array_extra(cx, REDUCE_RIGHT, argc, vp);
|
2007-01-10 01:47:58 +00:00
|
|
|
}
|
|
|
|
|
2005-04-17 18:31:59 +00:00
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_filter(JSContext *cx, uintN argc, jsval *vp)
|
2005-04-17 18:31:59 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
return array_extra(cx, FILTER, argc, vp);
|
2005-04-17 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_some(JSContext *cx, uintN argc, jsval *vp)
|
2005-04-17 18:31:59 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
return array_extra(cx, SOME, argc, vp);
|
2005-04-17 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-02 04:33:52 +00:00
|
|
|
array_every(JSContext *cx, uintN argc, jsval *vp)
|
2005-04-17 18:31:59 +00:00
|
|
|
{
|
2007-08-02 04:33:52 +00:00
|
|
|
return array_extra(cx, EVERY, argc, vp);
|
2005-04-17 18:31:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
static JSPropertySpec array_props[] = {
|
|
|
|
{js_length_str, -1, JSPROP_SHARED | JSPROP_PERMANENT,
|
|
|
|
array_length_getter, array_length_setter},
|
|
|
|
{0,0,0,0,0}
|
|
|
|
};
|
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
static JSFunctionSpec array_methods[] = {
|
1998-04-24 00:31:11 +00:00
|
|
|
#if JS_HAS_TOSOURCE
|
2007-08-02 06:05:44 +00:00
|
|
|
JS_FN(js_toSource_str, array_toSource, 0,0,0,0),
|
1998-04-24 00:31:11 +00:00
|
|
|
#endif
|
2007-08-02 06:05:44 +00:00
|
|
|
JS_FN(js_toString_str, array_toString, 0,0,0,0),
|
|
|
|
JS_FN(js_toLocaleString_str,array_toLocaleString,0,0,0,0),
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
/* Perl-ish methods. */
|
2007-08-02 06:05:44 +00:00
|
|
|
JS_FN("join", array_join, 1,1,JSFUN_GENERIC_NATIVE,0),
|
2007-08-02 04:33:52 +00:00
|
|
|
JS_FN("reverse", array_reverse, 0,0,JSFUN_GENERIC_NATIVE,2),
|
|
|
|
JS_FN("sort", array_sort, 0,1,JSFUN_GENERIC_NATIVE,1),
|
|
|
|
JS_FN("push", array_push, 1,1,JSFUN_GENERIC_NATIVE,0),
|
|
|
|
JS_FN("pop", array_pop, 0,0,JSFUN_GENERIC_NATIVE,0),
|
|
|
|
JS_FN("shift", array_shift, 0,0,JSFUN_GENERIC_NATIVE,1),
|
|
|
|
JS_FN("unshift", array_unshift, 0,1,JSFUN_GENERIC_NATIVE,1),
|
2007-08-02 06:05:44 +00:00
|
|
|
JS_FN("splice", array_splice, 0,2,JSFUN_GENERIC_NATIVE,1),
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
/* Python-esque sequence methods. */
|
2007-08-02 06:05:44 +00:00
|
|
|
JS_FN("concat", array_concat, 0,1,JSFUN_GENERIC_NATIVE,1),
|
|
|
|
JS_FN("slice", array_slice, 0,2,JSFUN_GENERIC_NATIVE,1),
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2005-04-17 18:31:59 +00:00
|
|
|
#if JS_HAS_ARRAY_EXTRAS
|
2007-08-02 04:33:52 +00:00
|
|
|
JS_FN("indexOf", array_indexOf, 1,1,JSFUN_GENERIC_NATIVE,0),
|
|
|
|
JS_FN("lastIndexOf", array_lastIndexOf, 1,1,JSFUN_GENERIC_NATIVE,0),
|
|
|
|
JS_FN("forEach", array_forEach, 1,1,JSFUN_GENERIC_NATIVE,0),
|
|
|
|
JS_FN("map", array_map, 1,1,JSFUN_GENERIC_NATIVE,0),
|
|
|
|
JS_FN("reduce", array_reduce, 1,1,JSFUN_GENERIC_NATIVE,0),
|
|
|
|
JS_FN("reduceRight", array_reduceRight, 1,1,JSFUN_GENERIC_NATIVE,0),
|
|
|
|
JS_FN("filter", array_filter, 1,1,JSFUN_GENERIC_NATIVE,0),
|
|
|
|
JS_FN("some", array_some, 1,1,JSFUN_GENERIC_NATIVE,0),
|
|
|
|
JS_FN("every", array_every, 1,1,JSFUN_GENERIC_NATIVE,0),
|
2005-04-17 18:31:59 +00:00
|
|
|
#endif
|
|
|
|
|
2007-08-02 04:33:52 +00:00
|
|
|
JS_FS_END
|
1998-03-28 02:44:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
Array(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
1998-04-24 00:31:11 +00:00
|
|
|
jsuint length;
|
1998-03-28 02:44:41 +00:00
|
|
|
jsval *vector;
|
1998-04-24 00:31:11 +00:00
|
|
|
|
|
|
|
/* If called without new, replace obj with a new Array object. */
|
Fix for bug 99663 (for loop resolves properties of the object being enumerated
with JSRESOLVE_ASSIGNING, wrongly), plus a few miscellaneous bugfixes.
- Combine the JSStackFrame members constructing, special, overrides, and
reserved into a uint32 flags member.
- Separate JOF_ASSIGNING from the JOF_SET bytecode format flag, and impute
JSRESOLVE_ASSIGNING from the presence of JOF_ASSIGNING among the current
opcode's format flags. To handle the for-in loop opcodes, which do more
than simply assign -- in particular, they do property lookups whose resolve
hook outcalls should not be flagged with JSRESOLVE_ASSIGNING -- a new frame
flag, JSFRAME_ASSIGNING, has been added.
- Fix interpreter version selection to respect JS_SetVersion, whose effect on
cx->version is "sticky".
- Fix js_DecompileValueGenerator to deal with JSOP_ENUMELEM -- it never had,
as this testcase shows (it crashes without this patch):
version(120);
eval("function fe(s) { for (it[s] in this); }");
try { fe('rdonly'); } catch (e) { print(e); }
2001-10-03 06:39:30 +00:00
|
|
|
if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {
|
2003-06-12 00:26:40 +00:00
|
|
|
obj = js_NewObject(cx, &js_ArrayClass, NULL, NULL);
|
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
*rval = OBJECT_TO_JSVAL(obj);
|
1998-04-24 00:31:11 +00:00
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
if (argc == 0) {
|
2003-06-12 00:26:40 +00:00
|
|
|
length = 0;
|
|
|
|
vector = NULL;
|
|
|
|
} else if (argc > 1) {
|
|
|
|
length = (jsuint) argc;
|
|
|
|
vector = argv;
|
|
|
|
} else if (!JSVAL_IS_NUMBER(argv[0])) {
|
|
|
|
length = 1;
|
|
|
|
vector = argv;
|
1998-03-28 02:44:41 +00:00
|
|
|
} else {
|
2003-06-12 00:26:40 +00:00
|
|
|
if (!ValueIsLength(cx, argv[0], &length))
|
|
|
|
return JS_FALSE;
|
|
|
|
vector = NULL;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
1998-04-24 00:31:11 +00:00
|
|
|
return InitArrayObject(cx, obj, length, vector);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitArrayClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
1998-04-24 00:31:11 +00:00
|
|
|
JSObject *proto;
|
|
|
|
|
|
|
|
proto = JS_InitClass(cx, obj, NULL, &js_ArrayClass, Array, 1,
|
2007-08-02 04:33:52 +00:00
|
|
|
array_props, array_methods, NULL, NULL);
|
1998-04-24 00:31:11 +00:00
|
|
|
|
|
|
|
/* Initialize the Array prototype object so it gets a length property. */
|
|
|
|
if (!proto || !InitArrayObject(cx, proto, 0, NULL))
|
2003-06-12 00:26:40 +00:00
|
|
|
return NULL;
|
1998-04-24 00:31:11 +00:00
|
|
|
return proto;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
1998-04-24 00:31:11 +00:00
|
|
|
js_NewArrayObject(JSContext *cx, jsuint length, jsval *vector)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2007-03-02 17:35:29 +00:00
|
|
|
JSTempValueRooter tvr;
|
1998-03-28 02:44:41 +00:00
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
obj = js_NewObject(cx, &js_ArrayClass, NULL, NULL);
|
|
|
|
if (!obj)
|
2003-06-12 00:26:40 +00:00
|
|
|
return NULL;
|
2007-03-02 17:35:29 +00:00
|
|
|
|
|
|
|
JS_PUSH_TEMP_ROOT_OBJECT(cx, obj, &tvr);
|
|
|
|
if (!InitArrayObject(cx, obj, length, vector))
|
|
|
|
obj = NULL;
|
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
|
|
|
|
|
|
|
/* Set/clear newborn root, in case we lost it. */
|
2007-09-16 13:03:17 +00:00
|
|
|
cx->weakRoots.newborn[GCX_OBJECT] = obj;
|
1998-03-28 02:44:41 +00:00
|
|
|
return obj;
|
|
|
|
}
|