[Go] Don't convert arrays to pointers if there is a "gotype" typemap entry.

Fixes #749
This commit is contained in:
Ian Lance Taylor 2022-03-15 12:18:47 -07:00
parent 46aaf11b01
commit b819363117
4 changed files with 50 additions and 2 deletions

View File

@ -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.

View File

@ -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")
}
}

View File

@ -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;
}
%}

View File

@ -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);
}