gd_mono_method_thunk.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*************************************************************************/
  2. /* gd_mono_method_thunk.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_METHOD_THUNK_H
  31. #define GD_MONO_METHOD_THUNK_H
  32. #include <type_traits>
  33. #include "gd_mono_class.h"
  34. #include "gd_mono_header.h"
  35. #include "gd_mono_marshal.h"
  36. #include "gd_mono_method.h"
  37. #include "gd_mono_utils.h"
  38. #if !defined(JAVASCRIPT_ENABLED) && !defined(IPHONE_ENABLED)
  39. #define HAVE_METHOD_THUNKS
  40. #endif
  41. #ifdef HAVE_METHOD_THUNKS
  42. template <class... ParamTypes>
  43. struct GDMonoMethodThunk {
  44. typedef void(GD_MONO_STDCALL *M)(ParamTypes... p_args, MonoException **);
  45. M mono_method_thunk = nullptr;
  46. public:
  47. _FORCE_INLINE_ void invoke(ParamTypes... p_args, MonoException **r_exc) {
  48. GD_MONO_BEGIN_RUNTIME_INVOKE;
  49. mono_method_thunk(p_args..., r_exc);
  50. GD_MONO_END_RUNTIME_INVOKE;
  51. }
  52. _FORCE_INLINE_ bool is_null() {
  53. return mono_method_thunk == nullptr;
  54. }
  55. _FORCE_INLINE_ void nullify() {
  56. mono_method_thunk = nullptr;
  57. }
  58. _FORCE_INLINE_ void set_from_method(GDMonoMethod *p_mono_method) {
  59. #ifdef DEBUG_ENABLED
  60. CRASH_COND(p_mono_method == nullptr);
  61. CRASH_COND(p_mono_method->get_return_type().type_encoding != MONO_TYPE_VOID);
  62. if (p_mono_method->is_static()) {
  63. CRASH_COND(p_mono_method->get_parameters_count() != sizeof...(ParamTypes));
  64. } else {
  65. CRASH_COND(p_mono_method->get_parameters_count() != (sizeof...(ParamTypes) - 1));
  66. }
  67. #endif
  68. mono_method_thunk = (M)mono_method_get_unmanaged_thunk(p_mono_method->get_mono_ptr());
  69. }
  70. GDMonoMethodThunk() {}
  71. explicit GDMonoMethodThunk(GDMonoMethod *p_mono_method) {
  72. set_from_method(p_mono_method);
  73. }
  74. };
  75. template <class R, class... ParamTypes>
  76. struct GDMonoMethodThunkR {
  77. typedef R(GD_MONO_STDCALL *M)(ParamTypes... p_args, MonoException **);
  78. M mono_method_thunk = nullptr;
  79. public:
  80. _FORCE_INLINE_ R invoke(ParamTypes... p_args, MonoException **r_exc) {
  81. GD_MONO_BEGIN_RUNTIME_INVOKE;
  82. R r = mono_method_thunk(p_args..., r_exc);
  83. GD_MONO_END_RUNTIME_INVOKE;
  84. return r;
  85. }
  86. _FORCE_INLINE_ bool is_null() {
  87. return mono_method_thunk == nullptr;
  88. }
  89. _FORCE_INLINE_ void nullify() {
  90. mono_method_thunk = nullptr;
  91. }
  92. _FORCE_INLINE_ void set_from_method(GDMonoMethod *p_mono_method) {
  93. #ifdef DEBUG_ENABLED
  94. CRASH_COND(p_mono_method == nullptr);
  95. CRASH_COND(p_mono_method->get_return_type().type_encoding == MONO_TYPE_VOID);
  96. if (p_mono_method->is_static()) {
  97. CRASH_COND(p_mono_method->get_parameters_count() != sizeof...(ParamTypes));
  98. } else {
  99. CRASH_COND(p_mono_method->get_parameters_count() != (sizeof...(ParamTypes) - 1));
  100. }
  101. #endif
  102. mono_method_thunk = (M)mono_method_get_unmanaged_thunk(p_mono_method->get_mono_ptr());
  103. }
  104. GDMonoMethodThunkR() {}
  105. explicit GDMonoMethodThunkR(GDMonoMethod *p_mono_method) {
  106. #ifdef DEBUG_ENABLED
  107. CRASH_COND(p_mono_method == nullptr);
  108. #endif
  109. mono_method_thunk = (M)mono_method_get_unmanaged_thunk(p_mono_method->get_mono_ptr());
  110. }
  111. };
  112. #else
  113. template <unsigned int ThunkParamCount, class P1, class... ParamTypes>
  114. struct VariadicInvokeMonoMethodImpl {
  115. static void invoke(GDMonoMethod *p_mono_method, P1 p_arg1, ParamTypes... p_args, MonoException **r_exc) {
  116. if (p_mono_method->is_static()) {
  117. void *args[ThunkParamCount] = { p_arg1, p_args... };
  118. p_mono_method->invoke_raw(nullptr, args, r_exc);
  119. } else {
  120. void *args[ThunkParamCount] = { p_args... };
  121. p_mono_method->invoke_raw((MonoObject *)p_arg1, args, r_exc);
  122. }
  123. }
  124. };
  125. template <unsigned int ThunkParamCount, class... ParamTypes>
  126. struct VariadicInvokeMonoMethod {
  127. static void invoke(GDMonoMethod *p_mono_method, ParamTypes... p_args, MonoException **r_exc) {
  128. VariadicInvokeMonoMethodImpl<ThunkParamCount, ParamTypes...>::invoke(p_mono_method, p_args..., r_exc);
  129. }
  130. };
  131. template <>
  132. struct VariadicInvokeMonoMethod<0> {
  133. static void invoke(GDMonoMethod *p_mono_method, MonoException **r_exc) {
  134. #ifdef DEBUG_ENABLED
  135. CRASH_COND(!p_mono_method->is_static());
  136. #endif
  137. p_mono_method->invoke_raw(nullptr, nullptr, r_exc);
  138. }
  139. };
  140. template <class P1>
  141. struct VariadicInvokeMonoMethod<1, P1> {
  142. static void invoke(GDMonoMethod *p_mono_method, P1 p_arg1, MonoException **r_exc) {
  143. if (p_mono_method->is_static()) {
  144. void *args[1] = { p_arg1 };
  145. p_mono_method->invoke_raw(nullptr, args, r_exc);
  146. } else {
  147. p_mono_method->invoke_raw((MonoObject *)p_arg1, nullptr, r_exc);
  148. }
  149. }
  150. };
  151. template <class R>
  152. R unbox_if_needed(MonoObject *p_val, const ManagedType &, typename std::enable_if<!std::is_pointer<R>::value>::type * = 0) {
  153. return GDMonoMarshal::unbox<R>(p_val);
  154. }
  155. template <class R>
  156. R unbox_if_needed(MonoObject *p_val, const ManagedType &p_type, typename std::enable_if<std::is_pointer<R>::value>::type * = 0) {
  157. if (mono_class_is_valuetype(p_type.type_class->get_mono_ptr())) {
  158. return GDMonoMarshal::unbox<R>(p_val);
  159. } else {
  160. // If it's not a value type, we assume 'R' is a pointer to 'MonoObject' or a compatible type, like 'MonoException'.
  161. return (R)p_val;
  162. }
  163. }
  164. template <unsigned int ThunkParamCount, class R, class P1, class... ParamTypes>
  165. struct VariadicInvokeMonoMethodRImpl {
  166. static R invoke(GDMonoMethod *p_mono_method, P1 p_arg1, ParamTypes... p_args, MonoException **r_exc) {
  167. if (p_mono_method->is_static()) {
  168. void *args[ThunkParamCount] = { p_arg1, p_args... };
  169. MonoObject *r = p_mono_method->invoke_raw(nullptr, args, r_exc);
  170. return unbox_if_needed<R>(r, p_mono_method->get_return_type());
  171. } else {
  172. void *args[ThunkParamCount] = { p_args... };
  173. MonoObject *r = p_mono_method->invoke_raw((MonoObject *)p_arg1, args, r_exc);
  174. return unbox_if_needed<R>(r, p_mono_method->get_return_type());
  175. }
  176. }
  177. };
  178. template <unsigned int ThunkParamCount, class R, class... ParamTypes>
  179. struct VariadicInvokeMonoMethodR {
  180. static R invoke(GDMonoMethod *p_mono_method, ParamTypes... p_args, MonoException **r_exc) {
  181. return VariadicInvokeMonoMethodRImpl<ThunkParamCount, R, ParamTypes...>::invoke(p_mono_method, p_args..., r_exc);
  182. }
  183. };
  184. template <class R>
  185. struct VariadicInvokeMonoMethodR<0, R> {
  186. static R invoke(GDMonoMethod *p_mono_method, MonoException **r_exc) {
  187. #ifdef DEBUG_ENABLED
  188. CRASH_COND(!p_mono_method->is_static());
  189. #endif
  190. MonoObject *r = p_mono_method->invoke_raw(nullptr, nullptr, r_exc);
  191. return unbox_if_needed<R>(r, p_mono_method->get_return_type());
  192. }
  193. };
  194. template <class R, class P1>
  195. struct VariadicInvokeMonoMethodR<1, R, P1> {
  196. static R invoke(GDMonoMethod *p_mono_method, P1 p_arg1, MonoException **r_exc) {
  197. if (p_mono_method->is_static()) {
  198. void *args[1] = { p_arg1 };
  199. MonoObject *r = p_mono_method->invoke_raw(nullptr, args, r_exc);
  200. return unbox_if_needed<R>(r, p_mono_method->get_return_type());
  201. } else {
  202. MonoObject *r = p_mono_method->invoke_raw((MonoObject *)p_arg1, nullptr, r_exc);
  203. return unbox_if_needed<R>(r, p_mono_method->get_return_type());
  204. }
  205. }
  206. };
  207. template <class... ParamTypes>
  208. struct GDMonoMethodThunk {
  209. GDMonoMethod *mono_method = nullptr;
  210. public:
  211. _FORCE_INLINE_ void invoke(ParamTypes... p_args, MonoException **r_exc) {
  212. VariadicInvokeMonoMethod<sizeof...(ParamTypes), ParamTypes...>::invoke(mono_method, p_args..., r_exc);
  213. }
  214. _FORCE_INLINE_ bool is_null() {
  215. return mono_method == nullptr;
  216. }
  217. _FORCE_INLINE_ void nullify() {
  218. mono_method = nullptr;
  219. }
  220. _FORCE_INLINE_ void set_from_method(GDMonoMethod *p_mono_method) {
  221. #ifdef DEBUG_ENABLED
  222. CRASH_COND(p_mono_method == nullptr);
  223. CRASH_COND(p_mono_method->get_return_type().type_encoding != MONO_TYPE_VOID);
  224. if (p_mono_method->is_static()) {
  225. CRASH_COND(p_mono_method->get_parameters_count() != sizeof...(ParamTypes));
  226. } else {
  227. CRASH_COND(p_mono_method->get_parameters_count() != (sizeof...(ParamTypes) - 1));
  228. }
  229. #endif
  230. mono_method = p_mono_method;
  231. }
  232. GDMonoMethodThunk() {}
  233. explicit GDMonoMethodThunk(GDMonoMethod *p_mono_method) {
  234. set_from_method(p_mono_method);
  235. }
  236. };
  237. template <class R, class... ParamTypes>
  238. struct GDMonoMethodThunkR {
  239. GDMonoMethod *mono_method = nullptr;
  240. public:
  241. _FORCE_INLINE_ R invoke(ParamTypes... p_args, MonoException **r_exc) {
  242. return VariadicInvokeMonoMethodR<sizeof...(ParamTypes), R, ParamTypes...>::invoke(mono_method, p_args..., r_exc);
  243. }
  244. _FORCE_INLINE_ bool is_null() {
  245. return mono_method == nullptr;
  246. }
  247. _FORCE_INLINE_ void nullify() {
  248. mono_method = nullptr;
  249. }
  250. _FORCE_INLINE_ void set_from_method(GDMonoMethod *p_mono_method) {
  251. #ifdef DEBUG_ENABLED
  252. CRASH_COND(p_mono_method == nullptr);
  253. CRASH_COND(p_mono_method->get_return_type().type_encoding == MONO_TYPE_VOID);
  254. if (p_mono_method->is_static()) {
  255. CRASH_COND(p_mono_method->get_parameters_count() != sizeof...(ParamTypes));
  256. } else {
  257. CRASH_COND(p_mono_method->get_parameters_count() != (sizeof...(ParamTypes) - 1));
  258. }
  259. #endif
  260. mono_method = p_mono_method;
  261. }
  262. GDMonoMethodThunkR() {}
  263. explicit GDMonoMethodThunkR(GDMonoMethod *p_mono_method) {
  264. set_from_method(p_mono_method);
  265. }
  266. };
  267. #endif
  268. #endif // GD_MONO_METHOD_THUNK_H