From 959b87342eaffdfa4ff036edd2e6dbc9d8452a77 Mon Sep 17 00:00:00 2001 From: Greg Date: Mon, 6 May 2019 09:00:38 -0400 Subject: [PATCH] Return Golang bools from Objective-C functions that return BOOL. Move nswrap command to cmd/nswrap. --- main.go => cmd/nswrap/main.go | 13 ++++++++++--- types/convert.go | 10 +++++++++- wrap/main.go | 3 +++ 3 files changed, 22 insertions(+), 4 deletions(-) rename main.go => cmd/nswrap/main.go (95%) diff --git a/main.go b/cmd/nswrap/main.go similarity index 95% rename from main.go rename to cmd/nswrap/main.go index 7321300..9bc6e77 100644 --- a/main.go +++ b/cmd/nswrap/main.go @@ -167,9 +167,16 @@ func Start() (err error) { if err != nil { // If clang fails it still prints out the AST, so we have to run it // again to get the real error. -// errBody, _ := exec.Command("clang", cargs...).CombinedOutput() - - panic("clang failed: " + err.Error() + ":\n\n") + //errBody, _ := exec.Command("clang", cargs...).CombinedOutput() + var txt string + switch x := err.(type) { + case *exec.ExitError: + txt = string(x.Stderr) + default: + txt = err.Error() + } + fmt.Printf("clang failed:\n%s\n", txt) + os.Exit(-1) } lines := readAST(astPP) diff --git a/types/convert.go b/types/convert.go index 9fe3300..7f42661 100644 --- a/types/convert.go +++ b/types/convert.go @@ -350,7 +350,12 @@ func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string { if IsGoInterface(rtgt) { rtgt = "*Id" } - ret.WriteString("return (" + rtgt + ")(") + if rtgt == "BOOL" { + ret.WriteString("return (") + rtgt = "bool" + } else { + ret.WriteString("return (" + rtgt + ")(") + } if rtype.IsPointer() { ret.WriteString("unsafe.Pointer(") } @@ -382,6 +387,9 @@ func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string { ret.WriteString(")") } } + if rt == "BOOL" { + ret.WriteString(" != 0") + } return ret.String() } diff --git a/wrap/main.go b/wrap/main.go index 7c63dff..f4bba42 100644 --- a/wrap/main.go +++ b/wrap/main.go @@ -610,6 +610,9 @@ func (w *Wrapper) _processMethod(m *Method,fun bool) { if types.IsGoInterface(grtype) { grtype = "*Id" } + if grtype == "BOOL" { // convert objective-c bools to Go bools + grtype = "bool" + } if gname == grtype { // avoid name conflicts between methods and types gname = "Get" + gname }