From 5b75bc1ecc28015fa2ea1c3b56139e1824f8b5d7 Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Thu, 3 Mar 2022 22:40:21 -0300 Subject: [PATCH 1/4] Increase StaticInitPriority of FFIType --- BeefLibs/corlib/src/FFI/Function.bf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BeefLibs/corlib/src/FFI/Function.bf b/BeefLibs/corlib/src/FFI/Function.bf index cfbd63b3..149f59b5 100644 --- a/BeefLibs/corlib/src/FFI/Function.bf +++ b/BeefLibs/corlib/src/FFI/Function.bf @@ -1,6 +1,6 @@ namespace System.FFI { - [CRepr] + [CRepr, StaticInitPriority(100)] struct FFIType { public enum TypeKind : uint16 From e4e5933ec92ac44d8250f33beccf26451817b400 Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Fri, 4 Mar 2022 11:42:10 -0300 Subject: [PATCH 2/4] Improve ConvertTo Variant --- BeefLibs/corlib/src/Reflection/Convert.bf | 3 +++ 1 file changed, 3 insertions(+) diff --git a/BeefLibs/corlib/src/Reflection/Convert.bf b/BeefLibs/corlib/src/Reflection/Convert.bf index 8c019151..e8a2032c 100644 --- a/BeefLibs/corlib/src/Reflection/Convert.bf +++ b/BeefLibs/corlib/src/Reflection/Convert.bf @@ -94,6 +94,9 @@ namespace System.Reflection int64 intVal = ToInt64(variant); switch (type.[Friend]mTypeCode) { + case .Boolean: + bool val = intVal != 0; + return Variant.Create(type, &val); case .Float: float val = (.)intVal; return Variant.Create(type, &val); From 9f441ed8f7ecfb8c261546632b40f670337595bf Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Fri, 4 Mar 2022 13:50:34 -0300 Subject: [PATCH 3/4] Add `AllocOwned` to `Variant` --- BeefLibs/corlib/src/Variant.bf | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/BeefLibs/corlib/src/Variant.bf b/BeefLibs/corlib/src/Variant.bf index 825b5f96..0aed61ad 100644 --- a/BeefLibs/corlib/src/Variant.bf +++ b/BeefLibs/corlib/src/Variant.bf @@ -265,6 +265,23 @@ namespace System } } + public static void* AllocOwned(Type type, out Variant variant) + { + variant = .(); + + if (type.IsObject) + { + return &variant.mData; + } + else + { + variant.mStructType = (int)Internal.UnsafeCastToPtr(type) | 1; + void* data = new uint8[type.[Friend]mSize]*; + variant.mData = (int)data; + return data; + } + } + public T Get() where T : class { Debug.Assert(IsObject); From 6a4b652721f21f008d56a807ce969d56db9864f6 Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Fri, 4 Mar 2022 14:25:04 -0300 Subject: [PATCH 4/4] Fix `CustomAttributeEnumerator` --- .../corlib/src/Reflection/AttributeInfo.bf | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/BeefLibs/corlib/src/Reflection/AttributeInfo.bf b/BeefLibs/corlib/src/Reflection/AttributeInfo.bf index a7f34f4f..4a230c30 100644 --- a/BeefLibs/corlib/src/Reflection/AttributeInfo.bf +++ b/BeefLibs/corlib/src/Reflection/AttributeInfo.bf @@ -157,7 +157,7 @@ namespace System.Reflection TypeInstance attrTypeInst = attrType as TypeInstance; MethodInfo methodInfo = .(attrTypeInst, attrTypeInst.[Friend]mMethodDataPtr + methodIdx); - Object[] args = scope Object[methodInfo.[Friend]mData.mMethodData.mParamCount]; + Variant[] args = scope Variant[methodInfo.[Friend]mData.mMethodData.mParamCount]; int argIdx = 0; while (mData < endPtr) @@ -170,32 +170,32 @@ namespace System.Reflection .Char8, .Boolean: let attrData = AttributeInfo.Decode!(mData); - args[argIdx] = scope:: box attrData; + args[argIdx] = Variant.Create(attrData); case .Int16, .UInt16, .Char16: let attrData = AttributeInfo.Decode!(mData); - args[argIdx] = scope:: box attrData; + args[argIdx] = Variant.Create(attrData); case .Int32, .UInt32, .Char32: let attrData = AttributeInfo.Decode!(mData); - args[argIdx] = scope:: box attrData; + args[argIdx] = Variant.Create(attrData); case .Float: let attrData = AttributeInfo.Decode!(mData); - args[argIdx] = scope:: box attrData; + args[argIdx] = Variant.Create(attrData); case .Int64, .UInt64, .Double: let attrData = AttributeInfo.Decode!(mData); - args[argIdx] = scope:: box attrData; + args[argIdx] = Variant.Create(attrData); case (TypeCode)typeof(TypeCode).MaxValue + 8: //BfConstType_TypeOf let argTypeId = AttributeInfo.Decode!(mData); - args[argIdx] = Type.[Friend]GetType((.)argTypeId); + args[argIdx] = Variant.Create(Type.[Friend]GetType((.)argTypeId)); case (TypeCode)255: let stringId = AttributeInfo.Decode!(mData); String str = String.[Friend]sIdStringLiterals[stringId]; - args[argIdx] = str; + args[argIdx] = Variant.Create(str); default: Runtime.FatalError("Not handled"); } @@ -203,10 +203,13 @@ namespace System.Reflection } mTargetAttr.Dispose(); - void* data = Variant.Alloc(attrType, out mTargetAttr); + Variant.AllocOwned(attrType, out mTargetAttr); - if (methodInfo.Invoke(data, params args) case .Ok(var val)) + if (methodInfo.Invoke(mTargetAttr, params args) case .Ok(var val)) val.Dispose(); + + for (var variant in ref args) + variant.Dispose(); mAttrIdx++; return true;