Skip to content

Commit

Permalink
Move GetRefAny (with FCThrow) to managed (#110344)
Browse files Browse the repository at this point in the history
* Move GetRefAny (with FCThrow) to managed

* Move impl. to TypeReference
  • Loading branch information
am11 authored Dec 4, 2024
1 parent 949c3ec commit e165eb4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// These are blob that must be dealt with by the compiler.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;

Expand All @@ -16,6 +17,22 @@ public ref partial struct TypedReference
private readonly ref byte _value;
private readonly IntPtr _type;

// implementation of CORINFO_HELP_GETREFANY
[StackTraceHidden]
internal static ref byte GetRefAny(IntPtr clsHnd, TypedReference typedByRef)
{
if (clsHnd != typedByRef._type)
{
ThrowInvalidCastException();
}

return ref typedByRef._value;

[DoesNotReturn]
[StackTraceHidden]
static void ThrowInvalidCastException() => throw new InvalidCastException();
}

private TypedReference(ref byte target, RuntimeType type)
{
_value = ref target;
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/inc/jithelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
DYNAMICJITHELPER(CORINFO_HELP_UNBOX_TYPETEST,NULL, METHOD__CASTHELPERS__UNBOX_TYPETEST)
DYNAMICJITHELPER(CORINFO_HELP_UNBOX_NULLABLE,NULL, METHOD__CASTHELPERS__UNBOX_NULLABLE)

JITHELPER(CORINFO_HELP_GETREFANY, JIT_GetRefAny, METHOD__NIL)
DYNAMICJITHELPER(CORINFO_HELP_GETREFANY, NULL, METHOD__TYPED_REFERENCE__GETREFANY)
DYNAMICJITHELPER(CORINFO_HELP_ARRADDR_ST, NULL, METHOD__CASTHELPERS__STELEMREF)
DYNAMICJITHELPER(CORINFO_HELP_LDELEMA_REF, NULL, METHOD__CASTHELPERS__LDELEMAREF)

Expand Down
7 changes: 5 additions & 2 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ DEFINE_METHOD(RT_TYPE_HANDLE, ALLOCATECOMOBJECT, AllocateComObject,
#endif
DEFINE_FIELD(RT_TYPE_HANDLE, M_TYPE, m_type)

// DEFINE_CLASS(TYPED_REFERENCE, System, TypedReference)
DEFINE_METHOD(TYPED_REFERENCE, GETREFANY, GetRefAny, NoSig)

DEFINE_CLASS(TYPE_NAME_RESOLVER, Reflection, TypeNameResolver)
DEFINE_METHOD(TYPE_NAME_RESOLVER, GET_TYPE_HELPER, GetTypeHelper, SM_Type_CharPtr_RuntimeAssembly_Bool_Bool_RetRuntimeType)

Expand Down Expand Up @@ -1178,8 +1181,8 @@ DEFINE_METHOD(CASTHELPERS, UNBOX, Unbox, NoSig)
DEFINE_METHOD(CASTHELPERS, STELEMREF, StelemRef, SM_ArrObject_IntPtr_Obj_RetVoid)
DEFINE_METHOD(CASTHELPERS, LDELEMAREF, LdelemaRef, SM_ArrObject_IntPtr_PtrVoid_RetRefObj)
DEFINE_METHOD(CASTHELPERS, ARRAYTYPECHECK, ArrayTypeCheck, SM_Obj_Array_RetVoid)
DEFINE_METHOD(CASTHELPERS, UNBOX_NULLABLE, Unbox_Nullable, NoSig)
DEFINE_METHOD(CASTHELPERS, UNBOX_TYPETEST, Unbox_TypeTest, NoSig)
DEFINE_METHOD(CASTHELPERS, UNBOX_NULLABLE, Unbox_Nullable, NoSig)
DEFINE_METHOD(CASTHELPERS, UNBOX_TYPETEST, Unbox_TypeTest, NoSig)

DEFINE_CLASS(VIRTUALDISPATCHHELPERS, CompilerServices, VirtualDispatchHelpers)
DEFINE_METHOD(VIRTUALDISPATCHHELPERS, VIRTUALFUNCTIONPOINTER, VirtualFunctionPointer, NoSig)
Expand Down
18 changes: 0 additions & 18 deletions src/coreclr/vm/jithelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,24 +1382,6 @@ HCIMPL2(Object*, JIT_Box, CORINFO_CLASS_HANDLE type, void* unboxedData)
}
HCIMPLEND

/*************************************************************/
HCIMPL2_IV(LPVOID, JIT_GetRefAny, CORINFO_CLASS_HANDLE type, TypedByRef typedByRef)
{
FCALL_CONTRACT;

TypeHandle clsHnd(type);
// <TODO>@TODO right now we check for precisely the correct type.
// do we want to allow inheritance? (watch out since value
// classes inherit from object but do not normal object layout).</TODO>
if (clsHnd != typedByRef.type) {
FCThrow(kInvalidCastException);
}

return(typedByRef.data);
}
HCIMPLEND


/*************************************************************/
HCIMPL2(BOOL, JIT_IsInstanceOfException, CORINFO_CLASS_HANDLE type, Object* obj)
{
Expand Down

0 comments on commit e165eb4

Please sign in to comment.