Skip to content

Commit 51389df

Browse files
committed
bind/objc: use lowercase for method names.
Fixes golang/go#12889. Change-Id: I4b8f5e4b2c4fe53146fc351889664cbeb5a1860b Reviewed-on: https://go-review.googlesource.com/15780 Reviewed-by: David Crawshaw <[email protected]>
1 parent 090457a commit 51389df

19 files changed

+276
-246
lines changed

bind/genobjc.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ import (
1111
"go/types"
1212
"math"
1313
"strings"
14+
"unicode"
15+
"unicode/utf8"
1416
)
1517

18+
// TODO(hyangah): handle method name conflicts.
19+
// - struct with SetF method and exported F field.
20+
// - method names conflicting with NSObject methods. e.g. Init
21+
// - interface type with InitWithRef.
22+
1623
// TODO(hyangah): error code/domain propagation
1724

1825
type objcGen struct {
@@ -132,10 +139,11 @@ func (g *objcGen) genH() error {
132139
g.Printf("@interface %s : NSObject \n", g.namePrefix)
133140
for _, obj := range g.vars {
134141
objcType := g.objcType(obj.Type())
135-
g.Printf("+ (%s) %s;\n", objcType, obj.Name())
142+
g.Printf("+ (%s) %s;\n", objcType, lowerFirst(obj.Name()))
136143
g.Printf("+ (void) set%s:(%s)v;\n", obj.Name(), objcType)
144+
g.Printf("\n")
137145
}
138-
g.Printf("@end\n")
146+
g.Printf("@end\n\n")
139147
}
140148

141149
// static functions.
@@ -173,7 +181,7 @@ func (g *objcGen) genM() error {
173181
g.Printf("\n")
174182

175183
g.Printf("@protocol goSeqRefInterface\n")
176-
g.Printf("-(GoSeqRef*) ref;\n")
184+
g.Printf("-(GoSeqRef*) _ref;\n")
177185
g.Printf("@end\n")
178186
g.Printf("\n")
179187

@@ -257,7 +265,7 @@ func (g *objcGen) genVarM(o *types.Var) {
257265

258266
// getter
259267
s2 := &funcSummary{
260-
name: o.Name(),
268+
name: lowerFirst(o.Name()),
261269
ret: objcType,
262270
retParams: []paramInfo{{typ: o.Type(), name: "ret"}},
263271
}
@@ -418,7 +426,7 @@ func (s *funcSummary) asMethod(g *objcGen) string {
418426
params = append(params, fmt.Sprintf("%s:(%s)%s", key, g.objcType(p.typ)+"*", p.name))
419427
}
420428
}
421-
return fmt.Sprintf("(%s)%s%s", s.ret, s.name, strings.Join(params, " "))
429+
return fmt.Sprintf("(%s)%s%s", s.ret, lowerFirst(s.name), strings.Join(params, " "))
422430
}
423431

424432
func (s *funcSummary) callMethod(g *objcGen) string {
@@ -439,7 +447,7 @@ func (s *funcSummary) callMethod(g *objcGen) string {
439447
params = append(params, fmt.Sprintf("%s:&%s", key, p.name))
440448
}
441449
}
442-
return fmt.Sprintf("%s%s", s.name, strings.Join(params, " "))
450+
return fmt.Sprintf("%s%s", lowerFirst(s.name), strings.Join(params, " "))
443451
}
444452

445453
func (s *funcSummary) returnsVal() bool {
@@ -479,7 +487,7 @@ func (g *objcGen) genGetter(desc string, f *types.Var) {
479487
t = types.Typ[types.String]
480488
}
481489
s := &funcSummary{
482-
name: f.Name(),
490+
name: lowerFirst(f.Name()),
483491
ret: g.objcType(t),
484492
retParams: []paramInfo{{typ: t, name: "ret_"}},
485493
}
@@ -513,15 +521,15 @@ func (g *objcGen) genFunc(pkgDesc, callDesc string, s *funcSummary, isMethod boo
513521
g.Printf("GoSeq in_ = {};\n")
514522
g.Printf("GoSeq out_ = {};\n")
515523
if isMethod {
516-
g.Printf("go_seq_writeRef(&in_, self.ref);\n")
524+
g.Printf("go_seq_writeRef(&in_, self._ref);\n")
517525
}
518526
for _, p := range s.params {
519527
st := g.seqType(p.typ)
520528
if st == "Ref" {
521529
g.Printf("if ([(id<NSObject>)(%s) isKindOfClass:[%s class]]) {\n", p.name, g.refTypeBase(p.typ))
522530
g.Indent()
523531
g.Printf("id<goSeqRefInterface> %[1]s_proxy = (id<goSeqRefInterface>)(%[1]s);\n", p.name)
524-
g.Printf("go_seq_writeRef(&in_, %s_proxy.ref);\n", p.name)
532+
g.Printf("go_seq_writeRef(&in_, %s_proxy._ref);\n", p.name)
525533
g.Outdent()
526534
g.Printf("} else {\n")
527535
g.Indent()
@@ -600,7 +608,7 @@ func (g *objcGen) genInterfaceInterface(obj *types.TypeName, summary ifaceSummar
600608
g.Printf(" <%[1]s%[2]s>", g.namePrefix, obj.Name())
601609
}
602610
g.Printf(" {\n}\n")
603-
g.Printf("@property(strong, readonly) id ref;\n")
611+
g.Printf("@property(strong, readonly) id _ref;\n")
604612
g.Printf("\n")
605613
g.Printf("- (id)initWithRef:(id)ref;\n")
606614
for _, m := range summary.callable {
@@ -647,7 +655,7 @@ func (g *objcGen) genInterfaceM(obj *types.TypeName, t *types.Interface) bool {
647655
g.Printf("- (id)initWithRef:(id)ref {\n")
648656
g.Indent()
649657
g.Printf("self = [super init];\n")
650-
g.Printf("if (self) { _ref = ref; }\n")
658+
g.Printf("if (self) { __ref = ref; }\n")
651659
g.Printf("return self;\n")
652660
g.Outdent()
653661
g.Printf("}\n")
@@ -734,7 +742,7 @@ func (g *objcGen) genInterfaceMethodProxy(obj *types.TypeName, s *funcSummary) {
734742
g.Printf("if ([(id<NSObject>)(returnVal) isKindOfClass:[%s class]]) {\n", g.refTypeBase(p.typ))
735743
g.Indent()
736744
g.Printf("id<goSeqRefInterface>retVal_proxy = (id<goSeqRefInterface>)(returnVal);\n")
737-
g.Printf("go_seq_writeRef(out, retVal_proxy.ref);\n")
745+
g.Printf("go_seq_writeRef(out, retVal_proxy._ref);\n")
738746
g.Outdent()
739747
g.Printf("} else {\n")
740748
g.Indent()
@@ -772,7 +780,7 @@ func (g *objcGen) genInterfaceMethodProxy(obj *types.TypeName, s *funcSummary) {
772780
g.Printf("if ([(id<NSObject>)(%s) isKindOfClass:[%s class]]) {\n", p.name, g.refTypeBase(p.typ))
773781
g.Indent()
774782
g.Printf("id<goSeqRefInterface>%[1]s_proxy = (id<goSeqRefInterface>)(%[1]s);\n", p.name)
775-
g.Printf("go_seq_writeRef(out, %s_proxy.ref);\n", p.name)
783+
g.Printf("go_seq_writeRef(out, %s_proxy._ref);\n", p.name)
776784
g.Outdent()
777785
g.Printf("} else {\n")
778786
g.Indent()
@@ -788,21 +796,21 @@ func (g *objcGen) genInterfaceMethodProxy(obj *types.TypeName, s *funcSummary) {
788796
func (g *objcGen) genStructH(obj *types.TypeName, t *types.Struct) {
789797
g.Printf("@interface %s%s : NSObject {\n", g.namePrefix, obj.Name())
790798
g.Printf("}\n")
791-
g.Printf("@property(strong, readonly) id ref;\n")
799+
g.Printf("@property(strong, readonly) id _ref;\n")
792800
g.Printf("\n")
793801
g.Printf("- (id)initWithRef:(id)ref;\n")
794802

795803
// accessors to exported fields.
796804
for _, f := range exportedFields(t) {
797805
name, typ := f.Name(), g.objcFieldType(f.Type())
798-
g.Printf("- (%s)%s;\n", typ, name)
806+
g.Printf("- (%s)%s;\n", typ, lowerFirst(name))
799807
g.Printf("- (void)set%s:(%s)v;\n", name, typ)
800808
}
801809

802810
// exported methods
803811
for _, m := range exportedMethodSet(types.NewPointer(obj.Type())) {
804812
s := g.funcSummary(m)
805-
g.Printf("- %s;\n", s.asMethod(g))
813+
g.Printf("- %s;\n", lowerFirst(s.asMethod(g)))
806814
}
807815
g.Printf("@end\n")
808816
}
@@ -827,7 +835,7 @@ func (g *objcGen) genStructM(obj *types.TypeName, t *types.Struct) {
827835
g.Printf("- (id)initWithRef:(id)ref {\n")
828836
g.Indent()
829837
g.Printf("self = [super init];\n")
830-
g.Printf("if (self) { _ref = ref; }\n")
838+
g.Printf("if (self) { __ref = ref; }\n")
831839
g.Printf("return self;\n")
832840
g.Outdent()
833841
g.Printf("}\n\n")
@@ -958,3 +966,11 @@ func (g *objcGen) objcType(typ types.Type) string {
958966
return "TODO"
959967
}
960968
}
969+
970+
func lowerFirst(s string) string {
971+
if s == "" {
972+
return ""
973+
}
974+
r, n := utf8.DecodeRuneInString(s)
975+
return string(unicode.ToLower(r)) + s[n:]
976+
}

bind/objc/SeqTest.m

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ void testStruct() {
113113
ERROR(@"GoTestpkgNewS returned NULL");
114114
}
115115

116-
double x = [s X];
117-
double y = [s Y];
118-
double sum = [s Sum];
116+
double x = [s x];
117+
double y = [s y];
118+
double sum = [s sum];
119119
if (x != 10.0 || y != 100.0 || sum != 110.0) {
120120
ERROR(@"GoTestpkgS(10.0, 100.0).X=%f Y=%f SUM=%f; want 10, 100, 110", x, y,
121121
sum);
@@ -128,16 +128,16 @@ void testStruct() {
128128

129129
[s setX:7];
130130
[s setY:70];
131-
x = [s X];
132-
y = [s Y];
133-
sum = [s Sum];
131+
x = [s x];
132+
y = [s y];
133+
sum = [s sum];
134134
if (x != 7 || y != 70 || sum != 77) {
135135
ERROR(@"GoTestpkgS(7, 70).X=%f Y=%f SUM=%f; want 7, 70, 77", x, y, sum);
136136
}
137137

138138
NSString *first = @"trytwotested";
139139
NSString *second = @"test";
140-
NSString *got = [s TryTwoStrings:first second:second];
140+
NSString *got = [s tryTwoStrings:first second:second];
141141
NSString *want = [first stringByAppendingString:second];
142142
if (![got isEqualToString:want]) {
143143
ERROR(@"GoTestpkgS_TryTwoStrings(%@, %@)= %@; want %@", first, second, got,
@@ -152,8 +152,9 @@ @interface Number : NSObject <GoTestpkgI> {
152152
}
153153
@property int32_t value;
154154

155-
- (BOOL)Error:(BOOL)e error:(NSError **)error;
156-
- (int64_t)Times:(int32_t)v;
155+
// TODO(hyangah): error:error is not good.
156+
- (BOOL)error:(BOOL)e error:(NSError **)error;
157+
- (int64_t)times:(int32_t)v;
157158
@end
158159

159160
// numI is incremented when the first numI objective-C implementation is
@@ -164,7 +165,7 @@ @implementation Number {
164165
}
165166
@synthesize value;
166167

167-
- (BOOL)StringError:(NSString *)s
168+
- (BOOL)stringError:(NSString *)s
168169
ret0_:(NSString **)ret0_
169170
error:(NSError **)error {
170171
if ([s isEqualTo:@"number"]) {
@@ -176,7 +177,7 @@ - (BOOL)StringError:(NSString *)s
176177
return false;
177178
}
178179

179-
- (BOOL)Error:(BOOL)triggerError error:(NSError **)error {
180+
- (BOOL)error:(BOOL)triggerError error:(NSError **)error {
180181
if (!triggerError) {
181182
return YES;
182183
}
@@ -186,7 +187,7 @@ - (BOOL)Error:(BOOL)triggerError error:(NSError **)error {
186187
return NO;
187188
}
188189

189-
- (int64_t)Times:(int32_t)v {
190+
- (int64_t)times:(int32_t)v {
190191
return v * value;
191192
}
192193

@@ -200,9 +201,9 @@ - (void)dealloc {
200201
void testInterface() {
201202
// Test Go object implementing testpkg.I is handled correctly.
202203
id<GoTestpkgI> goObj = GoTestpkgNewI();
203-
int64_t got = [goObj Times:10];
204+
int64_t got = [goObj times:10];
204205
if (got != 100) {
205-
ERROR(@"GoTestpkgNewI().Times(10) = %lld; want %d", got, 100);
206+
ERROR(@"GoTestpkgNewI().times(10) = %lld; want %d", got, 100);
206207
}
207208
int32_t key = -1;
208209
GoTestpkgRegisterI(key, goObj);
@@ -272,49 +273,49 @@ void testIssue12403() {
272273
}
273274

274275
void testVar() {
275-
NSString *s = GoTestpkg.StringVar;
276+
NSString *s = GoTestpkg.stringVar;
276277
if (![s isEqualToString:@"a string var"]) {
277278
ERROR(@"GoTestpkg.StringVar = %@, want 'a string var'", s);
278279
}
279280
s = @"a new string var";
280-
GoTestpkg.StringVar = s;
281-
NSString *s2 = GoTestpkg.StringVar;
281+
GoTestpkg.stringVar = s;
282+
NSString *s2 = GoTestpkg.stringVar;
282283
if (![s2 isEqualToString:s]) {
283-
ERROR(@"GoTestpkg.StringVar = %@, want %@", s2, s);
284+
ERROR(@"GoTestpkg.stringVar = %@, want %@", s2, s);
284285
}
285286

286-
int64_t i = GoTestpkg.IntVar;
287+
int64_t i = GoTestpkg.intVar;
287288
if (i != 77) {
288-
ERROR(@"GoTestpkg.IntVar = %lld, want 77", i);
289+
ERROR(@"GoTestpkg.intVar = %lld, want 77", i);
289290
}
290-
GoTestpkg.IntVar = 777;
291-
i = GoTestpkg.IntVar;
291+
GoTestpkg.intVar = 777;
292+
i = GoTestpkg.intVar;
292293
if (i != 777) {
293-
ERROR(@"GoTestpkg.IntVar = %lld, want 777", i);
294+
ERROR(@"GoTestpkg.intVar = %lld, want 777", i);
294295
}
295296
[GoTestpkg setIntVar:7777];
296-
i = [GoTestpkg IntVar];
297+
i = [GoTestpkg intVar];
297298
if (i != 7777) {
298-
ERROR(@"GoTestpkg.IntVar = %lld, want 7777", i);
299+
ERROR(@"GoTestpkg.intVar = %lld, want 7777", i);
299300
}
300301

301-
GoTestpkgNode *n0 = GoTestpkg.StructVar;
302-
if (![n0.V isEqualToString:@"a struct var"]) {
303-
ERROR(@"GoTestpkg.StructVar = %@, want 'a struct var'", n0.V);
302+
GoTestpkgNode *n0 = GoTestpkg.structVar;
303+
if (![n0.v isEqualToString:@"a struct var"]) {
304+
ERROR(@"GoTestpkg.structVar = %@, want 'a struct var'", n0.v);
304305
}
305306
GoTestpkgNode *n1 = GoTestpkgNewNode(@"a new struct var");
306-
GoTestpkg.StructVar = n1;
307-
GoTestpkgNode *n2 = GoTestpkg.StructVar;
308-
if (![n2.V isEqualToString:@"a new struct var"]) {
309-
ERROR(@"GoTestpkg.StructVar = %@, want 'a new struct var'", n2.V);
307+
GoTestpkg.structVar = n1;
308+
GoTestpkgNode *n2 = GoTestpkg.structVar;
309+
if (![n2.v isEqualToString:@"a new struct var"]) {
310+
ERROR(@"GoTestpkg.StructVar = %@, want 'a new struct var'", n2.v);
310311
}
311312

312313
Number *num = [[Number alloc] init];
313314
num.value = 12345;
314-
GoTestpkg.InterfaceVar = num;
315-
id<GoTestpkgI> iface = GoTestpkg.InterfaceVar;
316-
int64_t x = [iface Times:10];
317-
int64_t y = [num Times:10];
315+
GoTestpkg.interfaceVar = num;
316+
id<GoTestpkgI> iface = GoTestpkg.interfaceVar;
317+
int64_t x = [iface times:10];
318+
int64_t y = [num times:10];
318319
if (x != y) {
319320
ERROR(@"GoTestpkg.InterfaceVar Times 10 = %lld, want %lld", x, y);
320321
}

bind/objc/testpkg/objc_testpkg/GoTestpkg.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,33 @@
1313
@class GoTestpkgS;
1414

1515
@protocol GoTestpkgI
16-
- (BOOL)Error:(BOOL)triggerError error:(NSError**)error;
17-
- (BOOL)StringError:(NSString*)s ret0_:(NSString**)ret0_ error:(NSError**)error;
18-
- (int64_t)Times:(int32_t)v;
16+
- (BOOL)error:(BOOL)triggerError error:(NSError**)error;
17+
- (BOOL)stringError:(NSString*)s ret0_:(NSString**)ret0_ error:(NSError**)error;
18+
- (int64_t)times:(int32_t)v;
1919
@end
2020

2121
@interface GoTestpkgNode : NSObject {
2222
}
23-
@property(strong, readonly) id ref;
23+
@property(strong, readonly) id _ref;
2424

2525
- (id)initWithRef:(id)ref;
26-
- (NSString*)V;
26+
- (NSString*)v;
2727
- (void)setV:(NSString*)v;
28-
- (NSString*)Err;
28+
- (NSString*)err;
2929
- (void)setErr:(NSString*)v;
3030
@end
3131

3232
@interface GoTestpkgS : NSObject {
3333
}
34-
@property(strong, readonly) id ref;
34+
@property(strong, readonly) id _ref;
3535

3636
- (id)initWithRef:(id)ref;
37-
- (double)X;
37+
- (double)x;
3838
- (void)setX:(double)v;
39-
- (double)Y;
39+
- (double)y;
4040
- (void)setY:(double)v;
41-
- (double)Sum;
42-
- (NSString*)TryTwoStrings:(NSString*)first second:(NSString*)second;
41+
- (double)sum;
42+
- (NSString*)tryTwoStrings:(NSString*)first second:(NSString*)second;
4343
@end
4444

4545
FOUNDATION_EXPORT const BOOL GoTestpkgABool;
@@ -57,16 +57,16 @@ FOUNDATION_EXPORT const float GoTestpkgSmallestNonzeroFloat32;
5757
FOUNDATION_EXPORT const double GoTestpkgSmallestNonzeroFloat64;
5858

5959
@interface GoTestpkg : NSObject
60-
+ (int) IntVar;
60+
+ (int) intVar;
6161
+ (void) setIntVar:(int)v;
6262

63-
+ (id<GoTestpkgI>) InterfaceVar;
63+
+ (id<GoTestpkgI>) interfaceVar;
6464
+ (void) setInterfaceVar:(id<GoTestpkgI>)v;
6565

66-
+ (NSString*) StringVar;
66+
+ (NSString*) stringVar;
6767
+ (void) setStringVar:(NSString*)v;
6868

69-
+ (GoTestpkgNode*) StructVar;
69+
+ (GoTestpkgNode*) structVar;
7070
+ (void) setStructVar:(GoTestpkgNode*)v;
7171

7272
@end

0 commit comments

Comments
 (0)