Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed IntPtr unboxing #11949

Merged
merged 2 commits into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/mono/csharp_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ bool CSharpInstance::set(const StringName &p_name, const Variant &p_value) {
if (method) {
MonoObject *ret = method->invoke(mono_object, args);

if (ret && UNBOX_BOOLEAN(ret))
if (ret && GDMonoMarshal::unbox<MonoBoolean>(ret) == true)
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions modules/mono/mono_gd/gd_mono_assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ MonoAssembly *GDMonoAssembly::_search_hook(MonoAssemblyName *aname, void *user_d
if (no_search)
return NULL;

no_search = true; // Avoid the recursion madness

GDMonoAssembly **loaded_asm = GDMono::get_singleton()->get_loaded_assembly(has_extension ? name.get_basename() : name);
if (loaded_asm)
return (*loaded_asm)->get_assembly();

no_search = true; // Avoid the recursion madness

String path;
MonoAssembly *res = NULL;

Expand Down
4 changes: 2 additions & 2 deletions modules/mono/mono_gd/gd_mono_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ void GDMonoField::set_value(MonoObject *p_object, const Variant &p_value) {
}

bool GDMonoField::get_bool_value(MonoObject *p_object) {
return UNBOX_BOOLEAN(get_value(p_object));
return (bool)GDMonoMarshal::unbox<MonoBoolean>(get_value(p_object));
}

int GDMonoField::get_int_value(MonoObject *p_object) {
return UNBOX_INT32(get_value(p_object));
return GDMonoMarshal::unbox<int32_t>(get_value(p_object));
}

String GDMonoField::get_string_value(MonoObject *p_object) {
Expand Down
55 changes: 22 additions & 33 deletions modules/mono/mono_gd/gd_mono_marshal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ namespace GDMonoMarshal {
return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(m_t), raw); \
}

#define RETURN_UNBOXED_STRUCT(m_t, m_var_in) \
{ \
float *raw = UNBOX_FLOAT_PTR(m_var_in); \
MARSHALLED_IN(m_t, raw, ret); \
return ret; \
#define RETURN_UNBOXED_STRUCT(m_t, m_var_in) \
{ \
float *raw = (float *)mono_object_unbox(m_var_in); \
MARSHALLED_IN(m_t, raw, ret); \
return ret; \
}

Variant::Type managed_to_variant_type(const ManagedType &p_type) {
Expand Down Expand Up @@ -453,30 +453,30 @@ Variant mono_object_to_variant(MonoObject *p_obj) {
Variant mono_object_to_variant(MonoObject *p_obj, const ManagedType &p_type) {
switch (p_type.type_encoding) {
case MONO_TYPE_BOOLEAN:
return (bool)UNBOX_BOOLEAN(p_obj);
return (bool)unbox<MonoBoolean>(p_obj);

case MONO_TYPE_I1:
return UNBOX_INT8(p_obj);
return unbox<int8_t>(p_obj);
case MONO_TYPE_I2:
return UNBOX_INT16(p_obj);
return unbox<int16_t>(p_obj);
case MONO_TYPE_I4:
return UNBOX_INT32(p_obj);
return unbox<int32_t>(p_obj);
case MONO_TYPE_I8:
return UNBOX_INT64(p_obj);
return unbox<int64_t>(p_obj);

case MONO_TYPE_U1:
return UNBOX_UINT8(p_obj);
return unbox<uint8_t>(p_obj);
case MONO_TYPE_U2:
return UNBOX_UINT16(p_obj);
return unbox<uint16_t>(p_obj);
case MONO_TYPE_U4:
return UNBOX_UINT32(p_obj);
return unbox<uint32_t>(p_obj);
case MONO_TYPE_U8:
return UNBOX_UINT64(p_obj);
return unbox<uint64_t>(p_obj);

case MONO_TYPE_R4:
return UNBOX_FLOAT(p_obj);
return unbox<float>(p_obj);
case MONO_TYPE_R8:
return UNBOX_DOUBLE(p_obj);
return unbox<double>(p_obj);

case MONO_TYPE_STRING: {
String str = mono_string_to_godot((MonoString *)p_obj);
Expand Down Expand Up @@ -554,29 +554,18 @@ Variant mono_object_to_variant(MonoObject *p_obj, const ManagedType &p_type) {

// GodotObject
if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
GDMonoField *ptr_field = CACHED_FIELD(GodotObject, ptr);

ERR_FAIL_NULL_V(ptr_field, Variant());

void *ptr_to_unmanaged = UNBOX_PTR(ptr_field->get_value(p_obj));

if (!ptr_to_unmanaged) // IntPtr.Zero
return Variant();

Object *object_ptr = static_cast<Object *>(ptr_to_unmanaged);

if (!object_ptr)
return Variant();

return object_ptr;
Object *ptr = unbox<Object *>(CACHED_FIELD(GodotObject, ptr)->get_value(p_obj));
return ptr ? Variant(ptr) : Variant();
}

if (CACHED_CLASS(NodePath) == type_class) {
return UNBOX_PTR(CACHED_FIELD(NodePath, ptr)->get_value(p_obj));
NodePath *ptr = unbox<NodePath *>(CACHED_FIELD(NodePath, ptr)->get_value(p_obj));
return ptr ? Variant(*ptr) : Variant();
}

if (CACHED_CLASS(RID) == type_class) {
return UNBOX_PTR(CACHED_FIELD(RID, ptr)->get_value(p_obj));
RID *ptr = unbox<RID *>(CACHED_FIELD(RID, ptr)->get_value(p_obj));
return ptr ? Variant(*ptr) : Variant();
}
} break;

Expand Down
19 changes: 4 additions & 15 deletions modules/mono/mono_gd/gd_mono_marshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,10 @@

namespace GDMonoMarshal {

#define UNBOX_CHAR_PTR(x) (char *)mono_object_unbox(x)
#define UNBOX_FLOAT_PTR(x) (float *)mono_object_unbox(x)

#define UNBOX_DOUBLE(x) *(double *)mono_object_unbox(x)
#define UNBOX_FLOAT(x) *(float *)mono_object_unbox(x)
#define UNBOX_INT64(x) *(int64_t *)mono_object_unbox(x)
#define UNBOX_INT32(x) *(int32_t *)mono_object_unbox(x)
#define UNBOX_INT16(x) *(int16_t *)mono_object_unbox(x)
#define UNBOX_INT8(x) *(int8_t *)mono_object_unbox(x)
#define UNBOX_UINT64(x) *(uint64_t *)mono_object_unbox(x)
#define UNBOX_UINT32(x) *(uint32_t *)mono_object_unbox(x)
#define UNBOX_UINT16(x) *(uint16_t *)mono_object_unbox(x)
#define UNBOX_UINT8(x) *(uint8_t *)mono_object_unbox(x)
#define UNBOX_BOOLEAN(x) *(MonoBoolean *)mono_object_unbox(x)
#define UNBOX_PTR(x) mono_object_unbox(x)
template <typename T>
T unbox(MonoObject *p_obj) {
return *(T *)mono_object_unbox(p_obj);
}

#define BOX_DOUBLE(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(double), &x)
#define BOX_FLOAT(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(float), &x)
Expand Down