1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-20 17:08:00 +02:00

Add GetCustomAttributes

This commit is contained in:
disarray2077 2022-02-26 13:43:56 -03:00
parent 3dd4212ccd
commit 9eecf83c22
4 changed files with 329 additions and 0 deletions

View file

@ -1,3 +1,4 @@
using System.Collections;
namespace System.Reflection
{
class AttributeInfo
@ -112,5 +113,237 @@ namespace System.Reflection
return .Err;
}
public struct CustomAttributeEnumerator : IEnumerator<Object>, IDisposable
{
void* data;
int32 attrIdx;
uint8 count;
Object targetAttr;
public this(void* inAttrData)
{
data = inAttrData;
data++;
attrIdx = 0;
count = data != null ? AttributeInfo.Decode!<uint8>(data) : 0;
targetAttr = null;
}
public Object Current
{
get
{
return targetAttr;
}
}
public bool MoveNext() mut
{
if (attrIdx >= count || data == null)
return false;
void* startPtr = data;
var len = AttributeInfo.Decode!<uint16>(data);
void* endPtr = (uint8*)startPtr + len;
var typeId = AttributeInfo.Decode!<TypeId>(data);
var methodIdx = AttributeInfo.Decode!<uint16>(data);
Type attrType = Type.[Friend]GetType(typeId);
TypeInstance attrTypeInst = attrType as TypeInstance;
MethodInfo methodInfo = .(attrTypeInst, attrTypeInst.[Friend]mMethodDataPtr + methodIdx);
Object[] args = scope Object[methodInfo.[Friend]mData.mMethodData.mParamCount];
int argIdx = 0;
while (data < endPtr)
{
var attrDataType = AttributeInfo.Decode!<TypeCode>(data);
switch (attrDataType)
{
case .Int8,
.UInt8,
.Char8,
.Boolean:
let attrData = AttributeInfo.Decode!<int8>(data);
args[argIdx] = scope:: box attrData;
case .Int16,
.UInt16,
.Char16:
let attrData = AttributeInfo.Decode!<int16>(data);
args[argIdx] = scope:: box attrData;
case .Int32,
.UInt32,
.Char32:
let attrData = AttributeInfo.Decode!<int32>(data);
args[argIdx] = scope:: box attrData;
case .Float:
let attrData = AttributeInfo.Decode!<float>(data);
args[argIdx] = scope:: box attrData;
case .Int64,
.UInt64,
.Double:
let attrData = AttributeInfo.Decode!<int64>(data);
args[argIdx] = scope:: box attrData;
case (TypeCode)typeof(TypeCode).MaxValue + 8: //BfConstType_TypeOf
let argTypeId = AttributeInfo.Decode!<int32>(data);
args[argIdx] = Type.[Friend]GetType((.)argTypeId);
case (TypeCode)255:
let stringId = AttributeInfo.Decode!<int32>(data);
String str = String.[Friend]sIdStringLiterals[stringId];
args[argIdx] = str;
default:
Runtime.FatalError("Not handled");
}
argIdx++;
}
Type boxedAttrType = attrType.BoxedType;
delete targetAttr;
targetAttr = boxedAttrType.CreateObject().Get();
if (methodInfo.Invoke((uint8*)Internal.UnsafeCastToPtr(targetAttr) + boxedAttrType.[Friend]mMemberDataOffset, params args) case .Ok(var val))
val.Dispose();
attrIdx++;
return true;
}
public void Dispose()
{
delete targetAttr;
}
public Result<Object> GetNext() mut
{
if (!MoveNext())
return .Err;
return Current;
}
}
public struct CustomAttributeEnumerator<T> : IEnumerator<T>
{
void* data;
int32 attrIdx;
uint8 count;
T targetAttr;
public this(void* inAttrData)
{
data = inAttrData;
data++;
attrIdx = 0;
count = data != null ? AttributeInfo.Decode!<uint8>(data) : 0;
targetAttr = ?;
}
public T Current
{
get
{
return targetAttr;
}
}
public bool MoveNext() mut
{
if (attrIdx >= count || data == null)
return false;
void* endPtr = null;
TypeId typeId = 0;
while (true)
{
void* startPtr = data;
var len = AttributeInfo.Decode!<uint16>(data);
endPtr = (uint8*)startPtr + len;
typeId = AttributeInfo.Decode!<TypeId>(data);
if (typeId != typeof(T).TypeId)
{
attrIdx++;
if (attrIdx >= count)
return false;
data = endPtr;
continue;
}
break;
}
var methodIdx = AttributeInfo.Decode!<uint16>(data);
Type attrType = Type.[Friend]GetType(typeId);
TypeInstance attrTypeInst = attrType as TypeInstance;
MethodInfo methodInfo = .(attrTypeInst, attrTypeInst.[Friend]mMethodDataPtr + methodIdx);
Object[] args = scope Object[methodInfo.[Friend]mData.mMethodData.mParamCount];
int argIdx = 0;
while (data < endPtr)
{
var attrDataType = AttributeInfo.Decode!<TypeCode>(data);
switch (attrDataType)
{
case .Int8,
.UInt8,
.Char8,
.Boolean:
let attrData = AttributeInfo.Decode!<int8>(data);
args[argIdx] = scope:: box attrData;
case .Int16,
.UInt16,
.Char16:
let attrData = AttributeInfo.Decode!<int16>(data);
args[argIdx] = scope:: box attrData;
case .Int32,
.UInt32,
.Char32:
let attrData = AttributeInfo.Decode!<int32>(data);
args[argIdx] = scope:: box attrData;
case .Float:
let attrData = AttributeInfo.Decode!<float>(data);
args[argIdx] = scope:: box attrData;
case .Int64,
.UInt64,
.Double:
let attrData = AttributeInfo.Decode!<int64>(data);
args[argIdx] = scope:: box attrData;
case (TypeCode)typeof(TypeCode).MaxValue + 8: //BfConstType_TypeOf
let argTypeId = AttributeInfo.Decode!<int32>(data);
args[argIdx] = Type.[Friend]GetType((.)argTypeId);
case (TypeCode)255:
let stringId = AttributeInfo.Decode!<int32>(data);
String str = String.[Friend]sIdStringLiterals[stringId];
args[argIdx] = str;
default:
Runtime.FatalError("Not handled");
}
argIdx++;
}
if (methodInfo.Invoke(&targetAttr, params args) case .Ok(var val))
val.Dispose();
attrIdx++;
return true;
}
public void Dispose()
{
}
public Result<T> GetNext() mut
{
if (!MoveNext())
return .Err;
return Current;
}
}
}
}