Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Fix for #423, allow state machine to rethrow errors #425

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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