Convert *Id to any wrapped type. Shorten names for class methods

that return an instance of the class. Fix return types for Go
interfaces.
This commit is contained in:
Greg 2019-05-01 12:32:42 -04:00
parent 57ad898945
commit 11366be6d0
3 changed files with 33 additions and 12 deletions

View File

@ -8,13 +8,13 @@ import (
) )
func main() { func main() {
n1 := ns.NSStringStringWithUTF8String(ns.CharFromString("hi there")) n1 := ns.NSStringWithUTF8String(ns.CharFromString("hi there"))
c1 := n1.CapitalizedString() c1 := n1.CapitalizedString()
gs := c1.UTF8String().String() gs := c1.UTF8String().String()
fmt.Println(gs) fmt.Println(gs)
n2 := ns.NSStringStringWithUTF8String(ns.CharFromString("hi world")) n2 := ns.NSStringWithUTF8String(ns.CharFromString("hi world"))
n3 := ns.NSStringStringWithUTF8String(ns.CharFromString("ok bye")) n3 := ns.NSStringWithUTF8String(ns.CharFromString("ok bye"))
a := ns.NSMutableArrayArrayWithObjects(n1,n2,n3) a := ns.NSMutableArrayWithObjects(n1,n2,n3)
fmt.Println("Length(a) = ",a.Count()) fmt.Println("Length(a) = ",a.Count())
fmt.Println("is n2 in a?",a.ContainsObject(n2)) fmt.Println("is n2 in a?",a.ContainsObject(n2))
fmt.Println("is c1 in a?",a.ContainsObject(c1)) fmt.Println("is c1 in a?",a.ContainsObject(c1))
@ -23,4 +23,8 @@ func main() {
a.AddObject(n4) a.AddObject(n4)
a.AddObject(n5) a.AddObject(n5)
fmt.Println("Length(a) = ",a.Count()) fmt.Println("Length(a) = ",a.Count())
a2 := a.SubarrayWithRange(ns.NSMakeRange(1,3))
fmt.Println("Length(a2) = ",a2.Count())
i1 := a.ObjectAtIndex(1).NSString()
fmt.Println(i1.UTF8String())
} }

View File

@ -21,7 +21,7 @@ func shouldWrap(gt string) bool {
//are dereferenced to bare interface names. //are dereferenced to bare interface names.
var goInterfaces map[string]bool var goInterfaces map[string]bool
func isGoInterface(gt string) bool { func IsGoInterface(gt string) bool {
return goInterfaces[gt] return goInterfaces[gt]
} }
@ -198,7 +198,7 @@ func _goType(ct string) string {
ct = strings.Title(ct) ct = strings.Title(ct)
ct = strings.ReplaceAll(ct," ","") ct = strings.ReplaceAll(ct," ","")
ct = strings.ReplaceAll(ct,"Struct","") ct = strings.ReplaceAll(ct,"Struct","")
if len(ct) > 0 && ct[0] == '*' && isGoInterface(ct[1:]) { if len(ct) > 0 && ct[0] == '*' && IsGoInterface(ct[1:]) {
return ct[1:] return ct[1:]
} }
if ct == "Id" { if ct == "Id" {
@ -285,14 +285,15 @@ type %s interface {
} }
`,t.Node.Ctype(),t.BaseType().GoType(),gt) `,t.Node.Ctype(),t.BaseType().GoType(),gt)
} }
if isGoInterface(super) { if IsGoInterface(super) {
super = "Id" super = "Id"
} }
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 unsafe.Pointer(o) } func (o *%s) Ptr() unsafe.Pointer { return unsafe.Pointer(o) }
`,t.Node.Ctype(),t.BaseType().GoType(),gt,super,gt) func (o *Id) %s() *%s { return (*%s)(unsafe.Pointer(o)) }
`,t.Node.Ctype(),t.BaseType().GoType(),gt,super,gt,gt,gt,gt)
} }
func (t *Type) IsFunction() bool { func (t *Type) IsFunction() bool {
@ -337,7 +338,7 @@ 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 isGoInterface(rtgt) { if IsGoInterface(rtgt) {
rtgt = "*Id" rtgt = "*Id"
} }
ret.WriteString("return (" + rtgt + ")(") ret.WriteString("return (" + rtgt + ")(")
@ -350,7 +351,7 @@ func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string {
for i := 0; i < len(pnames); i++ { for i := 0; i < len(pnames); i++ {
pn,pt := pnames[i],ptypes[i] pn,pt := pnames[i],ptypes[i]
p := pn p := pn
if (shouldWrap(pt.GoType()) || isGoInterface(pt.GoType())) && !pt.Variadic { if (shouldWrap(pt.GoType()) || IsGoInterface(pt.GoType())) && !pt.Variadic {
p = pn + ".Ptr()" p = pn + ".Ptr()"
} else { } else {
switch { switch {

View File

@ -479,10 +479,23 @@ func (w *Wrapper) _processMethod(m *Method,fun bool) {
return return
} }
gname := strings.Title(m.Name) gname := strings.Title(m.Name)
if !m.ClassMethod { switch {
case !m.ClassMethod:
gname = "(o *" + m.Class + ") " + gname gname = "(o *" + m.Class + ") " + gname
} else { case m.Type.GoType() != "*" + m.Class:
gname = m.Class + gname gname = m.Class + gname
default:
lens1 := len(m.Class)
i := 0
if len(gname) < len(m.Class) { i = lens1 - len(gname) }
for ; i < lens1; i++ {
if m.Class[i:] == gname[:lens1 - i] { break }
}
if lens1 - i >= len(gname) {
gname = m.Class + gname
} else {
gname = m.Class + gname[lens1-i:]
}
} }
cname := m.Name cname := m.Name
if m.Class != "" { if m.Class != "" {
@ -495,6 +508,9 @@ func (w *Wrapper) _processMethod(m *Method,fun bool) {
if grtype == "Void" { if grtype == "Void" {
grtype = "" grtype = ""
} }
if types.IsGoInterface(grtype) {
grtype = "*Id"
}
w.goCode.WriteString(fmt.Sprintf(` w.goCode.WriteString(fmt.Sprintf(`
//%s //%s
func %s(%s) %s { func %s(%s) %s {