From e18f650fc9598a2abe63cec499da2b46c0d4d169 Mon Sep 17 00:00:00 2001 From: Nicholas Ng Date: Fri, 12 Oct 2018 16:11:49 +0100 Subject: [PATCH 1/2] Detect and panic if final statetype has error. --- .../core/codegen/statetype3/RPCoreSTSessionApiBuilder.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scribble-go/src/main/java/org/scribble/ext/go/core/codegen/statetype3/RPCoreSTSessionApiBuilder.java b/scribble-go/src/main/java/org/scribble/ext/go/core/codegen/statetype3/RPCoreSTSessionApiBuilder.java index 0db11d832..39f6a68db 100644 --- a/scribble-go/src/main/java/org/scribble/ext/go/core/codegen/statetype3/RPCoreSTSessionApiBuilder.java +++ b/scribble-go/src/main/java/org/scribble/ext/go/core/codegen/statetype3/RPCoreSTSessionApiBuilder.java @@ -569,7 +569,11 @@ public int compare(Entry o1, Entry o2) // cf. state chan builder // FIXME: chan struct reuse //"ini._" + init + ".id = 1\n" + "return f(ini._" + init + ")\n")*/ - + "return f(ini.Init())\n" + + "end := f(ini.Init())\n" + + "if end." + RPCoreSTApiGenConstants.GO_MPCHAN_ERR + " != nil {\n" + + "panic(end." + RPCoreSTApiGenConstants.GO_MPCHAN_ERR + ")\n" + + "}\n" + + "return end\n" + "}"; From 062522ae2caa7ce3ef39c17f95fe0542261468bd Mon Sep 17 00:00:00 2001 From: Nicholas Ng Date: Fri, 12 Oct 2018 17:44:31 +0100 Subject: [PATCH 2/2] Patch error handling for Send functions. --- .../statetype3/RPCoreSTSendActionBuilder.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/scribble-go/src/main/java/org/scribble/ext/go/core/codegen/statetype3/RPCoreSTSendActionBuilder.java b/scribble-go/src/main/java/org/scribble/ext/go/core/codegen/statetype3/RPCoreSTSendActionBuilder.java index 24b4cd687..9712de0e4 100644 --- a/scribble-go/src/main/java/org/scribble/ext/go/core/codegen/statetype3/RPCoreSTSendActionBuilder.java +++ b/scribble-go/src/main/java/org/scribble/ext/go/core/codegen/statetype3/RPCoreSTSendActionBuilder.java @@ -42,6 +42,24 @@ public String buildArgs(STStateChanApiBuilder api, EAction a) } } + /** + * checkError helper function surrounds the given expression expr with Go's error check + * and assigns errorField with err if it exists, i.e. + * + *
{@code
+	 * if err := $expression; err != nil {
+	 * 	 $errField = err
+	 * }
+	 * }
+ * + * @param expr Expression that returns error + * @param errField Location to store the error + * @return code fragment of expr with error check + */ + private String checkError(String expr, String errField) { + return "if err := " + expr + "; err != nil {\n" + errField + " = err\n" + "}\n"; + } + @Override public String buildBody(STStateChanApiBuilder api, EState curr, EAction a, EState succ) { @@ -131,23 +149,21 @@ public String buildBody(STStateChanApiBuilder api, EState curr, EAction a, EStat throw new RuntimeException("[rp-core] [param-api] TODO: " + a); } - res += errorField + " = " - + sEpWrite + res += checkError(sEpWrite + "." + (a.mid.isOp() ? RPCoreSTApiGenConstants.GO_MPCHAN_ISEND : RPCoreSTApiGenConstants.GO_MPCHAN_MSEND) + "(\"" + r.getName() + "\", i" + IntStream.range(0, a.payload.elems.size()).mapToObj(i -> ", &arg" + i + "[j]").collect(Collectors.joining("")) - + ")\n"; + + ")", errorField); } } else //(a.mid.isMessageSigName) { // FIXME: factor out with above - res += errorField + " = " - + sEpWrite + res += checkError( sEpWrite + "." + (a.mid.isOp() ? RPCoreSTApiGenConstants.GO_MPCHAN_ISEND : RPCoreSTApiGenConstants.GO_MPCHAN_MSEND) - + "(\"" + r.getName() + "\", i, &arg0[j])\n"; + + "(\"" + r.getName() + "\", i, &arg0[j])", errorField); } }