From 6d1d662a8b2a275bf6490cf4830e992f9caf2257 Mon Sep 17 00:00:00 2001 From: Antonino Porcino Date: Wed, 3 Jun 2015 18:34:27 +0200 Subject: [PATCH] Fix for #423, allow state machine to rethrow errors --- .../StateMachineRewrite/StateMachineRewriter.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Compiler/JSModel/StateMachineRewrite/StateMachineRewriter.cs b/Compiler/JSModel/StateMachineRewrite/StateMachineRewriter.cs index b57c7cfc..510d2d20 100644 --- a/Compiler/JSModel/StateMachineRewrite/StateMachineRewriter.cs +++ b/Compiler/JSModel/StateMachineRewrite/StateMachineRewriter.cs @@ -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 declarations = new[] { JsStatement.Declaration(_stateVariableName, JsExpression.Number(0)) }; + IEnumerable 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))); @@ -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(); + newGuarded.Add(reThrow); + newGuarded.AddRange(guarded.Statements); + guarded = JsStatement.Block(newGuarded); + currentBlock.Add(JsStatement.Try(guarded, @catch, @finally)); return true; }