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 <elias.naur@gmail.com>
This commit is contained in:
Tadas 2018-09-17 17:12:07 +00:00 committed by Elias Naur
parent 18ac050e90
commit 3409effab8
3 changed files with 69 additions and 2 deletions

View File

@ -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 {

View File

@ -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))
}

View File

@ -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