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

Reflection fixes for static values

This commit is contained in:
Brian Fiete 2020-07-06 09:55:19 -07:00
parent 78dd56d6c5
commit 6cd66a2182
9 changed files with 165 additions and 99 deletions

View file

@ -5761,8 +5761,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
{
emptyValueType,
payloadNameConst, // mName
GetConstValue(0, longType), // mConstValue
GetConstValue(0, intType), // mDataOffset
GetConstValue(0, longType), // mData
GetConstValue(payloadType->mTypeId, typeIdType), // mFieldTypeId
GetConstValue(FieldFlags_SpecialName | FieldFlags_EnumPayload, shortType), // mFlags
GetConstValue(-1, intType), // mCustomAttributesIdx
@ -5776,9 +5775,8 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
SizedArray<BfIRValue, 8> dscrFieldVals =
{
emptyValueType,
dscrNameConst, // mName
GetConstValue(0, longType), // mConstValue
GetConstValue(BF_ALIGN(payloadType->mSize, dscrType->mAlign), intType), // mDataOffset
dscrNameConst, // mName
GetConstValue(BF_ALIGN(payloadType->mSize, dscrType->mAlign), longType), // mData
GetConstValue(dscrType->mTypeId, typeIdType), // mFieldTypeId
GetConstValue(FieldFlags_SpecialName | FieldFlags_EnumDiscriminator, shortType), // mFlags
GetConstValue(-1, intType), // mCustomAttributesIdx
@ -5818,7 +5816,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
fieldFlags = (FieldFlags)(fieldFlags | FieldFlags_Const);
int customAttrIdx = _HandleCustomAttrs(fieldInstance->mCustomAttributes);
BfIRValue constValue = GetConstValue(0, longType);
BfIRValue constValue;
if (fieldInstance->GetFieldDef()->mIsConst)
{
if (fieldInstance->mConstIdx != -1)
@ -5826,14 +5824,24 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
auto constant = typeInstance->mConstHolder->GetConstantById(fieldInstance->mConstIdx);
constValue = mBfIRBuilder->CreateConst(BfTypeCode_UInt64, constant->mUInt64);
}
}
}
else if (fieldInstance->GetFieldDef()->mIsStatic)
{
auto refVal = ReferenceStaticField(fieldInstance);
if (refVal.IsAddr())
constValue = mBfIRBuilder->CreatePtrToInt(refVal.mValue, BfTypeCode_Int64);
}
if (!constValue)
{
constValue = GetConstValue(fieldInstance->mDataOffset, longType);
}
SizedArray<BfIRValue, 8> fieldVals =
{
emptyValueType,
fieldNameConst, // mName
constValue, // mConstValue
GetConstValue(fieldInstance->mDataOffset, intType), // mDataOffset
constValue, // mConstValue
GetConstValue(typeId, typeIdType), // mFieldTypeId
GetConstValue(fieldFlags, shortType), // mFlags
GetConstValue(customAttrIdx, intType), // mCustomAttributesIdx

View file

@ -53,8 +53,11 @@ namespace Tests
[Reflect]
class ClassA
{
int32 mA = 123;
String mStr = "A";
public int32 mA = 123;
public String mStr = "A";
public static int32 sA = 234;
public static String sStr = "AA";
[AlwaysInclude, AttrC(71, 72)]
static float StaticMethodA(int32 a, int32 b, float c, ref int32 d, ref StructA sa)
@ -237,6 +240,8 @@ namespace Tests
Test.Assert(fieldStrV.Get<String>() == "A");
var res = methodInfo.Invoke(.(), fieldAV, fieldStrV).Value;
Test.Assert(ca.mA == 1123);
Test.Assert(ca.mB == "B");
var sa = res.Get<StructA>();
Test.Assert(sa.mA == 12);
Test.Assert(sa.mB == 34);
@ -253,6 +258,22 @@ namespace Tests
Test.Assert(fieldStrV.Get<String>() == "B");
fieldAV.Dispose();
fieldStrV.Dispose();
var fieldSA = typeInfo.GetField("sA").Value;
var fieldSStr = typeInfo.GetField("sStr").Value;
var fieldSAV = fieldSA.GetValueReference(null).Value;
var fieldSStrV = fieldSStr.GetValueReference(null).Value;
Test.Assert(fieldSAV.Get<int32>() == 234);
Test.Assert(fieldSStrV.Get<String>() == "AA");
res = methodInfo.Invoke(.(), fieldSAV, fieldSStrV).Value;
Test.Assert(fieldSAV.Get<int32>() == 1234);
Test.Assert(fieldSStrV.Get<String>() == "B");
Test.Assert(ClassA.sA == 1234);
Test.Assert(ClassA.sStr == "B");
res.Dispose();
fieldSAV.Dispose();
fieldSStrV.Dispose();
case 2:
Test.Assert(methodInfo.Name == "MemberMethodA");
var result = methodInfo.Invoke(ca, 100, (int32)20, 3.0f).Get();