diff --git a/examples/foundation/main.go b/examples/foundation/main.go index e847263..a61638f 100644 --- a/examples/foundation/main.go +++ b/examples/foundation/main.go @@ -9,10 +9,13 @@ import ( func main() { fmt.Println("Started") - n := ns.StringWithUTF8String(ns.CharFromString("hi there")) - c := n.CapitalizedString() + n1 := ns.StringWithUTF8String(ns.CharFromString("hi there")) + c := n1.CapitalizedString() gstring := c.UTF8String().String() fmt.Println(gstring) + n2 := ns.StringWithUTF8String(ns.CharFromString("ok bye")) + n3 := ns.StringWithUTF8String(ns.CharFromString("one two three")) + a := ns.ArrayWithObjects(n1) fmt.Println("ok") } diff --git a/examples/foundation/nswrap.toml b/examples/foundation/nswrap.toml index 0aaf73d..e6b9190 100644 --- a/examples/foundation/nswrap.toml +++ b/examples/foundation/nswrap.toml @@ -1,10 +1,20 @@ InputFiles = [ - "/System/Library/Frameworks/Foundation.framework/Headers/NSDictionary.h", - "/System/Library/Frameworks/Foundation.framework/Headers/NSString.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 = [ - "NSString", + "NSArray", "NSDictionary", + "NSSet", + "NSDate", + "NSTimeZone", + "NSCalendar", + "NSLocale", + "NSCharacterSet", + "NSString", + "NSScanner", + "NSFileManager", ] SysImports = [ "Foundation/Foundation.h" ] Pragma = [ 'clang diagnostic ignored "-Wformat-security"' ] diff --git a/types/convert.go b/types/convert.go index e91a82b..a623689 100644 --- a/types/convert.go +++ b/types/convert.go @@ -92,12 +92,16 @@ func (t *Type) String() string { return t.Node.String() } +func (t *Type) PointsTo() *Type { + return NewType(t.Node.PointsTo(), t.Class) +} + func Wrap(s string) { if wrapped == nil { wrapped = make(map[string]bool) } // it is the pointers to this type that get wrapped - wrapped["*" + s] = true + wrapped[s] = true } func (t *Type) BaseType() *Type { @@ -105,9 +109,9 @@ func (t *Type) BaseType() *Type { t.Node.BaseType(), t.Class, ) - if ret.CType() == ret.Class + " *" { // "instancename" - ret.ctype = ret.Class - } +// if ret.CType() == ret.Class + " *" { // "instancename" +// ret.ctype = ret.Class +// } return ret } @@ -203,15 +207,14 @@ func (t *Type) GoInterfaceDecl() string { if gt[0] == '*' { gt = gt[1:] // dereference wrapped types } - x := "" super := Super(gt) if super == "" { super = "ptr unsafe.Pointer" } return fmt.Sprintf(` -//%s -%stype %s struct { %s } -`,t.CTypeAttrib(),x,gt,super) +//%s (%s) +type %s struct { %s } +`,t.Node.Ctype(),t.BaseType().GoType(),gt,super) } func (t *Type) IsFunction() bool { @@ -239,8 +242,11 @@ func (t *Type) CToGo(cval string) string { // cast C value to CGo func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string { var ret strings.Builder rt := rtype.CType() + wrap := func(gt string) bool { + return gt != "" && gt[0] == '*' && wrapped[gt[1:]] + } if rt != "void" { - if wrapped[rtype.GoType()] { + if wrap(rtype.GoType()) { ret.WriteString(" ret := &" + rtype.GoType()[1:] + "{}\n") ret.WriteString(" ret.ptr = unsafe.Pointer(") } else { @@ -255,7 +261,7 @@ func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string { for i := 0; i < len(pnames); i++ { pn,pt := pnames[i],ptypes[i] p := pn - if wrapped[pt.GoType()] { + if wrap(pt.GoType()) { p = pn + ".ptr" } else { if pt.Node.IsPointer() { @@ -269,7 +275,7 @@ func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string { ret.WriteString(strings.Join(parms,", ")) ret.WriteString(")") if rt != "void" { - if wrapped[rtype.GoType()] { + if wrap(rtype.GoType()) { ret.WriteString(`) return ret `) diff --git a/wrap/main.go b/wrap/main.go index 074c874..1765653 100644 --- a/wrap/main.go +++ b/wrap/main.go @@ -299,7 +299,6 @@ func (w *Wrapper) GetParms(n *ast.ObjCMethodDecl,class string) ([]Parameter,bool return ret, true } -// new version of ProcessType func (w *Wrapper) processTypes(tps []*types.Type) { switch len(tps) { case 0: @@ -315,21 +314,27 @@ func (w *Wrapper) processTypes(tps []*types.Type) { } func (w *Wrapper) processType(tp *types.Type) { - bt := tp.BaseType() - if w.Processed[bt.GoType()] { return } - w.Processed[bt.GoType()] = true - if bt.IsFunction() { + gt := tp.GoType() + if gt == "" { + return + } + if gt[0] == '*' { + w.processType(tp.PointsTo()) + return + } + if w.Processed[gt] { return } + w.Processed[gt] = true + if gt == "Char" { + w.CharHelpers() + } + if tp.IsFunction() { return } w.goTypes.WriteString(tp.GoTypeDecl()) - if tp.GoType() == "*Char" { - w.CharHelpers() - } - super := types.Super(bt.GoType()) + super := types.Super(gt) if super != "" { types.Wrap(super) - pt := types.NewTypeFromString(super + "*","") - w.processType(pt) + w.processType(types.NewTypeFromString(super,"")) } }