2017-10-02 23:24:00 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gd_mono_method.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2018-01-01 14:40:08 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-10-02 23:24:00 +02: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
#include "gd_mono_method.h"
|
|
|
|
|
|
|
|
#include "gd_mono_class.h"
|
|
|
|
#include "gd_mono_marshal.h"
|
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
#include <mono/metadata/attrdefs.h>
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
void GDMonoMethod::_update_signature() {
|
|
|
|
// Apparently MonoMethodSignature needs not to be freed.
|
|
|
|
// mono_method_signature caches the result, we don't need to cache it ourselves.
|
|
|
|
|
|
|
|
MonoMethodSignature *method_sig = mono_method_signature(mono_method);
|
|
|
|
_update_signature(method_sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDMonoMethod::_update_signature(MonoMethodSignature *p_method_sig) {
|
|
|
|
params_count = mono_signature_get_param_count(p_method_sig);
|
|
|
|
|
|
|
|
MonoType *ret_type = mono_signature_get_return_type(p_method_sig);
|
|
|
|
if (ret_type) {
|
|
|
|
return_type.type_encoding = mono_type_get_type(ret_type);
|
|
|
|
|
|
|
|
if (return_type.type_encoding != MONO_TYPE_VOID) {
|
|
|
|
MonoClass *ret_type_class = mono_class_from_mono_type(ret_type);
|
|
|
|
return_type.type_class = GDMono::get_singleton()->get_class(ret_type_class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *iter = NULL;
|
|
|
|
MonoType *param_raw_type;
|
|
|
|
while ((param_raw_type = mono_signature_get_params(p_method_sig, &iter)) != NULL) {
|
|
|
|
ManagedType param_type;
|
|
|
|
|
|
|
|
param_type.type_encoding = mono_type_get_type(param_raw_type);
|
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
MonoClass *param_type_class = mono_class_from_mono_type(param_raw_type);
|
|
|
|
param_type.type_class = GDMono::get_singleton()->get_class(param_type_class);
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
param_types.push_back(param_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
bool GDMonoMethod::is_static() {
|
|
|
|
return mono_method_get_flags(mono_method, NULL) & MONO_METHOD_ATTR_STATIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
GDMonoClassMember::Visibility GDMonoMethod::get_visibility() {
|
|
|
|
switch (mono_method_get_flags(mono_method, NULL) & MONO_METHOD_ATTR_ACCESS_MASK) {
|
|
|
|
case MONO_METHOD_ATTR_PRIVATE:
|
|
|
|
return GDMonoClassMember::PRIVATE;
|
|
|
|
case MONO_METHOD_ATTR_FAM_AND_ASSEM:
|
|
|
|
return GDMonoClassMember::PROTECTED_AND_INTERNAL;
|
|
|
|
case MONO_METHOD_ATTR_ASSEM:
|
|
|
|
return GDMonoClassMember::INTERNAL;
|
|
|
|
case MONO_METHOD_ATTR_FAMILY:
|
|
|
|
return GDMonoClassMember::PROTECTED;
|
|
|
|
case MONO_METHOD_ATTR_PUBLIC:
|
|
|
|
return GDMonoClassMember::PUBLIC;
|
|
|
|
default:
|
|
|
|
ERR_FAIL_V(GDMonoClassMember::PRIVATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
void *GDMonoMethod::get_thunk() {
|
|
|
|
return mono_method_get_unmanaged_thunk(mono_method);
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoObject *GDMonoMethod::invoke(MonoObject *p_object, const Variant **p_params, MonoObject **r_exc) {
|
|
|
|
if (get_return_type().type_encoding != MONO_TYPE_VOID || get_parameters_count() > 0) {
|
|
|
|
MonoArray *params = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(MonoObject), get_parameters_count());
|
|
|
|
|
|
|
|
for (int i = 0; i < params_count; i++) {
|
|
|
|
MonoObject *boxed_param = GDMonoMarshal::variant_to_mono_object(p_params[i], param_types[i]);
|
|
|
|
mono_array_set(params, MonoObject *, i, boxed_param);
|
|
|
|
}
|
|
|
|
|
2017-10-23 00:35:04 +02:00
|
|
|
MonoObject *exc = NULL;
|
|
|
|
MonoObject *ret = mono_runtime_invoke_array(mono_method, p_object, params, &exc);
|
|
|
|
|
|
|
|
if (exc) {
|
2018-01-04 21:05:46 +01:00
|
|
|
ret = NULL;
|
2017-10-23 00:35:04 +02:00
|
|
|
if (r_exc) {
|
|
|
|
*r_exc = exc;
|
|
|
|
} else {
|
2018-01-04 21:05:46 +01:00
|
|
|
GDMonoUtils::print_unhandled_exception(exc);
|
2017-10-23 00:35:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2017-10-02 23:24:00 +02:00
|
|
|
} else {
|
2017-10-23 00:35:04 +02:00
|
|
|
MonoObject *exc = NULL;
|
|
|
|
mono_runtime_invoke(mono_method, p_object, NULL, &exc);
|
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
if (r_exc) {
|
|
|
|
*r_exc = exc;
|
|
|
|
} else {
|
2018-01-04 21:05:46 +01:00
|
|
|
GDMonoUtils::print_unhandled_exception(exc);
|
2017-10-23 00:35:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoObject *GDMonoMethod::invoke(MonoObject *p_object, MonoObject **r_exc) {
|
|
|
|
ERR_FAIL_COND_V(get_parameters_count() > 0, NULL);
|
|
|
|
return invoke_raw(p_object, NULL, r_exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoObject *GDMonoMethod::invoke_raw(MonoObject *p_object, void **p_params, MonoObject **r_exc) {
|
2017-10-23 00:35:04 +02:00
|
|
|
MonoObject *exc = NULL;
|
|
|
|
MonoObject *ret = mono_runtime_invoke(mono_method, p_object, p_params, &exc);
|
|
|
|
|
|
|
|
if (exc) {
|
2018-01-04 21:05:46 +01:00
|
|
|
ret = NULL;
|
2017-10-23 00:35:04 +02:00
|
|
|
if (r_exc) {
|
|
|
|
*r_exc = exc;
|
|
|
|
} else {
|
2018-01-04 21:05:46 +01:00
|
|
|
GDMonoUtils::print_unhandled_exception(exc);
|
2017-10-23 00:35:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoMethod::has_attribute(GDMonoClass *p_attr_class) {
|
|
|
|
ERR_FAIL_NULL_V(p_attr_class, false);
|
|
|
|
|
|
|
|
if (!attrs_fetched)
|
|
|
|
fetch_attributes();
|
|
|
|
|
|
|
|
if (!attributes)
|
|
|
|
return false;
|
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MonoObject *GDMonoMethod::get_attribute(GDMonoClass *p_attr_class) {
|
|
|
|
ERR_FAIL_NULL_V(p_attr_class, NULL);
|
|
|
|
|
|
|
|
if (!attrs_fetched)
|
|
|
|
fetch_attributes();
|
|
|
|
|
|
|
|
if (!attributes)
|
|
|
|
return NULL;
|
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDMonoMethod::fetch_attributes() {
|
|
|
|
ERR_FAIL_COND(attributes != NULL);
|
|
|
|
attributes = mono_custom_attrs_from_method(mono_method);
|
|
|
|
attrs_fetched = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
String GDMonoMethod::get_full_name(bool p_signature) const {
|
|
|
|
char *res = mono_method_full_name(mono_method, p_signature);
|
|
|
|
String full_name(res);
|
|
|
|
mono_free(res);
|
|
|
|
return full_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
String GDMonoMethod::get_full_name_no_class() const {
|
|
|
|
String res;
|
|
|
|
|
|
|
|
MonoMethodSignature *method_sig = mono_method_signature(mono_method);
|
|
|
|
|
|
|
|
char *ret_str = mono_type_full_name(mono_signature_get_return_type(method_sig));
|
|
|
|
res += ret_str;
|
|
|
|
mono_free(ret_str);
|
|
|
|
|
|
|
|
res += " ";
|
|
|
|
res += name;
|
|
|
|
res += "(";
|
|
|
|
|
|
|
|
char *sig_desc = mono_signature_get_desc(method_sig, true);
|
|
|
|
res += sig_desc;
|
|
|
|
mono_free(sig_desc);
|
|
|
|
|
|
|
|
res += ")";
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String GDMonoMethod::get_ret_type_full_name() const {
|
|
|
|
MonoMethodSignature *method_sig = mono_method_signature(mono_method);
|
|
|
|
char *ret_str = mono_type_full_name(mono_signature_get_return_type(method_sig));
|
|
|
|
String res = ret_str;
|
|
|
|
mono_free(ret_str);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String GDMonoMethod::get_signature_desc(bool p_namespaces) const {
|
|
|
|
MonoMethodSignature *method_sig = mono_method_signature(mono_method);
|
|
|
|
char *sig_desc = mono_signature_get_desc(method_sig, p_namespaces);
|
|
|
|
String res = sig_desc;
|
|
|
|
mono_free(sig_desc);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-01-18 23:27:43 +01:00
|
|
|
void GDMonoMethod::get_parameter_names(Vector<StringName> &names) const {
|
2018-02-20 06:24:52 +01:00
|
|
|
const char *_names = memnew_arr(char, params_count);
|
|
|
|
mono_method_get_param_names(mono_method, &_names);
|
2018-01-18 23:27:43 +01:00
|
|
|
for (int i = 0; i < params_count; ++i) {
|
2018-02-20 06:24:52 +01:00
|
|
|
names.push_back(StringName(&_names[i]));
|
2018-01-18 23:27:43 +01:00
|
|
|
}
|
2018-02-20 06:24:52 +01:00
|
|
|
memdelete_arr(_names);
|
2018-01-18 23:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDMonoMethod::get_parameter_types(Vector<ManagedType> &types) const {
|
|
|
|
for (int i = 0; i < param_types.size(); ++i) {
|
|
|
|
types.push_back(param_types[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
GDMonoMethod::GDMonoMethod(StringName p_name, MonoMethod *p_method) {
|
|
|
|
name = p_name;
|
|
|
|
|
|
|
|
mono_method = p_method;
|
|
|
|
|
|
|
|
attrs_fetched = false;
|
|
|
|
attributes = NULL;
|
|
|
|
|
|
|
|
_update_signature();
|
|
|
|
}
|
|
|
|
|
|
|
|
GDMonoMethod::~GDMonoMethod() {
|
|
|
|
if (attributes) {
|
|
|
|
mono_custom_attrs_free(attributes);
|
|
|
|
}
|
|
|
|
}
|