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 package main
//go:generate nswrap
import ( import (
"fmt" "fmt"

View File

@ -1,7 +1,5 @@
InputFiles = [ InputFiles = [
"/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h", "/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 = [ Classes = [
"NSArray", "NSArray",

View File

@ -1,21 +1,24 @@
package main package main
//go:generate nswrap
import ( import (
"fmt" "fmt"
"gitlab.wow.st/gmp/nswrap/examples/simple/ClassOne" ns "gitlab.wow.st/gmp/nswrap/examples/simple/ClassOne"
) )
func main() { func main() {
o := ClassOne.NewClassOne().Init() o := ns.NewClassOne().Init()
fmt.Println("i1 = ",o.Geti1()) fmt.Println("i1 = ",o.Geti1())
fmt.Println("p1 = ",o.Getp1()) fmt.Println("p1 = ",o.Getp1())
p1 := o.Getp1() p1 := o.Getp1()
fmt.Println("*p1 = ", *p1) fmt.Println("*p1 = ", *p1)
*p1 = 17 *p1 = 17
fmt.Println("*p1 = ", *o.Getp1()) fmt.Println("*p1 = ", *o.Getp1())
ns := o.Nstru1() ns1 := o.Nstru1()
np := o.Nstru2() np := o.Nstru2()
fmt.Println(o.Hi1(ns)) fmt.Println(o.Hi1(ns1))
fmt.Println(o.Hi2(np)) 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 { type Type struct {
Node *Node Node *Node
Class string Class string
//ctype string ctype string
Variadic bool Variadic bool
} }
func clean(n *Node,c string) (*Node,bool) { func clean(n *Node,c string) (*Node,bool) {
if n == nil {
return nil,false
}
ret := NewNode(n.Kind,n.Content) ret := NewNode(n.Kind,n.Content)
ret.Children = n.Children ret.Children = n.Children
//fmt.Printf("clean(%s,%s)\n",n.Ctype(),c) //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 { func (t *Type) _CType(attrib bool) string {
//if !attrib && c.ctype != "" ... FIXME? //if !attrib && c.ctype != "" ... FIXME?
//if t.ctype != "" { // cache if t.ctype != "" { // cache
// return t.ctype return t.ctype
//} }
var ct string var ct string
if attrib { if attrib {
ignore := map[string]bool { "GenericList": true } ignore := map[string]bool { "GenericList": true }
@ -193,22 +196,13 @@ func (t *Type) _CType(attrib bool) string {
} else { } else {
ct = t.Node.CtypeSimplified() 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" { if len(ct) > 1 && ct[:2] == "id" {
ct = "NSObject*" + ct[2:] 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 { if attrib {
t._CType(false) t._CType(false)
//} else { } else {
//t.ctype = ct t.ctype = ct
} }
return ct return ct
} }
@ -262,7 +256,7 @@ type %s interface {
return fmt.Sprintf(` return fmt.Sprintf(`
//%s (%s) //%s (%s)
type %s struct { %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) `,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() rt := rtype.CType()
if rt != "void" { if rt != "void" {
rtgt := rtype.GoType() rtgt := rtype.GoType()
if shouldWrap(rtgt) || isGoInterface(rtgt) { if isGoInterface(rtgt) {
if isGoInterface(rtgt) { rtgt = "*Id"
rtgt = "Id" }
} else { ret.WriteString(" return (" + rtgt + ")(")
rtgt = rtgt[1:] if rtype.Node.IsPointer() {
} ret.WriteString("unsafe.Pointer(")
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(")
}
} }
} }
ret.WriteString("C." + name + "(") 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(strings.Join(parms,", "))
ret.WriteString(")") ret.WriteString(")")
if rt != "void" { if rt != "void" {
if shouldWrap(rtype.GoType()) || isGoInterface(rtype.GoType()) { ret.WriteString(")")
ret.WriteString(`) if rtype.Node.IsPointer() {
return ret
`)
} else {
ret.WriteString(")") ret.WriteString(")")
if rtype.Node.IsPointer() {
ret.WriteString(")")
}
ret.WriteString("\n")
} }
ret.WriteString("\n")
} }
return ret.String() return ret.String()
} }

View File

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