[Go] Add a test for godirectorin with const char **

This exercises a bug that was found with this fairly complex mapping, it
wasn't putting newlines in the proper place.  A previous commit added
the newlines, this makes sure it doesn't happen again.

Signed-off-by: Corey Minyard <cminyard@mvista.com>
This commit is contained in:
Corey Minyard 2022-04-18 19:53:59 -05:00
commit 3cc67d42f3
2 changed files with 112 additions and 1 deletions

View file

@ -34,6 +34,15 @@ func (p *GoMyClass) S3(s *string) {
*s = "R3"
}
func (p *GoMyClass) S4(s []string) {
if s[0] != "T1" {
panic(s)
}
if s[1] != "T2" {
panic(s)
}
}
func main() {
a := wrap.NewDirectorMyClass(&GoMyClass{})
m := map[string]interface{}{
@ -56,6 +65,8 @@ func main() {
panic(str)
}
a.CallS4([]string{ "T1", "T2" })
a = wrap.NewDirectorMyClass(nil)
s = a.Adjust(m)
if s.Str != `{"first":"second"}` {

View file

@ -120,6 +120,92 @@ type GoRetStruct struct {
$1.assign(tmp->p, tmp->p + tmp->n);
}
%inline %{
// Helper functions for converting string arrays
#include <stdlib.h>
void *alloc_ptr_array(unsigned int len)
{
return calloc(len, sizeof(void *));
}
void set_ptr_array(void *ain, unsigned int pos, void *val)
{
void **a = (void **) ain;
a[pos] = val;
}
void *get_ptr_array(void *ain, unsigned int pos)
{
void **a = (void **) ain;
return a[pos];
}
void free_ptr_array(void *ain)
{
void **a = (void **) ain;
unsigned int i;
if (!a)
return;
for (i = 0; a[i]; i++) {
free(a[i]);
}
free(a);
}
char *uintptr_to_string(void *in)
{
return (char *) in;
}
void *string_to_uintptr(char *in)
{
return strdup(in);
}
%}
// These typemaps convert between an array of strings in Go and a
// const char** that is NULL terminated in C++.
%typemap(gotype) (const char * const *) "[]string";
%typemap(imtype) (const char * const *) "uintptr";
%typemap(goin) (const char * const *) {
if $input == nil || len($input) == 0 {
$result = 0
} else {
$result = Alloc_ptr_array(uint(len($input) + 1))
defer func() {
Free_ptr_array($result)
}()
var i uint
for i = 0; i < uint(len($input)); i++ {
Set_ptr_array($result, i, String_to_uintptr($input[i]))
}
}
}
%typemap(in) (const char * const *) {
$1 = (char **) $input;
}
%typemap(godirectorin) (const char * const *) {
if ($input == 0) {
$result = nil
} else {
var i uint
for i = 0; ; i++ {
var v uintptr = Get_ptr_array($input, i)
if v == 0 {
break
}
}
if i == 0 {
$result = nil
} else {
$result = make([]string, i)
for i = 0; ; i++ {
var v uintptr = Get_ptr_array($input, i)
if v == 0 {
break
}
$result[i] = Uintptr_to_string(v)
}
}
}
}
%feature("director") MyClass;
%inline
@ -134,9 +220,23 @@ class MyClass {
return r;
}
virtual void S1(std::string s) = 0;
void CallS4(const char * const *strarray);
virtual void S1(std::string s);
virtual void S2(std::string& s) = 0;
virtual void S3(std::string* s) = 0;
virtual void S4(const char * const *strarray);
};
void MyClass::S1(std::string s) {
throw "Base S1 called!";
}
void MyClass::S4(const char * const *strarray) {
throw "Base S4 called!";
}
void MyClass::CallS4(const char * const *strarray) {
this->S4(strarray);
}
%}