2
0
mirror of synced 2025-02-23 06:48:15 +00:00
mobile/bind/objc/seq_darwin.go
Hyang-Ah (Hana) Kim 5c3e18f08f bind/objc: obj-c binding for functions using numeric, string types.
test.bash is a simple script that builds the c-shared library from
test_main.go, compiles SeqTest.m and the objective-c bindings of testpkg
(testpkg/objc_testpkg/GoTestpkg.*), and runs the output. Eventually this
will be replaced with coed that runs gomobile bind & xcodebuild.

The code under testpkg/go_testpkg and testpkg/objc_testpkg is the output
of gobind (now manually-generated). I am adding it to repo now in order
to get the testpkg/objc_tstpkg reviewed. Eventually, this will be
removed from the repo.

Change-Id: I8d6af3732337992af922cb4615a63f385e19d489
Reviewed-on: https://go-review.googlesource.com/9826
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-05-13 22:28:22 +00:00

79 lines
1.7 KiB
Go

// Copyright 2015 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
/*
#cgo LDFLAGS: -framework Foundation
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
*/
import "C"
import (
"fmt"
"unsafe"
"golang.org/x/mobile/bind/seq"
)
const debug = true
const maxSliceLen = 1<<31 - 1
//export Send
func Send(descriptor string, code int, req *C.uint8_t, reqlen C.size_t, res **C.uint8_t, reslen *C.size_t) {
fn := seq.Registry[descriptor][code]
if fn == nil {
panic(fmt.Sprintf("invalid descriptor(%s) and code(0x%x)", descriptor, code))
}
var in, out *seq.Buffer
if reqlen > 0 {
in = &seq.Buffer{Data: (*[maxSliceLen]byte)(unsafe.Pointer(req))[:reqlen]}
}
if reslen != nil {
out = new(seq.Buffer)
}
fn(out, in)
if out != nil {
// sender does not expect any results.
seqToBuf(res, reslen, out)
}
}
func seqToBuf(bufptr **C.uint8_t, lenptr *C.size_t, buf *seq.Buffer) {
if debug {
fmt.Printf("seqToBuf tag 1, len(buf.Data)=%d, *lenptr=%d\n", len(buf.Data), *lenptr)
}
if len(buf.Data) == 0 {
*lenptr = 0
return
}
if len(buf.Data) > int(*lenptr) {
// TODO(crawshaw): realloc
C.free(unsafe.Pointer(*bufptr))
m := C.malloc(C.size_t(len(buf.Data)))
if uintptr(m) == 0 {
panic(fmt.Sprintf("malloc failed, size=%d", len(buf.Data)))
}
*bufptr = (*C.uint8_t)(m)
*lenptr = C.size_t(len(buf.Data))
}
C.memcpy(unsafe.Pointer(*bufptr), unsafe.Pointer(&buf.Data[0]), C.size_t(len(buf.Data)))
}
func init() {
// TODO: seq.FinalizeRef, seq.Transact.
seq.EncString = func(out *seq.Buffer, v string) {
out.WriteUTF8(v)
}
seq.DecString = func(in *seq.Buffer) string {
return in.ReadUTF8()
}
}