Fix js_FindDuplicateFormal to crack the fun->u.i.names union correctly, and spruce it up with C++ (532041, r=jimb).

This commit is contained in:
Brendan Eich 2009-12-01 14:48:05 -08:00
parent dbe4c12454
commit 5bcc54508f
3 changed files with 17 additions and 17 deletions

View File

@ -112,7 +112,7 @@ GetArgsLength(JSObject *obj)
return argc;
}
static inline void
static inline void
SetArgsPrivateNative(JSObject *argsobj, js_ArgsPrivateNative *apn)
{
JS_ASSERT(STOBJ_GET_CLASS(argsobj) == &js_ArgumentsClass);
@ -292,7 +292,7 @@ js_Arguments(JSContext *cx, JSObject *parent, uint32 argc, JSObject *callee,
}
#endif
JS_DEFINE_CALLINFO_6(extern, OBJECT, js_Arguments, CONTEXT, OBJECT, UINT32, OBJECT,
JS_DEFINE_CALLINFO_6(extern, OBJECT, js_Arguments, CONTEXT, OBJECT, UINT32, OBJECT,
DOUBLEPTR, APNPTR, 0, 0)
/* FIXME change the return type to void. */
@ -3062,16 +3062,17 @@ js_FreezeLocalNames(JSContext *cx, JSFunction *fun)
#endif
}
extern JSAtom *
js_FindDuplicateFormal(JSFunction *fun)
JSAtom *
JSFunction::findDuplicateFormal() const
{
unsigned nargs = fun->nargs;
if (nargs <= 1)
return NULL;
/* Function with two to MAX_ARRAY_LOCALS parameters use an aray. */
if (nargs <= MAX_ARRAY_LOCALS) {
jsuword *array = fun->u.i.names.array;
unsigned n = nargs + u.i.nvars + u.i.nupvars;
if (n <= MAX_ARRAY_LOCALS) {
jsuword *array = u.i.names.array;
/* Quadratic, but MAX_ARRAY_LOCALS is 8, so at most 28 comparisons. */
for (unsigned i = 0; i < nargs; i++) {
for (unsigned j = i + 1; j < nargs; j++) {
@ -3082,11 +3083,11 @@ js_FindDuplicateFormal(JSFunction *fun)
return NULL;
}
/*
/*
* Functions with more than MAX_ARRAY_LOCALS parameters use a hash
* table. Hashed local name maps have already made a list of any
* duplicate argument names for us.
*/
JSNameIndexPair *dup = fun->u.i.names.map->lastdup;
JSNameIndexPair *dup = u.i.names.map->lastdup;
return dup ? dup->name : NULL;
}

View File

@ -180,6 +180,12 @@ struct JSFunction : public JSObject {
int sharpSlotBase(JSContext *cx);
/*
* If fun's formal parameters include any duplicate names, return one
* of them (chosen arbitrarily). If they are all unique, return NULL.
*/
JSAtom *findDuplicateFormal() const;
uint32 countInterpretedReservedSlots() const;
};
@ -436,13 +442,6 @@ js_GetLocalNameArray(JSContext *cx, JSFunction *fun, struct JSArenaPool *pool);
extern void
js_FreezeLocalNames(JSContext *cx, JSFunction *fun);
/*
* If fun's formal parameters include any duplicate names, return one
* of them (chosen arbitrarily). If they are all unique, return NULL.
*/
extern JSAtom *
js_FindDuplicateFormal(JSFunction *fun);
extern JSBool
js_fun_apply(JSContext *cx, uintN argc, jsval *vp);

View File

@ -1263,7 +1263,7 @@ CheckStrictFormals(JSContext *cx, JSTreeContext *tc, JSFunction *fun,
if (!tc->needStrictChecks())
return true;
atom = js_FindDuplicateFormal(fun);
atom = fun->findDuplicateFormal();
if (atom) {
/*
* We have found a duplicate parameter name. If we can find the