bni.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. /**
  6. * @file bni.h
  7. * @date Mon Jul 2 16:54:58 2012
  8. *
  9. * @brief Beihai native interface.
  10. */
  11. #ifndef BNI_H
  12. #define BNI_H
  13. #include "bh_types.h"
  14. /* Primitive types */
  15. typedef uint8 jboolean;
  16. typedef int8 jbyte;
  17. typedef uint16 jchar;
  18. typedef int16 jshort;
  19. typedef int32 jint;
  20. typedef int64 jlong;
  21. typedef float jfloat;
  22. typedef double jdouble;
  23. typedef jint jsize;
  24. /* Predefined Java class types. */
  25. struct _jobject;
  26. typedef struct _jobject *jobject;
  27. struct _jclass;
  28. typedef struct _jclass *jclass;
  29. struct _jstring;
  30. typedef struct _jstring *jstring;
  31. /* Values of jboolean: */
  32. #define BNI_FALSE 0
  33. #define BNI_TRUE 1
  34. /**
  35. * Return the length of the array object.
  36. *
  37. * @param array Java array object
  38. *
  39. * @return the length of the Java array
  40. */
  41. #define bni_array_length(array) ((jsize)((uint32)(array)->__length >> 2))
  42. /**
  43. * Return the address of the first element of array object.
  44. *
  45. * @param array Java array object
  46. *
  47. * @return the address of the first element of array object
  48. */
  49. #define bni_array_elem(array) ((array)->__elem)
  50. /**
  51. * Find the Java class with given class name.
  52. *
  53. * @param name Java class name
  54. *
  55. * @return class object of the Java class if found, NULL otherwise
  56. *
  57. * @throws OutOfMemoryError if VM runs out of memory.
  58. */
  59. jclass
  60. bni_find_class(const char *name);
  61. /**
  62. * Throw an exception of given class with message.
  63. *
  64. * @param clazz class object of a subclass of java.lang.Throwable
  65. * @param msg message for the exception or NULL if no message
  66. *
  67. * @return 0 if succeeds, nonzero otherwise
  68. */
  69. jint
  70. bni_throw_new(jclass clazz, const char *msg);
  71. /**
  72. * Throw a NullPointerException.
  73. *
  74. * @throws NullPointerException
  75. */
  76. void
  77. bni_throw_npe(void);
  78. /**
  79. * Throw an ArrayIndexOutOfBoundsException
  80. *
  81. * @param index the index used to access the array
  82. *
  83. * @throws ArrayIndexOutOfBoundsException
  84. */
  85. void
  86. bni_throw_aioobe(int index);
  87. /**
  88. * Determine whether an exception is being thrown.
  89. *
  90. * @return exception object if exception is thrown, NULL otherwise
  91. */
  92. jobject
  93. bni_exception_occurred(void);
  94. /**
  95. * Print the current exception to error-reporting channel.
  96. */
  97. void
  98. bni_exception_describe(void);
  99. /**
  100. * Clear the currently thrown exception.
  101. */
  102. void
  103. bni_exception_clear(void);
  104. /**
  105. * Return the Unicode character number of a string.
  106. *
  107. * @param str Java string object
  108. *
  109. * @return the Unicode character number of the string
  110. */
  111. jsize
  112. bni_string_length(jstring str);
  113. /**
  114. * Return the length in bytes of the modified UTF-8 representation of
  115. * a string.
  116. *
  117. * @param str Java string object
  118. * @param start start offset in the string
  119. * @param len number of Unicode characters
  120. *
  121. * @return the UTF-8 length of the string
  122. *
  123. * @throws StringIndexOutOfBoundsException on index overflow.
  124. */
  125. jsize
  126. bni_string_utf_length(jstring str, jsize start, jsize len);
  127. /**
  128. * Copies len number of Unicode characters beginning at offset start
  129. * to the given buffer buf.
  130. *
  131. * @param str Java string object
  132. * @param start start offset in the string
  133. * @param len number of Unicode characters to copy
  134. * @param buf buffer for storing the result
  135. */
  136. void
  137. bni_string_region(jstring str, jsize start, jsize len, jchar *buf);
  138. /**
  139. * Translates len number of Unicode characters beginning at offset
  140. * start into modified UTF-8 encoding and place the result in the
  141. * given buffer buf.
  142. *
  143. * @param str Java string object
  144. * @param start start offset in the string
  145. * @param len number of Unicode characters to copy
  146. * @param buf buffer for storing the result
  147. *
  148. * @throws StringIndexOutOfBoundsException on index overflow.
  149. */
  150. void
  151. bni_string_utf_region(jstring str, jsize start, jsize len, char *buf);
  152. /**
  153. * Translate Unicode characters into modified UTF-8 encoding and return
  154. * the result.
  155. *
  156. * @param str Java string object
  157. *
  158. * @return the UTF-8 encoding string if succeeds, NULL otherwise
  159. */
  160. char *
  161. bni_string_get_utf_chars(jstring str);
  162. /**
  163. * Get the given Java object's class index.
  164. *
  165. * @param obj Java object
  166. *
  167. * @return -1 if obj is an array, class index of the object otherwise
  168. */
  169. jint
  170. bni_object_class_index(jobject obj);
  171. /**
  172. * Allocate memory from the current instance's private heap.
  173. *
  174. * @param size bytes to allocate
  175. *
  176. * @return pointer to the allocated memory
  177. *
  178. * @throws OutOfMemoryError if VM runs out of memory.
  179. */
  180. void*
  181. bni_malloc(unsigned size);
  182. /**
  183. * Allocate memory from the current instance's private heap and clear
  184. * to zero.
  185. *
  186. * @param size bytes to allocate
  187. *
  188. * @return pointer to the allocated memory
  189. *
  190. * @throws OutOfMemoryError if VM runs out of memory.
  191. */
  192. void*
  193. bni_calloc(unsigned size);
  194. /**
  195. * Free the memory allocated from the current instance's private heap.
  196. *
  197. * @param ptr pointer to the memory in current instance's private heap
  198. */
  199. void
  200. bni_free(void *ptr);
  201. #endif