/**************************************************************************/ /* gd_mono_wasm_m2n.h */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #ifndef GD_MONO_WASM_M2N_H #define GD_MONO_WASM_M2N_H #ifdef JAVASCRIPT_ENABLED #include "core/typedefs.h" #include "core/string/ustring.h" #include #include #include #include extern "C" { struct Mono_InterpMethodArguments { size_t ilen; void **iargs; size_t flen; double *fargs; void **retval; size_t is_float_ret; //#ifdef TARGET_WASM void *sig; //#endif }; } // extern "C" namespace GDMonoWasmM2n { template struct IndexSequence {}; template struct BuildIndexSequence : BuildIndexSequence {}; template struct BuildIndexSequence<0, Is...> : IndexSequence {}; template struct array { T elems[Size]; }; template constexpr char get_m2n_cookie_impl() { #define M2N_REG_COOKIE(m_type, m_cookie) \ if (std::is_same::value) { \ return m_cookie; \ } M2N_REG_COOKIE(MonoBoolean, 'I'); M2N_REG_COOKIE(int8_t, 'I'); M2N_REG_COOKIE(uint8_t, 'I'); M2N_REG_COOKIE(int16_t, 'I'); M2N_REG_COOKIE(uint16_t, 'I'); M2N_REG_COOKIE(int32_t, 'I'); M2N_REG_COOKIE(uint32_t, 'I'); M2N_REG_COOKIE(int64_t, 'L'); M2N_REG_COOKIE(uint64_t, 'L'); M2N_REG_COOKIE(float, 'F'); M2N_REG_COOKIE(double, 'D'); if (std::is_pointer::value) { if (sizeof(void *) == 4) { return 'I'; } else { return 'L'; } } if (std::is_void::value) { return 'V'; } return 'X'; #undef M2N_REG_COOKIE } template constexpr char get_m2n_cookie() { constexpr char cookie = get_m2n_cookie_impl(); static_assert(cookie != 'X', "Type not supported in internal call signature."); return cookie; } template constexpr array get_m2n_cookies() { return array{ 'V', get_m2n_cookie()..., '\0' }; } template constexpr array get_m2n_cookies_r() { return array{ get_m2n_cookie(), get_m2n_cookie()..., '\0' }; } template constexpr size_t calc_m2n_index(size_t &r_int_idx, size_t &r_float_idx) { constexpr char cookie = get_m2n_cookie(); static_assert(cookie == 'I' || cookie == 'L' || cookie == 'F' || cookie == 'D', "Cookie should be I, L, F or D."); if (cookie == 'I' || cookie == 'L') { size_t ret = r_int_idx; r_int_idx += cookie == 'I' ? 1 : 2; return ret; } else { size_t ret = r_float_idx; r_float_idx += cookie == 'F' ? 1 : 2; return ret; } } template constexpr array get_indices_for_type() { size_t int_idx = 0; size_t float_idx = 0; (void)int_idx; // Suppress 'unused' warning when parameter count is 0 (void)float_idx; // Suppress 'unused' warning when parameter count is 0 return array{ calc_m2n_index

(int_idx, float_idx)... }; } constexpr size_t fidx(size_t p_x) { if (sizeof(void *) == 4) { return p_x * 2; } else { return p_x; } } template struct m2n_arg_cast_helper; template struct m2n_arg_cast_helper { static T cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) { return (T)(size_t)p_margs->iargs[p_idx]; } }; template struct m2n_arg_cast_helper { static T cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) { static_assert(std::is_same::value || std::is_same::value || (sizeof(void *) == 8 && std::is_pointer::value), "Invalid type for cookie 'L'."); union { T l; struct { int32_t lo; int32_t hi; } pair; } p; p.pair.lo = (int32_t)(size_t)p_margs->iargs[p_idx]; p.pair.hi = (int32_t)(size_t)p_margs->iargs[p_idx + 1]; return p.l; } }; template struct m2n_arg_cast_helper { static T cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) { return *reinterpret_cast(&p_margs->fargs[fidx(p_idx)]); } }; template struct m2n_arg_cast_helper { static T cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) { return (T)p_margs->fargs[p_idx]; } }; template T m2n_arg_cast(Mono_InterpMethodArguments *p_margs, size_t p_idx) { constexpr char cookie = get_m2n_cookie(); static_assert(cookie == 'I' || cookie == 'L' || cookie == 'F' || cookie == 'D', "Cookie should be I, L, F or D."); return m2n_arg_cast_helper::cast(p_margs, p_idx); } template void m2n_trampoline_with_idx_seq(void *p_target_func, Mono_InterpMethodArguments *p_margs, IndexSequence) { constexpr array indices = get_indices_for_type(); (void)indices; // Suppress 'unused' warning when parameter count is 0 typedef void (*Func)(P...); Func func = (Func)p_target_func; func(m2n_arg_cast

(p_margs, indices.elems[Is])...); } template void m2n_trampoline_with_idx_seq_r(void *p_target_func, Mono_InterpMethodArguments *p_margs, IndexSequence) { constexpr array indices = get_indices_for_type(); (void)indices; // Suppress 'unused' warning when parameter count is 0 typedef R (*Func)(P...); Func func = (Func)p_target_func; R res = func(m2n_arg_cast

(p_margs, indices.elems[Is])...); *reinterpret_cast(p_margs->retval) = res; } template void m2n_trampoline(void *p_target_func, Mono_InterpMethodArguments *p_margs) { m2n_trampoline_with_idx_seq(p_target_func, p_margs, BuildIndexSequence{}); } template void m2n_trampoline_r(void *p_target_func, Mono_InterpMethodArguments *p_margs) { m2n_trampoline_with_idx_seq_r(p_target_func, p_margs, BuildIndexSequence{}); } typedef void (*TrampolineFunc)(void *p_target_func, Mono_InterpMethodArguments *p_margs); void set_trampoline(const char *cookies, TrampolineFunc trampoline_func); void lazy_initialize(); template struct ICallTrampolines { static constexpr array cookies = get_m2n_cookies(); static void add() { lazy_initialize(); set_trampoline(cookies.elems, &m2n_trampoline); } }; template constexpr array ICallTrampolines::cookies; template struct ICallTrampolinesR { static constexpr array cookies = get_m2n_cookies_r(); static void add() { lazy_initialize(); set_trampoline(cookies.elems, &m2n_trampoline_r); } }; template constexpr array ICallTrampolinesR::cookies; void initialize(); } // namespace GDMonoWasmM2n #endif #endif // GD_MONO_WASM_M2N_H