From afa98e40b627f969a70b9e9ef9e5274cebae7af5 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Wed, 2 Mar 2022 09:09:06 -0800 Subject: [PATCH] Fix conversion from primitives to nullables --- BeefLibs/corlib/src/Reflection/Convert.bf | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/BeefLibs/corlib/src/Reflection/Convert.bf b/BeefLibs/corlib/src/Reflection/Convert.bf index 693e97f2..8c019151 100644 --- a/BeefLibs/corlib/src/Reflection/Convert.bf +++ b/BeefLibs/corlib/src/Reflection/Convert.bf @@ -131,6 +131,11 @@ namespace System.Reflection var (objType, dataPtr) = GetTypeAndPointer(obj); + if (objType == type) + { + return Variant.Create(type, dataPtr); + } + if (objType.IsPrimitive) { if (objType.IsInteger) @@ -166,6 +171,25 @@ namespace System.Reflection } } + if (var unspecializedType = type as SpecializedGenericType) + { + if (unspecializedType.UnspecializedType == typeof(Nullable<>)) + { + switch (ConvertTo(obj, unspecializedType.GetGenericArg(0))) + { + case .Ok(var ref val): + Variant.Alloc(type, var nullableVariant); + Internal.MemCpy(nullableVariant.DataPtr, val.DataPtr, val.VariantType.Size); + *((bool*)nullableVariant.DataPtr + val.VariantType.Size) = true; + return nullableVariant; + case .Err: + return .Err; + } + } + } + + + return .Err; } }