seq_android.go.support 2.7 KB

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