github.com/danbrough/mobile@v0.0.3-beta03/bind/java/seq_support.go.support (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 6 //go:build linux || windows || android 7 // +build windows linux android 8 9 package main 10 11 // Go support functions for bindings. This file is copied into the 12 // generated main package and compiled along with the generated binding 13 // files. 14 15 //#cgo CFLAGS: -Werror 16 //#cgo android LDFLAGS: -llog 17 //#include <jni.h> 18 //#include <stdint.h> 19 //#include <stdlib.h> 20 //#include "seq_support.h" 21 import "C" 22 import ( 23 "unsafe" 24 25 "github.com/danbrough/mobile/bind/seq" 26 ) 27 28 // DestroyRef is called by Java to inform Go it is done with a reference. 29 //export DestroyRef 30 func DestroyRef(refnum C.int32_t) { 31 seq.Delete(int32(refnum)) 32 } 33 34 // encodeString returns a copy of a Go string as a UTF16 encoded nstring. 35 // The returned data is freed in go_seq_to_java_string. 36 // 37 // encodeString uses UTF16 as the intermediate format. Note that UTF8 is an obvious 38 // alternative, but JNI only supports a C-safe variant of UTF8 (modified UTF8). 39 func encodeString(s string) C.nstring { 40 n := C.int(len(s)) 41 if n == 0 { 42 return C.nstring{} 43 } 44 // Allocate enough for the worst case estimate, every character is a surrogate pair 45 worstCaseLen := 4 * len(s) 46 utf16buf := C.malloc(C.size_t(worstCaseLen)) 47 if utf16buf == nil { 48 panic("encodeString: malloc failed") 49 } 50 chars := (*[1<<30 - 1]uint16)(unsafe.Pointer(utf16buf))[:worstCaseLen/2 : worstCaseLen/2] 51 nchars := seq.UTF16Encode(s, chars) 52 return C.nstring{chars: unsafe.Pointer(utf16buf), len: C.jsize(nchars*2)} 53 } 54 55 // decodeString decodes a UTF8 encoded nstring to a Go string. The data 56 // in str is freed after use. 57 func decodeString(str C.nstring) string { 58 if str.chars == nil { 59 return "" 60 } 61 chars := (*[1<<31 - 1]byte)(str.chars)[:str.len] 62 s := string(chars) 63 C.free(str.chars) 64 return s 65 } 66 67 // fromSlice converts a slice to a jbyteArray cast as a nbyteslice. If cpy 68 // is set, the returned slice is a copy to be free by go_seq_to_java_bytearray. 69 func fromSlice(s []byte, cpy bool) C.nbyteslice { 70 if s == nil || len(s) == 0 { 71 return C.nbyteslice{} 72 } 73 var ptr *C.jbyte 74 n := C.jsize(len(s)) 75 if cpy { 76 ptr = (*C.jbyte)(C.malloc(C.size_t(n))) 77 if ptr == nil { 78 panic("fromSlice: malloc failed") 79 } 80 copy((*[1<<31 - 1]byte)(unsafe.Pointer(ptr))[:n], s) 81 } else { 82 ptr = (*C.jbyte)(unsafe.Pointer(&s[0])) 83 } 84 return C.nbyteslice{ptr: unsafe.Pointer(ptr), len: n} 85 } 86 87 // toSlice takes a nbyteslice (jbyteArray) and returns a byte slice 88 // with the data. If cpy is set, the slice contains a copy of the data and is 89 // freed. 90 func toSlice(s C.nbyteslice, cpy bool) []byte { 91 if s.ptr == nil || s.len == 0 { 92 return nil 93 } 94 var b []byte 95 if cpy { 96 b = C.GoBytes(s.ptr, C.int(s.len)) 97 C.free(s.ptr) 98 } else { 99 b = (*[1<<31 - 1]byte)(unsafe.Pointer(s.ptr))[:s.len:s.len] 100 } 101 return b 102 }