mirror of https://github.com/go-python/gopy.git
bind: first stab at a gopy object model
This commit is contained in:
parent
c8ce550bd3
commit
c1d3b45735
|
@ -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)++
|
||||
}
|
||||
|
|
|
@ -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) \
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue