gd_mono_method.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**************************************************************************/
  2. /* gd_mono_method.cpp */
  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. #include "gd_mono_method.h"
  31. #include <mono/metadata/attrdefs.h>
  32. #include <mono/metadata/debug-helpers.h>
  33. #include "gd_mono_cache.h"
  34. #include "gd_mono_class.h"
  35. #include "gd_mono_marshal.h"
  36. #include "gd_mono_utils.h"
  37. void GDMonoMethod::_update_signature() {
  38. // Apparently MonoMethodSignature needs not to be freed.
  39. // mono_method_signature caches the result, we don't need to cache it ourselves.
  40. MonoMethodSignature *method_sig = mono_method_signature(mono_method);
  41. _update_signature(method_sig);
  42. }
  43. void GDMonoMethod::_update_signature(MonoMethodSignature *p_method_sig) {
  44. params_count = mono_signature_get_param_count(p_method_sig);
  45. MonoType *ret_type = mono_signature_get_return_type(p_method_sig);
  46. if (ret_type) {
  47. return_type.type_encoding = mono_type_get_type(ret_type);
  48. if (return_type.type_encoding != MONO_TYPE_VOID) {
  49. MonoClass *ret_type_class = mono_class_from_mono_type(ret_type);
  50. return_type.type_class = GDMono::get_singleton()->get_class(ret_type_class);
  51. }
  52. }
  53. void *iter = NULL;
  54. MonoType *param_raw_type;
  55. while ((param_raw_type = mono_signature_get_params(p_method_sig, &iter)) != NULL) {
  56. ManagedType param_type;
  57. param_type.type_encoding = mono_type_get_type(param_raw_type);
  58. MonoClass *param_type_class = mono_class_from_mono_type(param_raw_type);
  59. param_type.type_class = GDMono::get_singleton()->get_class(param_type_class);
  60. param_types.push_back(param_type);
  61. }
  62. // clear the cache
  63. method_info_fetched = false;
  64. method_info = MethodInfo();
  65. for (int i = 0; i < params_count; i++) {
  66. params_buffer_size += GDMonoMarshal::variant_get_managed_unboxed_size(param_types[i]);
  67. }
  68. }
  69. GDMonoClass *GDMonoMethod::get_enclosing_class() const {
  70. return GDMono::get_singleton()->get_class(mono_method_get_class(mono_method));
  71. }
  72. bool GDMonoMethod::is_static() {
  73. return mono_method_get_flags(mono_method, NULL) & MONO_METHOD_ATTR_STATIC;
  74. }
  75. IMonoClassMember::Visibility GDMonoMethod::get_visibility() {
  76. switch (mono_method_get_flags(mono_method, NULL) & MONO_METHOD_ATTR_ACCESS_MASK) {
  77. case MONO_METHOD_ATTR_PRIVATE:
  78. return IMonoClassMember::PRIVATE;
  79. case MONO_METHOD_ATTR_FAM_AND_ASSEM:
  80. return IMonoClassMember::PROTECTED_AND_INTERNAL;
  81. case MONO_METHOD_ATTR_ASSEM:
  82. return IMonoClassMember::INTERNAL;
  83. case MONO_METHOD_ATTR_FAMILY:
  84. return IMonoClassMember::PROTECTED;
  85. case MONO_METHOD_ATTR_PUBLIC:
  86. return IMonoClassMember::PUBLIC;
  87. default:
  88. ERR_FAIL_V(IMonoClassMember::PRIVATE);
  89. }
  90. }
  91. MonoObject *GDMonoMethod::invoke(MonoObject *p_object, const Variant **p_params, MonoException **r_exc) const {
  92. MonoException *exc = NULL;
  93. MonoObject *ret;
  94. if (params_count > 0) {
  95. void **params = (void **)alloca(params_count * sizeof(void *));
  96. uint8_t *buffer = (uint8_t *)alloca(params_buffer_size);
  97. unsigned int offset = 0;
  98. for (int i = 0; i < params_count; i++) {
  99. params[i] = GDMonoMarshal::variant_to_managed_unboxed(p_params[i], param_types[i], buffer + offset, offset);
  100. }
  101. ret = GDMonoUtils::runtime_invoke(mono_method, p_object, params, &exc);
  102. } else {
  103. ret = GDMonoUtils::runtime_invoke(mono_method, p_object, NULL, &exc);
  104. }
  105. if (exc) {
  106. ret = NULL;
  107. if (r_exc) {
  108. *r_exc = exc;
  109. } else {
  110. GDMonoUtils::set_pending_exception(exc);
  111. }
  112. }
  113. return ret;
  114. }
  115. MonoObject *GDMonoMethod::invoke(MonoObject *p_object, MonoException **r_exc) const {
  116. ERR_FAIL_COND_V(get_parameters_count() > 0, NULL);
  117. return invoke_raw(p_object, NULL, r_exc);
  118. }
  119. MonoObject *GDMonoMethod::invoke_raw(MonoObject *p_object, void **p_params, MonoException **r_exc) const {
  120. MonoException *exc = NULL;
  121. MonoObject *ret = GDMonoUtils::runtime_invoke(mono_method, p_object, p_params, &exc);
  122. if (exc) {
  123. ret = NULL;
  124. if (r_exc) {
  125. *r_exc = exc;
  126. } else {
  127. GDMonoUtils::set_pending_exception(exc);
  128. }
  129. }
  130. return ret;
  131. }
  132. bool GDMonoMethod::has_attribute(GDMonoClass *p_attr_class) {
  133. ERR_FAIL_NULL_V(p_attr_class, false);
  134. if (!attrs_fetched)
  135. fetch_attributes();
  136. if (!attributes)
  137. return false;
  138. return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
  139. }
  140. MonoObject *GDMonoMethod::get_attribute(GDMonoClass *p_attr_class) {
  141. ERR_FAIL_NULL_V(p_attr_class, NULL);
  142. if (!attrs_fetched)
  143. fetch_attributes();
  144. if (!attributes)
  145. return NULL;
  146. return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
  147. }
  148. void GDMonoMethod::fetch_attributes() {
  149. ERR_FAIL_COND(attributes != NULL);
  150. attributes = mono_custom_attrs_from_method(mono_method);
  151. attrs_fetched = true;
  152. }
  153. String GDMonoMethod::get_full_name(bool p_signature) const {
  154. char *res = mono_method_full_name(mono_method, p_signature);
  155. String full_name(res);
  156. mono_free(res);
  157. return full_name;
  158. }
  159. String GDMonoMethod::get_full_name_no_class() const {
  160. String res;
  161. MonoMethodSignature *method_sig = mono_method_signature(mono_method);
  162. char *ret_str = mono_type_full_name(mono_signature_get_return_type(method_sig));
  163. res += ret_str;
  164. mono_free(ret_str);
  165. res += " ";
  166. res += name;
  167. res += "(";
  168. char *sig_desc = mono_signature_get_desc(method_sig, true);
  169. res += sig_desc;
  170. mono_free(sig_desc);
  171. res += ")";
  172. return res;
  173. }
  174. String GDMonoMethod::get_ret_type_full_name() const {
  175. MonoMethodSignature *method_sig = mono_method_signature(mono_method);
  176. char *ret_str = mono_type_full_name(mono_signature_get_return_type(method_sig));
  177. String res = ret_str;
  178. mono_free(ret_str);
  179. return res;
  180. }
  181. String GDMonoMethod::get_signature_desc(bool p_namespaces) const {
  182. MonoMethodSignature *method_sig = mono_method_signature(mono_method);
  183. char *sig_desc = mono_signature_get_desc(method_sig, p_namespaces);
  184. String res = sig_desc;
  185. mono_free(sig_desc);
  186. return res;
  187. }
  188. void GDMonoMethod::get_parameter_names(Vector<StringName> &names) const {
  189. if (params_count > 0) {
  190. const char **_names = memnew_arr(const char *, params_count);
  191. mono_method_get_param_names(mono_method, _names);
  192. for (int i = 0; i < params_count; ++i) {
  193. names.push_back(StringName(_names[i]));
  194. }
  195. memdelete_arr(_names);
  196. }
  197. }
  198. void GDMonoMethod::get_parameter_types(Vector<ManagedType> &types) const {
  199. for (int i = 0; i < param_types.size(); ++i) {
  200. types.push_back(param_types[i]);
  201. }
  202. }
  203. const MethodInfo &GDMonoMethod::get_method_info() {
  204. if (!method_info_fetched) {
  205. method_info.name = name;
  206. method_info.return_val = PropertyInfo(GDMonoMarshal::managed_to_variant_type(return_type), "");
  207. Vector<StringName> names;
  208. get_parameter_names(names);
  209. for (int i = 0; i < params_count; ++i) {
  210. method_info.arguments.push_back(PropertyInfo(GDMonoMarshal::managed_to_variant_type(param_types[i]), names[i]));
  211. }
  212. // TODO: default arguments
  213. method_info_fetched = true;
  214. }
  215. return method_info;
  216. }
  217. GDMonoMethod::GDMonoMethod(StringName p_name, MonoMethod *p_method) {
  218. name = p_name;
  219. mono_method = p_method;
  220. params_buffer_size = 0;
  221. method_info_fetched = false;
  222. attrs_fetched = false;
  223. attributes = NULL;
  224. _update_signature();
  225. }
  226. GDMonoMethod::~GDMonoMethod() {
  227. if (attributes) {
  228. mono_custom_attrs_free(attributes);
  229. }
  230. }