gd_mono_wasm_m2n.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/string/ustring.h"
  34. #include "core/typedefs.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 <typename T, size_t Size>
  54. struct array {
  55. T elems[Size];
  56. };
  57. template <typename T>
  58. constexpr char get_m2n_cookie_impl() {
  59. #define M2N_REG_COOKIE(m_type, m_cookie) \
  60. if constexpr (std::is_same_v<m_type, T>) { \
  61. return m_cookie; \
  62. }
  63. M2N_REG_COOKIE(MonoBoolean, 'I');
  64. M2N_REG_COOKIE(int8_t, 'I');
  65. M2N_REG_COOKIE(uint8_t, 'I');
  66. M2N_REG_COOKIE(int16_t, 'I');
  67. M2N_REG_COOKIE(uint16_t, 'I');
  68. M2N_REG_COOKIE(int32_t, 'I');
  69. M2N_REG_COOKIE(uint32_t, 'I');
  70. M2N_REG_COOKIE(int64_t, 'L');
  71. M2N_REG_COOKIE(uint64_t, 'L');
  72. M2N_REG_COOKIE(float, 'F');
  73. M2N_REG_COOKIE(double, 'D');
  74. if constexpr (std::is_pointer_v<T>) {
  75. if constexpr (sizeof(void *) == 4) {
  76. return 'I';
  77. } else {
  78. return 'L';
  79. }
  80. }
  81. if constexpr (std::is_void_v<T>) {
  82. return 'V';
  83. }
  84. return 'X';
  85. #undef M2N_REG_COOKIE
  86. }
  87. template <typename T>
  88. constexpr char get_m2n_cookie() {
  89. constexpr char cookie = get_m2n_cookie_impl<T>();
  90. static_assert(cookie != 'X', "Type not supported in internal call signature.");
  91. return cookie;
  92. }
  93. template <typename... T>
  94. constexpr array<const char, sizeof...(T) + 2> get_m2n_cookies() {
  95. return array<const char, sizeof...(T) + 2>{ 'V', get_m2n_cookie<T>()..., '\0' };
  96. }
  97. template <typename R, typename... T>
  98. constexpr array<const char, sizeof...(T) + 2> get_m2n_cookies_r() {
  99. return array<const char, sizeof...(T) + 2>{ get_m2n_cookie<R>(), get_m2n_cookie<T>()..., '\0' };
  100. }
  101. template <typename T>
  102. constexpr size_t calc_m2n_index(size_t &r_int_idx, size_t &r_float_idx) {
  103. constexpr char cookie = get_m2n_cookie<T>();
  104. static_assert(cookie == 'I' || cookie == 'L' || cookie == 'F' || cookie == 'D');
  105. if constexpr (cookie == 'I' || cookie == 'L') {
  106. size_t ret = r_int_idx;
  107. r_int_idx += cookie == 'I' ? 1 : 2;
  108. return ret;
  109. } else {
  110. size_t ret = r_float_idx;
  111. r_float_idx += cookie == 'F' ? 1 : 2;
  112. return ret;
  113. }
  114. }
  115. template <typename... P>
  116. constexpr array<size_t, sizeof...(P)> get_indices_for_type() {
  117. size_t int_idx = 0;
  118. size_t float_idx = 0;
  119. return array<size_t, sizeof...(P)>{ calc_m2n_index<P>(int_idx, float_idx)... };
  120. }
  121. constexpr size_t fidx(size_t p_x) {
  122. if constexpr (sizeof(void *) == 4) {
  123. return p_x * 2;
  124. } else {
  125. return p_x;
  126. }
  127. }
  128. template <typename T>
  129. T m2n_arg_cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) {
  130. constexpr char cookie = get_m2n_cookie<T>();
  131. static_assert(cookie == 'I' || cookie == 'L' || cookie == 'F' || cookie == 'D');
  132. if constexpr (cookie == 'I') {
  133. return (T)(size_t)p_margs->iargs[p_idx];
  134. } else if constexpr (cookie == 'L') {
  135. static_assert(std::is_same_v<T, int64_t> || std::is_same_v<T, uint64_t> ||
  136. (sizeof(void *) == 8 && std::is_pointer_v<T>),
  137. "Invalid type for cookie 'L'.");
  138. union {
  139. T l;
  140. struct {
  141. int32_t lo;
  142. int32_t hi;
  143. } pair;
  144. } p;
  145. p.pair.lo = (int32_t)(size_t)p_margs->iargs[p_idx];
  146. p.pair.hi = (int32_t)(size_t)p_margs->iargs[p_idx + 1];
  147. return p.l;
  148. } else if constexpr (cookie == 'F') {
  149. return *reinterpret_cast<float *>(&p_margs->fargs[fidx(p_idx)]);
  150. } else if constexpr (cookie == 'D') {
  151. return (T)(size_t)p_margs->fargs[p_idx];
  152. }
  153. }
  154. template <typename... P, size_t... Is>
  155. void m2n_trampoline_with_idx_seq(void *p_target_func, Mono_InterpMethodArguments *p_margs, IndexSequence<Is...>) {
  156. constexpr array<size_t, sizeof...(P)> indices = get_indices_for_type<P...>();
  157. typedef void (*Func)(P...);
  158. Func func = (Func)p_target_func;
  159. func(m2n_arg_cast<P>(p_margs, indices.elems[Is])...);
  160. }
  161. template <typename R, typename... P, size_t... Is>
  162. void m2n_trampoline_with_idx_seq_r(void *p_target_func, Mono_InterpMethodArguments *p_margs, IndexSequence<Is...>) {
  163. constexpr array<size_t, sizeof...(P)> indices = get_indices_for_type<P...>();
  164. typedef R (*Func)(P...);
  165. Func func = (Func)p_target_func;
  166. R res = func(m2n_arg_cast<P>(p_margs, indices.elems[Is])...);
  167. *reinterpret_cast<R *>(p_margs->retval) = res;
  168. }
  169. inline void m2n_trampoline_with_idx_seq_0(void *p_target_func, Mono_InterpMethodArguments *p_margs) {
  170. typedef void (*Func)();
  171. Func func = (Func)p_target_func;
  172. func();
  173. }
  174. template <typename R>
  175. void m2n_trampoline_with_idx_seq_r0(void *p_target_func, Mono_InterpMethodArguments *p_margs) {
  176. typedef R (*Func)();
  177. Func func = (Func)p_target_func;
  178. R res = func();
  179. *reinterpret_cast<R *>(p_margs->retval) = res;
  180. }
  181. template <typename... P>
  182. void m2n_trampoline(void *p_target_func, Mono_InterpMethodArguments *p_margs) {
  183. if constexpr (sizeof...(P) == 0) {
  184. m2n_trampoline_with_idx_seq_0(p_target_func, p_margs);
  185. } else {
  186. m2n_trampoline_with_idx_seq<P...>(p_target_func, p_margs, BuildIndexSequence<sizeof...(P)>{});
  187. }
  188. }
  189. template <typename R, typename... P>
  190. void m2n_trampoline_r(void *p_target_func, Mono_InterpMethodArguments *p_margs) {
  191. if constexpr (sizeof...(P) == 0) {
  192. m2n_trampoline_with_idx_seq_r0<R>(p_target_func, p_margs);
  193. } else {
  194. m2n_trampoline_with_idx_seq_r<R, P...>(p_target_func, p_margs, BuildIndexSequence<sizeof...(P)>{});
  195. }
  196. }
  197. typedef void (*TrampolineFunc)(void *p_target_func, Mono_InterpMethodArguments *p_margs);
  198. void set_trampoline(const char *cookies, TrampolineFunc trampoline_func);
  199. void lazy_initialize();
  200. template <typename... P>
  201. struct ICallTrampolines {
  202. static constexpr auto cookies = get_m2n_cookies<P...>();
  203. static void add() {
  204. lazy_initialize();
  205. set_trampoline(cookies.elems, &m2n_trampoline<P...>);
  206. }
  207. };
  208. template <typename R, typename... P>
  209. struct ICallTrampolinesR {
  210. static constexpr auto cookies = get_m2n_cookies_r<R, P...>();
  211. static void add() {
  212. lazy_initialize();
  213. set_trampoline(cookies.elems, &m2n_trampoline_r<R, P...>);
  214. }
  215. };
  216. void initialize();
  217. } // namespace GDMonoWasmM2n
  218. #endif
  219. #endif // GD_MONO_WASM_M2N_H