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