@@ -756,11 +756,11 @@ public static unsafe void StelemRef(Array array, nint index, object obj)
756
756
{
757
757
// TODO: If both array and obj are null, we're likely going to throw Redhawk's NullReferenceException.
758
758
// This should blame the caller.
759
- throw obj . MethodTable ->GetClasslibException ( ExceptionIDs . NullReference ) ;
759
+ throw obj . GetMethodTable ( ) ->GetClasslibException ( ExceptionIDs . NullReference ) ;
760
760
}
761
761
if ( ( uint ) index >= ( uint ) array . Length )
762
762
{
763
- throw array . MethodTable ->GetClasslibException ( ExceptionIDs . IndexOutOfRange ) ;
763
+ throw array . GetMethodTable ( ) ->GetClasslibException ( ExceptionIDs . IndexOutOfRange ) ;
764
764
}
765
765
ref object rawData = ref Unsafe . As < byte , object > ( ref Unsafe . As < RawArrayData > ( array ) . Data ) ;
766
766
ref object element = ref Unsafe . Add ( ref rawData , index ) ;
@@ -813,24 +813,44 @@ private static unsafe void StelemRef_Helper(ref object element, MethodTable* ele
813
813
}
814
814
}
815
815
816
+ // This weird structure is for parity with CoreCLR - allows potentially to be tailcalled
817
+ private static unsafe ref object ThrowArrayMismatchException ( Array array )
818
+ {
819
+ // Throw the array type mismatch exception defined by the classlib, using the input array's MethodTable*
820
+ // to find the correct classlib.
821
+ throw array . GetMethodTable ( ) ->GetClasslibException ( ExceptionIDs . ArrayTypeMismatch ) ;
822
+ }
823
+
816
824
[ RuntimeExport ( "RhpLdelemaRef" ) ]
817
825
public static unsafe ref object LdelemaRef ( Array array , nint index , IntPtr elementType )
818
826
{
819
- Debug . Assert ( array . GetMethodTable ( ) ->IsArray , "first argument must be an array" ) ;
827
+ Debug . Assert ( array is null || array . GetMethodTable ( ) ->IsArray , "first argument must be an array" ) ;
828
+
829
+ #if INPLACE_RUNTIME
830
+ // this will throw appropriate exceptions if array is null or access is out of range.
831
+ ref object element = ref Unsafe . As < ArrayElement [ ] > ( array ) [ index ] . Value ;
832
+ #else
833
+ if ( array is null )
834
+ {
835
+ throw ( ( MethodTable * ) elementType ) ->GetClasslibException ( ExceptionIDs . NullReference ) ;
836
+ }
837
+ if ( ( uint ) index >= ( uint ) array . Length )
838
+ {
839
+ throw ( ( MethodTable * ) elementType ) ->GetClasslibException ( ExceptionIDs . IndexOutOfRange ) ;
840
+ }
841
+ ref object rawData = ref Unsafe . As < byte , object > ( ref Unsafe . As < RawArrayData > ( array ) . Data ) ;
842
+ ref object element = ref Unsafe . Add ( ref rawData , index ) ;
843
+ #endif
820
844
821
845
MethodTable * elemType = ( MethodTable * ) elementType ;
822
846
MethodTable * arrayElemType = array . GetMethodTable ( ) ->RelatedParameterType ;
823
847
824
- if ( ! AreTypesEquivalent ( elemType , arrayElemType ) )
848
+ if ( AreTypesEquivalent ( elemType , arrayElemType ) )
825
849
{
826
- // Throw the array type mismatch exception defined by the classlib, using the input array's MethodTable*
827
- // to find the correct classlib.
828
-
829
- throw array . GetMethodTable ( ) ->GetClasslibException ( ExceptionIDs . ArrayTypeMismatch ) ;
850
+ return ref element ;
830
851
}
831
852
832
- ref object rawData = ref Unsafe . As < byte , object > ( ref Unsafe . As < RawArrayData > ( array ) . Data ) ;
833
- return ref Unsafe . Add ( ref rawData , index ) ;
853
+ return ref ThrowArrayMismatchException ( array ) ;
834
854
}
835
855
836
856
internal static unsafe bool IsDerived ( MethodTable * pDerivedType , MethodTable * pBaseType )
0 commit comments