Change Go types to simple unsafe Pointers instead of pointers

wrapped in structs. It could be possible to handle pointers to
pointers now.
This commit is contained in:
Greg 2019-04-30 09:14:25 -04:00
parent 0e89d2c29a
commit 3573aa9144
5 changed files with 30 additions and 50 deletions

View File

@ -1,4 +1,5 @@
package main
//go:generate nswrap
import (
"fmt"

View File

@ -1,7 +1,5 @@
InputFiles = [
"/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h",
# "/System/Library/Frameworks/Foundation.framework/Headers/NSDictionary.h",
# "/System/Library/Frameworks/Foundation.framework/Headers/NSString.h",
]
Classes = [
"NSArray",

View File

@ -1,21 +1,24 @@
package main
//go:generate nswrap
import (
"fmt"
"gitlab.wow.st/gmp/nswrap/examples/simple/ClassOne"
ns "gitlab.wow.st/gmp/nswrap/examples/simple/ClassOne"
)
func main() {
o := ClassOne.NewClassOne().Init()
o := ns.NewClassOne().Init()
fmt.Println("i1 = ",o.Geti1())
fmt.Println("p1 = ",o.Getp1())
p1 := o.Getp1()
fmt.Println("*p1 = ", *p1)
*p1 = 17
fmt.Println("*p1 = ", *o.Getp1())
ns := o.Nstru1()
ns1 := o.Nstru1()
np := o.Nstru2()
fmt.Println(o.Hi1(ns))
fmt.Println(o.Hi1(ns1))
fmt.Println(o.Hi2(np))
o2 := ns.NewClassTwo().Init()
fmt.Println(o2.Hi1(ns1))
}

View File

@ -67,11 +67,14 @@ func AddTypedef(n,t string) {
type Type struct {
Node *Node
Class string
//ctype string
ctype string
Variadic bool
}
func clean(n *Node,c string) (*Node,bool) {
if n == nil {
return nil,false
}
ret := NewNode(n.Kind,n.Content)
ret.Children = n.Children
//fmt.Printf("clean(%s,%s)\n",n.Ctype(),c)
@ -183,9 +186,9 @@ func (t *Type) CTypeAttrib() string {
func (t *Type) _CType(attrib bool) string {
//if !attrib && c.ctype != "" ... FIXME?
//if t.ctype != "" { // cache
// return t.ctype
//}
if t.ctype != "" { // cache
return t.ctype
}
var ct string
if attrib {
ignore := map[string]bool { "GenericList": true }
@ -193,22 +196,13 @@ func (t *Type) _CType(attrib bool) string {
} else {
ct = t.Node.CtypeSimplified()
}
/*
ct = strings.ReplaceAll(ct,"instancename",t.Class)
ct = strings.ReplaceAll(ct,"instancetype",t.Class + " *")
*/
if len(ct) > 1 && ct[:2] == "id" {
ct = "NSObject*" + ct[2:]
}
/*
if len(ct) > 11 {
if ct[:12] == "instancename" { ct = t.Class + ct[12:] }
if ct[:12] == "instancetype" { ct = t.Class + ct[12:] + " *" }
}*/
if attrib {
t._CType(false)
//} else {
//t.ctype = ct
} else {
t.ctype = ct
}
return ct
}
@ -262,7 +256,7 @@ type %s interface {
return fmt.Sprintf(`
//%s (%s)
type %s struct { %s }
func (o *%s) Ptr() unsafe.Pointer { return o.ptr }
func (o *%s) Ptr() unsafe.Pointer { return unsafe.Pointer(o) }
`,t.Node.Ctype(),t.BaseType().GoType(),gt,super,gt)
}
@ -293,19 +287,12 @@ func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string {
rt := rtype.CType()
if rt != "void" {
rtgt := rtype.GoType()
if shouldWrap(rtgt) || isGoInterface(rtgt) {
if isGoInterface(rtgt) {
rtgt = "Id"
} else {
rtgt = rtgt[1:]
}
ret.WriteString(" ret := &" + rtgt + "{}\n")
ret.WriteString(" ret.ptr = unsafe.Pointer(")
} else {
ret.WriteString(" return (" + rtype.GoType() + ")(")
if rtype.Node.IsPointer() {
ret.WriteString("unsafe.Pointer(")
}
if isGoInterface(rtgt) {
rtgt = "*Id"
}
ret.WriteString(" return (" + rtgt + ")(")
if rtype.Node.IsPointer() {
ret.WriteString("unsafe.Pointer(")
}
}
ret.WriteString("C." + name + "(")
@ -330,17 +317,11 @@ func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string {
ret.WriteString(strings.Join(parms,", "))
ret.WriteString(")")
if rt != "void" {
if shouldWrap(rtype.GoType()) || isGoInterface(rtype.GoType()) {
ret.WriteString(`)
return ret
`)
} else {
ret.WriteString(")")
if rtype.Node.IsPointer() {
ret.WriteString(")")
if rtype.Node.IsPointer() {
ret.WriteString(")")
}
ret.WriteString("\n")
}
ret.WriteString("\n")
}
return ret.String()
}

View File

@ -40,8 +40,8 @@ func NewWrapper(debug bool) *Wrapper {
#cgo LDFLAGS: -framework Foundation
`)
ret.goTypes.WriteString(`
type Id struct { ptr unsafe.Pointer }
func (o *Id) Ptr() unsafe.Pointer { return o.ptr }
type Id struct { }
func (o *Id) Ptr() unsafe.Pointer { return unsafe.Pointer(o) }
`)
return ret
}
@ -403,10 +403,7 @@ func (w *Wrapper) Wrap(toproc []string) {
w.goCode.WriteString(fmt.Sprintf(`
func New%s() *%s {
ret := &%s{}
ret.ptr = unsafe.Pointer(C.New%s())
//ret = ret.Init()
return ret
return (*%s)(unsafe.Pointer(C.New%s()))
}
`,i.Name,i.Name,i.Name,i.Name))