-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTryCatchNode.cs
More file actions
55 lines (42 loc) · 2.08 KB
/
Copy pathTryCatchNode.cs
File metadata and controls
55 lines (42 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using NodeDev.Core.Connections;
using NodeDev.Core.Types;
using System.Linq.Expressions;
namespace NodeDev.Core.Nodes.Flow;
public class TryCatchNode : FlowNode
{
public override bool IsFlowNode => true;
public TryCatchNode(Graph graph, string? id = null) : base(graph, id)
{
Name = "TryCatch";
Inputs.Add(new("Exec", this, TypeFactory.ExecType));
Outputs.Add(new("Try", this, TypeFactory.ExecType));
Outputs.Add(new("Catch", this, TypeFactory.ExecType));
Outputs.Add(new("Finally", this, TypeFactory.ExecType));
Outputs.Add(new("Exception", this, new UndefinedGenericType("T"), linkedExec: Outputs[1]));
}
public override string GetExecOutputPathId(string pathId, Connection execOutput)
{
return pathId + "-" + execOutput.Id;
}
// We're allowed to have 'nothing' in the blocks, ex an empty "finally" or an empty "catch"
public override bool DoesOutputPathAllowDeadEnd(Connection execOutput)
{
return execOutput.Connections.Count == 0;
}
/// <summary>
/// We allow merging back together at the end. Technically all paths could continue, such as :
/// Try { } catch( Exception ex) {} finally { } ...
/// </summary>
public override bool DoesOutputPathAllowMerge(Connection execOutput) => true;
internal override Expression BuildExpression(Dictionary<Connection, Graph.NodePathChunks>? subChunks, BuildExpressionInfo info)
{
ArgumentNullException.ThrowIfNull(subChunks);
var tryBlock = Expression.Block(Graph.BuildExpression(subChunks[Outputs[0]], info));
var catchBlock = Expression.Block(Graph.BuildExpression(subChunks[Outputs[1]], info));
var finallyBlock = Expression.Block(Graph.BuildExpression(subChunks[Outputs[2]], info));
//var exceptionVariable = Expression.Variable(Outputs[3].Type.MakeRealType(), "ex");
//info.LocalVariables[Outputs[3]] = exceptionVariable; // Make sure other pieces of code use the right variable for that exception
var catchClause = Expression.Catch(Outputs[3].Type.MakeRealType(), catchBlock);
return Expression.Block(Expression.TryCatchFinally(tryBlock, Outputs[2].Connections.Count == 0 ? null : finallyBlock, catchClause));
}
}