Skip to content

Various fixes for generics #3163

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

Merged
merged 3 commits into from
Apr 30, 2025
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
1 change: 1 addition & 0 deletions src/CLR/Core/CLR_RT_HeapBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ HRESULT CLR_RT_HeapBlock::SetGenericInstanceObject(const CLR_RT_TypeSpec_Index &

m_data.genericInstance.genericType = genericType;
m_data.genericInstance.ptr = nullptr;
m_id.raw = CLR_RT_HEAPBLOCK_RAW_ID(DATATYPE_GENERICINST, 0, 1);

NANOCLR_NOCLEANUP();
}
Expand Down
22 changes: 13 additions & 9 deletions src/CLR/Core/Execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ HRESULT CLR_RT_ExecutionEngine::ExecutionEngine_Initialize()
// CLR_RT_Thread* m_cctorThread;
//
#if !defined(NANOCLR_APPDOMAINS)
m_globalLock = nullptr; // CLR_RT_HeapBlock* m_globalLock;
m_globalLock = nullptr; // CLR_RT_HeapBlock* m_globalLock;
m_outOfMemoryException = nullptr; // CLR_RT_HeapBlock* m_outOfMemoryException;
#endif

Expand Down Expand Up @@ -2262,27 +2262,31 @@ HRESULT CLR_RT_ExecutionEngine::NewGenericInstanceObject(
const CLR_RECORD_FIELDDEF *target = nullptr;
CLR_RT_Assembly *assm = nullptr;
CLR_RT_TypeDef_Instance instSub = instance;
CLR_RT_HeapBlock_GenericInstance *giHeader = nullptr;
CLR_RT_HeapBlock *fieldCursor = nullptr;

reference.SetObjectReference(nullptr);

int clsFields = instance.target->instanceFieldsCount;
int totFields = instance.CrossReference().totalFields + CLR_RT_HeapBlock::HB_Object_Fields_Offset;

CLR_RT_HeapBlock_GenericInstance *genericInst;
giHeader = (CLR_RT_HeapBlock_GenericInstance *)ExtractHeapBlocksForGenericInstance(0, genericInstance, totFields);
CHECK_ALLOCATION(giHeader);

genericInst =
(CLR_RT_HeapBlock_GenericInstance *)ExtractHeapBlocksForGenericInstance(0, genericInstance, totFields);
CHECK_ALLOCATION(genericInst);
reference.SetObjectReference(giHeader);

reference.SetObjectReference(genericInst);
// Associate the instance with its declaring type for reflection & casting utilities.
giHeader->SetObjectCls(instance);

//
// Initialize field types, from last to first.
//
// We do the decrement BEFORE the comparison because we want to stop short of the first field, the
// object descriptor (already initialized).
//
genericInst += totFields;

fieldCursor = reinterpret_cast<CLR_RT_HeapBlock *>(giHeader) + totFields;

while (--totFields > 0)
{
while (clsFields == 0)
Expand All @@ -2302,11 +2306,11 @@ HRESULT CLR_RT_ExecutionEngine::NewGenericInstanceObject(
target = assm->GetFieldDef(instSub.target->firstInstanceField + clsFields);
}

genericInst--;
fieldCursor--;
target--;
clsFields--;

NANOCLR_CHECK_HRESULT(InitializeReference(*genericInst, target, assm));
NANOCLR_CHECK_HRESULT(InitializeReference(*fieldCursor, target, assm));
}

if (instance.HasFinalizer())
Expand Down
23 changes: 20 additions & 3 deletions src/CLR/Core/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,7 @@ HRESULT CLR_RT_Thread::Execute_IL(CLR_RT_StackFrame &stackArg)
{
case DATATYPE_CLASS:
case DATATYPE_VALUETYPE:
case DATATYPE_GENERICINST:
evalPos[0].Assign(obj[fieldInst.CrossReference().offset]);
goto Execute_LoadAndPromote;
case DATATYPE_DATETIME:
Expand Down Expand Up @@ -2600,13 +2601,17 @@ HRESULT CLR_RT_Thread::Execute_IL(CLR_RT_StackFrame &stackArg)
#if defined(NANOCLR_APPDOMAINS)
_ASSERTE(dt != DATATYPE_TRANSPARENT_PROXY);
#endif
if (dt == DATATYPE_CLASS || dt == DATATYPE_VALUETYPE)
if (dt == DATATYPE_CLASS || dt == DATATYPE_VALUETYPE || dt == DATATYPE_GENERICINST)
{
// This is a reference to the field.
// We need to make sure that the object is not a transparent proxy.
evalPos[0].SetReference(obj[fieldInst.CrossReference().offset]);
}
else if (dt == DATATYPE_DATETIME || dt == DATATYPE_TIMESPAN) // Special case.
// Special case.
else if (dt == DATATYPE_DATETIME || dt == DATATYPE_TIMESPAN)
{
NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE); // NOT SUPPORTED.
// NOT SUPPORTED.
NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
}
else
{
Expand Down Expand Up @@ -3212,6 +3217,18 @@ HRESULT CLR_RT_Thread::Execute_IL(CLR_RT_StackFrame &stackArg)
}
break;

case TBL_MethodSpec:
{
CLR_RT_MethodDef_Instance method{};
if (!method.ResolveToken(arg, assm))
{
NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
}

evalPos[0].SetReflection(method);
}
break;

default:
NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/CLR/Include/nanoCLR_Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ struct CLR_RT_MethodSpec_Index

CLR_INDEX Method() const
{
return (data & 0x7FFF);
return (CLR_INDEX)data;
}
};

Expand Down
1 change: 1 addition & 0 deletions src/CLR/Include/nanoCLR_Runtime__HeapBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,7 @@ struct CLR_RT_HeapBlock
{
m_id.raw = CLR_RT_HEAPBLOCK_RAW_ID(DATATYPE_GENERICINST, 0, 1);
m_data.genericInstance.genericType.data = genericType.data;
m_data.genericInstance.ptr = nullptr;
}

const CLR_RT_TypeSpec_Index &ObjectGenericType() const
Expand Down