2018-01-04 21:05:46 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gd_mono_property.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-01-04 21:05:46 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2019-01-01 12:46:36 +01:00
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
#include "gd_mono_property.h"
|
|
|
|
|
2019-11-10 17:10:38 +01:00
|
|
|
#include "gd_mono_cache.h"
|
2018-01-04 21:05:46 +01:00
|
|
|
#include "gd_mono_class.h"
|
|
|
|
#include "gd_mono_marshal.h"
|
2019-11-10 17:10:38 +01:00
|
|
|
#include "gd_mono_utils.h"
|
2018-01-04 21:05:46 +01:00
|
|
|
|
|
|
|
#include <mono/metadata/attrdefs.h>
|
|
|
|
|
|
|
|
GDMonoProperty::GDMonoProperty(MonoProperty *p_mono_property, GDMonoClass *p_owner) {
|
|
|
|
owner = p_owner;
|
|
|
|
mono_property = p_mono_property;
|
|
|
|
name = mono_property_get_name(mono_property);
|
|
|
|
|
|
|
|
MonoMethod *prop_method = mono_property_get_get_method(mono_property);
|
|
|
|
|
|
|
|
if (prop_method) {
|
|
|
|
MonoMethodSignature *getter_sig = mono_method_signature(prop_method);
|
|
|
|
|
|
|
|
MonoType *ret_type = mono_signature_get_return_type(getter_sig);
|
|
|
|
|
|
|
|
type.type_encoding = mono_type_get_type(ret_type);
|
|
|
|
MonoClass *ret_type_class = mono_class_from_mono_type(ret_type);
|
|
|
|
type.type_class = GDMono::get_singleton()->get_class(ret_type_class);
|
|
|
|
} else {
|
|
|
|
prop_method = mono_property_get_set_method(mono_property);
|
|
|
|
|
|
|
|
MonoMethodSignature *setter_sig = mono_method_signature(prop_method);
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
void *iter = nullptr;
|
2018-01-04 21:05:46 +01:00
|
|
|
MonoType *param_raw_type = mono_signature_get_params(setter_sig, &iter);
|
|
|
|
|
|
|
|
type.type_encoding = mono_type_get_type(param_raw_type);
|
|
|
|
MonoClass *param_type_class = mono_class_from_mono_type(param_raw_type);
|
|
|
|
type.type_class = GDMono::get_singleton()->get_class(param_type_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
attrs_fetched = false;
|
2020-04-02 01:20:12 +02:00
|
|
|
attributes = nullptr;
|
2018-01-04 21:05:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GDMonoProperty::~GDMonoProperty() {
|
|
|
|
if (attributes) {
|
|
|
|
mono_custom_attrs_free(attributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoProperty::is_static() {
|
|
|
|
MonoMethod *prop_method = mono_property_get_get_method(mono_property);
|
2020-07-05 19:19:36 +02:00
|
|
|
if (prop_method == nullptr) {
|
2018-01-04 21:05:46 +01:00
|
|
|
prop_method = mono_property_get_set_method(mono_property);
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
return mono_method_get_flags(prop_method, nullptr) & MONO_METHOD_ATTR_STATIC;
|
2018-01-04 21:05:46 +01:00
|
|
|
}
|
|
|
|
|
2019-01-29 00:02:35 +01:00
|
|
|
IMonoClassMember::Visibility GDMonoProperty::get_visibility() {
|
2018-01-04 21:05:46 +01:00
|
|
|
MonoMethod *prop_method = mono_property_get_get_method(mono_property);
|
2020-07-05 19:19:36 +02:00
|
|
|
if (prop_method == nullptr) {
|
2018-01-04 21:05:46 +01:00
|
|
|
prop_method = mono_property_get_set_method(mono_property);
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-01-04 21:05:46 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
switch (mono_method_get_flags(prop_method, nullptr) & MONO_METHOD_ATTR_ACCESS_MASK) {
|
2018-01-04 21:05:46 +01:00
|
|
|
case MONO_METHOD_ATTR_PRIVATE:
|
2019-01-29 00:02:35 +01:00
|
|
|
return IMonoClassMember::PRIVATE;
|
2018-01-04 21:05:46 +01:00
|
|
|
case MONO_METHOD_ATTR_FAM_AND_ASSEM:
|
2019-01-29 00:02:35 +01:00
|
|
|
return IMonoClassMember::PROTECTED_AND_INTERNAL;
|
2018-01-04 21:05:46 +01:00
|
|
|
case MONO_METHOD_ATTR_ASSEM:
|
2019-01-29 00:02:35 +01:00
|
|
|
return IMonoClassMember::INTERNAL;
|
2018-01-04 21:05:46 +01:00
|
|
|
case MONO_METHOD_ATTR_FAMILY:
|
2019-01-29 00:02:35 +01:00
|
|
|
return IMonoClassMember::PROTECTED;
|
2018-01-04 21:05:46 +01:00
|
|
|
case MONO_METHOD_ATTR_PUBLIC:
|
2019-01-29 00:02:35 +01:00
|
|
|
return IMonoClassMember::PUBLIC;
|
2018-01-04 21:05:46 +01:00
|
|
|
default:
|
2019-01-29 00:02:35 +01:00
|
|
|
ERR_FAIL_V(IMonoClassMember::PRIVATE);
|
2018-01-04 21:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoProperty::has_attribute(GDMonoClass *p_attr_class) {
|
|
|
|
ERR_FAIL_NULL_V(p_attr_class, false);
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!attrs_fetched) {
|
2018-01-04 21:05:46 +01:00
|
|
|
fetch_attributes();
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-01-04 21:05:46 +01:00
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!attributes) {
|
2018-01-04 21:05:46 +01:00
|
|
|
return false;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-01-04 21:05:46 +01:00
|
|
|
|
|
|
|
return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoObject *GDMonoProperty::get_attribute(GDMonoClass *p_attr_class) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_NULL_V(p_attr_class, nullptr);
|
2018-01-04 21:05:46 +01:00
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!attrs_fetched) {
|
2018-01-04 21:05:46 +01:00
|
|
|
fetch_attributes();
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-01-04 21:05:46 +01:00
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!attributes) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-01-04 21:05:46 +01:00
|
|
|
|
|
|
|
return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDMonoProperty::fetch_attributes() {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND(attributes != nullptr);
|
2018-01-04 21:05:46 +01:00
|
|
|
attributes = mono_custom_attrs_from_property(owner->get_mono_ptr(), mono_property);
|
|
|
|
attrs_fetched = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoProperty::has_getter() {
|
2020-04-02 01:20:12 +02:00
|
|
|
return mono_property_get_get_method(mono_property) != nullptr;
|
2018-01-04 21:05:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoProperty::has_setter() {
|
2020-04-02 01:20:12 +02:00
|
|
|
return mono_property_get_set_method(mono_property) != nullptr;
|
2018-01-04 21:05:46 +01:00
|
|
|
}
|
|
|
|
|
2018-06-26 21:03:42 +02:00
|
|
|
void GDMonoProperty::set_value(MonoObject *p_object, MonoObject *p_value, MonoException **r_exc) {
|
2018-07-31 19:40:38 +02:00
|
|
|
MonoMethod *prop_method = mono_property_get_set_method(mono_property);
|
2020-12-05 00:19:15 +01:00
|
|
|
void *params[1] = { p_value };
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoException *exc = nullptr;
|
2020-12-05 00:19:15 +01:00
|
|
|
GDMonoUtils::runtime_invoke(prop_method, p_object, params, &exc);
|
2018-07-31 19:40:38 +02:00
|
|
|
if (exc) {
|
|
|
|
if (r_exc) {
|
|
|
|
*r_exc = exc;
|
|
|
|
} else {
|
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 21:05:46 +01:00
|
|
|
}
|
|
|
|
|
2018-06-26 21:03:42 +02:00
|
|
|
void GDMonoProperty::set_value(MonoObject *p_object, void **p_params, MonoException **r_exc) {
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoException *exc = nullptr;
|
2018-07-31 19:40:38 +02:00
|
|
|
GDMonoUtils::property_set_value(mono_property, p_object, p_params, &exc);
|
2018-01-04 21:05:46 +01:00
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
if (r_exc) {
|
|
|
|
*r_exc = exc;
|
|
|
|
} else {
|
2018-06-26 21:03:42 +02:00
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
2018-01-04 21:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 21:03:42 +02:00
|
|
|
MonoObject *GDMonoProperty::get_value(MonoObject *p_object, MonoException **r_exc) {
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoException *exc = nullptr;
|
|
|
|
MonoObject *ret = GDMonoUtils::property_get_value(mono_property, p_object, nullptr, &exc);
|
2018-01-04 21:05:46 +01:00
|
|
|
|
|
|
|
if (exc) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ret = nullptr;
|
2018-01-04 21:05:46 +01:00
|
|
|
if (r_exc) {
|
|
|
|
*r_exc = exc;
|
|
|
|
} else {
|
2018-06-26 21:03:42 +02:00
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
2018-01-04 21:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoProperty::get_bool_value(MonoObject *p_object) {
|
|
|
|
return (bool)GDMonoMarshal::unbox<MonoBoolean>(get_value(p_object));
|
|
|
|
}
|
|
|
|
|
|
|
|
int GDMonoProperty::get_int_value(MonoObject *p_object) {
|
|
|
|
return GDMonoMarshal::unbox<int32_t>(get_value(p_object));
|
|
|
|
}
|
|
|
|
|
|
|
|
String GDMonoProperty::get_string_value(MonoObject *p_object) {
|
|
|
|
MonoObject *val = get_value(p_object);
|
|
|
|
return GDMonoMarshal::mono_string_to_godot((MonoString *)val);
|
|
|
|
}
|