- Chouck's changes to grow a JSIdArray if necessary in JS_Enumerate, with my

code review and fixes (r=chouck@geocast.com).  He needs this cuz he has no
  knowledge of exact number of properties before new-style enumerating them.
- Patch up jsdbgapi.c a bit -- it needs to use OBJ_GET_ATTRIBUTES and new APIs
  to do a better job describing properties to a debugger.
- Add JSMSG_CANT_DESCRIBE_PROPS for bogus non-native error case in jsdbgapi.c.
- Fix "Inappropriate" => "invalid" in JSMSG_BAD_ARRAY_LENGTH message.
This commit is contained in:
brendan%mozilla.org 1999-10-25 19:24:03 +00:00
parent 7aa6773f73
commit 526bfd1116
5 changed files with 51 additions and 15 deletions

View File

@ -224,4 +224,5 @@ MSG_DEF(JSMSG_INVALID_BACKREF, 148, 0, JSEXN_SYNTAXERR, "non-octal digit
MSG_DEF(JSMSG_BAD_BACKREF, 149, 0, JSEXN_SYNTAXERR, "back-reference exceeds number of capturing parentheses")
MSG_DEF(JSMSG_PRECISION_RANGE, 150, 1, JSEXN_RANGEERR, "precision {0} out of range")
MSG_DEF(JSMSG_BAD_GETTER_OR_SETTER, 151, 1, JSEXN_SYNTAXERR, "invalid {0} usage")
MSG_DEF(JSMSG_BAD_ARRAY_LENGTH, 152, 0, JSEXN_RANGEERR, "Inappropriate array length")
MSG_DEF(JSMSG_BAD_ARRAY_LENGTH, 152, 0, JSEXN_RANGEERR, "invalid array length")
MSG_DEF(JSMSG_CANT_DESCRIBE_PROPS, 153, 1, JSEXN_NONE, "can't describe non-native properties of class {0}")

View File

@ -2064,24 +2064,37 @@ JS_Enumerate(JSContext *cx, JSObject *obj)
goto error;
}
/* Grow as needed if we don't know the exact amount ahead of time. */
n = JSVAL_TO_INT(num_properties);
if (n <= 0)
n = 8;
/* Create an array of jsids large enough to hold all the properties */
ida = js_NewIdArray(cx, n);
if (n) {
i = 0;
vector = &ida->vector[0];
while (1) {
if (!OBJ_ENUMERATE(cx, obj, JSENUMERATE_NEXT, &iter_state, &id))
if (!ida)
goto error;
i = 0;
vector = &ida->vector[0];
while (1) {
if (i == ida->length) {
/* Grow length by factor of 1.5 instead of doubling. */
jsint newlen = ida->length + (((jsuint)ida->length + 1) >> 1);
ida = js_GrowIdArray(cx, ida, newlen);
if (!ida)
goto error;
/* No more jsid's to enumerate ? */
if (iter_state == JSVAL_NULL)
break;
vector[i++] = id;
vector = &ida->vector[0];
}
}
if (!OBJ_ENUMERATE(cx, obj, JSENUMERATE_NEXT, &iter_state, &id))
goto error;
/* No more jsid's to enumerate ? */
if (iter_state == JSVAL_NULL)
break;
vector[i++] = id;
}
ida->length = i;
return ida;
error:

View File

@ -723,6 +723,8 @@ JS_EvaluateInStackFrame(JSContext *cx, JSStackFrame *fp,
/************************************************************************/
/* XXXbe this all needs to be reworked to avoid requiring JSScope types. */
JS_PUBLIC_API(JSScopeProperty *)
JS_PropertyIterator(JSObject *obj, JSScopeProperty **iteratorp)
{
@ -777,17 +779,24 @@ JS_GetPropertyDesc(JSContext *cx, JSObject *obj, JSScopeProperty *sprop,
JS_PUBLIC_API(JSBool)
JS_GetPropertyDescArray(JSContext *cx, JSObject *obj, JSPropertyDescArray *pda)
{
JSClass *clasp;
JSScope *scope;
uint32 i, n;
JSPropertyDesc *pd;
JSScopeProperty *sprop;
jsval state;
jsid num_prop;
if (!OBJ_ENUMERATE(cx, obj, JSENUMERATE_INIT, &state, &num_prop))
clasp = OBJ_GET_CLASS(cx, obj);
if (!OBJ_IS_NATIVE(obj) || (clasp->flags & JSCLASS_NEW_ENUMERATE)) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_CANT_DESCRIBE_PROPS, clasp->name);
return JS_FALSE;
}
if (!clasp->enumerate(cx, obj))
return JS_FALSE;
scope = (JSScope *)obj->map;
/* have no props, or object's scope has not mutated from that of proto */
scope = (JSScope *)obj->map;
if (!scope->props ||
(OBJ_GET_PROTO(cx,obj) &&
scope == (JSScope *)(OBJ_GET_PROTO(cx,obj)->map))) {
@ -795,6 +804,7 @@ JS_GetPropertyDescArray(JSContext *cx, JSObject *obj, JSPropertyDescArray *pda)
pda->array = NULL;
return JS_TRUE;
}
n = scope->map.freeslot;
pd = JS_malloc(cx, (size_t)n * sizeof(JSPropertyDesc));
if (!pd)

View File

@ -2330,6 +2330,15 @@ js_NewIdArray(JSContext *cx, jsint length)
return ida;
}
extern JSIdArray *
js_GrowIdArray(JSContext *cx, JSIdArray *ida, jsint length)
{
ida = JS_realloc(cx, ida, sizeof(JSIdArray) + (length - 1) * sizeof(jsval));
if (ida)
ida->length = length;
return ida;
}
/* Private type used to iterate over all properties of a native JS object */
typedef struct JSNativeIteratorState {
jsint next_index; /* index into jsid array */

View File

@ -339,6 +339,9 @@ js_XDRObject(JSXDRState *xdr, JSObject **objp);
extern JSIdArray *
js_NewIdArray(JSContext *cx, jsint length);
extern JSIdArray *
js_GrowIdArray(JSContext *cx, JSIdArray *ida, jsint length);
JS_END_EXTERN_C
#endif /* jsobj_h___ */