gd_mono_method.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*************************************************************************/
  2. /* gd_mono_method.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #include "gd_mono_method.h"
  31. #include "gd_mono_class.h"
  32. #include "gd_mono_marshal.h"
  33. void GDMonoMethod::_update_signature() {
  34. // Apparently MonoMethodSignature needs not to be freed.
  35. // mono_method_signature caches the result, we don't need to cache it ourselves.
  36. MonoMethodSignature *method_sig = mono_method_signature(mono_method);
  37. _update_signature(method_sig);
  38. }
  39. void GDMonoMethod::_update_signature(MonoMethodSignature *p_method_sig) {
  40. is_instance = mono_signature_is_instance(p_method_sig);
  41. params_count = mono_signature_get_param_count(p_method_sig);
  42. MonoType *ret_type = mono_signature_get_return_type(p_method_sig);
  43. if (ret_type) {
  44. return_type.type_encoding = mono_type_get_type(ret_type);
  45. if (return_type.type_encoding != MONO_TYPE_VOID) {
  46. MonoClass *ret_type_class = mono_class_from_mono_type(ret_type);
  47. return_type.type_class = GDMono::get_singleton()->get_class(ret_type_class);
  48. }
  49. }
  50. void *iter = NULL;
  51. MonoType *param_raw_type;
  52. while ((param_raw_type = mono_signature_get_params(p_method_sig, &iter)) != NULL) {
  53. ManagedType param_type;
  54. param_type.type_encoding = mono_type_get_type(param_raw_type);
  55. if (param_type.type_encoding != MONO_TYPE_VOID) {
  56. MonoClass *param_type_class = mono_class_from_mono_type(param_raw_type);
  57. param_type.type_class = GDMono::get_singleton()->get_class(param_type_class);
  58. }
  59. param_types.push_back(param_type);
  60. }
  61. }
  62. void *GDMonoMethod::get_thunk() {
  63. return mono_method_get_unmanaged_thunk(mono_method);
  64. }
  65. MonoObject *GDMonoMethod::invoke(MonoObject *p_object, const Variant **p_params, MonoObject **r_exc) {
  66. if (get_return_type().type_encoding != MONO_TYPE_VOID || get_parameters_count() > 0) {
  67. MonoArray *params = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(MonoObject), get_parameters_count());
  68. for (int i = 0; i < params_count; i++) {
  69. MonoObject *boxed_param = GDMonoMarshal::variant_to_mono_object(p_params[i], param_types[i]);
  70. mono_array_set(params, MonoObject *, i, boxed_param);
  71. }
  72. MonoObject *exc = NULL;
  73. MonoObject *ret = mono_runtime_invoke_array(mono_method, p_object, params, &exc);
  74. if (exc) {
  75. if (r_exc) {
  76. *r_exc = exc;
  77. } else {
  78. ERR_PRINT(GDMonoUtils::get_exception_name_and_message(exc).utf8());
  79. mono_print_unhandled_exception(exc);
  80. }
  81. }
  82. return ret;
  83. } else {
  84. MonoObject *exc = NULL;
  85. mono_runtime_invoke(mono_method, p_object, NULL, &exc);
  86. if (exc) {
  87. if (r_exc) {
  88. *r_exc = exc;
  89. } else {
  90. ERR_PRINT(GDMonoUtils::get_exception_name_and_message(exc).utf8());
  91. mono_print_unhandled_exception(exc);
  92. }
  93. }
  94. return NULL;
  95. }
  96. }
  97. MonoObject *GDMonoMethod::invoke(MonoObject *p_object, MonoObject **r_exc) {
  98. ERR_FAIL_COND_V(get_parameters_count() > 0, NULL);
  99. return invoke_raw(p_object, NULL, r_exc);
  100. }
  101. MonoObject *GDMonoMethod::invoke_raw(MonoObject *p_object, void **p_params, MonoObject **r_exc) {
  102. MonoObject *exc = NULL;
  103. MonoObject *ret = mono_runtime_invoke(mono_method, p_object, p_params, &exc);
  104. if (exc) {
  105. if (r_exc) {
  106. *r_exc = exc;
  107. } else {
  108. ERR_PRINT(GDMonoUtils::get_exception_name_and_message(exc).utf8());
  109. mono_print_unhandled_exception(exc);
  110. }
  111. }
  112. return ret;
  113. }
  114. bool GDMonoMethod::has_attribute(GDMonoClass *p_attr_class) {
  115. ERR_FAIL_NULL_V(p_attr_class, false);
  116. if (!attrs_fetched)
  117. fetch_attributes();
  118. if (!attributes)
  119. return false;
  120. return mono_custom_attrs_has_attr(attributes, p_attr_class->get_raw());
  121. }
  122. MonoObject *GDMonoMethod::get_attribute(GDMonoClass *p_attr_class) {
  123. ERR_FAIL_NULL_V(p_attr_class, NULL);
  124. if (!attrs_fetched)
  125. fetch_attributes();
  126. if (!attributes)
  127. return NULL;
  128. return mono_custom_attrs_get_attr(attributes, p_attr_class->get_raw());
  129. }
  130. void GDMonoMethod::fetch_attributes() {
  131. ERR_FAIL_COND(attributes != NULL);
  132. attributes = mono_custom_attrs_from_method(mono_method);
  133. attrs_fetched = true;
  134. }
  135. String GDMonoMethod::get_full_name(bool p_signature) const {
  136. char *res = mono_method_full_name(mono_method, p_signature);
  137. String full_name(res);
  138. mono_free(res);
  139. return full_name;
  140. }
  141. String GDMonoMethod::get_full_name_no_class() const {
  142. String res;
  143. MonoMethodSignature *method_sig = mono_method_signature(mono_method);
  144. char *ret_str = mono_type_full_name(mono_signature_get_return_type(method_sig));
  145. res += ret_str;
  146. mono_free(ret_str);
  147. res += " ";
  148. res += name;
  149. res += "(";
  150. char *sig_desc = mono_signature_get_desc(method_sig, true);
  151. res += sig_desc;
  152. mono_free(sig_desc);
  153. res += ")";
  154. return res;
  155. }
  156. String GDMonoMethod::get_ret_type_full_name() const {
  157. MonoMethodSignature *method_sig = mono_method_signature(mono_method);
  158. char *ret_str = mono_type_full_name(mono_signature_get_return_type(method_sig));
  159. String res = ret_str;
  160. mono_free(ret_str);
  161. return res;
  162. }
  163. String GDMonoMethod::get_signature_desc(bool p_namespaces) const {
  164. MonoMethodSignature *method_sig = mono_method_signature(mono_method);
  165. char *sig_desc = mono_signature_get_desc(method_sig, p_namespaces);
  166. String res = sig_desc;
  167. mono_free(sig_desc);
  168. return res;
  169. }
  170. GDMonoMethod::GDMonoMethod(StringName p_name, MonoMethod *p_method) {
  171. name = p_name;
  172. mono_method = p_method;
  173. attrs_fetched = false;
  174. attributes = NULL;
  175. _update_signature();
  176. }
  177. GDMonoMethod::~GDMonoMethod() {
  178. if (attributes) {
  179. mono_custom_attrs_free(attributes);
  180. }
  181. }