diff --git a/_examples/simple/simple.go b/_examples/simple/simple.go index 5eba42a..7ae7c76 100644 --- a/_examples/simple/simple.go +++ b/_examples/simple/simple.go @@ -16,3 +16,11 @@ func Add(i, j int) int { func Bool(b bool) bool { return b } + +func Comp64Add(i, j complex64) complex64 { + return i + j +} + +func Comp128Add(i, j complex128) complex128 { + return i + j +} diff --git a/_examples/simple/test.py b/_examples/simple/test.py index f202c28..040f283 100644 --- a/_examples/simple/test.py +++ b/_examples/simple/test.py @@ -18,3 +18,7 @@ fct() print("pkg.Add(1,2)= %s" % (pkg.Add(1,2),)) print("pkg.Bool(True)= %s" % pkg.Bool(True)) print("pkg.Bool(False)= %s" % pkg.Bool(False)) +a = 3+4j +b = 2+5j +print("pkg.Comp64Add(%s, %s) = %s" % (a, b, pkg.Comp128Add(a, b))) +print("pkg.Comp128Add(%s, %s) = %s" % (a, b, pkg.Comp128Add(a, b))) diff --git a/bind/gencffi.go b/bind/gencffi.go index da400de..8bc1744 100644 --- a/bind/gencffi.go +++ b/bind/gencffi.go @@ -39,7 +39,11 @@ typedef void *GoMap; typedef void *GoChan; typedef struct { void *t; void *v; } GoInterface; typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; +typedef struct { GoFloat32 real; GoFloat32 imag; } GoComplex64; +typedef struct { GoFloat64 real; GoFloat64 imag; } GoComplex128; +extern GoComplex64 _cgopy_GoComplex64(GoFloat32 p0, GoFloat32 p1); +extern GoComplex128 _cgopy_GoComplex128(GoFloat64 p0, GoFloat64 p1); extern GoString _cgopy_GoString(char* p0); extern char* _cgopy_CString(GoString p0); extern void _cgopy_FreeCString(char* p0); @@ -62,6 +66,20 @@ class _cffi_helper(object): def cffi_cgopy_cnv_py2c_bool(o): return ffi.cast('_Bool', o) + @staticmethod + def cffi_cgopy_cnv_py2c_complex64(o): + real = o.real + imag = o.imag + complex64 = _cffi_helper.lib._cgopy_GoComplex64(real, imag) + return complex64 + + @staticmethod + def cffi_cgopy_cnv_py2c_complex128(o): + real = o.real + imag = o.imag + complex128 = _cffi_helper.lib._cgopy_GoComplex128(real, imag) + return complex128 + @staticmethod def cffi_cgopy_cnv_py2c_string(o): s = ffi.new("char[]", o) @@ -87,6 +105,14 @@ class _cffi_helper(object): def cffi_cgopy_cnv_c2py_bool(c): return bool(c) + @staticmethod + def cffi_cgopy_cnv_c2py_complex64(c): + return complex(c.real, c.imag) + + @staticmethod + def cffi_cgopy_cnv_c2py_complex128(c): + return complex(c.real, c.imag) + @staticmethod def cffi_cgopy_cnv_c2py_string(c): s = _cffi_helper.lib._cgopy_CString(c) diff --git a/bind/gengo.go b/bind/gengo.go index 68ee6ad..72af0eb 100644 --- a/bind/gengo.go +++ b/bind/gengo.go @@ -94,6 +94,16 @@ func _cgopy_FreeCString(cs *C.char) { C.free(unsafe.Pointer(cs)) } +//export _cgopy_GoComplex64 +func _cgopy_GoComplex64(real, imag float32) complex64 { + return complex(real, imag) +} + +//export _cgopy_GoComplex128 +func _cgopy_GoComplex128(real, imag float64) complex128 { + return complex(real, imag) +} + // --- end cgo helpers --- // --- begin cref helpers --- diff --git a/bind/symtab.go b/bind/symtab.go index b1595dc..993229e 100644 --- a/bind/symtab.go +++ b/bind/symtab.go @@ -963,7 +963,7 @@ func init() { goname: "complex64", cpyname: "float complex", cgoname: "GoComplex64", - pyfmt: "D", + pyfmt: "O&", pybuf: "ff", pysig: "complex", c2py: "cgopy_cnv_c2py_complex64", @@ -979,7 +979,7 @@ func init() { goname: "complex128", cpyname: "double complex", cgoname: "GoComplex128", - pyfmt: "D", + pyfmt: "O&", pybuf: "dd", pysig: "complex", c2py: "cgopy_cnv_c2py_complex128", diff --git a/main_test.go b/main_test.go index 934ac52..fd57211 100644 --- a/main_test.go +++ b/main_test.go @@ -378,6 +378,8 @@ fct()... pkg.Add(1,2)= 3 pkg.Bool(True)= True pkg.Bool(False)= False +pkg.Comp64Add((3+4j), (2+5j)) = (5+9j) +pkg.Comp128Add((3+4j), (2+5j)) = (5+9j) `), }) @@ -391,6 +393,8 @@ fct()... pkg.Add(1,2)= 3 pkg.Bool(True)= True pkg.Bool(False)= False +pkg.Comp64Add((3+4j), (2+5j)) = (5+9j) +pkg.Comp128Add((3+4j), (2+5j)) = (5+9j) `), }) }