Skip to content

Commit

Permalink
Fix for Saltarelle#423, allow state machine to rethrow errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nippur72 committed Jun 3, 2015
1 parent ea32c51 commit 6d1d662
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Compiler/JSModel/StateMachineRewrite/StateMachineRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ private JsBlockStatement ProcessAsyncMethod(JsBlockStatement statement, string s
if (taskCompletionSource != null)
body = JsStatement.Try(body, JsStatement.Catch(catchVariable, JsStatement.Block(makeSetException(JsExpression.Identifier(catchVariable)))), null);

IEnumerable<JsVariableDeclaration> declarations = new[] { JsStatement.Declaration(_stateVariableName, JsExpression.Number(0)) };
IEnumerable<JsVariableDeclaration> declarations = new[] { JsStatement.Declaration(_stateVariableName, JsExpression.Number(0)),
JsStatement.Declaration("$$ex", null)
};
if (taskCompletionSource != null)
declarations = declarations.Concat(new[] { taskCompletionSource });
declarations = declarations.Concat(hoistResult.Item2.Select(v => JsStatement.Declaration(v, null)));
Expand Down Expand Up @@ -499,6 +501,16 @@ private bool HandleTryStatement(JsTryStatement stmt, StackEntry location, Immuta
currentBlock.Add(newBlock);
}

// adds: if ($$ex !== undefined) throw $$ex;
var reThrow = JsStatement.If( JsExpression.NotSame(JsExpression.Identifier("$$ex"),JsExpression.Identifier("undefined")),
JsStatement.Throw(JsExpression.Identifier("$$ex")),null);

// rewrite 'guarded' putting 'reThrow' as first statement of the block
var newGuarded = new List<JsStatement>();
newGuarded.Add(reThrow);
newGuarded.AddRange(guarded.Statements);
guarded = JsStatement.Block(newGuarded);

currentBlock.Add(JsStatement.Try(guarded, @catch, @finally));
return true;
}
Expand Down

0 comments on commit 6d1d662

Please sign in to comment.