[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
commit b819363117
4 changed files with 50 additions and 2 deletions

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