2
0
mirror of synced 2025-02-23 14:58:12 +00:00

bind,cmd: accept ObjC wrapper types in bound packages

Accept ObjC API wrapper types as arguments and return values from
bound Go package functions and methods. Also, allow Go structs
to extend ObjC classes and implement ObjC protocols as well as override
and implement methods.

This is the third and final part of the implementation of the golang/go#17102
proposal.

Fixes golang/go#17102

Change-Id: I601d90fb6d22b8d6f8b7d5fe0130daa1a4dd4734
Reviewed-on: https://go-review.googlesource.com/29175
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Elias Naur 2016-09-13 06:11:15 +02:00
parent 63993740dd
commit 1c49d29d1c
31 changed files with 955 additions and 132 deletions

View File

@ -50,6 +50,7 @@ var javaTests = []string{
var objcTests = []string{ var objcTests = []string{
"testdata/objc.go", "testdata/objc.go",
"testdata/objcw.go",
} }
var fset = token.NewFileSet() var fset = token.NewFileSet()
@ -136,7 +137,7 @@ func TestGenObjc(t *testing.T) {
Pkg: pkg, Pkg: pkg,
}, },
} }
g.Init() g.Init(nil)
testcases := []struct { testcases := []struct {
suffix string suffix string
@ -463,7 +464,7 @@ func TestCustomPrefix(t *testing.T) {
Pkg: pkg, Pkg: pkg,
}, },
} }
og.Init() og.Init(nil)
testCases := []struct { testCases := []struct {
golden string golden string
gen func(w io.Writer) error gen func(w io.Writer) error

View File

@ -333,7 +333,7 @@ func (g *Generator) isSigSupported(t types.Type) bool {
// isSupported returns whether the generators can handle the type. // isSupported returns whether the generators can handle the type.
func (g *Generator) isSupported(t types.Type) bool { func (g *Generator) isSupported(t types.Type) bool {
if isErrorType(t) || isJavaType(t) { if isErrorType(t) || isWrapperType(t) {
return true return true
} }
switch t := t.(type) { switch t := t.(type) {

View File

@ -410,10 +410,11 @@ func (g *goGen) genRead(toVar, fromVar string, typ types.Type, mode varMode) {
if iface, ok := t.Underlying().(*types.Interface); ok { if iface, ok := t.Underlying().(*types.Interface); ok {
hasProxy = makeIfaceSummary(iface).implementable hasProxy = makeIfaceSummary(iface).implementable
} }
isJava := isJavaType(t) pkgFirst := typePkgFirstElem(t)
isWrapper := pkgFirst == "Java" || pkgFirst == "ObjC"
o := t.Obj() o := t.Obj()
oPkg := o.Pkg() oPkg := o.Pkg()
if !isErrorType(t) && !g.validPkg(oPkg) && !isJava { if !isErrorType(t) && !g.validPkg(oPkg) && !isWrapper {
g.errorf("type %s is defined in %s, which is not bound", t, oPkg) g.errorf("type %s is defined in %s, which is not bound", t, oPkg)
return return
} }
@ -424,8 +425,14 @@ func (g *goGen) genRead(toVar, fromVar string, typ types.Type, mode varMode) {
g.Printf(" %s = %s_ref.Get().(%s%s)\n", toVar, toVar, g.pkgName(oPkg), o.Name()) g.Printf(" %s = %s_ref.Get().(%s%s)\n", toVar, toVar, g.pkgName(oPkg), o.Name())
if hasProxy { if hasProxy {
g.Printf(" } else { // foreign object \n") g.Printf(" } else { // foreign object \n")
if isJava { if isWrapper {
clsName := flattenName(classNameFor(t)) var clsName string
switch pkgFirst {
case "Java":
clsName = flattenName(classNameFor(t))
case "ObjC":
clsName = t.Obj().Name()
}
g.Printf(" %s = (*proxy_class_%s)(%s_ref)\n", toVar, clsName, toVar) g.Printf(" %s = (*proxy_class_%s)(%s_ref)\n", toVar, clsName, toVar)
} else { } else {
g.Printf(" %s = (*proxy%s_%s)(%s_ref)\n", toVar, pkgPrefix(oPkg), o.Name(), toVar) g.Printf(" %s = (*proxy%s_%s)(%s_ref)\n", toVar, pkgPrefix(oPkg), o.Name(), toVar)
@ -451,7 +458,7 @@ func (g *goGen) typeString(typ types.Type) string {
return types.TypeString(typ, types.RelativeTo(pkg)) return types.TypeString(typ, types.RelativeTo(pkg))
} }
oPkg := obj.Pkg() oPkg := obj.Pkg()
if !g.validPkg(oPkg) && !isJavaType(t) { if !g.validPkg(oPkg) && !isWrapperType(t) {
g.errorf("type %s is defined in %s, which is not bound", t, oPkg) g.errorf("type %s is defined in %s, which is not bound", t, oPkg)
return "TODO" return "TODO"
} }

View File

@ -1490,6 +1490,10 @@ func classNameFor(t types.Type) string {
return strings.Replace(pkg.Path()[len("Java/"):], "/", ".", -1) + "." + obj.Name() return strings.Replace(pkg.Path()[len("Java/"):], "/", ".", -1) + "." + obj.Name()
} }
func isJavaType(t types.Type) bool {
return typePkgFirstElem(t) == "Java"
}
const ( const (
javaPreamble = `// Java class %[1]s.%[2]s is a proxy for talking to a Go program. javaPreamble = `// Java class %[1]s.%[2]s is a proxy for talking to a Go program.
// gobind %[3]s %[4]s // gobind %[3]s %[4]s

View File

@ -10,6 +10,8 @@ import (
"go/types" "go/types"
"math" "math"
"strings" "strings"
"golang.org/x/mobile/internal/importers/objc"
) )
// TODO(hyangah): handle method name conflicts. // TODO(hyangah): handle method name conflicts.
@ -22,15 +24,64 @@ import (
type ObjcGen struct { type ObjcGen struct {
Prefix string // prefix arg passed by flag. Prefix string // prefix arg passed by flag.
*Generator
// fields set by init. // fields set by init.
namePrefix string namePrefix string
// Map of all wrapped Objc types
*Generator wrapMap map[string]*objc.Named
// Structs that embeds Objc wrapper types.
ostructs map[*types.TypeName]*objcClassInfo
modules []string
} }
func (g *ObjcGen) Init() { type objcClassInfo struct {
// The Objc class this class extends.
extends *objc.Named
// All classes and protocols this class extends and conforms to.
supers []*objc.Named
methods map[string]*objc.Func
}
func (g *ObjcGen) Init(wrappers []*objc.Named) {
g.Generator.Init() g.Generator.Init()
g.namePrefix = g.namePrefixOf(g.Pkg) g.namePrefix = g.namePrefixOf(g.Pkg)
g.wrapMap = make(map[string]*objc.Named)
modMap := make(map[string]struct{})
for _, w := range wrappers {
g.wrapMap[w.GoName] = w
if _, exists := modMap[w.Module]; !exists {
g.modules = append(g.modules, w.Module)
modMap[w.Module] = struct{}{}
}
}
if _, exists := modMap["Foundation"]; !exists {
g.modules = append(g.modules, "Foundation")
}
g.ostructs = make(map[*types.TypeName]*objcClassInfo)
for _, s := range g.structs {
embds := embeddedObjcTypes(s.t)
if len(embds) == 0 {
continue
}
inf := &objcClassInfo{
methods: make(map[string]*objc.Func),
}
for _, n := range embds {
t := g.wrapMap[n]
for _, f := range t.AllMethods {
inf.methods[f.GoName] = f
}
inf.supers = append(inf.supers, t)
if !t.Protocol {
if inf.extends != nil {
g.errorf("%s embeds more than one ObjC class; only one is allowed.", s.obj)
}
inf.extends = t
}
}
g.ostructs[s.obj] = inf
}
} }
func (g *ObjcGen) namePrefixOf(pkg *types.Package) string { func (g *ObjcGen) namePrefixOf(pkg *types.Package) string {
@ -86,8 +137,11 @@ func (g *ObjcGen) GenH() error {
g.Printf("#ifndef __%s_H__\n", g.namePrefix) g.Printf("#ifndef __%s_H__\n", g.namePrefix)
g.Printf("#define __%s_H__\n", g.namePrefix) g.Printf("#define __%s_H__\n", g.namePrefix)
g.Printf("\n") g.Printf("\n")
g.Printf("#include <Foundation/Foundation.h>\n") for _, m := range g.modules {
g.Printf("#include \"GoUniverse.h\"\n") g.Printf("@import %s;\n", m)
}
g.Printf("#include \"GoUniverse.h\"\n\n")
if g.Pkg != nil { if g.Pkg != nil {
for _, pkg := range g.Pkg.Imports() { for _, pkg := range g.Pkg.Imports() {
if g.validPkg(pkg) { if g.validPkg(pkg) {
@ -347,9 +401,11 @@ func (g *ObjcGen) genConstM(o *types.Const) {
type funcSummary struct { type funcSummary struct {
name string name string
goname string
ret string ret string
sig *types.Signature sig *types.Signature
params, retParams []paramInfo params, retParams []paramInfo
hasthis bool
} }
type paramInfo struct { type paramInfo struct {
@ -357,20 +413,63 @@ type paramInfo struct {
name string name string
} }
func (g *ObjcGen) funcSummary(obj *types.Func) *funcSummary { func (g *ObjcGen) funcSummary(oinf *objcClassInfo, obj *types.Func) *funcSummary {
sig := obj.Type().(*types.Signature) sig := obj.Type().(*types.Signature)
s := &funcSummary{name: obj.Name(), sig: sig} s := &funcSummary{goname: obj.Name(), sig: sig}
var om *objc.Func
var sigElems []string
if oinf != nil {
om = oinf.methods[obj.Name()]
}
if om != nil {
sigElems = strings.Split(om.Sig, ":")
s.name = sigElems[0]
} else {
s.name = obj.Name()
}
params := sig.Params() params := sig.Params()
for i := 0; i < params.Len(); i++ { first := 0
if om != nil {
// Check the implicit this argument and the argument count of the overridden method
excess := params.Len() - len(om.Params)
if excess < 0 {
g.errorf("method %s has fewer arguments than the method it overrides", obj.Name())
} else if excess > 1 {
g.errorf("overriding method %s has more arguments than the method it overrides", obj.Name())
} else if excess == 1 {
s.hasthis = true
first = 1
t := params.At(0).Type()
if !isObjcType(t) {
g.errorf("the `this` argument to method %s is not a ObjC type", obj.Name())
return s
} else {
ot := g.wrapMap[t.(*types.Named).Obj().Name()]
found := false
for _, sup := range oinf.supers {
if ot == sup {
found = true
break
}
}
if !found {
g.errorf("the type %s of the `this` argument to method %s is not a super class of the enclosing struct", ot.Name, obj.Name())
}
}
}
}
for i := first; i < params.Len(); i++ {
p := params.At(i) p := params.At(i)
v := paramInfo{ v := paramInfo{
typ: p.Type(), typ: p.Type(),
name: g.paramName(params, i), }
if om != nil {
v.name = sigElems[i-first]
} else {
v.name = g.paramName(params, i)
} }
s.params = append(s.params, v) s.params = append(s.params, v)
} }
res := sig.Results() res := sig.Results()
switch res.Len() { switch res.Len() {
case 0: case 0:
@ -488,13 +587,13 @@ func (g *ObjcGen) genFuncH(obj *types.Func) {
g.Printf("// skipped function %s with unsupported parameter or return types\n\n", obj.Name()) g.Printf("// skipped function %s with unsupported parameter or return types\n\n", obj.Name())
return return
} }
if s := g.funcSummary(obj); s != nil { if s := g.funcSummary(nil, obj); s != nil {
g.Printf("FOUNDATION_EXPORT %s;\n", s.asFunc(g)) g.Printf("FOUNDATION_EXPORT %s;\n", s.asFunc(g))
} }
} }
func (g *ObjcGen) genFuncM(obj *types.Func) { func (g *ObjcGen) genFuncM(obj *types.Func) {
s := g.funcSummary(obj) s := g.funcSummary(nil, obj)
if s == nil { if s == nil {
return return
} }
@ -555,20 +654,20 @@ func (g *ObjcGen) genWrite(varName string, t types.Type, mode varMode) {
case *types.Named: case *types.Named:
switch u := t.Underlying().(type) { switch u := t.Underlying().(type) {
case *types.Interface: case *types.Interface:
g.genRefWrite(varName, t) g.genRefWrite(varName)
default: default:
g.errorf("unsupported named type: %s / %T", u, u) g.errorf("unsupported named type: %s / %T", u, u)
} }
case *types.Pointer: case *types.Pointer:
g.genRefWrite(varName, t) g.genRefWrite(varName)
default: default:
g.Printf("%s _%s = (%s)%s;\n", g.cgoType(t), varName, g.cgoType(t), varName) g.Printf("%s _%s = (%s)%s;\n", g.cgoType(t), varName, g.cgoType(t), varName)
} }
} }
func (g *ObjcGen) genRefWrite(varName string, t types.Type) { func (g *ObjcGen) genRefWrite(varName string) {
g.Printf("int32_t _%s;\n", varName) g.Printf("int32_t _%s;\n", varName)
g.Printf("if ([(id<NSObject>)(%s) isKindOfClass:[%s class]]) {\n", varName, g.refTypeBase(t)) g.Printf("if ([%s conformsToProtocol:@protocol(goSeqRefInterface)]) {\n", varName)
g.Indent() g.Indent()
g.Printf("id<goSeqRefInterface> %[1]s_proxy = (id<goSeqRefInterface>)(%[1]s);\n", varName) g.Printf("id<goSeqRefInterface> %[1]s_proxy = (id<goSeqRefInterface>)(%[1]s);\n", varName)
g.Printf("_%s = go_seq_go_to_refnum(%s_proxy._ref);\n", varName, varName) g.Printf("_%s = go_seq_go_to_refnum(%s_proxy._ref);\n", varName, varName)
@ -587,7 +686,11 @@ func (g *ObjcGen) genRefRead(toName, fromName string, t types.Type) {
g.Printf("if (%s_ref != NULL) {\n", toName) g.Printf("if (%s_ref != NULL) {\n", toName)
g.Printf(" %s = %s_ref.obj;\n", toName, toName) g.Printf(" %s = %s_ref.obj;\n", toName, toName)
g.Printf(" if (%s == nil) {\n", toName) g.Printf(" if (%s == nil) {\n", toName)
g.Printf(" %s = [[%s alloc] initWithRef:%s_ref];\n", toName, ptype, toName) if isObjcType(t) {
g.Printf(" LOG_FATAL(@\"unexpected NULL reference\");\n")
} else {
g.Printf(" %s = [[%s alloc] initWithRef:%s_ref];\n", toName, ptype, toName)
}
g.Printf(" }\n") g.Printf(" }\n")
g.Printf("}\n") g.Printf("}\n")
} }
@ -637,6 +740,9 @@ func (g *ObjcGen) genRead(toName, fromName string, t types.Type, mode varMode) {
func (g *ObjcGen) genFunc(s *funcSummary, objName string) { func (g *ObjcGen) genFunc(s *funcSummary, objName string) {
if objName != "" { if objName != "" {
g.Printf("int32_t refnum = go_seq_go_to_refnum(self._ref);\n") g.Printf("int32_t refnum = go_seq_go_to_refnum(self._ref);\n")
if s.hasthis {
g.genRefWrite("self")
}
} }
for _, p := range s.params { for _, p := range s.params {
g.genWrite(p.name, p.typ, modeTransient) g.genWrite(p.name, p.typ, modeTransient)
@ -647,12 +753,15 @@ func (g *ObjcGen) genFunc(s *funcSummary, objName string) {
g.Printf("%s r0 = ", g.cgoType(s.retParams[0].typ)) g.Printf("%s r0 = ", g.cgoType(s.retParams[0].typ))
} else { } else {
resPrefix = "res." resPrefix = "res."
g.Printf("struct proxy%s_%s_%s_return res = ", g.pkgPrefix, objName, s.name) g.Printf("struct proxy%s_%s_%s_return res = ", g.pkgPrefix, objName, s.goname)
} }
} }
g.Printf("proxy%s_%s_%s(", g.pkgPrefix, objName, s.name) g.Printf("proxy%s_%s_%s(", g.pkgPrefix, objName, s.goname)
if objName != "" { if objName != "" {
g.Printf("refnum") g.Printf("refnum")
if s.hasthis {
g.Printf(", _self")
}
} }
for i, p := range s.params { for i, p := range s.params {
if i > 0 || objName != "" { if i > 0 || objName != "" {
@ -700,9 +809,11 @@ func (g *ObjcGen) genInterfaceInterface(obj *types.TypeName, summary ifaceSummar
} else { } else {
g.Printf("NSObject") g.Printf("NSObject")
} }
prots := []string{"goSeqRefInterface"}
if isProtocol { if isProtocol {
g.Printf(" <%[1]s%[2]s>", g.namePrefix, obj.Name()) prots = append(prots, fmt.Sprintf("%[1]s%[2]s", g.namePrefix, obj.Name()))
} }
g.Printf(" <%s>", strings.Join(prots, ", "))
g.Printf(" {\n}\n") g.Printf(" {\n}\n")
g.Printf("@property(strong, readonly) id _ref;\n") g.Printf("@property(strong, readonly) id _ref;\n")
g.Printf("\n") g.Printf("\n")
@ -712,7 +823,7 @@ func (g *ObjcGen) genInterfaceInterface(obj *types.TypeName, summary ifaceSummar
g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name())
continue continue
} }
s := g.funcSummary(m) s := g.funcSummary(nil, m)
g.Printf("- %s;\n", s.asMethod(g)) g.Printf("- %s;\n", s.asMethod(g))
} }
g.Printf("@end\n") g.Printf("@end\n")
@ -724,13 +835,13 @@ func (g *ObjcGen) genInterfaceH(obj *types.TypeName, t *types.Interface) {
g.genInterfaceInterface(obj, summary, false) g.genInterfaceInterface(obj, summary, false)
return return
} }
g.Printf("@protocol %s%s\n", g.namePrefix, obj.Name()) g.Printf("@protocol %s%s <NSObject>\n", g.namePrefix, obj.Name())
for _, m := range makeIfaceSummary(t).callable { for _, m := range makeIfaceSummary(t).callable {
if !g.isSigSupported(m.Type()) { if !g.isSigSupported(m.Type()) {
g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name())
continue continue
} }
s := g.funcSummary(m) s := g.funcSummary(nil, m)
g.Printf("- %s;\n", s.asMethod(g)) g.Printf("- %s;\n", s.asMethod(g))
} }
g.Printf("@end\n") g.Printf("@end\n")
@ -764,7 +875,7 @@ func (g *ObjcGen) genInterfaceM(obj *types.TypeName, t *types.Interface) bool {
g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name())
continue continue
} }
s := g.funcSummary(m) s := g.funcSummary(nil, m)
g.Printf("- %s {\n", s.asMethod(g)) g.Printf("- %s {\n", s.asMethod(g))
g.Indent() g.Indent()
g.genFunc(s, obj.Name()) g.genFunc(s, obj.Name())
@ -779,7 +890,7 @@ func (g *ObjcGen) genInterfaceM(obj *types.TypeName, t *types.Interface) bool {
func (g *ObjcGen) genInterfaceMethodProxy(obj *types.TypeName, m *types.Func) { func (g *ObjcGen) genInterfaceMethodProxy(obj *types.TypeName, m *types.Func) {
oName := obj.Name() oName := obj.Name()
s := g.funcSummary(m) s := g.funcSummary(nil, m)
g.genInterfaceMethodSignature(m, oName, false, g.paramName) g.genInterfaceMethodSignature(m, oName, false, g.paramName)
g.Indent() g.Indent()
g.Printf("@autoreleasepool {\n") g.Printf("@autoreleasepool {\n")
@ -875,11 +986,31 @@ func (g *ObjcGen) genRelease(varName string, t types.Type, mode varMode) {
} }
func (g *ObjcGen) genStructH(obj *types.TypeName, t *types.Struct) { func (g *ObjcGen) genStructH(obj *types.TypeName, t *types.Struct) {
g.Printf("@interface %s%s : NSObject {\n", g.namePrefix, obj.Name()) g.Printf("@interface %s%s : ", g.namePrefix, obj.Name())
oinf := g.ostructs[obj]
if oinf != nil {
var prots []string
for _, sup := range oinf.supers {
if !sup.Protocol {
g.Printf(sup.Name)
} else {
prots = append(prots, sup.Name)
}
}
if len(prots) > 0 {
g.Printf(" <%s>", strings.Join(prots, ", "))
}
} else {
g.Printf("NSObject <goSeqRefInterface>")
}
g.Printf(" {\n")
g.Printf("}\n") g.Printf("}\n")
g.Printf("@property(strong, readonly) id _ref;\n") g.Printf("@property(strong, readonly) id _ref;\n")
g.Printf("\n") g.Printf("\n")
g.Printf("- (id)initWithRef:(id)ref;\n") g.Printf("- (id)initWithRef:(id)ref;\n")
if oinf != nil {
g.Printf("- (id)init;\n")
}
// accessors to exported fields. // accessors to exported fields.
for _, f := range exportedFields(t) { for _, f := range exportedFields(t) {
@ -898,7 +1029,7 @@ func (g *ObjcGen) genStructH(obj *types.TypeName, t *types.Struct) {
g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name())
continue continue
} }
s := g.funcSummary(m) s := g.funcSummary(g.ostructs[obj], m)
g.Printf("- %s;\n", objcNameReplacer(lowerFirst(s.asMethod(g)))) g.Printf("- %s;\n", objcNameReplacer(lowerFirst(s.asMethod(g))))
} }
g.Printf("@end\n") g.Printf("@end\n")
@ -909,6 +1040,7 @@ func (g *ObjcGen) genStructM(obj *types.TypeName, t *types.Struct) {
methods := exportedMethodSet(types.NewPointer(obj.Type())) methods := exportedMethodSet(types.NewPointer(obj.Type()))
g.Printf("\n") g.Printf("\n")
oinf := g.ostructs[obj]
g.Printf("@implementation %s%s {\n", g.namePrefix, obj.Name()) g.Printf("@implementation %s%s {\n", g.namePrefix, obj.Name())
g.Printf("}\n\n") g.Printf("}\n\n")
g.Printf("- (id)initWithRef:(id)ref {\n") g.Printf("- (id)initWithRef:(id)ref {\n")
@ -918,6 +1050,19 @@ func (g *ObjcGen) genStructM(obj *types.TypeName, t *types.Struct) {
g.Printf("return self;\n") g.Printf("return self;\n")
g.Outdent() g.Outdent()
g.Printf("}\n\n") g.Printf("}\n\n")
if oinf != nil {
g.Printf("- (id)init {\n")
g.Indent()
g.Printf("self = [super init];\n")
g.Printf("if (self) {\n")
g.Indent()
g.Printf("__ref = go_seq_from_refnum(new_%s_%s());\n", g.pkgPrefix, obj.Name())
g.Outdent()
g.Printf("}\n")
g.Printf("return self;\n")
g.Outdent()
g.Printf("}\n\n")
}
for _, f := range fields { for _, f := range fields {
if !g.isSupported(f.Type()) { if !g.isSupported(f.Type()) {
@ -933,14 +1078,14 @@ func (g *ObjcGen) genStructM(obj *types.TypeName, t *types.Struct) {
g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name())
continue continue
} }
s := g.funcSummary(m) s := g.funcSummary(g.ostructs[obj], m)
g.Printf("- %s {\n", s.asMethod(g)) g.Printf("- %s {\n", s.asMethod(g))
g.Indent() g.Indent()
g.genFunc(s, obj.Name()) g.genFunc(s, obj.Name())
g.Outdent() g.Outdent()
g.Printf("}\n\n") g.Printf("}\n\n")
} }
g.Printf("@end\n") g.Printf("@end\n\n")
} }
func (g *ObjcGen) errorf(format string, args ...interface{}) { func (g *ObjcGen) errorf(format string, args ...interface{}) {
@ -955,6 +1100,9 @@ func (g *ObjcGen) refTypeBase(typ types.Type) string {
} }
case *types.Named: case *types.Named:
n := typ.Obj() n := typ.Obj()
if isObjcType(typ) {
return g.wrapMap[n.Name()].Name
}
if isErrorType(typ) || g.validPkg(n.Pkg()) { if isErrorType(typ) || g.validPkg(n.Pkg()) {
switch typ.Underlying().(type) { switch typ.Underlying().(type) {
case *types.Interface, *types.Struct: case *types.Interface, *types.Struct:
@ -1031,6 +1179,10 @@ func (g *ObjcGen) objcType(typ types.Type) string {
return "TODO" return "TODO"
case *types.Named: case *types.Named:
n := typ.Obj() n := typ.Obj()
if isObjcType(typ) {
w := g.wrapMap[n.Name()]
return w.ObjcType()
}
if !isErrorType(typ) && !g.validPkg(n.Pkg()) { if !isErrorType(typ) && !g.validPkg(n.Pkg()) {
g.errorf("type %s is in package %s, which is not bound", n.Name(), n.Pkg().Name()) g.errorf("type %s is in package %s, which is not bound", n.Name(), n.Pkg().Name())
return "TODO" return "TODO"
@ -1053,6 +1205,31 @@ func (g *ObjcGen) objcType(typ types.Type) string {
} }
} }
// embeddedObjcTypes returns the possible empty list of Objc types embedded
// in the given struct type.
func embeddedObjcTypes(t *types.Struct) []string {
typeSet := make(map[string]struct{})
var typs []string
for i := 0; i < t.NumFields(); i++ {
f := t.Field(i)
if !f.Exported() {
continue
}
if ft := f.Type(); isObjcType(ft) {
name := ft.(*types.Named).Obj().Name()
if _, exists := typeSet[name]; !exists {
typeSet[name] = struct{}{}
typs = append(typs, name)
}
}
}
return typs
}
func isObjcType(t types.Type) bool {
return typePkgFirstElem(t) == "ObjC"
}
var objcNameReplacer = newNameSanitizer([]string{ var objcNameReplacer = newNameSanitizer([]string{
"void", "char", "short", "int", "long", "float", "double", "signed", "void", "char", "short", "int", "long", "float", "double", "signed",
"unsigned", "id", "const", "volatile", "in", "out", "inout", "bycopy", "unsigned", "id", "const", "volatile", "in", "out", "inout", "bycopy",

View File

@ -35,13 +35,13 @@
@implementation wrappers @implementation wrappers
- (void)setUp { - (void)setUp {
[super setUp]; [super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class. // Put setup code here. This method is called before the invocation of each test method in the class.
} }
- (void)tearDown { - (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class. // Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown]; [super tearDown];
} }
- (void)testFunction { - (void)testFunction {
@ -59,4 +59,39 @@
- (void)testError { - (void)testError {
GoObjcpkgError(); GoObjcpkgError();
} }
- (void)testClass {
GoObjcpkgGoNSDate *d = [[GoObjcpkgGoNSDate alloc] init];
NSString *desc = [d description]; // Also stores this
XCTAssertEqual(d, [d this], "GoNSDate this not identical");
XCTAssertEqual(GoObjcpkgHash, [d hash], "GoNSDate this not identical");
XCTAssertTrue([desc isEqualToString:GoObjcpkgDescriptionStr], "GoNSDate description mismatch: %@", desc);
GoObjcpkgGoUIResponder *resp = [[GoObjcpkgGoUIResponder alloc] init];
[resp pressesBegan:nil withEvent:nil];
XCTAssertTrue([resp called], "GoUIResponder.pressesBegan not called");
}
- (void)testSuper {
GoObjcpkgGoNSObject *o = [[GoObjcpkgGoNSObject alloc] init];
struct objc_super _super = {
.receiver = o,
.super_class = [NSObject class],
};
NSString *superDesc = ((NSString *(*)(struct objc_super*, SEL))objc_msgSendSuper)(&_super, @selector(description));
XCTAssertTrue([superDesc isEqualToString:[o description]], "GoNSObject description mismatch");
[o setUseThis:TRUE];
XCTAssertTrue([GoObjcpkgDescriptionStr isEqualToString:[o description]], "GoNSObject description mismatch");
}
- (void)testIdentity {
NSDate *d = [[NSDate alloc] init];
NSDate *d2 = GoObjcpkgDupNSDate(d);
XCTAssertEqual(d, d2, @"GoObjcpkgDupNSDate failed to duplicate ObjC instance");
GoObjcpkgGoNSDate *gd = [[GoObjcpkgGoNSDate alloc] init];
NSDate *gd2 = GoObjcpkgDupNSDate(gd);
XCTAssertEqual(gd, gd2, @"GoObjcpkgDupNSDate failed to duplicate Go instance");
NSDate *gd3 = GoObjcpkgNewGoNSDate();
NSDate *gd4 = GoObjcpkgDupNSDate(gd3);
XCTAssertEqual(gd4, gd3, @"GoObjcpkgDupNSDate failed to duplicate instance created in Go");
}
@end @end

View File

@ -6,6 +6,7 @@
#define __GO_SEQ_HDR__ #define __GO_SEQ_HDR__
#include <Foundation/Foundation.h> #include <Foundation/Foundation.h>
#include "ref.h"
#include "GoUniverse.h" #include "GoUniverse.h"
#ifdef DEBUG #ifdef DEBUG
@ -24,32 +25,6 @@
userInfo:NULL]; \ userInfo:NULL]; \
} }
// GoSeqRef is an object tagged with an integer for passing back and
// forth across the language boundary. A GoSeqRef may represent either
// an instance of a Go object, or an Objective-C object passed to Go.
// The explicit allocation of a GoSeqRef is used to pin a Go object
// when it is passed to Objective-C. The Go seq package maintains a
// reference to the Go object in a map keyed by the refnum. When the
// GoSeqRef is deallocated, the Go seq package will clear the
// corresponding entry in the map.
// TODO(hyangah): update the doc as golang.org/issue/10933 is fixed.
@interface GoSeqRef : NSObject {
}
@property(readonly) int32_t refnum;
@property(strong) id obj; // NULL when representing a Go object.
// new GoSeqRef object to proxy a Go object. The refnum must be
// provided from Go side.
- (instancetype)initWithRefnum:(int32_t)refnum obj:(id)obj;
- (int32_t)incNum;
@end
@protocol goSeqRefInterface
-(GoSeqRef*) _ref;
@end
// Platform specific types // Platform specific types
typedef struct nstring { typedef struct nstring {
void *ptr; void *ptr;

View File

@ -6,9 +6,10 @@
#ifndef __GoBasictypes_H__ #ifndef __GoBasictypes_H__
#define __GoBasictypes_H__ #define __GoBasictypes_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
FOUNDATION_EXPORT const BOOL GoBasictypesABool; FOUNDATION_EXPORT const BOOL GoBasictypesABool;
FOUNDATION_EXPORT const double GoBasictypesAFloat; FOUNDATION_EXPORT const double GoBasictypesAFloat;
FOUNDATION_EXPORT NSString* const GoBasictypesALongString; FOUNDATION_EXPORT NSString* const GoBasictypesALongString;

View File

@ -6,9 +6,10 @@
#ifndef __EXCustomprefix_H__ #ifndef __EXCustomprefix_H__
#define __EXCustomprefix_H__ #define __EXCustomprefix_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
FOUNDATION_EXPORT void EXCustomprefixF(); FOUNDATION_EXPORT void EXCustomprefixF();
#endif #endif

View File

@ -6,14 +6,15 @@
#ifndef __GoIgnore_H__ #ifndef __GoIgnore_H__
#define __GoIgnore_H__ #define __GoIgnore_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
@class GoIgnoreS; @class GoIgnoreS;
@protocol GoIgnoreI; @protocol GoIgnoreI;
@class GoIgnoreI; @class GoIgnoreI;
@interface GoIgnoreS : NSObject { @interface GoIgnoreS : NSObject <goSeqRefInterface> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -26,7 +27,7 @@
@end @end
@protocol GoIgnoreI @protocol GoIgnoreI <NSObject>
// skipped method I.Argument with unsupported parameter or return types // skipped method I.Argument with unsupported parameter or return types
// skipped method I.Result with unsupported parameter or return types // skipped method I.Result with unsupported parameter or return types
@ -51,7 +52,7 @@
@class GoIgnoreI; @class GoIgnoreI;
@interface GoIgnoreI : NSObject <GoIgnoreI> { @interface GoIgnoreI : NSObject <goSeqRefInterface, GoIgnoreI> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;

View File

@ -26,6 +26,7 @@
@end @end
@implementation GoIgnoreI { @implementation GoIgnoreI {
} }

View File

@ -6,9 +6,10 @@
#ifndef __GoInterfaces_H__ #ifndef __GoInterfaces_H__
#define __GoInterfaces_H__ #define __GoInterfaces_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
@protocol GoInterfacesError; @protocol GoInterfacesError;
@class GoInterfacesError; @class GoInterfacesError;
@protocol GoInterfacesI; @protocol GoInterfacesI;
@ -24,15 +25,15 @@
@protocol GoInterfacesWithParam; @protocol GoInterfacesWithParam;
@class GoInterfacesWithParam; @class GoInterfacesWithParam;
@protocol GoInterfacesError @protocol GoInterfacesError <NSObject>
- (BOOL)err:(NSError**)error; - (BOOL)err:(NSError**)error;
@end @end
@protocol GoInterfacesI @protocol GoInterfacesI <NSObject>
- (int32_t)rand; - (int32_t)rand;
@end @end
@interface GoInterfacesI1 : NSObject { @interface GoInterfacesI1 : NSObject <goSeqRefInterface> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -40,7 +41,7 @@
- (void)j; - (void)j;
@end @end
@interface GoInterfacesI2 : NSObject { @interface GoInterfacesI2 : NSObject <goSeqRefInterface> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -48,20 +49,20 @@
- (void)g; - (void)g;
@end @end
@protocol GoInterfacesI3 @protocol GoInterfacesI3 <NSObject>
- (GoInterfacesI1*)f; - (GoInterfacesI1*)f;
@end @end
@protocol GoInterfacesLargerI @protocol GoInterfacesLargerI <NSObject>
- (void)anotherFunc; - (void)anotherFunc;
- (int32_t)rand; - (int32_t)rand;
@end @end
@protocol GoInterfacesSameI @protocol GoInterfacesSameI <NSObject>
- (int32_t)rand; - (int32_t)rand;
@end @end
@protocol GoInterfacesWithParam @protocol GoInterfacesWithParam <NSObject>
- (void)hasParam:(BOOL)p0; - (void)hasParam:(BOOL)p0;
@end @end
@ -83,7 +84,7 @@ FOUNDATION_EXPORT id<GoInterfacesI> GoInterfacesSeven();
@class GoInterfacesWithParam; @class GoInterfacesWithParam;
@interface GoInterfacesError : NSObject <GoInterfacesError> { @interface GoInterfacesError : NSObject <goSeqRefInterface, GoInterfacesError> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -91,7 +92,7 @@ FOUNDATION_EXPORT id<GoInterfacesI> GoInterfacesSeven();
- (BOOL)err:(NSError**)error; - (BOOL)err:(NSError**)error;
@end @end
@interface GoInterfacesI : NSObject <GoInterfacesI> { @interface GoInterfacesI : NSObject <goSeqRefInterface, GoInterfacesI> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -99,7 +100,7 @@ FOUNDATION_EXPORT id<GoInterfacesI> GoInterfacesSeven();
- (int32_t)rand; - (int32_t)rand;
@end @end
@interface GoInterfacesI3 : NSObject <GoInterfacesI3> { @interface GoInterfacesI3 : NSObject <goSeqRefInterface, GoInterfacesI3> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -107,7 +108,7 @@ FOUNDATION_EXPORT id<GoInterfacesI> GoInterfacesSeven();
- (GoInterfacesI1*)f; - (GoInterfacesI1*)f;
@end @end
@interface GoInterfacesLargerI : NSObject <GoInterfacesLargerI> { @interface GoInterfacesLargerI : NSObject <goSeqRefInterface, GoInterfacesLargerI> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -116,7 +117,7 @@ FOUNDATION_EXPORT id<GoInterfacesI> GoInterfacesSeven();
- (int32_t)rand; - (int32_t)rand;
@end @end
@interface GoInterfacesSameI : NSObject <GoInterfacesSameI> { @interface GoInterfacesSameI : NSObject <goSeqRefInterface, GoInterfacesSameI> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -124,7 +125,7 @@ FOUNDATION_EXPORT id<GoInterfacesI> GoInterfacesSeven();
- (int32_t)rand; - (int32_t)rand;
@end @end
@interface GoInterfacesWithParam : NSObject <GoInterfacesWithParam> { @interface GoInterfacesWithParam : NSObject <goSeqRefInterface, GoInterfacesWithParam> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;

View File

@ -180,7 +180,7 @@
int32_t GoInterfacesAdd3(id<GoInterfacesI> r) { int32_t GoInterfacesAdd3(id<GoInterfacesI> r) {
int32_t _r; int32_t _r;
if ([(id<NSObject>)(r) isKindOfClass:[GoInterfacesI class]]) { if ([r conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> r_proxy = (id<goSeqRefInterface>)(r); id<goSeqRefInterface> r_proxy = (id<goSeqRefInterface>)(r);
_r = go_seq_go_to_refnum(r_proxy._ref); _r = go_seq_go_to_refnum(r_proxy._ref);
} else { } else {
@ -193,7 +193,7 @@ int32_t GoInterfacesAdd3(id<GoInterfacesI> r) {
BOOL GoInterfacesCallErr(id<GoInterfacesError> e, NSError** error) { BOOL GoInterfacesCallErr(id<GoInterfacesError> e, NSError** error) {
int32_t _e; int32_t _e;
if ([(id<NSObject>)(e) isKindOfClass:[GoInterfacesError class]]) { if ([e conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> e_proxy = (id<goSeqRefInterface>)(e); id<goSeqRefInterface> e_proxy = (id<goSeqRefInterface>)(e);
_e = go_seq_go_to_refnum(e_proxy._ref); _e = go_seq_go_to_refnum(e_proxy._ref);
} else { } else {
@ -237,7 +237,7 @@ int32_t cproxyinterfaces_Error_Err(int32_t refnum) {
_error = error; _error = error;
} }
int32_t __error; int32_t __error;
if ([(id<NSObject>)(_error) isKindOfClass:[GoUniverseerror class]]) { if ([_error conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> _error_proxy = (id<goSeqRefInterface>)(_error); id<goSeqRefInterface> _error_proxy = (id<goSeqRefInterface>)(_error);
__error = go_seq_go_to_refnum(_error_proxy._ref); __error = go_seq_go_to_refnum(_error_proxy._ref);
} else { } else {
@ -275,7 +275,7 @@ int32_t cproxyinterfaces_I3_F(int32_t refnum) {
GoInterfacesI3* o = go_seq_objc_from_refnum(refnum); GoInterfacesI3* o = go_seq_objc_from_refnum(refnum);
GoInterfacesI1* returnVal = [o f]; GoInterfacesI1* returnVal = [o f];
int32_t _returnVal; int32_t _returnVal;
if ([(id<NSObject>)(returnVal) isKindOfClass:[GoInterfacesI1 class]]) { if ([returnVal conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> returnVal_proxy = (id<goSeqRefInterface>)(returnVal); id<goSeqRefInterface> returnVal_proxy = (id<goSeqRefInterface>)(returnVal);
_returnVal = go_seq_go_to_refnum(returnVal_proxy._ref); _returnVal = go_seq_go_to_refnum(returnVal_proxy._ref);
} else { } else {

View File

@ -6,14 +6,15 @@
#ifndef __GoIssue10788_H__ #ifndef __GoIssue10788_H__
#define __GoIssue10788_H__ #define __GoIssue10788_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
@class GoIssue10788TestStruct; @class GoIssue10788TestStruct;
@protocol GoIssue10788TestInterface; @protocol GoIssue10788TestInterface;
@class GoIssue10788TestInterface; @class GoIssue10788TestInterface;
@interface GoIssue10788TestStruct : NSObject { @interface GoIssue10788TestStruct : NSObject <goSeqRefInterface> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -22,14 +23,14 @@
- (void)setValue:(NSString*)v; - (void)setValue:(NSString*)v;
@end @end
@protocol GoIssue10788TestInterface @protocol GoIssue10788TestInterface <NSObject>
- (void)doSomeWork:(GoIssue10788TestStruct*)s; - (void)doSomeWork:(GoIssue10788TestStruct*)s;
- (void)multipleUnnamedParams:(long)p0 p1:(NSString*)p1 日本:(int64_t)日本; - (void)multipleUnnamedParams:(long)p0 p1:(NSString*)p1 日本:(int64_t)日本;
@end @end
@class GoIssue10788TestInterface; @class GoIssue10788TestInterface;
@interface GoIssue10788TestInterface : NSObject <GoIssue10788TestInterface> { @interface GoIssue10788TestInterface : NSObject <goSeqRefInterface, GoIssue10788TestInterface> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;

View File

@ -33,6 +33,7 @@
@end @end
@implementation GoIssue10788TestInterface { @implementation GoIssue10788TestInterface {
} }
@ -45,7 +46,7 @@
- (void)doSomeWork:(GoIssue10788TestStruct*)s { - (void)doSomeWork:(GoIssue10788TestStruct*)s {
int32_t refnum = go_seq_go_to_refnum(self._ref); int32_t refnum = go_seq_go_to_refnum(self._ref);
int32_t _s; int32_t _s;
if ([(id<NSObject>)(s) isKindOfClass:[GoIssue10788TestStruct class]]) { if ([s conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> s_proxy = (id<goSeqRefInterface>)(s); id<goSeqRefInterface> s_proxy = (id<goSeqRefInterface>)(s);
_s = go_seq_go_to_refnum(s_proxy._ref); _s = go_seq_go_to_refnum(s_proxy._ref);
} else { } else {

View File

@ -6,12 +6,13 @@
#ifndef __GoIssue12328_H__ #ifndef __GoIssue12328_H__
#define __GoIssue12328_H__ #define __GoIssue12328_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
@class GoIssue12328T; @class GoIssue12328T;
@interface GoIssue12328T : NSObject { @interface GoIssue12328T : NSObject <goSeqRefInterface> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;

View File

@ -35,7 +35,7 @@
- (void)setErr:(NSError*)v { - (void)setErr:(NSError*)v {
int32_t refnum = go_seq_go_to_refnum(self._ref); int32_t refnum = go_seq_go_to_refnum(self._ref);
int32_t _v; int32_t _v;
if ([(id<NSObject>)(v) isKindOfClass:[GoUniverseerror class]]) { if ([v conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> v_proxy = (id<goSeqRefInterface>)(v); id<goSeqRefInterface> v_proxy = (id<goSeqRefInterface>)(v);
_v = go_seq_go_to_refnum(v_proxy._ref); _v = go_seq_go_to_refnum(v_proxy._ref);
} else { } else {
@ -47,6 +47,7 @@
@end @end
__attribute__((constructor)) static void init() { __attribute__((constructor)) static void init() {
init_seq(); init_seq();
} }

View File

@ -6,20 +6,21 @@
#ifndef __GoIssue12403_H__ #ifndef __GoIssue12403_H__
#define __GoIssue12403_H__ #define __GoIssue12403_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
@protocol GoIssue12403Parsable; @protocol GoIssue12403Parsable;
@class GoIssue12403Parsable; @class GoIssue12403Parsable;
@protocol GoIssue12403Parsable @protocol GoIssue12403Parsable <NSObject>
- (NSString*)fromJSON:(NSString*)jstr; - (NSString*)fromJSON:(NSString*)jstr;
- (BOOL)toJSON:(NSString**)ret0_ error:(NSError**)error; - (BOOL)toJSON:(NSString**)ret0_ error:(NSError**)error;
@end @end
@class GoIssue12403Parsable; @class GoIssue12403Parsable;
@interface GoIssue12403Parsable : NSObject <GoIssue12403Parsable> { @interface GoIssue12403Parsable : NSObject <goSeqRefInterface, GoIssue12403Parsable> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;

View File

@ -70,7 +70,7 @@ struct cproxyissue12403_Parsable_ToJSON_return cproxyissue12403_Parsable_ToJSON(
_error = error; _error = error;
} }
int32_t __error; int32_t __error;
if ([(id<NSObject>)(_error) isKindOfClass:[GoUniverseerror class]]) { if ([_error conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> _error_proxy = (id<goSeqRefInterface>)(_error); id<goSeqRefInterface> _error_proxy = (id<goSeqRefInterface>)(_error);
__error = go_seq_go_to_refnum(_error_proxy._ref); __error = go_seq_go_to_refnum(_error_proxy._ref);
} else { } else {

View File

@ -6,13 +6,14 @@
#ifndef __GoKeywords_H__ #ifndef __GoKeywords_H__
#define __GoKeywords_H__ #define __GoKeywords_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
@protocol GoKeywordsKeywordCaller; @protocol GoKeywordsKeywordCaller;
@class GoKeywordsKeywordCaller; @class GoKeywordsKeywordCaller;
@protocol GoKeywordsKeywordCaller @protocol GoKeywordsKeywordCaller <NSObject>
- (void)abstract; - (void)abstract;
- (void)assert; - (void)assert;
- (void)boolean; - (void)boolean;
@ -70,7 +71,7 @@
@class GoKeywordsKeywordCaller; @class GoKeywordsKeywordCaller;
@interface GoKeywordsKeywordCaller : NSObject <GoKeywordsKeywordCaller> { @interface GoKeywordsKeywordCaller : NSObject <goSeqRefInterface, GoKeywordsKeywordCaller> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;

39
bind/testdata/objcw.go vendored Normal file
View File

@ -0,0 +1,39 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package objc
import (
"ObjC/Foundation"
"ObjC/UIKit"
)
type GoNSDate struct {
Foundation.NSDate
this Foundation.NSDate
}
func (d *GoNSDate) Hash(this Foundation.NSDate) int {
return 0
}
type GoNSObject struct {
C Foundation.NSObjectC // The class
P Foundation.NSObjectP // The protocol
}
func (o *GoNSObject) Description(this Foundation.NSObjectC) string {
return ""
}
func DupNSDate(date Foundation.NSDate) Foundation.NSDate {
return date
}
type GoUIResponder struct {
UIKit.UIResponder
}
func (r *GoUIResponder) PressesBegan(_ Foundation.NSSet, _ UIKit.UIPressesEvent) {
}

487
bind/testdata/objcw.go.golden vendored Normal file
View File

@ -0,0 +1,487 @@
//File is generated by gobind. Do not edit.
package ObjC
// Used to silence this package not used errors
const Dummy = 0
type Foundation_NSDate interface {
Hash() (uint)
Description() (string)
Super() Foundation_NSDate
}
type Foundation_NSObjectC interface {
Hash() (uint)
Description() (string)
Super() Foundation_NSObjectC
}
type Foundation_NSObjectP interface {
Hash() (uint)
Description() (string)
}
type Foundation_NSSet interface {
Hash() (uint)
Description() (string)
Super() Foundation_NSSet
}
type UIKit_UIResponder interface {
Hash() (uint)
Description() (string)
Super() UIKit_UIResponder
}
type UIKit_UIPressesEvent interface {
Hash() (uint)
Description() (string)
Super() UIKit_UIPressesEvent
}
// File is generated by gobind. Do not edit.
package gomobile_bind
// #cgo CFLAGS: -fobjc-arc -fmodules -fblocks
// #include "interfaces.h"
import "C"
import "ObjC"
import _seq "golang.org/x/mobile/bind/seq"
type proxy interface { Bind_proxy_refnum__() int32 }
// Suppress unused package error
var _ = _seq.FromRefNum
const _ = ObjC.Dummy
func init() {
}
type proxy_class_NSDate _seq.Ref
func (p *proxy_class_NSDate) Bind_proxy_refnum__() int32 { return (*_seq.Ref)(p).Bind_IncNum() }
func (p *proxy_class_NSDate) Hash() (uint) {
res := C.cproxy_NSDate_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *proxy_class_NSDate) Description() (string) {
res := C.cproxy_NSDate_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
func (p *proxy_class_NSDate) Super() ObjC.Foundation_NSDate {
return &super_NSDate{p}
}
type super_NSDate struct {*proxy_class_NSDate}
func (p *super_NSDate) Hash() (uint) {
res := C.csuper_NSDate_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *super_NSDate) Description() (string) {
res := C.csuper_NSDate_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
func init() {
}
type proxy_class_NSObjectC _seq.Ref
func (p *proxy_class_NSObjectC) Bind_proxy_refnum__() int32 { return (*_seq.Ref)(p).Bind_IncNum() }
func (p *proxy_class_NSObjectC) Hash() (uint) {
res := C.cproxy_NSObjectC_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *proxy_class_NSObjectC) Description() (string) {
res := C.cproxy_NSObjectC_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
func (p *proxy_class_NSObjectC) Super() ObjC.Foundation_NSObjectC {
return &super_NSObjectC{p}
}
type super_NSObjectC struct {*proxy_class_NSObjectC}
func (p *super_NSObjectC) Hash() (uint) {
res := C.csuper_NSObjectC_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *super_NSObjectC) Description() (string) {
res := C.csuper_NSObjectC_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
func init() {
}
type proxy_class_NSObjectP _seq.Ref
func (p *proxy_class_NSObjectP) Bind_proxy_refnum__() int32 { return (*_seq.Ref)(p).Bind_IncNum() }
func (p *proxy_class_NSObjectP) Hash() (uint) {
res := C.cproxy_NSObjectP_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *proxy_class_NSObjectP) Description() (string) {
res := C.cproxy_NSObjectP_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
func init() {
}
type proxy_class_NSSet _seq.Ref
func (p *proxy_class_NSSet) Bind_proxy_refnum__() int32 { return (*_seq.Ref)(p).Bind_IncNum() }
func (p *proxy_class_NSSet) Hash() (uint) {
res := C.cproxy_NSSet_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *proxy_class_NSSet) Description() (string) {
res := C.cproxy_NSSet_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
func (p *proxy_class_NSSet) Super() ObjC.Foundation_NSSet {
return &super_NSSet{p}
}
type super_NSSet struct {*proxy_class_NSSet}
func (p *super_NSSet) Hash() (uint) {
res := C.csuper_NSSet_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *super_NSSet) Description() (string) {
res := C.csuper_NSSet_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
func init() {
}
type proxy_class_UIResponder _seq.Ref
func (p *proxy_class_UIResponder) Bind_proxy_refnum__() int32 { return (*_seq.Ref)(p).Bind_IncNum() }
func (p *proxy_class_UIResponder) Hash() (uint) {
res := C.cproxy_UIResponder_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *proxy_class_UIResponder) Description() (string) {
res := C.cproxy_UIResponder_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
func (p *proxy_class_UIResponder) Super() ObjC.UIKit_UIResponder {
return &super_UIResponder{p}
}
type super_UIResponder struct {*proxy_class_UIResponder}
func (p *super_UIResponder) Hash() (uint) {
res := C.csuper_UIResponder_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *super_UIResponder) Description() (string) {
res := C.csuper_UIResponder_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
func init() {
}
type proxy_class_UIPressesEvent _seq.Ref
func (p *proxy_class_UIPressesEvent) Bind_proxy_refnum__() int32 { return (*_seq.Ref)(p).Bind_IncNum() }
func (p *proxy_class_UIPressesEvent) Hash() (uint) {
res := C.cproxy_UIPressesEvent_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *proxy_class_UIPressesEvent) Description() (string) {
res := C.cproxy_UIPressesEvent_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
func (p *proxy_class_UIPressesEvent) Super() ObjC.UIKit_UIPressesEvent {
return &super_UIPressesEvent{p}
}
type super_UIPressesEvent struct {*proxy_class_UIPressesEvent}
func (p *super_UIPressesEvent) Hash() (uint) {
res := C.csuper_UIPressesEvent_Hash(C.int(p.Bind_proxy_refnum__()))
_res := uint(res)
return _res
}
func (p *super_UIPressesEvent) Description() (string) {
res := C.csuper_UIPressesEvent_Description(C.int(p.Bind_proxy_refnum__()))
_res := decodeString(res)
return _res
}
// Package gomobile_bind is an autogenerated binder stub for package objc.
// gobind -lang=go objcw
//
// File is generated by gobind. Do not edit.
package gomobile_bind
/*
#include <stdlib.h>
#include <stdint.h>
#include "seq.h"
#include "objc.h"
*/
import "C"
import (
"ObjC/Foundation"
"ObjC/UIKit"
_seq "golang.org/x/mobile/bind/seq"
"objcw"
)
// suppress the error if seq ends up unused
var _ = _seq.FromRefNum
//export proxyobjc_GoNSDate_NSDate_Set
func proxyobjc_GoNSDate_NSDate_Set(refnum C.int32_t, v C.int32_t) {
ref := _seq.FromRefNum(int32(refnum))
var _v Foundation.NSDate
_v_ref := _seq.FromRefNum(int32(v))
if _v_ref != nil {
if v < 0 { // go object
_v = _v_ref.Get().(Foundation.NSDate)
} else { // foreign object
_v = (*proxy_class_NSDate)(_v_ref)
}
}
ref.Get().(*objc.GoNSDate).NSDate = _v
}
//export proxyobjc_GoNSDate_NSDate_Get
func proxyobjc_GoNSDate_NSDate_Get(refnum C.int32_t) C.int32_t {
ref := _seq.FromRefNum(int32(refnum))
v := ref.Get().(*objc.GoNSDate).NSDate
var _v C.int32_t = _seq.NullRefNum
if v != nil {
_v = C.int32_t(_seq.ToRefNum(v))
}
return _v
}
//export proxyobjc_GoNSDate_Hash
func proxyobjc_GoNSDate_Hash(refnum C.int32_t, param_this C.int32_t) C.nint {
ref := _seq.FromRefNum(int32(refnum))
v := ref.Get().(*objc.GoNSDate)
var _param_this Foundation.NSDate
_param_this_ref := _seq.FromRefNum(int32(param_this))
if _param_this_ref != nil {
if param_this < 0 { // go object
_param_this = _param_this_ref.Get().(Foundation.NSDate)
} else { // foreign object
_param_this = (*proxy_class_NSDate)(_param_this_ref)
}
}
res_0 := v.Hash(_param_this)
_res_0 := C.nint(res_0)
return _res_0
}
//export new_objc_GoNSDate
func new_objc_GoNSDate() C.int32_t {
return C.int32_t(_seq.ToRefNum(new(objc.GoNSDate)))
}
//export proxyobjc_GoNSObject_C_Set
func proxyobjc_GoNSObject_C_Set(refnum C.int32_t, v C.int32_t) {
ref := _seq.FromRefNum(int32(refnum))
var _v Foundation.NSObjectC
_v_ref := _seq.FromRefNum(int32(v))
if _v_ref != nil {
if v < 0 { // go object
_v = _v_ref.Get().(Foundation.NSObjectC)
} else { // foreign object
_v = (*proxy_class_NSObjectC)(_v_ref)
}
}
ref.Get().(*objc.GoNSObject).C = _v
}
//export proxyobjc_GoNSObject_C_Get
func proxyobjc_GoNSObject_C_Get(refnum C.int32_t) C.int32_t {
ref := _seq.FromRefNum(int32(refnum))
v := ref.Get().(*objc.GoNSObject).C
var _v C.int32_t = _seq.NullRefNum
if v != nil {
_v = C.int32_t(_seq.ToRefNum(v))
}
return _v
}
//export proxyobjc_GoNSObject_P_Set
func proxyobjc_GoNSObject_P_Set(refnum C.int32_t, v C.int32_t) {
ref := _seq.FromRefNum(int32(refnum))
var _v Foundation.NSObjectP
_v_ref := _seq.FromRefNum(int32(v))
if _v_ref != nil {
if v < 0 { // go object
_v = _v_ref.Get().(Foundation.NSObjectP)
} else { // foreign object
_v = (*proxy_class_NSObjectP)(_v_ref)
}
}
ref.Get().(*objc.GoNSObject).P = _v
}
//export proxyobjc_GoNSObject_P_Get
func proxyobjc_GoNSObject_P_Get(refnum C.int32_t) C.int32_t {
ref := _seq.FromRefNum(int32(refnum))
v := ref.Get().(*objc.GoNSObject).P
var _v C.int32_t = _seq.NullRefNum
if v != nil {
_v = C.int32_t(_seq.ToRefNum(v))
}
return _v
}
//export proxyobjc_GoNSObject_Description
func proxyobjc_GoNSObject_Description(refnum C.int32_t, param_this C.int32_t) C.nstring {
ref := _seq.FromRefNum(int32(refnum))
v := ref.Get().(*objc.GoNSObject)
var _param_this Foundation.NSObjectC
_param_this_ref := _seq.FromRefNum(int32(param_this))
if _param_this_ref != nil {
if param_this < 0 { // go object
_param_this = _param_this_ref.Get().(Foundation.NSObjectC)
} else { // foreign object
_param_this = (*proxy_class_NSObjectC)(_param_this_ref)
}
}
res_0 := v.Description(_param_this)
_res_0 := encodeString(res_0)
return _res_0
}
//export new_objc_GoNSObject
func new_objc_GoNSObject() C.int32_t {
return C.int32_t(_seq.ToRefNum(new(objc.GoNSObject)))
}
//export proxyobjc_GoUIResponder_UIResponder_Set
func proxyobjc_GoUIResponder_UIResponder_Set(refnum C.int32_t, v C.int32_t) {
ref := _seq.FromRefNum(int32(refnum))
var _v UIKit.UIResponder
_v_ref := _seq.FromRefNum(int32(v))
if _v_ref != nil {
if v < 0 { // go object
_v = _v_ref.Get().(UIKit.UIResponder)
} else { // foreign object
_v = (*proxy_class_UIResponder)(_v_ref)
}
}
ref.Get().(*objc.GoUIResponder).UIResponder = _v
}
//export proxyobjc_GoUIResponder_UIResponder_Get
func proxyobjc_GoUIResponder_UIResponder_Get(refnum C.int32_t) C.int32_t {
ref := _seq.FromRefNum(int32(refnum))
v := ref.Get().(*objc.GoUIResponder).UIResponder
var _v C.int32_t = _seq.NullRefNum
if v != nil {
_v = C.int32_t(_seq.ToRefNum(v))
}
return _v
}
//export proxyobjc_GoUIResponder_PressesBegan
func proxyobjc_GoUIResponder_PressesBegan(refnum C.int32_t, param_p0 C.int32_t, param_p1 C.int32_t) {
ref := _seq.FromRefNum(int32(refnum))
v := ref.Get().(*objc.GoUIResponder)
var _param_p0 Foundation.NSSet
_param_p0_ref := _seq.FromRefNum(int32(param_p0))
if _param_p0_ref != nil {
if param_p0 < 0 { // go object
_param_p0 = _param_p0_ref.Get().(Foundation.NSSet)
} else { // foreign object
_param_p0 = (*proxy_class_NSSet)(_param_p0_ref)
}
}
var _param_p1 UIKit.UIPressesEvent
_param_p1_ref := _seq.FromRefNum(int32(param_p1))
if _param_p1_ref != nil {
if param_p1 < 0 { // go object
_param_p1 = _param_p1_ref.Get().(UIKit.UIPressesEvent)
} else { // foreign object
_param_p1 = (*proxy_class_UIPressesEvent)(_param_p1_ref)
}
}
v.PressesBegan(_param_p0, _param_p1)
}
//export new_objc_GoUIResponder
func new_objc_GoUIResponder() C.int32_t {
return C.int32_t(_seq.ToRefNum(new(objc.GoUIResponder)))
}
//export proxyobjc__DupNSDate
func proxyobjc__DupNSDate(param_date C.int32_t) C.int32_t {
var _param_date Foundation.NSDate
_param_date_ref := _seq.FromRefNum(int32(param_date))
if _param_date_ref != nil {
if param_date < 0 { // go object
_param_date = _param_date_ref.Get().(Foundation.NSDate)
} else { // foreign object
_param_date = (*proxy_class_NSDate)(_param_date_ref)
}
}
res_0 := objc.DupNSDate(_param_date)
var _res_0 C.int32_t = _seq.NullRefNum
if res_0 != nil {
_res_0 = C.int32_t(_seq.ToRefNum(res_0))
}
return _res_0
}

View File

@ -6,15 +6,16 @@
#ifndef __GoStructs_H__ #ifndef __GoStructs_H__
#define __GoStructs_H__ #define __GoStructs_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
@class GoStructsS; @class GoStructsS;
@class GoStructsS2; @class GoStructsS2;
@protocol GoStructsI; @protocol GoStructsI;
@class GoStructsI; @class GoStructsI;
@interface GoStructsS : NSObject { @interface GoStructsS : NSObject <goSeqRefInterface> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -27,7 +28,7 @@
- (double)sum; - (double)sum;
@end @end
@interface GoStructsS2 : NSObject { @interface GoStructsS2 : NSObject <goSeqRefInterface> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
@ -36,7 +37,7 @@
- (NSString*)string; - (NSString*)string;
@end @end
@protocol GoStructsI @protocol GoStructsI <NSObject>
- (void)m; - (void)m;
@end @end
@ -46,7 +47,7 @@ FOUNDATION_EXPORT BOOL GoStructsIdentityWithError(GoStructsS* s, GoStructsS** re
@class GoStructsI; @class GoStructsI;
@interface GoStructsI : NSObject <GoStructsI> { @interface GoStructsI : NSObject <goSeqRefInterface, GoStructsI> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;

View File

@ -80,6 +80,7 @@
@end @end
@implementation GoStructsS2 { @implementation GoStructsS2 {
} }
@ -103,6 +104,7 @@
@end @end
@implementation GoStructsI { @implementation GoStructsI {
} }
@ -123,7 +125,7 @@
GoStructsS* GoStructsIdentity(GoStructsS* s) { GoStructsS* GoStructsIdentity(GoStructsS* s) {
int32_t _s; int32_t _s;
if ([(id<NSObject>)(s) isKindOfClass:[GoStructsS class]]) { if ([s conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> s_proxy = (id<goSeqRefInterface>)(s); id<goSeqRefInterface> s_proxy = (id<goSeqRefInterface>)(s);
_s = go_seq_go_to_refnum(s_proxy._ref); _s = go_seq_go_to_refnum(s_proxy._ref);
} else { } else {
@ -143,7 +145,7 @@ GoStructsS* GoStructsIdentity(GoStructsS* s) {
BOOL GoStructsIdentityWithError(GoStructsS* s, GoStructsS** ret0_, NSError** error) { BOOL GoStructsIdentityWithError(GoStructsS* s, GoStructsS** ret0_, NSError** error) {
int32_t _s; int32_t _s;
if ([(id<NSObject>)(s) isKindOfClass:[GoStructsS class]]) { if ([s conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> s_proxy = (id<goSeqRefInterface>)(s); id<goSeqRefInterface> s_proxy = (id<goSeqRefInterface>)(s);
_s = go_seq_go_to_refnum(s_proxy._ref); _s = go_seq_go_to_refnum(s_proxy._ref);
} else { } else {

View File

@ -6,9 +6,10 @@
#ifndef __GoTry_H__ #ifndef __GoTry_H__
#define __GoTry_H__ #define __GoTry_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
FOUNDATION_EXPORT NSString* GoTryThis(); FOUNDATION_EXPORT NSString* GoTryThis();
#endif #endif

View File

@ -6,21 +6,22 @@
#ifndef __GoVars_H__ #ifndef __GoVars_H__
#define __GoVars_H__ #define __GoVars_H__
#include <Foundation/Foundation.h> @import Foundation;
#include "GoUniverse.h" #include "GoUniverse.h"
@class GoVarsS; @class GoVarsS;
@protocol GoVarsI; @protocol GoVarsI;
@class GoVarsI; @class GoVarsI;
@interface GoVarsS : NSObject { @interface GoVarsS : NSObject <goSeqRefInterface> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;
- (id)initWithRef:(id)ref; - (id)initWithRef:(id)ref;
@end @end
@protocol GoVarsI @protocol GoVarsI <NSObject>
@end @end
@interface GoVars : NSObject @interface GoVars : NSObject
@ -64,7 +65,7 @@
@class GoVarsI; @class GoVarsI;
@interface GoVarsI : NSObject <GoVarsI> { @interface GoVarsI : NSObject <goSeqRefInterface, GoVarsI> {
} }
@property(strong, readonly) id _ref; @property(strong, readonly) id _ref;

View File

@ -20,6 +20,7 @@
@end @end
@implementation GoVarsI { @implementation GoVarsI {
} }
@ -90,7 +91,7 @@
+ (void) setAStructPtr:(GoVarsS*)v { + (void) setAStructPtr:(GoVarsS*)v {
int32_t _v; int32_t _v;
if ([(id<NSObject>)(v) isKindOfClass:[GoVarsS class]]) { if ([v conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> v_proxy = (id<goSeqRefInterface>)(v); id<goSeqRefInterface> v_proxy = (id<goSeqRefInterface>)(v);
_v = go_seq_go_to_refnum(v_proxy._ref); _v = go_seq_go_to_refnum(v_proxy._ref);
} else { } else {
@ -169,7 +170,7 @@
+ (void) setAnInterface:(id<GoVarsI>)v { + (void) setAnInterface:(id<GoVarsI>)v {
int32_t _v; int32_t _v;
if ([(id<NSObject>)(v) isKindOfClass:[GoVarsI class]]) { if ([v conformsToProtocol:@protocol(goSeqRefInterface)]) {
id<goSeqRefInterface> v_proxy = (id<goSeqRefInterface>)(v); id<goSeqRefInterface> v_proxy = (id<goSeqRefInterface>)(v);
_v = go_seq_go_to_refnum(v_proxy._ref); _v = go_seq_go_to_refnum(v_proxy._ref);
} else { } else {

View File

@ -0,0 +1,68 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package objcpkg
import (
"ObjC/Foundation"
"ObjC/UIKit"
)
const (
DescriptionStr = "Descriptrion from Go"
Hash = 42
)
type GoNSDate struct {
Foundation.NSDate
this Foundation.NSDate
}
func (d *GoNSDate) Hash(this Foundation.NSDate) int {
return Hash
}
func (d *GoNSDate) Description(this Foundation.NSDate) string {
// Test this call
if h := this.Hash(); h != Hash {
panic("hash mismatch")
}
d.this = this
return DescriptionStr
}
func (d *GoNSDate) This() Foundation.NSDate {
return d.this
}
func NewGoNSDate() *GoNSDate {
return new(GoNSDate)
}
type GoNSObject struct {
C Foundation.NSObjectC // The class
P Foundation.NSObjectP // The protocol
UseThis bool
}
func (o *GoNSObject) Description(this Foundation.NSObjectC) string {
if o.UseThis {
return DescriptionStr
} else {
return this.Super().Description()
}
}
func DupNSDate(date Foundation.NSDate) Foundation.NSDate {
return date
}
type GoUIResponder struct {
UIKit.UIResponder
Called bool
}
func (r *GoUIResponder) PressesBegan(_ Foundation.NSSet, _ UIKit.UIPressesEvent) {
r.Called = true
}

View File

@ -76,9 +76,9 @@ func exportedMethodSet(T types.Type) []*types.Func {
if !obj.Exported() { if !obj.Exported() {
continue continue
} }
// Skip methods from the embedded java classes, so that // Skip methods from the embedded classes, so that
// only methods that are implemented in Go are included. // only methods that are implemented in Go are included.
if isJavaPkg(obj.Pkg()) { if pref := pkgFirstElem(obj.Pkg()); pref == "Java" || pref == "ObjC" {
continue continue
} }
switch obj := obj.(type) { switch obj := obj.(type) {
@ -142,14 +142,27 @@ func isRefType(t types.Type) bool {
} }
} }
func isJavaType(t types.Type) bool { func typePkgFirstElem(t types.Type) string {
nt, ok := t.(*types.Named) nt, ok := t.(*types.Named)
if !ok { if !ok {
return false return ""
} }
return isJavaPkg(nt.Obj().Pkg()) return pkgFirstElem(nt.Obj().Pkg())
} }
func isJavaPkg(p *types.Package) bool { func pkgFirstElem(p *types.Package) string {
return p != nil && strings.HasPrefix(p.Path(), "Java/") if p == nil {
return ""
}
path := p.Path()
idx := strings.Index(path, "/")
if idx == -1 {
return ""
}
return path[:idx]
}
func isWrapperType(t types.Type) bool {
e := typePkgFirstElem(t)
return e == "Java" || e == "ObjC"
} }

View File

@ -119,7 +119,7 @@ func genPkg(p *types.Package, allPkg []*types.Package, classes []*java.Class) {
}, },
Prefix: *prefix, Prefix: *prefix,
} }
g.Init() g.Init(nil)
w, closer := writer(gohname) w, closer := writer(gohname)
processErr(g.GenGoH()) processErr(g.GenGoH())

View File

@ -220,7 +220,7 @@ func (b *binder) GenObjc(pkg *types.Package, allPkg []*types.Package, outdir str
}, },
Prefix: bindPrefix, Prefix: bindPrefix,
} }
g.Init() g.Init(wrappers)
generate := func(w io.Writer) error { generate := func(w io.Writer) error {
if buildX { if buildX {