From 0f54384ee377a4dcd39d4c83909349276ff452c9 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 18 Apr 2016 15:30:06 -0700 Subject: [PATCH] [Go] Fix use of goout typemap when calling base method by forcing the "type" attribute to the value we need. --- CHANGES.current | 4 +++ Examples/test-suite/go/go_inout_runme.go | 10 +++++++ Examples/test-suite/go_inout.i | 35 ++++++++++++++++++++++++ Source/Modules/go.cxx | 6 ++++ 4 files changed, 55 insertions(+) diff --git a/CHANGES.current b/CHANGES.current index c23a2d935..1627a09ec 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,6 +5,10 @@ See the RELEASENOTES file for a summary of changes in each release. Version 3.0.9 (in progress) =========================== +2016-04-18: ianlancetaylor + [Go] Fix use of goout typemap when calling base method by + forcing the "type" attribute to the value we need. + 2016-04-17: ianlancetaylor [Go] Fixes for Go 1.6: avoid returning Go pointers from directors that return string values; add a trailing 0 byte diff --git a/Examples/test-suite/go/go_inout_runme.go b/Examples/test-suite/go/go_inout_runme.go index 13c429b87..9aa0cd0c5 100644 --- a/Examples/test-suite/go/go_inout_runme.go +++ b/Examples/test-suite/go/go_inout_runme.go @@ -40,4 +40,14 @@ func main() { fmt.Println("got", a, "want", dwant) panic(a) } + + c2 := go_inout.NewC2() + pm := c2.M() + want = map[string]interface{}{ + "ID": float64(1), + } + if !reflect.DeepEqual(*pm, want) { + fmt.Println("for c2.M got", pm, "want", want) + panic(pm) + } } diff --git a/Examples/test-suite/go_inout.i b/Examples/test-suite/go_inout.i index a174246f1..0bcb979ef 100644 --- a/Examples/test-suite/go_inout.i +++ b/Examples/test-suite/go_inout.i @@ -68,6 +68,26 @@ type In json.Marshaler } %} +%typemap(gotype) RetStruct* "*map[string]interface{}" + +%typemap(imtype) RetStruct* "*string" + +%typemap(out,fragment="AllocateString") RetStruct* +%{ + $result = (_gostring_*)malloc(sizeof(_gostring_)); + *$result = Swig_AllocateString($1->str.data(), $1->str.length()); +%} + +%typemap(goout,fragment="CopyString") RetStruct* +%{ + defer Swig_free(uintptr(unsafe.Pointer($1))) + var rm map[string]interface{} + if err := json.Unmarshal([]byte(swigCopyString(*$1)), &rm); err != nil { + panic(err) + } + $result = &rm +%} + %inline %{ @@ -206,3 +226,18 @@ void DoubleArray(MyArray* v) { } } %} + +%inline +%{ +class C1 { + public: + RetStruct* M() { + RetStruct* r = new RetStruct; + r->str = "{\"ID\":1}"; + return r; + } +}; + +class C2 : public C1 { +}; +%} diff --git a/Source/Modules/go.cxx b/Source/Modules/go.cxx index c34d423ba..637e5e3a6 100644 --- a/Source/Modules/go.cxx +++ b/Source/Modules/go.cxx @@ -1362,6 +1362,10 @@ private: goargout(info->parms); if (SwigType_type(info->result) != T_VOID) { + + Swig_save("cgoGoWrapper", info->n, "type", "tmap:goout", NULL); + Setattr(info->n, "type", info->result); + String *goout = goTypemapLookup("goout", info->n, "swig_r"); if (goout == NULL) { Printv(f_go_wrappers, "\treturn swig_r\n", NULL); @@ -1374,6 +1378,8 @@ private: Printv(f_go_wrappers, goout, "\n", NULL); Printv(f_go_wrappers, "\treturn swig_r_1\n", NULL); } + + Swig_restore(info->n); } Printv(f_go_wrappers, "}\n\n", NULL);