gd_mono_property.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*************************************************************************/
  2. /* gd_mono_property.cpp */
  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. #include "gd_mono_property.h"
  31. #include "gd_mono_cache.h"
  32. #include "gd_mono_class.h"
  33. #include "gd_mono_marshal.h"
  34. #include "gd_mono_utils.h"
  35. #include <mono/metadata/attrdefs.h>
  36. GDMonoProperty::GDMonoProperty(MonoProperty *p_mono_property, GDMonoClass *p_owner) {
  37. owner = p_owner;
  38. mono_property = p_mono_property;
  39. name = String::utf8(mono_property_get_name(mono_property));
  40. MonoMethod *prop_method = mono_property_get_get_method(mono_property);
  41. if (prop_method) {
  42. MonoMethodSignature *getter_sig = mono_method_signature(prop_method);
  43. MonoType *ret_type = mono_signature_get_return_type(getter_sig);
  44. type.type_encoding = mono_type_get_type(ret_type);
  45. MonoClass *ret_type_class = mono_class_from_mono_type(ret_type);
  46. type.type_class = GDMono::get_singleton()->get_class(ret_type_class);
  47. } else {
  48. prop_method = mono_property_get_set_method(mono_property);
  49. MonoMethodSignature *setter_sig = mono_method_signature(prop_method);
  50. void *iter = nullptr;
  51. MonoType *param_raw_type = mono_signature_get_params(setter_sig, &iter);
  52. type.type_encoding = mono_type_get_type(param_raw_type);
  53. MonoClass *param_type_class = mono_class_from_mono_type(param_raw_type);
  54. type.type_class = GDMono::get_singleton()->get_class(param_type_class);
  55. }
  56. attrs_fetched = false;
  57. attributes = nullptr;
  58. }
  59. GDMonoProperty::~GDMonoProperty() {
  60. if (attributes) {
  61. mono_custom_attrs_free(attributes);
  62. }
  63. }
  64. bool GDMonoProperty::is_static() {
  65. MonoMethod *prop_method = mono_property_get_get_method(mono_property);
  66. if (prop_method == nullptr) {
  67. prop_method = mono_property_get_set_method(mono_property);
  68. }
  69. return mono_method_get_flags(prop_method, nullptr) & MONO_METHOD_ATTR_STATIC;
  70. }
  71. IMonoClassMember::Visibility GDMonoProperty::get_visibility() {
  72. MonoMethod *prop_method = mono_property_get_get_method(mono_property);
  73. if (prop_method == nullptr) {
  74. prop_method = mono_property_get_set_method(mono_property);
  75. }
  76. switch (mono_method_get_flags(prop_method, nullptr) & 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. bool GDMonoProperty::has_attribute(GDMonoClass *p_attr_class) {
  92. ERR_FAIL_NULL_V(p_attr_class, false);
  93. if (!attrs_fetched) {
  94. fetch_attributes();
  95. }
  96. if (!attributes) {
  97. return false;
  98. }
  99. return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
  100. }
  101. MonoObject *GDMonoProperty::get_attribute(GDMonoClass *p_attr_class) {
  102. ERR_FAIL_NULL_V(p_attr_class, nullptr);
  103. if (!attrs_fetched) {
  104. fetch_attributes();
  105. }
  106. if (!attributes) {
  107. return nullptr;
  108. }
  109. return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
  110. }
  111. void GDMonoProperty::fetch_attributes() {
  112. ERR_FAIL_COND(attributes != nullptr);
  113. attributes = mono_custom_attrs_from_property(owner->get_mono_ptr(), mono_property);
  114. attrs_fetched = true;
  115. }
  116. bool GDMonoProperty::has_getter() {
  117. return mono_property_get_get_method(mono_property) != nullptr;
  118. }
  119. bool GDMonoProperty::has_setter() {
  120. return mono_property_get_set_method(mono_property) != nullptr;
  121. }
  122. void GDMonoProperty::set_value(MonoObject *p_object, MonoObject *p_value, MonoException **r_exc) {
  123. MonoMethod *prop_method = mono_property_get_set_method(mono_property);
  124. void *params[1] = { p_value };
  125. MonoException *exc = nullptr;
  126. GDMonoUtils::runtime_invoke(prop_method, p_object, params, &exc);
  127. if (exc) {
  128. if (r_exc) {
  129. *r_exc = exc;
  130. } else {
  131. GDMonoUtils::set_pending_exception(exc);
  132. }
  133. }
  134. }
  135. void GDMonoProperty::set_value(MonoObject *p_object, void **p_params, MonoException **r_exc) {
  136. MonoException *exc = nullptr;
  137. GDMonoUtils::property_set_value(mono_property, p_object, p_params, &exc);
  138. if (exc) {
  139. if (r_exc) {
  140. *r_exc = exc;
  141. } else {
  142. GDMonoUtils::set_pending_exception(exc);
  143. }
  144. }
  145. }
  146. MonoObject *GDMonoProperty::get_value(MonoObject *p_object, MonoException **r_exc) {
  147. MonoException *exc = nullptr;
  148. MonoObject *ret = GDMonoUtils::property_get_value(mono_property, p_object, nullptr, &exc);
  149. if (exc) {
  150. ret = nullptr;
  151. if (r_exc) {
  152. *r_exc = exc;
  153. } else {
  154. GDMonoUtils::set_pending_exception(exc);
  155. }
  156. }
  157. return ret;
  158. }
  159. bool GDMonoProperty::get_bool_value(MonoObject *p_object) {
  160. return (bool)GDMonoMarshal::unbox<MonoBoolean>(get_value(p_object));
  161. }
  162. int GDMonoProperty::get_int_value(MonoObject *p_object) {
  163. return GDMonoMarshal::unbox<int32_t>(get_value(p_object));
  164. }
  165. String GDMonoProperty::get_string_value(MonoObject *p_object) {
  166. MonoObject *val = get_value(p_object);
  167. return GDMonoMarshal::mono_string_to_godot((MonoString *)val);
  168. }