[Go] Fixes for Go 1.6: avoid returning Go pointers from
directors that return string values; add a trailing 0 byte when treating Go string as C char*.
This commit is contained in:
parent
f1d1d7b490
commit
223c2a4835
7 changed files with 81 additions and 7 deletions
|
|
@ -68,6 +68,10 @@ TAscii *DigitsGlobalC;
|
|||
// Director test
|
||||
%feature("director");
|
||||
|
||||
#if defined(SWIGGO)
|
||||
%typemap(godirectorout) CharPtr, CCharPtr ""
|
||||
#endif
|
||||
|
||||
%inline %{
|
||||
struct DirectorTest {
|
||||
virtual UCharPtr UCharFunction(UCharPtr str) { return str; }
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ struct RetStruct {
|
|||
|
||||
// Write a typemap that calls C++ by converting in and out of JSON.
|
||||
|
||||
%go_import("encoding/json", "bytes", "encoding/binary")
|
||||
%go_import("encoding/json", "bytes", "encoding/binary", "reflect", "unsafe")
|
||||
|
||||
%insert(go_header)
|
||||
%{
|
||||
|
|
@ -87,6 +87,10 @@ struct MyArray {
|
|||
std::vector<std::string> strings;
|
||||
};
|
||||
|
||||
void* Allocate(int n) {
|
||||
return new char[n];
|
||||
}
|
||||
|
||||
static uint64_t getuint64(const char* s) {
|
||||
uint64_t ret = 0;
|
||||
for (int i = 0; i < 8; i++, s++) {
|
||||
|
|
@ -121,7 +125,12 @@ static void putuint64(std::string *s, size_t off, uint64_t v) {
|
|||
buf.Write(b[:])
|
||||
buf.WriteString(s)
|
||||
}
|
||||
str := buf.String()
|
||||
bb := buf.Bytes()
|
||||
p := Allocate(len(bb))
|
||||
copy((*[1<<15]byte)(unsafe.Pointer(p))[:len(bb)], bb)
|
||||
var str string
|
||||
(*reflect.StringHeader)(unsafe.Pointer(&str)).Data = uintptr(unsafe.Pointer(p))
|
||||
(*reflect.StringHeader)(unsafe.Pointer(&str)).Len = len(bb)
|
||||
$result = &str
|
||||
}
|
||||
%}
|
||||
|
|
|
|||
|
|
@ -109,7 +109,11 @@ namespace test {
|
|||
#ifdef SWIGGO
|
||||
%typemap(gotype) string_class * "string"
|
||||
%typemap(in) string_class * {
|
||||
$1 = new string_class($input.p);
|
||||
char* buf = new char[$input.n + 1];
|
||||
memcpy(buf, $input.p, $input.n);
|
||||
buf[$input.n] = '\0';
|
||||
$1 = new string_class(buf);
|
||||
delete[] buf;
|
||||
}
|
||||
%typemap(freearg) string_class * {
|
||||
delete $1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue