Bug 461170 - Detect when typeof is applied to an expression before constant folding so we correctly throw unbound name exceptions from the folded expression. r=brendan

This commit is contained in:
Blake Kaplan 2009-11-06 18:27:17 +01:00
parent cf951c4671
commit d561cdff98
2 changed files with 17 additions and 3 deletions

View File

@ -6087,8 +6087,10 @@ js_EmitTree(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn)
}
#endif
pn2 = pn->pn_kid;
if (op == JSOP_TYPEOF && pn2->pn_type != TOK_NAME)
op = JSOP_TYPEOFEXPR;
/* See js_FoldConstants for why this assertion holds true. */
JS_ASSERT_IF(op == JSOP_TYPEOF, pn2->pn_type == TOK_NAME);
oldflags = cg->flags;
cg->flags &= ~TCF_IN_FOR_INIT;
if (!js_EmitTree(cx, cg, pn2))

View File

@ -8772,8 +8772,20 @@ js_FoldConstants(JSContext *cx, JSParseNode *pn, JSTreeContext *tc, bool inCond)
break;
case PN_UNARY:
/* Our kid may be null (e.g. return; vs. return e;). */
pn1 = pn->pn_kid;
/*
* Kludge to deal with typeof expressions: because constant folding
* can turn an expression into a name node, we have to check here,
* before folding, to see if we should throw undefined name errors.
*
* NB: We know that if pn->pn_op is JSOP_TYPEOF, pn1 will not be
* null. This assumption does not hold true for other unary
* expressions.
*/
if (pn->pn_op == JSOP_TYPEOF && pn1->pn_type != TOK_NAME)
pn->pn_op = JSOP_TYPEOFEXPR;
if (pn1 && !js_FoldConstants(cx, pn1, tc, pn->pn_op == JSOP_NOT))
return JS_FALSE;
break;