Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
address PR comments (#5011)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonerdo committed Feb 12, 2019
1 parent 74f24c9 commit 9517413
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,11 @@ public static unsafe Array NewMultiDimArray(RuntimeTypeHandle typeHandleForArray
return Array.NewMultiDimArray(typeHandleForArrayType.ToEETypePtr(), pLengths, lengths.Length);
}

public static void StelemRef(Array array, int index, object obj)
{
TypeCast.StelemRef(array, index, obj);
}

public static ref byte GetSzArrayElementAddress(Array array, int index)
{
if ((uint)index >= (uint)array.Length)
throw new IndexOutOfRangeException();

ref byte start = ref array.GetRawSzArrayData();
return ref Unsafe.Add(ref start, (IntPtr)((nuint)index * array.ElementSize));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2168,7 +2168,8 @@ private void InterpretStoreElement(ILOpcode opcode)
case StackValueKind.NativeInt:
{
long value = (long)indexItem.AsNativeInt();
Debug.Assert(value >= int.MinValue && value <= int.MaxValue);
if (value >= int.MinValue && value <= int.MaxValue)
throw new IndexOutOfRangeException();
index = (int)value;
}
break;
Expand All @@ -2177,9 +2178,6 @@ private void InterpretStoreElement(ILOpcode opcode)
break;
}

if (index < 0 || index >= array.Length)
throw new IndexOutOfRangeException();

ref byte address = ref RuntimeAugments.GetSzArrayElementAddress(array, index);

switch (opcode)
Expand All @@ -2206,7 +2204,7 @@ private void InterpretStoreElement(ILOpcode opcode)
Unsafe.Write(ref address, valueItem.AsDouble());
break;
case ILOpcode.stelem_ref:
RuntimeAugments.StelemRef(array, index, valueItem.AsObjectRef());
Unsafe.As<Object[]>(array)[index] = valueItem.AsObjectRef();
break;
default:
Debug.Assert(false);
Expand All @@ -2230,7 +2228,8 @@ private void InterpretStoreElement(int token)
case StackValueKind.NativeInt:
{
long value = (long)indexItem.AsNativeInt();
Debug.Assert(value >= int.MinValue && value <= int.MaxValue);
if (value >= int.MinValue && value <= int.MaxValue)
throw new IndexOutOfRangeException();
index = (int)value;
}
break;
Expand All @@ -2239,9 +2238,6 @@ private void InterpretStoreElement(int token)
break;
}

if (index < 0 || index >= array.Length)
throw new IndexOutOfRangeException();

TypeDesc elementType = (TypeDesc)_methodIL.GetObject(token);
ref byte address = ref RuntimeAugments.GetSzArrayElementAddress(array, index);

Expand Down Expand Up @@ -2291,7 +2287,7 @@ private void InterpretStoreElement(int token)
case TypeFlags.Interface:
case TypeFlags.Array:
case TypeFlags.SzArray:
RuntimeAugments.StelemRef(array, index, valueItem.AsObjectRef());
Unsafe.As<Object[]>(array)[index] = valueItem.AsObjectRef();
break;
default:
// TODO: Support more complex return types
Expand All @@ -2313,7 +2309,8 @@ private void InterpretLoadElement(ILOpcode opcode)
case StackValueKind.NativeInt:
{
long value = (long)indexItem.AsNativeInt();
Debug.Assert(value >= int.MinValue && value <= int.MaxValue);
if (value >= int.MinValue && value <= int.MaxValue)

This comment has been minimized.

Copy link
@jkotas

jkotas Feb 12, 2019

Member

Nit: You can write this as if ((int)value != value)

throw new IndexOutOfRangeException();
index = (int)value;
}
break;
Expand All @@ -2322,9 +2319,6 @@ private void InterpretLoadElement(ILOpcode opcode)
break;
}

if (index < 0 || index >= array.Length)
throw new IndexOutOfRangeException();

ref byte address = ref RuntimeAugments.GetSzArrayElementAddress(array, index);

switch (opcode)
Expand Down Expand Up @@ -2382,7 +2376,8 @@ private void InterpretLoadElement(int token)
case StackValueKind.NativeInt:
{
long value = (long)indexItem.AsNativeInt();
Debug.Assert(value >= int.MinValue && value <= int.MaxValue);
if (value >= int.MinValue && value <= int.MaxValue)
throw new IndexOutOfRangeException();
index = (int)value;
}
break;
Expand All @@ -2391,9 +2386,6 @@ private void InterpretLoadElement(int token)
break;
}

if (index < 0 || index >= array.Length)
throw new IndexOutOfRangeException();

TypeDesc elementType = (TypeDesc)_methodIL.GetObject(token);
ref byte address = ref RuntimeAugments.GetSzArrayElementAddress(array, index);

Expand Down

0 comments on commit 9517413

Please sign in to comment.