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 078caa3e64
commit 94fceb75d4
3 changed files with 17 additions and 17 deletions

View File

@ -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++) {
@ -3087,6 +3088,6 @@ js_FindDuplicateFormal(JSFunction *fun)
* 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