diff --git a/js/src/jsemit.cpp b/js/src/jsemit.cpp index df4125777e8b..4d457604bae1 100644 --- a/js/src/jsemit.cpp +++ b/js/src/jsemit.cpp @@ -5797,12 +5797,7 @@ js_EmitTree(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn) #if JS_HAS_GENERATORS case TOK_YIELD: - if (!cg->inFunction()) { - ReportCompileErrorNumber(cx, CG_TS(cg), pn, JSREPORT_ERROR, - JSMSG_BAD_RETURN_OR_YIELD, - js_yield_str); - return JS_FALSE; - } + JS_ASSERT(cg->inFunction()); if (pn->pn_kid) { if (!js_EmitTree(cx, cg, pn->pn_kid)) return JS_FALSE; diff --git a/js/src/jsparse.cpp b/js/src/jsparse.cpp index c7359707c985..34023d77a7d5 100644 --- a/js/src/jsparse.cpp +++ b/js/src/jsparse.cpp @@ -4786,8 +4786,13 @@ Parser::returnOrYield(bool useAssignExpr) return NULL; #if JS_HAS_GENERATORS - if (tt == TOK_YIELD) + if (tt == TOK_YIELD) { + if (!tc->inFunction()) { + reportErrorNumber(NULL, JSREPORT_ERROR, JSMSG_BAD_RETURN_OR_YIELD, js_yield_str); + return NULL; + } tc->flags |= TCF_FUN_IS_GENERATOR; + } #endif /* This is ugly, but we don't want to require a semicolon. */ diff --git a/js/src/tests/js1_8_5/extensions/reflect-parse.js b/js/src/tests/js1_8_5/extensions/reflect-parse.js index 97bbc76e9465..74d26f773950 100644 --- a/js/src/tests/js1_8_5/extensions/reflect-parse.js +++ b/js/src/tests/js1_8_5/extensions/reflect-parse.js @@ -390,6 +390,17 @@ assertStmt("try { } catch (e if foo) { } catch (e if bar) { } catch (e) { } fina catchClause(ident("e"), null, blockStmt([])) ], blockStmt([]))); +// Bug 632028: yield outside of a function should throw +(function() { + var threw = false; + try { + Reflect.parse("yield 0"); + } catch (expected) { + threw = true; + } + assertEq(threw, true); +})(); + // redeclarations (TOK_NAME nodes with lexdef) assertStmt("function f() { function g() { } function g() { } }",