Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 6f9cf50

Browse files
committed
Move S.R.Metadata throw helpers together
1 parent 3384c2a commit 6f9cf50

27 files changed

+227
-241
lines changed

src/System.Reflection.Metadata/src/System.Reflection.Metadata.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<Compile Include="System\Reflection\PortableExecutable\PEReader.cs" />
156156
<Compile Include="System\Reflection\PortableExecutable\PEStreamOptions.cs" />
157157
<Compile Include="System\Reflection\PortableExecutable\SectionHeader.cs" />
158+
<Compile Include="System\Reflection\Throw.cs" />
158159
<Compile Include="System\Reflection\System.Reflection.cs" />
159160
</ItemGroup>
160161
<ItemGroup>

src/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/MemoryBlockProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public AbstractMemoryBlock GetMemoryBlock(int start, int size)
2929
// Negative start or size is handle by overflow to greater than maximum size = int.MaxValue.
3030
if ((ulong)(unchecked((uint)start)) + unchecked((uint)size) > (ulong)this.Size)
3131
{
32-
PEBinaryReader.ThrowImageTooSmallOrContainsInvalidOffsetOrCount();
32+
Throw.ImageTooSmallOrContainsInvalidOffsetOrCount();
3333
}
3434

3535
return GetMemoryBlockImpl(start, size);

src/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/MemoryBlock.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,10 @@ private void CheckBounds(int offset, int byteCount)
2828
{
2929
if (unchecked((ulong)(uint)offset + (uint)byteCount) > (ulong)Length)
3030
{
31-
ThrowOutOfBounds();
31+
Throw.OutOfBounds();
3232
}
3333
}
3434

35-
[MethodImpl(MethodImplOptions.NoInlining)]
36-
private static void ThrowOutOfBounds()
37-
{
38-
throw new BadImageFormatException(SR.OutOfBoundsRead);
39-
}
40-
41-
[MethodImpl(MethodImplOptions.NoInlining)]
42-
private static void ThrowReferenceOverflow()
43-
{
44-
throw new BadImageFormatException(SR.RowIdOrHeapOffsetTooLarge);
45-
}
46-
4735
internal byte[] ToArray()
4836
{
4937
return Pointer == null ? null : PeekBytes(0, Length);
@@ -176,7 +164,7 @@ internal int PeekReference(int offset, bool smallRefSize)
176164

177165
if (!TokenTypeIds.IsValidRowId(value))
178166
{
179-
ThrowReferenceOverflow();
167+
Throw.ReferenceOverflow();
180168
}
181169

182170
return (int)value;
@@ -194,7 +182,7 @@ internal int PeekHeapReference(int offset, bool smallRefSize)
194182

195183
if (!HeapHandleType.IsValidHeapOffset(value))
196184
{
197-
ThrowReferenceOverflow();
185+
Throw.ReferenceOverflow();
198186
}
199187

200188
return (int)value;

src/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobReader.cs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ internal void Align(byte alignment)
128128
{
129129
if (!TryAlign(alignment))
130130
{
131-
ThrowOutOfBounds();
131+
Throw.OutOfBounds();
132132
}
133133
}
134134

@@ -160,18 +160,12 @@ internal MemoryBlock GetMemoryBlockAt(int offset, int length)
160160

161161
#region Bounds Checking
162162

163-
[MethodImpl(MethodImplOptions.NoInlining)]
164-
private static void ThrowOutOfBounds()
165-
{
166-
throw new BadImageFormatException(SR.OutOfBoundsRead);
167-
}
168-
169163
[MethodImpl(MethodImplOptions.AggressiveInlining)]
170164
private void CheckBounds(int offset, int byteCount)
171165
{
172166
if (unchecked((ulong)(uint)offset + (uint)byteCount) > (ulong)(_endPointer - _currentPointer))
173167
{
174-
ThrowOutOfBounds();
168+
Throw.OutOfBounds();
175169
}
176170
}
177171

@@ -180,7 +174,7 @@ private void CheckBounds(int byteCount)
180174
{
181175
if (unchecked((uint)byteCount) > (_endPointer - _currentPointer))
182176
{
183-
ThrowOutOfBounds();
177+
Throw.OutOfBounds();
184178
}
185179
}
186180

@@ -191,7 +185,7 @@ private void CheckBounds(int byteCount)
191185

192186
if (unchecked((uint)length) > (uint)(_endPointer - p))
193187
{
194-
ThrowOutOfBounds();
188+
Throw.OutOfBounds();
195189
}
196190

197191
_currentPointer = p + length;
@@ -205,7 +199,7 @@ private void CheckBounds(int byteCount)
205199

206200
if (p == _endPointer)
207201
{
208-
ThrowOutOfBounds();
202+
Throw.OutOfBounds();
209203
}
210204

211205
_currentPointer = p + 1;
@@ -359,7 +353,7 @@ public int ReadCompressedInteger()
359353
int value;
360354
if (!TryReadCompressedInteger(out value))
361355
{
362-
ThrowInvalidCompressedInteger();
356+
Throw.InvalidCompressedInteger();
363357
}
364358
return value;
365359
}
@@ -415,23 +409,11 @@ public int ReadCompressedSignedInteger()
415409
int value;
416410
if (!TryReadCompressedSignedInteger(out value))
417411
{
418-
ThrowInvalidCompressedInteger();
412+
Throw.InvalidCompressedInteger();
419413
}
420414
return value;
421415
}
422416

423-
[MethodImpl(MethodImplOptions.NoInlining)]
424-
private static void ThrowInvalidCompressedInteger()
425-
{
426-
throw new BadImageFormatException(SR.InvalidCompressedInteger);
427-
}
428-
429-
[MethodImpl(MethodImplOptions.NoInlining)]
430-
private static void ThrowInvalidSerializedString()
431-
{
432-
throw new BadImageFormatException(SR.InvalidSerializedString);
433-
}
434-
435417
/// <summary>
436418
/// Reads type code encoded in a serialized custom attribute value.
437419
/// </summary>
@@ -490,7 +472,7 @@ public string ReadSerializedString()
490472

491473
if (ReadByte() != 0xFF)
492474
{
493-
ThrowInvalidSerializedString();
475+
Throw.InvalidSerializedString();
494476
}
495477

496478
return null;

src/System.Reflection.Metadata/src/System/Reflection/Metadata/Ecma335/MetadataReaderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static int GetTableRowCount(this MetadataReader reader, TableIndex tableI
2626

2727
if ((int)tableIndex >= TableIndexExtensions.Count)
2828
{
29-
throw new ArgumentOutOfRangeException("tableIndex");
29+
Throw.TableIndexOutOfRange();
3030
}
3131

3232
return (int)reader.TableRowCounts[(int)tableIndex];

src/System.Reflection.Metadata/src/System/Reflection/Metadata/Ecma335/MetadataTokens.cs

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static int GetHeapOffset(this MetadataReader reader, Handle handle)
4444
{
4545
if (!handle.IsHeapHandle)
4646
{
47-
ThrowHeapHandleRequired();
47+
Throw.HeapHandleRequired();
4848
}
4949

5050
if (handle.IsVirtual)
@@ -68,7 +68,7 @@ public static int GetToken(this MetadataReader reader, Handle handle)
6868
{
6969
if (!handle.IsEntityOrUserStringHandle)
7070
{
71-
ThrowEntityOrUserStringHandleRequired();
71+
Throw.EntityOrUserStringHandleRequired();
7272
}
7373

7474
if (handle.IsVirtual)
@@ -123,7 +123,7 @@ public static int GetHeapOffset(Handle handle)
123123
{
124124
if (!handle.IsHeapHandle)
125125
{
126-
ThrowHeapHandleRequired();
126+
Throw.HeapHandleRequired();
127127
}
128128

129129
if (handle.IsVirtual)
@@ -149,7 +149,7 @@ public static int GetToken(Handle handle)
149149
{
150150
if (!handle.IsEntityOrUserStringHandle)
151151
{
152-
ThrowEntityOrUserStringHandleRequired();
152+
Throw.EntityOrUserStringHandleRequired();
153153
}
154154

155155
if (handle.IsVirtual)
@@ -224,7 +224,7 @@ public static Handle Handle(int token)
224224
{
225225
if (!TokenTypeIds.IsEntityOrUserStringToken(unchecked((uint)token)))
226226
{
227-
ThrowInvalidToken();
227+
Throw.InvalidToken();
228228
}
229229

230230
return Metadata.Handle.FromVToken((uint)token);
@@ -238,7 +238,7 @@ public static EntityHandle EntityHandle(int token)
238238
{
239239
if (!TokenTypeIds.IsEntityToken(unchecked((uint)token)))
240240
{
241-
ThrowInvalidToken();
241+
Throw.InvalidToken();
242242
}
243243

244244
return new EntityHandle((uint)token);
@@ -255,7 +255,7 @@ public static EntityHandle Handle(TableIndex tableIndex, int rowNumber)
255255

256256
if (!TokenTypeIds.IsEntityOrUserStringToken(unchecked((uint)token)))
257257
{
258-
ThrowInvalidTableIndex();
258+
Throw.TableIndexOutOfRange();
259259
}
260260

261261
return new EntityHandle((uint)token);
@@ -397,35 +397,5 @@ public static GuidHandle GuidHandle(int offset)
397397
}
398398

399399
#endregion
400-
401-
[MethodImpl(MethodImplOptions.NoInlining)]
402-
private static void ThrowEntityHandleRequired()
403-
{
404-
throw new ArgumentException(SR.NotMetadataTableHandle, "handle");
405-
}
406-
407-
[MethodImpl(MethodImplOptions.NoInlining)]
408-
private static void ThrowHeapHandleRequired()
409-
{
410-
throw new ArgumentException(SR.NotMetadataHeapHandle, "handle");
411-
}
412-
413-
[MethodImpl(MethodImplOptions.NoInlining)]
414-
private static void ThrowEntityOrUserStringHandleRequired()
415-
{
416-
throw new ArgumentException(SR.NotMetadataTableOrUserStringHandle, "handle");
417-
}
418-
419-
[MethodImpl(MethodImplOptions.NoInlining)]
420-
private static void ThrowInvalidToken()
421-
{
422-
throw new ArgumentException(SR.InvalidToken, "token");
423-
}
424-
425-
[MethodImpl(MethodImplOptions.NoInlining)]
426-
private static void ThrowInvalidTableIndex()
427-
{
428-
throw new ArgumentOutOfRangeException("tableIndex");
429-
}
430400
}
431401
}

src/System.Reflection.Metadata/src/System/Reflection/Metadata/HandleCollections.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.Reflection.Metadata.Ecma335;
8-
using System.Runtime.CompilerServices;
98

109
namespace System.Reflection.Metadata
1110
{
@@ -37,19 +36,13 @@ public GenericParameterHandle this[int index]
3736
{
3837
if (index < 0 || index >= _count)
3938
{
40-
ThrowIndexOutOfRange();
39+
Throw.IndexOutOfRange();
4140
}
4241

4342
return GenericParameterHandle.FromRowId(_firstRowId + index);
4443
}
4544
}
4645

47-
[MethodImpl(MethodImplOptions.NoInlining)]
48-
private static void ThrowIndexOutOfRange()
49-
{
50-
throw new ArgumentOutOfRangeException("index");
51-
}
52-
5346
public Enumerator GetEnumerator()
5447
{
5548
return new Enumerator(_firstRowId, _firstRowId + _count - 1);
@@ -151,19 +144,13 @@ public GenericParameterConstraintHandle this[int index]
151144
{
152145
if (index < 0 || index >= _count)
153146
{
154-
ThrowIndexOutOfRange();
147+
Throw.IndexOutOfRange();
155148
}
156149

157150
return GenericParameterConstraintHandle.FromRowId(_firstRowId + index);
158151
}
159152
}
160153

161-
[MethodImpl(MethodImplOptions.NoInlining)]
162-
private static void ThrowIndexOutOfRange()
163-
{
164-
throw new ArgumentOutOfRangeException("index");
165-
}
166-
167154
public Enumerator GetEnumerator()
168155
{
169156
return new Enumerator(_firstRowId, _firstRowId + _count - 1);

0 commit comments

Comments
 (0)