From c1d3b45735eb633360b954eb2c3779efd73d956a Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Fri, 14 Aug 2015 13:02:17 +0200 Subject: [PATCH] bind: first stab at a gopy object model --- _examples/pointers/pointers.go | 19 +++++++++++++------ bind/gencpy.go | 25 +++++++++++++++++++++++++ bind/gencpy_struct.go | 1 + bind/gencpy_type.go | 2 ++ bind/gengo.go | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/_examples/pointers/pointers.go b/_examples/pointers/pointers.go index fd3ded1..9f9c731 100644 --- a/_examples/pointers/pointers.go +++ b/_examples/pointers/pointers.go @@ -4,18 +4,25 @@ package pointers -import ( - "github.com/go-python/gopy/_examples/cpkg" -) - //type SPtr *S + type S struct { Value int } // Inc increments the value of s func Inc(s *S) { - cpkg.Printf("==> go: s.Value==%d\n", s.Value) s.Value++ - cpkg.Printf("<== go: s.Value==%d\n", s.Value) +} + +type MyInt int + +// IncInt increments an integer +func IncMyInt(i *MyInt) { + (*i)++ +} + +// IncInt increments an integer +func IncInt(i *int) { + (*i)++ } diff --git a/bind/gencpy.go b/bind/gencpy.go index c76adfa..ce49438 100644 --- a/bind/gencpy.go +++ b/bind/gencpy.go @@ -29,6 +29,31 @@ const ( // header exported from 'go tool cgo' #include "%[3]s.h" +#if PY_VERSION_HEX > 0x03000000 +#error "Python-3 is not yet supported by gopy" +#endif + + +// --- gopy object model --- + +struct _gopy_object; + +// empty interface converter +typedef GoInterface (*gopy_efacefunc)(struct _gopy_object *); + + +// proxy for all go values +struct _gopy_object { + PyObject_HEAD + void *go; /* handle to address of go value */ + gopy_efacefunc eface; +}; + +typedef struct _gopy_object gopy_object; + +// --- gopy object model --- + + // helpers for cgopy #define def_cnv(name, c2py, py2c, gotype) \ diff --git a/bind/gencpy_struct.go b/bind/gencpy_struct.go index 88d582f..c35016c 100644 --- a/bind/gencpy_struct.go +++ b/bind/gencpy_struct.go @@ -25,6 +25,7 @@ func (g *cpyGen) genStruct(cpy Struct) { cpy.sym.cgoname, cpy.ID(), ) + g.decl.Printf("gopy_efacefunc eface;\n") g.decl.Outdent() g.decl.Printf("} %s;\n", cpy.sym.cpyname) g.decl.Printf("\n\n") diff --git a/bind/gencpy_type.go b/bind/gencpy_type.go index 3f4af4e..1eddc87 100644 --- a/bind/gencpy_type.go +++ b/bind/gencpy_type.go @@ -45,6 +45,7 @@ func (g *cpyGen) genType(sym *symbol) { sym.id, ) } + g.decl.Printf("gopy_efacefunc eface;\n") g.decl.Outdent() g.decl.Printf("} %s;\n", sym.cpyname) g.decl.Printf("\n\n") @@ -152,6 +153,7 @@ func (g *cpyGen) genTypeNew(sym *symbol) { g.impl.Printf("%s *self;\n", sym.cpyname) g.impl.Printf("self = (%s *)type->tp_alloc(type, 0);\n", sym.cpyname) g.impl.Printf("self->cgopy = cgo_func_%s_new();\n", sym.id) + g.impl.Printf("self->eface = (gopy_efacefunc)cgo_func_%s_eface;\n", sym.id) g.impl.Printf("return (PyObject*)self;\n") g.impl.Outdent() g.impl.Printf("}\n\n") diff --git a/bind/gengo.go b/bind/gengo.go index 18e7fd7..a9a8ca1 100644 --- a/bind/gengo.go +++ b/bind/gengo.go @@ -360,6 +360,23 @@ func (g *goGen) genStruct(s Struct) { g.Outdent() g.Printf("}\n\n") + // empty interface converter + g.Printf("//export cgo_func_%[1]s_eface\n", s.ID()) + g.Printf("func cgo_func_%[1]s_eface(self %[2]s) interface{} {\n", + s.sym.id, + s.sym.cgoname, + ) + g.Indent() + g.Printf("var v interface{} = ") + if s.sym.isBasic() { + g.Printf("%[1]s(self)\n", s.sym.gofmt()) + } else { + g.Printf("*(*%[1]s)(unsafe.Pointer(self))\n", s.sym.gofmt()) + } + g.Printf("return v\n") + g.Outdent() + g.Printf("}\n\n") + // support for __str__ g.Printf("//export cgo_func_%[1]s_str\n", s.ID()) g.Printf( @@ -537,6 +554,23 @@ func (g *goGen) genType(sym *symbol) { g.Outdent() g.Printf("}\n\n") + // empty interface converter + g.Printf("//export cgo_func_%[1]s_eface\n", sym.id) + g.Printf("func cgo_func_%[1]s_eface(self %[2]s) interface{} {\n", + sym.id, + sym.cgoname, + ) + g.Indent() + g.Printf("var v interface{} = ") + if sym.isBasic() { + g.Printf("%[1]s(self)\n", sym.gofmt()) + } else { + g.Printf("*(*%[1]s)(unsafe.Pointer(self))\n", sym.gofmt()) + } + g.Printf("return v\n") + g.Outdent() + g.Printf("}\n\n") + // support for __str__ g.Printf("//export cgo_func_%[1]s_str\n", sym.id) g.Printf(