Repository: lucy-clownfish Updated Branches: refs/heads/master bc67c0695 -> 6dc5deb06
Nil-check nullable retvals from Go. Type assertions in Go for interfaces fail on nil, so switch to checking for nil then returning nil for nullable method return values. Only perform the type assertion when non-nil, which is guaranteed to succeed unless there is a bug in the method implementation. Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/17fdbba7 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/17fdbba7 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/17fdbba7 Branch: refs/heads/master Commit: 17fdbba7b1d53df6f84556d70d32dfd1c88144bd Parents: bc67c06 Author: Marvin Humphrey <[email protected]> Authored: Mon Oct 5 18:41:45 2015 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Mon Oct 5 18:41:45 2015 -0700 ---------------------------------------------------------------------- compiler/src/CFCGoFunc.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/17fdbba7/compiler/src/CFCGoFunc.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCGoFunc.c b/compiler/src/CFCGoFunc.c index 1d94f04..83b6775 100644 --- a/compiler/src/CFCGoFunc.c +++ b/compiler/src/CFCGoFunc.c @@ -312,10 +312,28 @@ CFCGoFunc_return_statement(CFCParcel *parcel, CFCType *return_type, char *go_type_name = CFCGoTypeMap_go_type_name(return_type, parcel); char *pattern; if (CFCType_incremented(return_type)) { - pattern = "\treturn %sWRAPAny(unsafe.Pointer(retvalCF)).(%s)\n"; + if (CFCType_nullable(return_type)) { + pattern = + "\tretvalGO := %sWRAPAny(unsafe.Pointer(retvalCF))\n" + "\tif retvalGO == nil { return nil }\n" + "\treturn retvalGO.(%s)\n" + ; + } + else { + pattern = "\treturn %sWRAPAny(unsafe.Pointer(retvalCF)).(%s)\n"; + } } else { - pattern = "\treturn %sWRAPAny(unsafe.Pointer(C.cfish_inc_refcount(unsafe.Pointer(retvalCF)))).(%s)\n"; + if (CFCType_nullable(return_type)) { + pattern = + "\tretvalGO := %sWRAPAny(unsafe.Pointer(C.cfish_incref(unsafe.Pointer(retvalCF))))\n" + "\tif retvalGO == nil { return nil }\n" + "\treturn retvalGO.(%s)\n" + ; + } + else { + pattern = "\treturn %sWRAPAny(unsafe.Pointer(C.cfish_inc_refcount(unsafe.Pointer(retvalCF)))).(%s)\n"; + } } statement = CFCUtil_sprintf(pattern, clownfish_dot, go_type_name); FREEMEM(go_type_name);
