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