From 3409effab8d1c46107aa098c73321533c4b6dc58 Mon Sep 17 00:00:00 2001 From: Tadas Date: Mon, 17 Sep 2018 17:12:07 +0000 Subject: [PATCH] bind: pass correct arguments to fromSlice and toSlice Fixes golang/go#27705 Change-Id: I6d26b02b49a9904a4d949e5ca5a9d234f6092ea4 GitHub-Last-Rev: 9b9314fed184247f69a5f7e567f502b22aa5b135 GitHub-Pull-Request: golang/mobile#22 Reviewed-on: https://go-review.googlesource.com/135423 Reviewed-by: Elias Naur --- bind/genobjcw.go | 4 +-- bind/testdata/objcw.go | 13 +++++++++ bind/testdata/objcw.go.golden | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/bind/genobjcw.go b/bind/genobjcw.go index 94152c6..ba5d5de 100644 --- a/bind/genobjcw.go +++ b/bind/genobjcw.go @@ -473,7 +473,7 @@ func (g *ObjcWrapper) genWrite(a *objc.Param) { case objc.String: g.Printf("_%s := encodeString(%s)\n", a.Name, a.Name) case objc.Data: - g.Printf("_%s := fromSlice(%s, 0)\n", a.Name, a.Name) + g.Printf("_%s := fromSlice(%s, false)\n", a.Name, a.Name) case objc.Bool: g.Printf("_%s := %s(0)\n", a.Name, g.cgoType(a.Type)) g.Printf("if %s {\n", a.Name) @@ -500,7 +500,7 @@ func (g *ObjcWrapper) genRead(to, from string, t *objc.Type) { case objc.String: g.Printf("%s := decodeString(%s)\n", to, from) case objc.Data: - g.Printf("%s := toSlice(%s, 1)\n", to, from) + g.Printf("%s := toSlice(%s, true)\n", to, from) case objc.Protocol, objc.Class: var proxyName string if n := g.lookupImported(t); n != nil { diff --git a/bind/testdata/objcw.go b/bind/testdata/objcw.go index 136c9c9..e1d104a 100644 --- a/bind/testdata/objcw.go +++ b/bind/testdata/objcw.go @@ -6,6 +6,7 @@ package objc import ( "ObjC/Foundation" + "ObjC/Foundation/NSMutableString" "ObjC/UIKit" ) @@ -37,3 +38,15 @@ type GoUIResponder struct { func (r *GoUIResponder) PressesBegan(_ Foundation.NSSet, _ UIKit.UIPressesEvent) { } + +const NSUTF8StringEncoding = 8 + +func CreateReadNSMutableString() { + myData := []byte{'A', 'B'} + // Test byte slices. Use NSMutableString because NSString is + // represented as Go strings in bindings. + // Pass slice from Go to native. + mString := NSMutableString.NewWithData(myData, uint(NSUTF8StringEncoding)) + // Pass slice from native to Go. + _ = mString.DataUsingEncoding(uint(NSUTF8StringEncoding)) +} diff --git a/bind/testdata/objcw.go.golden b/bind/testdata/objcw.go.golden index dc5ba55..d28a62b 100644 --- a/bind/testdata/objcw.go.golden +++ b/bind/testdata/objcw.go.golden @@ -25,6 +25,12 @@ type Foundation_NSSet interface { Description() (string) } +type Foundation_NSMutableString interface { + Hash() (uint) + Description() (string) + DataUsingEncoding(encoding uint) ([]byte) +} + type UIKit_UIResponder interface { Hash() (uint) Description() (string) @@ -62,6 +68,7 @@ import "C" import "ObjC" import _seq "golang.org/x/mobile/bind/seq" +import "ObjC/Foundation/NSMutableString" type proxy interface { Bind_proxy_refnum__() int32 } @@ -146,6 +153,48 @@ func (p *proxy_class_NSSet) Description() (string) { return _res } +func init() { + NSMutableString.NewWithData = func(data []byte, encoding uint) (ObjC.Foundation_NSMutableString) { + _data := fromSlice(data, false) + _encoding := C.ulong(encoding) + res := C.cproxy_s_NSMutableString_NewWithData(_data, _encoding) + var _res ObjC.Foundation_NSMutableString + _res_ref := _seq.FromRefNum(int32(res)) + if _res_ref != nil { + if res < 0 { // go object + _res = _res_ref.Get().(ObjC.Foundation_NSMutableString) + } else { // foreign object + _res = (*proxy_class_NSMutableString)(_res_ref) + } + } + return _res + } + +} + +type proxy_class_NSMutableString _seq.Ref + +func (p *proxy_class_NSMutableString) Bind_proxy_refnum__() int32 { return (*_seq.Ref)(p).Bind_IncNum() } + +func (p *proxy_class_NSMutableString) Hash() (uint) { + res := C.cproxy_NSMutableString_Hash(C.int(p.Bind_proxy_refnum__())) + _res := uint(res) + return _res +} + +func (p *proxy_class_NSMutableString) Description() (string) { + res := C.cproxy_NSMutableString_Description(C.int(p.Bind_proxy_refnum__())) + _res := decodeString(res) + return _res +} + +func (p *proxy_class_NSMutableString) DataUsingEncoding(encoding uint) ([]byte) { + _encoding := C.ulong(encoding) + res := C.cproxy_NSMutableString_DataUsingEncoding(C.int(p.Bind_proxy_refnum__()), _encoding) + _res := toSlice(res, true) + return _res +} + func init() { } @@ -500,6 +549,11 @@ func new_objc_GoUIResponder() C.int32_t { return C.int32_t(_seq.ToRefNum(new(objc.GoUIResponder))) } +//export proxyobjc__CreateReadNSMutableString +func proxyobjc__CreateReadNSMutableString() { + objc.CreateReadNSMutableString() +} + //export proxyobjc__DupNSDate func proxyobjc__DupNSDate(param_date C.int32_t) C.int32_t { var _param_date Foundation.NSDate