diff --git a/CHANGES.current b/CHANGES.current index a9a67201d..64e4c4402 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.1.0 (in progress) =========================== +2022-03-15: ianlancetaylor + [Go] Don't convert arrays to pointers if there is a "gotype" + typemap entry. + 2022-03-15: ianlancetaylor [Go] Add documentation note about Go and C++ exceptions. diff --git a/Examples/test-suite/go/go_inout_runme.go b/Examples/test-suite/go/go_inout_runme.go index 84dec7a0b..981b7f2b3 100644 --- a/Examples/test-suite/go/go_inout_runme.go +++ b/Examples/test-suite/go/go_inout_runme.go @@ -54,4 +54,8 @@ func main() { c1 := go_inout.NewC1() c2.M2(c1) c2.M2(nil) + + if !go_inout.Strings([]string{"1", "2"}) { + panic("Strings failed") + } } diff --git a/Examples/test-suite/go_inout.i b/Examples/test-suite/go_inout.i index bb73f1b90..ae68178a0 100644 --- a/Examples/test-suite/go_inout.i +++ b/Examples/test-suite/go_inout.i @@ -243,3 +243,43 @@ class C2 : public C1 { void M2(C1*) {} }; %} + +%typemap(gotype) (char *ps[], int cs) "[]string" + +%typemap(in) (char *ps[], int cs) +%{ + { + int i; + _gostring_* a; + + $2 = $input.len; + a = (_gostring_*) $input.array; + $1 = (char **) malloc (($2 + 1) * sizeof (char *)); + for (i = 0; i < $2; i++) { + _gostring_ *ps = &a[i]; + $1[i] = (char *) malloc(ps->n + 1); + memcpy($1[i], ps->p, ps->n); + $1[i][ps->n] = '\0'; + } + $1[i] = NULL; + } +%} + +%typemap(freearg) (char *ps[], int cs) +%{ + { + int i; + + for (i = 0; i < $2; i++) { + free($1[i]); + } + free($1); + } +%} + +%inline +%{ +bool Strings(char *ps[], int cs) { + return cs == 2 && strcmp(ps[0], "1") == 0 && strcmp(ps[1], "2") == 0 & ps[2] == NULL; +} +%} diff --git a/Source/Modules/go.cxx b/Source/Modules/go.cxx index c2e95228d..ed1bc125c 100644 --- a/Source/Modules/go.cxx +++ b/Source/Modules/go.cxx @@ -1463,7 +1463,7 @@ private: p = getParm(p); SwigType *pt = Copy(Getattr(p, "type")); - if (SwigType_isarray(pt)) { + if (SwigType_isarray(pt) && Getattr(p, "tmap:gotype") == NULL) { SwigType_del_array(pt); SwigType_add_pointer(pt); } @@ -5109,7 +5109,7 @@ private: } String *t = Copy(type); - if (SwigType_isarray(t)) { + if (SwigType_isarray(t) && Getattr(n, "tmap:gotype") == NULL) { SwigType_del_array(t); SwigType_add_pointer(t); }