Skip to content

Commit 6f3f4cc

Browse files
committed
Move GetRefAny (with FCThrow) to managed
1 parent 758bbc6 commit 6f3f4cc

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastHelpers.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Diagnostics;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Runtime.InteropServices;
@@ -24,6 +25,9 @@ internal static void ThrowInvalidCastException(object fromType, void* toTypeHnd)
2425
throw null!; // Provide hint to the inliner that this method does not return
2526
}
2627

28+
[DoesNotReturn]
29+
internal static void ThrowInvalidCastException() => throw new InvalidCastException();
30+
2731
[MethodImpl(MethodImplOptions.InternalCall)]
2832
private static extern object IsInstanceOfAny_NoCacheLookup(void* toTypeHnd, object obj);
2933

@@ -33,6 +37,19 @@ internal static void ThrowInvalidCastException(object fromType, void* toTypeHnd)
3337
[MethodImpl(MethodImplOptions.InternalCall)]
3438
private static extern void WriteBarrier(ref object? dst, object? obj);
3539

40+
internal static ref byte GetRefAny(IntPtr clsHnd, TypedReference typedByRef)
41+
{
42+
// <TODO>@TODO right now we check for precisely the correct type.
43+
// do we want to allow inheritance? (watch out since value
44+
// classes inherit from object but do not normal object layout).</TODO>
45+
if (clsHnd != typedByRef.Type)
46+
{
47+
ThrowInvalidCastException();
48+
}
49+
50+
return ref typedByRef.Value;
51+
}
52+
3653
// IsInstanceOf test used for unusual cases (naked type parameters, variant generic types)
3754
// Unlike the IsInstanceOfInterface and IsInstanceOfClass functions,
3855
// this test must deal with all kinds of type tests

src/coreclr/System.Private.CoreLib/src/System/TypedReference.CoreCLR.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public ref partial struct TypedReference
1616
private readonly ref byte _value;
1717
private readonly IntPtr _type;
1818

19+
internal readonly IntPtr Type => _type;
20+
internal readonly ref byte Value => ref _value;
21+
1922
private TypedReference(ref byte target, RuntimeType type)
2023
{
2124
_value = ref target;

src/coreclr/inc/jithelpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
DYNAMICJITHELPER(CORINFO_HELP_UNBOX_TYPETEST,NULL, METHOD__CASTHELPERS__UNBOX_TYPETEST)
118118
DYNAMICJITHELPER(CORINFO_HELP_UNBOX_NULLABLE,NULL, METHOD__CASTHELPERS__UNBOX_NULLABLE)
119119

120-
JITHELPER(CORINFO_HELP_GETREFANY, JIT_GetRefAny, METHOD__NIL)
120+
DYNAMICJITHELPER(CORINFO_HELP_GETREFANY, NULL, METHOD__CASTHELPERS__GETREFANY)
121121
DYNAMICJITHELPER(CORINFO_HELP_ARRADDR_ST, NULL, METHOD__CASTHELPERS__STELEMREF)
122122
DYNAMICJITHELPER(CORINFO_HELP_LDELEMA_REF, NULL, METHOD__CASTHELPERS__LDELEMAREF)
123123

src/coreclr/vm/corelib.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,8 +1178,9 @@ DEFINE_METHOD(CASTHELPERS, UNBOX, Unbox, NoSig)
11781178
DEFINE_METHOD(CASTHELPERS, STELEMREF, StelemRef, SM_ArrObject_IntPtr_Obj_RetVoid)
11791179
DEFINE_METHOD(CASTHELPERS, LDELEMAREF, LdelemaRef, SM_ArrObject_IntPtr_PtrVoid_RetRefObj)
11801180
DEFINE_METHOD(CASTHELPERS, ARRAYTYPECHECK, ArrayTypeCheck, SM_Obj_Array_RetVoid)
1181-
DEFINE_METHOD(CASTHELPERS, UNBOX_NULLABLE, Unbox_Nullable, NoSig)
1182-
DEFINE_METHOD(CASTHELPERS, UNBOX_TYPETEST, Unbox_TypeTest, NoSig)
1181+
DEFINE_METHOD(CASTHELPERS, UNBOX_NULLABLE, Unbox_Nullable, NoSig)
1182+
DEFINE_METHOD(CASTHELPERS, UNBOX_TYPETEST, Unbox_TypeTest, NoSig)
1183+
DEFINE_METHOD(CASTHELPERS, GETREFANY, GetRefAny, NoSig)
11831184

11841185
DEFINE_CLASS(VIRTUALDISPATCHHELPERS, CompilerServices, VirtualDispatchHelpers)
11851186
DEFINE_METHOD(VIRTUALDISPATCHHELPERS, VIRTUALFUNCTIONPOINTER, VirtualFunctionPointer, NoSig)

src/coreclr/vm/jithelpers.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,24 +1382,6 @@ HCIMPL2(Object*, JIT_Box, CORINFO_CLASS_HANDLE type, void* unboxedData)
13821382
}
13831383
HCIMPLEND
13841384

1385-
/*************************************************************/
1386-
HCIMPL2_IV(LPVOID, JIT_GetRefAny, CORINFO_CLASS_HANDLE type, TypedByRef typedByRef)
1387-
{
1388-
FCALL_CONTRACT;
1389-
1390-
TypeHandle clsHnd(type);
1391-
// <TODO>@TODO right now we check for precisely the correct type.
1392-
// do we want to allow inheritance? (watch out since value
1393-
// classes inherit from object but do not normal object layout).</TODO>
1394-
if (clsHnd != typedByRef.type) {
1395-
FCThrow(kInvalidCastException);
1396-
}
1397-
1398-
return(typedByRef.data);
1399-
}
1400-
HCIMPLEND
1401-
1402-
14031385
/*************************************************************/
14041386
HCIMPL2(BOOL, JIT_IsInstanceOfException, CORINFO_CLASS_HANDLE type, Object* obj)
14051387
{

0 commit comments

Comments
 (0)