gd_mono_wasm_m2n.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**************************************************************************/
  2. /* gd_mono_wasm_m2n.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef GD_MONO_WASM_M2N_H
  31. #define GD_MONO_WASM_M2N_H
  32. #ifdef JAVASCRIPT_ENABLED
  33. #include "core/typedefs.h"
  34. #include "core/ustring.h"
  35. #include <mono/metadata/loader.h>
  36. #include <mono/utils/mono-publib.h>
  37. #include <stdexcept>
  38. #include <type_traits>
  39. extern "C" {
  40. struct Mono_InterpMethodArguments {
  41. size_t ilen;
  42. void **iargs;
  43. size_t flen;
  44. double *fargs;
  45. void **retval;
  46. size_t is_float_ret;
  47. //#ifdef TARGET_WASM
  48. void *sig;
  49. //#endif
  50. };
  51. } // extern "C"
  52. namespace GDMonoWasmM2n {
  53. template <size_t... Is>
  54. struct IndexSequence {};
  55. template <size_t N, size_t... Is>
  56. struct BuildIndexSequence : BuildIndexSequence<N - 1, N - 1, Is...> {};
  57. template <size_t... Is>
  58. struct BuildIndexSequence<0, Is...> : IndexSequence<Is...> {};
  59. template <typename T, size_t Size>
  60. struct array {
  61. T elems[Size];
  62. };
  63. template <typename T>
  64. constexpr char get_m2n_cookie_impl() {
  65. #define M2N_REG_COOKIE(m_type, m_cookie) \
  66. if (std::is_same<m_type, T>::value) { \
  67. return m_cookie; \
  68. }
  69. M2N_REG_COOKIE(MonoBoolean, 'I');
  70. M2N_REG_COOKIE(int8_t, 'I');
  71. M2N_REG_COOKIE(uint8_t, 'I');
  72. M2N_REG_COOKIE(int16_t, 'I');
  73. M2N_REG_COOKIE(uint16_t, 'I');
  74. M2N_REG_COOKIE(int32_t, 'I');
  75. M2N_REG_COOKIE(uint32_t, 'I');
  76. M2N_REG_COOKIE(int64_t, 'L');
  77. M2N_REG_COOKIE(uint64_t, 'L');
  78. M2N_REG_COOKIE(float, 'F');
  79. M2N_REG_COOKIE(double, 'D');
  80. if (std::is_pointer<T>::value) {
  81. if (sizeof(void *) == 4) {
  82. return 'I';
  83. } else {
  84. return 'L';
  85. }
  86. }
  87. if (std::is_void<T>::value) {
  88. return 'V';
  89. }
  90. return 'X';
  91. #undef M2N_REG_COOKIE
  92. }
  93. template <typename T>
  94. constexpr char get_m2n_cookie() {
  95. constexpr char cookie = get_m2n_cookie_impl<T>();
  96. static_assert(cookie != 'X', "Type not supported in internal call signature.");
  97. return cookie;
  98. }
  99. template <typename... T>
  100. constexpr array<const char, sizeof...(T) + 2> get_m2n_cookies() {
  101. return array<const char, sizeof...(T) + 2>{ 'V', get_m2n_cookie<T>()..., '\0' };
  102. }
  103. template <typename R, typename... T>
  104. constexpr array<const char, sizeof...(T) + 2> get_m2n_cookies_r() {
  105. return array<const char, sizeof...(T) + 2>{ get_m2n_cookie<R>(), get_m2n_cookie<T>()..., '\0' };
  106. }
  107. template <typename T>
  108. constexpr size_t calc_m2n_index(size_t &r_int_idx, size_t &r_float_idx) {
  109. constexpr char cookie = get_m2n_cookie<T>();
  110. static_assert(cookie == 'I' || cookie == 'L' || cookie == 'F' || cookie == 'D', "Cookie should be I, L, F or D.");
  111. if (cookie == 'I' || cookie == 'L') {
  112. size_t ret = r_int_idx;
  113. r_int_idx += cookie == 'I' ? 1 : 2;
  114. return ret;
  115. } else {
  116. size_t ret = r_float_idx;
  117. r_float_idx += cookie == 'F' ? 1 : 2;
  118. return ret;
  119. }
  120. }
  121. template <typename... P>
  122. constexpr array<size_t, sizeof...(P)> get_indices_for_type() {
  123. size_t int_idx = 0;
  124. size_t float_idx = 0;
  125. (void)int_idx; // Suppress 'unused' warning when parameter count is 0
  126. (void)float_idx; // Suppress 'unused' warning when parameter count is 0
  127. return array<size_t, sizeof...(P)>{ calc_m2n_index<P>(int_idx, float_idx)... };
  128. }
  129. constexpr size_t fidx(size_t p_x) {
  130. if (sizeof(void *) == 4) {
  131. return p_x * 2;
  132. } else {
  133. return p_x;
  134. }
  135. }
  136. template <typename T, char cookie>
  137. struct m2n_arg_cast_helper;
  138. template <typename T>
  139. struct m2n_arg_cast_helper<T, 'I'> {
  140. static T cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) {
  141. return (T)(size_t)p_margs->iargs[p_idx];
  142. }
  143. };
  144. template <typename T>
  145. struct m2n_arg_cast_helper<T, 'L'> {
  146. static T cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) {
  147. static_assert(std::is_same<T, int64_t>::value || std::is_same<T, uint64_t>::value ||
  148. (sizeof(void *) == 8 && std::is_pointer<T>::value),
  149. "Invalid type for cookie 'L'.");
  150. union {
  151. T l;
  152. struct {
  153. int32_t lo;
  154. int32_t hi;
  155. } pair;
  156. } p;
  157. p.pair.lo = (int32_t)(size_t)p_margs->iargs[p_idx];
  158. p.pair.hi = (int32_t)(size_t)p_margs->iargs[p_idx + 1];
  159. return p.l;
  160. }
  161. };
  162. template <typename T>
  163. struct m2n_arg_cast_helper<T, 'F'> {
  164. static T cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) {
  165. return *reinterpret_cast<float *>(&p_margs->fargs[fidx(p_idx)]);
  166. }
  167. };
  168. template <typename T>
  169. struct m2n_arg_cast_helper<T, 'D'> {
  170. static T cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) {
  171. return (T)p_margs->fargs[p_idx];
  172. }
  173. };
  174. template <typename T>
  175. T m2n_arg_cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) {
  176. constexpr char cookie = get_m2n_cookie<T>();
  177. static_assert(cookie == 'I' || cookie == 'L' || cookie == 'F' || cookie == 'D', "Cookie should be I, L, F or D.");
  178. return m2n_arg_cast_helper<T, cookie>::cast(p_margs, p_idx);
  179. }
  180. template <typename... P, size_t... Is>
  181. void m2n_trampoline_with_idx_seq(void *p_target_func, Mono_InterpMethodArguments *p_margs, IndexSequence<Is...>) {
  182. constexpr array<size_t, sizeof...(P)> indices = get_indices_for_type<P...>();
  183. (void)indices; // Suppress 'unused' warning when parameter count is 0
  184. typedef void (*Func)(P...);
  185. Func func = (Func)p_target_func;
  186. func(m2n_arg_cast<P>(p_margs, indices.elems[Is])...);
  187. }
  188. template <typename R, typename... P, size_t... Is>
  189. void m2n_trampoline_with_idx_seq_r(void *p_target_func, Mono_InterpMethodArguments *p_margs, IndexSequence<Is...>) {
  190. constexpr array<size_t, sizeof...(P)> indices = get_indices_for_type<P...>();
  191. (void)indices; // Suppress 'unused' warning when parameter count is 0
  192. typedef R (*Func)(P...);
  193. Func func = (Func)p_target_func;
  194. R res = func(m2n_arg_cast<P>(p_margs, indices.elems[Is])...);
  195. *reinterpret_cast<R *>(p_margs->retval) = res;
  196. }
  197. template <typename... P>
  198. void m2n_trampoline(void *p_target_func, Mono_InterpMethodArguments *p_margs) {
  199. m2n_trampoline_with_idx_seq<P...>(p_target_func, p_margs, BuildIndexSequence<sizeof...(P)>{});
  200. }
  201. template <typename R, typename... P>
  202. void m2n_trampoline_r(void *p_target_func, Mono_InterpMethodArguments *p_margs) {
  203. m2n_trampoline_with_idx_seq_r<R, P...>(p_target_func, p_margs, BuildIndexSequence<sizeof...(P)>{});
  204. }
  205. typedef void (*TrampolineFunc)(void *p_target_func, Mono_InterpMethodArguments *p_margs);
  206. void set_trampoline(const char *cookies, TrampolineFunc trampoline_func);
  207. void lazy_initialize();
  208. template <typename... P>
  209. struct ICallTrampolines {
  210. static constexpr array<const char, sizeof...(P) + 2> cookies = get_m2n_cookies<P...>();
  211. static void add() {
  212. lazy_initialize();
  213. set_trampoline(cookies.elems, &m2n_trampoline<P...>);
  214. }
  215. };
  216. template <typename... P>
  217. constexpr array<const char, sizeof...(P) + 2> ICallTrampolines<P...>::cookies;
  218. template <typename R, typename... P>
  219. struct ICallTrampolinesR {
  220. static constexpr array<const char, sizeof...(P) + 2> cookies = get_m2n_cookies_r<R, P...>();
  221. static void add() {
  222. lazy_initialize();
  223. set_trampoline(cookies.elems, &m2n_trampoline_r<R, P...>);
  224. }
  225. };
  226. template <typename R, typename... P>
  227. constexpr array<const char, sizeof...(P) + 2> ICallTrampolinesR<R, P...>::cookies;
  228. void initialize();
  229. } // namespace GDMonoWasmM2n
  230. #endif
  231. #endif // GD_MONO_WASM_M2N_H