bind/objc: change exported var accessors to class methods.
for example,
package testpkg
var AnInt int64
will be mapped to
@interface GoTestpkg: NSObject
+ (int64_t) AnInt;
+ (void) setAnInt:(int64_t)v;
@end
Followup of cl/15340
Update golang/go#12475
Change-Id: Ie26c92af977fc3dd62dcad2b10c6a5c1c1b8941b
Reviewed-on: https://go-review.googlesource.com/15770
Reviewed-by: David Crawshaw <crawshaw@golang.org>
diff --git a/bind/genobjc.go b/bind/genobjc.go
index 441d355..f6d1183 100644
--- a/bind/genobjc.go
+++ b/bind/genobjc.go
@@ -114,6 +114,7 @@
}
// const
+ // TODO: prefix with k?, or use a class method?
for _, obj := range g.constants {
switch b := obj.Type().(*types.Basic); b.Kind() {
case types.String, types.UntypedString:
@@ -127,11 +128,14 @@
}
// var
- for _, obj := range g.vars {
- objcType := g.objcType(obj.Type())
- g.Printf("FOUNDATION_EXPORT void %s_set%s(%s v);\n", g.namePrefix, obj.Name(), objcType)
- g.Printf("FOUNDATION_EXPORT %s %s%s();\n", objcType, g.namePrefix, obj.Name())
- g.Printf("\n")
+ if len(g.vars) > 0 {
+ g.Printf("@interface %s : NSObject \n", g.namePrefix)
+ for _, obj := range g.vars {
+ objcType := g.objcType(obj.Type())
+ g.Printf("+ (%s) %s;\n", objcType, obj.Name())
+ g.Printf("+ (void) set%s:(%s)v;\n", obj.Name(), objcType)
+ }
+ g.Printf("@end\n")
}
// static functions.
@@ -203,11 +207,12 @@
}
// vars
- for _, o := range g.vars {
- g.genVarM(o)
- }
if len(g.vars) > 0 {
- g.Printf("\n")
+ g.Printf("@implementation %s\n", g.namePrefix)
+ for _, o := range g.vars {
+ g.genVarM(o)
+ }
+ g.Printf("@end\n\n")
}
// global functions.
@@ -240,23 +245,23 @@
// setter
s1 := &funcSummary{
- name: g.namePrefix + "_set" + o.Name(),
+ name: "set" + o.Name(),
ret: "void",
params: []paramInfo{{typ: o.Type(), name: "v"}},
}
- g.Printf("void %s(%s v) {\n", s1.name, objcType)
+ g.Printf("+ (void) %s:(%s)v {\n", s1.name, objcType)
g.Indent()
- g.genFunc(varDesc, "1", s1, false)
+ g.genFunc(varDesc, "1", s1, false) // false: not instance method.
g.Outdent()
g.Printf("}\n\n")
// getter
s2 := &funcSummary{
- name: g.namePrefix + o.Name(),
+ name: o.Name(),
ret: objcType,
retParams: []paramInfo{{typ: o.Type(), name: "ret"}},
}
- g.Printf("%s %s() {\n", s2.ret, s2.name)
+ g.Printf("+ (%s) %s {\n", s2.ret, s2.name)
g.Indent()
g.genFunc(varDesc, "2", s2, false)
g.Outdent()
diff --git a/bind/objc/SeqTest.m b/bind/objc/SeqTest.m
index 77a1281..6943b1f 100644
--- a/bind/objc/SeqTest.m
+++ b/bind/objc/SeqTest.m
@@ -272,46 +272,51 @@
}
void testVar() {
- NSString *s = GoTestpkgStringVar();
+ NSString *s = GoTestpkg.StringVar;
if (![s isEqualToString:@"a string var"]) {
- ERROR(@"GoTestpkgStringVar = %@, want 'a string var'", s);
+ ERROR(@"GoTestpkg.StringVar = %@, want 'a string var'", s);
}
s = @"a new string var";
- GoTestpkg_setStringVar(s);
- NSString *s2 = GoTestpkgStringVar();
+ GoTestpkg.StringVar = s;
+ NSString *s2 = GoTestpkg.StringVar;
if (![s2 isEqualToString:s]) {
- ERROR(@"GoTestpkgStringVar = %@, want %@", s2, s);
+ ERROR(@"GoTestpkg.StringVar = %@, want %@", s2, s);
}
- int64_t i = GoTestpkgIntVar();
+ int64_t i = GoTestpkg.IntVar;
if (i != 77) {
- ERROR(@"GoTestpkgIntVar = %lld, want 77", i);
+ ERROR(@"GoTestpkg.IntVar = %lld, want 77", i);
}
- GoTestpkg_setIntVar(777);
- i = GoTestpkgIntVar();
+ GoTestpkg.IntVar = 777;
+ i = GoTestpkg.IntVar;
if (i != 777) {
- ERROR(@"GoTestpkgIntVar = %lld, want 777", i);
+ ERROR(@"GoTestpkg.IntVar = %lld, want 777", i);
+ }
+ [GoTestpkg setIntVar:7777];
+ i = [GoTestpkg IntVar];
+ if (i != 7777) {
+ ERROR(@"GoTestpkg.IntVar = %lld, want 7777", i);
}
- GoTestpkgNode *n0 = GoTestpkgStructVar();
+ GoTestpkgNode *n0 = GoTestpkg.StructVar;
if (![n0.V isEqualToString:@"a struct var"]) {
- ERROR(@"GoTestpkgStructVar = %@, want 'a struct var'", n0.V);
+ ERROR(@"GoTestpkg.StructVar = %@, want 'a struct var'", n0.V);
}
GoTestpkgNode *n1 = GoTestpkgNewNode(@"a new struct var");
- GoTestpkg_setStructVar(n1);
- GoTestpkgNode *n2 = GoTestpkgStructVar();
+ GoTestpkg.StructVar = n1;
+ GoTestpkgNode *n2 = GoTestpkg.StructVar;
if (![n2.V isEqualToString:@"a new struct var"]) {
- ERROR(@"GoTestpkgStructVar = %@, want 'a new struct var'", n2.V);
+ ERROR(@"GoTestpkg.StructVar = %@, want 'a new struct var'", n2.V);
}
Number *num = [[Number alloc] init];
num.value = 12345;
- GoTestpkg_setInterfaceVar(num);
- id<GoTestpkgI> iface = GoTestpkgInterfaceVar();
+ GoTestpkg.InterfaceVar = num;
+ id<GoTestpkgI> iface = GoTestpkg.InterfaceVar;
int64_t x = [iface Times:10];
int64_t y = [num Times:10];
if (x != y) {
- ERROR(@"GoTestpkgInterfaceVar Times 10 = %lld, want %lld", x, y);
+ ERROR(@"GoTestpkg.InterfaceVar Times 10 = %lld, want %lld", x, y);
}
}
diff --git a/bind/objc/testpkg/objc_testpkg/GoTestpkg.h b/bind/objc/testpkg/objc_testpkg/GoTestpkg.h
index 5f4a469..39dcf20 100644
--- a/bind/objc/testpkg/objc_testpkg/GoTestpkg.h
+++ b/bind/objc/testpkg/objc_testpkg/GoTestpkg.h
@@ -56,17 +56,20 @@
FOUNDATION_EXPORT const float GoTestpkgSmallestNonzeroFloat32;
FOUNDATION_EXPORT const double GoTestpkgSmallestNonzeroFloat64;
-FOUNDATION_EXPORT void GoTestpkg_setIntVar(int v);
-FOUNDATION_EXPORT int GoTestpkgIntVar();
+@interface GoTestpkg : NSObject
++ (int) IntVar;
++ (void) setIntVar:(int)v;
-FOUNDATION_EXPORT void GoTestpkg_setInterfaceVar(id<GoTestpkgI> v);
-FOUNDATION_EXPORT id<GoTestpkgI> GoTestpkgInterfaceVar();
++ (id<GoTestpkgI>) InterfaceVar;
++ (void) setInterfaceVar:(id<GoTestpkgI>)v;
-FOUNDATION_EXPORT void GoTestpkg_setStringVar(NSString* v);
-FOUNDATION_EXPORT NSString* GoTestpkgStringVar();
++ (NSString*) StringVar;
++ (void) setStringVar:(NSString*)v;
-FOUNDATION_EXPORT void GoTestpkg_setStructVar(GoTestpkgNode* v);
-FOUNDATION_EXPORT GoTestpkgNode* GoTestpkgStructVar();
++ (GoTestpkgNode*) StructVar;
++ (void) setStructVar:(GoTestpkgNode*)v;
+
+@end
FOUNDATION_EXPORT NSData* GoTestpkgBytesAppend(NSData* a, NSData* b);
diff --git a/bind/objc/testpkg/objc_testpkg/GoTestpkg.m b/bind/objc/testpkg/objc_testpkg/GoTestpkg.m
index 72b96db..1a1a0bb 100644
--- a/bind/objc/testpkg/objc_testpkg/GoTestpkg.m
+++ b/bind/objc/testpkg/objc_testpkg/GoTestpkg.m
@@ -312,7 +312,8 @@
const float GoTestpkgSmallestNonzeroFloat32 = 0;
const double GoTestpkgSmallestNonzeroFloat64 = 5e-324;
-void GoTestpkg_setIntVar(int v) {
+@implementation GoTestpkg
++ (void) setIntVar:(int)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeInt(&in_, v);
@@ -321,7 +322,7 @@
go_seq_free(&out_);
}
-int GoTestpkgIntVar() {
++ (int) IntVar {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("testpkg.IntVar", 2, &in_, &out_);
@@ -331,7 +332,7 @@
return ret;
}
-void GoTestpkg_setInterfaceVar(id<GoTestpkgI> v) {
++ (void) setInterfaceVar:(id<GoTestpkgI>)v {
GoSeq in_ = {};
GoSeq out_ = {};
if ([(id<NSObject>)(v) isKindOfClass:[GoTestpkgI class]]) {
@@ -345,7 +346,7 @@
go_seq_free(&out_);
}
-id<GoTestpkgI> GoTestpkgInterfaceVar() {
++ (id<GoTestpkgI>) InterfaceVar {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("testpkg.InterfaceVar", 2, &in_, &out_);
@@ -359,7 +360,7 @@
return ret;
}
-void GoTestpkg_setStringVar(NSString* v) {
++ (void) setStringVar:(NSString*)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeUTF8(&in_, v);
@@ -368,7 +369,7 @@
go_seq_free(&out_);
}
-NSString* GoTestpkgStringVar() {
++ (NSString*) StringVar {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("testpkg.StringVar", 2, &in_, &out_);
@@ -378,7 +379,7 @@
return ret;
}
-void GoTestpkg_setStructVar(GoTestpkgNode* v) {
++ (void) setStructVar:(GoTestpkgNode*)v {
GoSeq in_ = {};
GoSeq out_ = {};
if ([(id<NSObject>)(v) isKindOfClass:[GoTestpkgNode class]]) {
@@ -392,7 +393,7 @@
go_seq_free(&out_);
}
-GoTestpkgNode* GoTestpkgStructVar() {
++ (GoTestpkgNode*) StructVar {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("testpkg.StructVar", 2, &in_, &out_);
@@ -406,6 +407,7 @@
return ret;
}
+@end
NSData* GoTestpkgBytesAppend(NSData* a, NSData* b) {
GoSeq in_ = {};
diff --git a/bind/testdata/vars.objc.h.golden b/bind/testdata/vars.objc.h.golden
index 7984017..36f3d80 100644
--- a/bind/testdata/vars.objc.h.golden
+++ b/bind/testdata/vars.objc.h.golden
@@ -20,40 +20,30 @@
- (id)initWithRef:(id)ref;
@end
-FOUNDATION_EXPORT void GoVars_setABool(BOOL v);
-FOUNDATION_EXPORT BOOL GoVarsABool();
-
-FOUNDATION_EXPORT void GoVars_setAFloat(double v);
-FOUNDATION_EXPORT double GoVarsAFloat();
-
-FOUNDATION_EXPORT void GoVars_setAFloat32(float v);
-FOUNDATION_EXPORT float GoVarsAFloat32();
-
-FOUNDATION_EXPORT void GoVars_setAFloat64(double v);
-FOUNDATION_EXPORT double GoVarsAFloat64();
-
-FOUNDATION_EXPORT void GoVars_setAString(NSString* v);
-FOUNDATION_EXPORT NSString* GoVarsAString();
-
-FOUNDATION_EXPORT void GoVars_setAStructPtr(GoVarsS* v);
-FOUNDATION_EXPORT GoVarsS* GoVarsAStructPtr();
-
-FOUNDATION_EXPORT void GoVars_setAnInt(int v);
-FOUNDATION_EXPORT int GoVarsAnInt();
-
-FOUNDATION_EXPORT void GoVars_setAnInt16(int16_t v);
-FOUNDATION_EXPORT int16_t GoVarsAnInt16();
-
-FOUNDATION_EXPORT void GoVars_setAnInt32(int32_t v);
-FOUNDATION_EXPORT int32_t GoVarsAnInt32();
-
-FOUNDATION_EXPORT void GoVars_setAnInt64(int64_t v);
-FOUNDATION_EXPORT int64_t GoVarsAnInt64();
-
-FOUNDATION_EXPORT void GoVars_setAnInt8(int8_t v);
-FOUNDATION_EXPORT int8_t GoVarsAnInt8();
-
-FOUNDATION_EXPORT void GoVars_setAnInterface(id<GoVarsI> v);
-FOUNDATION_EXPORT id<GoVarsI> GoVarsAnInterface();
-
+@interface GoVars : NSObject
++ (BOOL) ABool;
++ (void) setABool:(BOOL)v;
++ (double) AFloat;
++ (void) setAFloat:(double)v;
++ (float) AFloat32;
++ (void) setAFloat32:(float)v;
++ (double) AFloat64;
++ (void) setAFloat64:(double)v;
++ (NSString*) AString;
++ (void) setAString:(NSString*)v;
++ (GoVarsS*) AStructPtr;
++ (void) setAStructPtr:(GoVarsS*)v;
++ (int) AnInt;
++ (void) setAnInt:(int)v;
++ (int16_t) AnInt16;
++ (void) setAnInt16:(int16_t)v;
++ (int32_t) AnInt32;
++ (void) setAnInt32:(int32_t)v;
++ (int64_t) AnInt64;
++ (void) setAnInt64:(int64_t)v;
++ (int8_t) AnInt8;
++ (void) setAnInt8:(int8_t)v;
++ (id<GoVarsI>) AnInterface;
++ (void) setAnInterface:(id<GoVarsI>)v;
+@end
#endif
diff --git a/bind/testdata/vars.objc.m.golden b/bind/testdata/vars.objc.m.golden
index 3bb1288..86de8db 100644
--- a/bind/testdata/vars.objc.m.golden
+++ b/bind/testdata/vars.objc.m.golden
@@ -56,7 +56,8 @@
@end
-void GoVars_setABool(BOOL v) {
+@implementation GoVars
++ (void) setABool:(BOOL)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeBool(&in_, v);
@@ -65,7 +66,7 @@
go_seq_free(&out_);
}
-BOOL GoVarsABool() {
++ (BOOL) ABool {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.ABool", 2, &in_, &out_);
@@ -75,7 +76,7 @@
return ret;
}
-void GoVars_setAFloat(double v) {
++ (void) setAFloat:(double)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeFloat64(&in_, v);
@@ -84,7 +85,7 @@
go_seq_free(&out_);
}
-double GoVarsAFloat() {
++ (double) AFloat {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AFloat", 2, &in_, &out_);
@@ -94,7 +95,7 @@
return ret;
}
-void GoVars_setAFloat32(float v) {
++ (void) setAFloat32:(float)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeFloat32(&in_, v);
@@ -103,7 +104,7 @@
go_seq_free(&out_);
}
-float GoVarsAFloat32() {
++ (float) AFloat32 {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AFloat32", 2, &in_, &out_);
@@ -113,7 +114,7 @@
return ret;
}
-void GoVars_setAFloat64(double v) {
++ (void) setAFloat64:(double)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeFloat64(&in_, v);
@@ -122,7 +123,7 @@
go_seq_free(&out_);
}
-double GoVarsAFloat64() {
++ (double) AFloat64 {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AFloat64", 2, &in_, &out_);
@@ -132,7 +133,7 @@
return ret;
}
-void GoVars_setAString(NSString* v) {
++ (void) setAString:(NSString*)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeUTF8(&in_, v);
@@ -141,7 +142,7 @@
go_seq_free(&out_);
}
-NSString* GoVarsAString() {
++ (NSString*) AString {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AString", 2, &in_, &out_);
@@ -151,7 +152,7 @@
return ret;
}
-void GoVars_setAStructPtr(GoVarsS* v) {
++ (void) setAStructPtr:(GoVarsS*)v {
GoSeq in_ = {};
GoSeq out_ = {};
if ([(id<NSObject>)(v) isKindOfClass:[GoVarsS class]]) {
@@ -165,7 +166,7 @@
go_seq_free(&out_);
}
-GoVarsS* GoVarsAStructPtr() {
++ (GoVarsS*) AStructPtr {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AStructPtr", 2, &in_, &out_);
@@ -179,7 +180,7 @@
return ret;
}
-void GoVars_setAnInt(int v) {
++ (void) setAnInt:(int)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeInt(&in_, v);
@@ -188,7 +189,7 @@
go_seq_free(&out_);
}
-int GoVarsAnInt() {
++ (int) AnInt {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AnInt", 2, &in_, &out_);
@@ -198,7 +199,7 @@
return ret;
}
-void GoVars_setAnInt16(int16_t v) {
++ (void) setAnInt16:(int16_t)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeInt16(&in_, v);
@@ -207,7 +208,7 @@
go_seq_free(&out_);
}
-int16_t GoVarsAnInt16() {
++ (int16_t) AnInt16 {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AnInt16", 2, &in_, &out_);
@@ -217,7 +218,7 @@
return ret;
}
-void GoVars_setAnInt32(int32_t v) {
++ (void) setAnInt32:(int32_t)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeInt32(&in_, v);
@@ -226,7 +227,7 @@
go_seq_free(&out_);
}
-int32_t GoVarsAnInt32() {
++ (int32_t) AnInt32 {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AnInt32", 2, &in_, &out_);
@@ -236,7 +237,7 @@
return ret;
}
-void GoVars_setAnInt64(int64_t v) {
++ (void) setAnInt64:(int64_t)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeInt64(&in_, v);
@@ -245,7 +246,7 @@
go_seq_free(&out_);
}
-int64_t GoVarsAnInt64() {
++ (int64_t) AnInt64 {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AnInt64", 2, &in_, &out_);
@@ -255,7 +256,7 @@
return ret;
}
-void GoVars_setAnInt8(int8_t v) {
++ (void) setAnInt8:(int8_t)v {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_writeInt8(&in_, v);
@@ -264,7 +265,7 @@
go_seq_free(&out_);
}
-int8_t GoVarsAnInt8() {
++ (int8_t) AnInt8 {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AnInt8", 2, &in_, &out_);
@@ -274,7 +275,7 @@
return ret;
}
-void GoVars_setAnInterface(id<GoVarsI> v) {
++ (void) setAnInterface:(id<GoVarsI>)v {
GoSeq in_ = {};
GoSeq out_ = {};
if ([(id<NSObject>)(v) isKindOfClass:[GoVarsI class]]) {
@@ -288,7 +289,7 @@
go_seq_free(&out_);
}
-id<GoVarsI> GoVarsAnInterface() {
++ (id<GoVarsI>) AnInterface {
GoSeq in_ = {};
GoSeq out_ = {};
go_seq_send("vars.AnInterface", 2, &in_, &out_);
@@ -302,6 +303,7 @@
return ret;
}
+@end
__attribute__((constructor)) static void init() {
go_seq_register_proxy("go.vars.I", proxyGoVarsI);