Bug fix and start improvements to subclassing.

This commit is contained in:
Greg 2019-05-24 00:12:46 -04:00
parent a616761475
commit 38c7856b89
5 changed files with 41 additions and 8 deletions

View File

@ -216,8 +216,8 @@ func Start() (err error) {
case *ast.ObjCInterfaceDecl:
w.AddInterface(x)
for _,ss := range Config.Subclasses {
for ps,_ := range ss {
if matches(x.Name,[]string{ps}) {
if sc,ok := ss["superclass"]; ok {
if matches(x.Name,sc) {
Config.Classes = append(Config.Classes,x.Name)
}
}

View File

@ -6,6 +6,11 @@ import (
ns "gitlab.wow.st/gmp/nswrap/examples/simple/ClassOne"
)
func cb(super ns.ClassThreeSupermethods) ns.Int {
fmt.Printf("In Go callback\n")
return 0
}
func main() {
o := ns.ClassOneAlloc().Init()
fmt.Println("i1 = ",o.Geti1())
@ -20,7 +25,10 @@ func main() {
fmt.Println(o.Hi2(np))
o2 := ns.ClassTwoAlloc().Init()
fmt.Println(o2.Hi1(ns1))
o3 := ns.ClassThreeAlloc().Init()
o3 := ns.ClassThreeAlloc()
o3.Init()
o3.Geti1Callback(cb)
fmt.Println(o3.Hi2(np))
fmt.Println(o3.Geti1())
}

View File

@ -6,6 +6,8 @@ classes:
subclasses:
ClassThree:
ClassTwo:
- .*
- geti1
- (void)myMethod:int
imports: [ simple.h ]
frameworks: [ Foundation ]

View File

@ -24,6 +24,7 @@ func IsGoInterface(gt string) bool {
return goInterfaces[gt]
}
//TypeParameters maps, for each class, a TypedefName to a type, representing
//the Objective-C type parameters for that class
var TypeParameters map[string]map[string]string
@ -194,6 +195,9 @@ func _goType(ct string) string {
if ct == "Id" {
ct = "Id"
}
if len(ct) > 1 && ShouldWrap(ct[1:]) {
return ct[1:]
}
if len(ct) > 4 && ct[len(ct)-4:len(ct)] == "Void" {
ct = ct[:len(ct)-5] + "unsafe.Pointer"
}

View File

@ -981,14 +981,30 @@ func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string,sub bool)
i++
var ms map[string]*Method
if sub {
if i > 1 {
fmt.Printf("Multiple inheritance is not permitted:\n subclass %s already inherits from %s\n",dname,supr)
os.Exit(-1)
}
interf := w.Interfaces[pname]
if interf == nil {
fmt.Printf("Failed to find interface %s for subclass %s\n",pname,dname)
os.Exit(-1)
}
//fmt.Printf(" subclass for %s\n",pname)
ms = interf.InstanceMethods
supr = interf.GoName
//fmt.Printf(" subclass for %s\n",pname)
ms = map[string]*Method{}
var addmeths func(s string)
addmeths = func(s string) {
if sup := types.Super(s); w.Interfaces[sup] != nil {
addmeths(sup)
}
fmt.Printf("Adding methods for %s\n",s)
for k,v := range w.Interfaces[s].InstanceMethods {
ms[k] = v
}
}
//for subclasses, add all superclass methods, depth first
addmeths(interf.Name)
} else {
proto := w.Protocols[pname]
if proto == nil {
@ -1003,6 +1019,9 @@ func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string,sub bool)
//note:we may have capitalized the first character to make a goname,
//but m.Name is not disambiguated for polymorphics...
if !matches(string(m.Name[0])+gname[1:],pats) {
if sub {
//Add newly defined methods that don't match anything...
}
continue
}
if m.Type.IsFunction() || m.Type.IsFunctionPtr() || m.hasFunctionParam() {
@ -1299,7 +1318,7 @@ func (d *%s) %sCallback(f func(%s)%s) {
sper := ""
if sub && len(gnames) > 0 {
sper = fmt.Sprintf(
`self := (*%s)(o)
` self := (*%s)(o)
super := %sSupermethods{
%s,
}
@ -1486,7 +1505,7 @@ import (
of.WriteString(w.goCode.String())
of.Close()
if len(w.Delegates) > 0 {
if len(w.Delegates) > 0 || len(w.Subclasses) > 0 {
ef.WriteString("package " + w.Package + "\n\n")
ef.WriteString(w.cgoFlags.String() + "\n")
ef.WriteString(w.cImports.String() + "\n")