fixed being unable to use 'yield throw'

This commit is contained in:
Andreas Lubbe 2015-02-15 19:01:00 +01:00
parent b49b41320e
commit e3f6e19950
3 changed files with 13 additions and 2 deletions

View File

@ -2530,7 +2530,7 @@
if (o.scope.parent == null) {
this.error('yield statements must occur within a function generator.');
}
if (indexOf.call(Object.keys(this.first), 'expression') >= 0) {
if (indexOf.call(Object.keys(this.first), 'expression') >= 0 && !(this.first instanceof Throw)) {
if (this.first.expression != null) {
parts.push(this.first.expression.compileToFragments(o, LEVEL_OP));
}

View File

@ -1801,7 +1801,7 @@ exports.Op = class Op extends Base
op = @operator
if not o.scope.parent?
@error 'yield statements must occur within a function generator.'
if 'expression' in Object.keys @first
if 'expression' in Object.keys(@first) and not (@first instanceof Throw)
parts.push @first.expression.compileToFragments o, LEVEL_OP if @first.expression?
else
parts.push [@makeCode "(#{op} "]

View File

@ -124,3 +124,14 @@ test "switch expressions", ->
ok z.value is 1337 and z.done is false
z = x.next 3
ok z.value is 3 and z.done is true
test "`yield` can be thrown", ->
x = do ->
throw yield null
x.next()
throws -> x.next new Error "boom"
test "`throw` can be yielded", ->
x = do ->
yield throw new Error "boom"
throws -> x.next()