This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
PEBuilder implementation #5354
Merged
Merged
PEBuilder implementation #5354
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/System.Reflection.Metadata/src/System/Reflection/Blob.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
|
||
#if SRM | ||
namespace System.Reflection | ||
#else | ||
namespace Roslyn.Reflection | ||
#endif | ||
{ | ||
#if SRM | ||
public | ||
#endif | ||
struct Blob | ||
{ | ||
internal readonly byte[] Buffer; | ||
internal readonly int Start; | ||
public int Length { get; } | ||
|
||
internal Blob(byte[] buffer, int start, int length) | ||
{ | ||
Buffer = buffer; | ||
Start = start; | ||
Length = length; | ||
} | ||
|
||
public bool IsDefault => Buffer == null; | ||
|
||
public ArraySegment<byte> GetBytes() => new ArraySegment<byte>(Buffer, Start, Length); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/System.Reflection.Metadata/src/System/Reflection/BlobBuilder.Enumerators.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Diagnostics; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System; | ||
|
||
#if SRM | ||
namespace System.Reflection | ||
#else | ||
namespace Roslyn.Reflection | ||
#endif | ||
{ | ||
partial class BlobBuilder | ||
{ | ||
// internal for testing | ||
internal struct Chunks : IEnumerable<BlobBuilder>, IEnumerator<BlobBuilder>, IEnumerator | ||
{ | ||
private readonly BlobBuilder _head; | ||
private BlobBuilder _next; | ||
private BlobBuilder _currentOpt; | ||
|
||
internal Chunks(BlobBuilder builder) | ||
{ | ||
Debug.Assert(builder.IsHead); | ||
|
||
_head = builder; | ||
_next = builder.FirstChunk; | ||
_currentOpt = null; | ||
} | ||
|
||
object IEnumerator.Current => Current; | ||
public BlobBuilder Current => _currentOpt; | ||
|
||
public bool MoveNext() | ||
{ | ||
if (_currentOpt == _head) | ||
{ | ||
return false; | ||
} | ||
|
||
if (_currentOpt == _head._nextOrPrevious) | ||
{ | ||
_currentOpt = _head; | ||
return true; | ||
} | ||
|
||
_currentOpt = _next; | ||
_next = _next._nextOrPrevious; | ||
return true; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
_currentOpt = null; | ||
_next = _head.FirstChunk; | ||
} | ||
|
||
void IDisposable.Dispose() { } | ||
|
||
// IEnumerable: | ||
public Chunks GetEnumerator() => this; | ||
IEnumerator<BlobBuilder> IEnumerable<BlobBuilder>.GetEnumerator() => GetEnumerator(); | ||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} | ||
|
||
public struct Blobs : IEnumerable<Blob>, IEnumerator<Blob>, IEnumerator | ||
{ | ||
private Chunks _chunks; | ||
|
||
internal Blobs(BlobBuilder builder) | ||
{ | ||
_chunks = new Chunks(builder); | ||
} | ||
|
||
object IEnumerator.Current => Current; | ||
|
||
public Blob Current | ||
{ | ||
get | ||
{ | ||
var current = _chunks.Current; | ||
if (current != null) | ||
{ | ||
return new Blob(current._buffer, 0, current.Length); | ||
} | ||
else | ||
{ | ||
return default(Blob); | ||
} | ||
} | ||
} | ||
|
||
public bool MoveNext() => _chunks.MoveNext(); | ||
public void Reset() => _chunks.Reset(); | ||
|
||
void IDisposable.Dispose() { } | ||
|
||
// IEnumerable: | ||
public Blobs GetEnumerator() => this; | ||
IEnumerator<Blob> IEnumerable<Blob>.GetEnumerator() => GetEnumerator(); | ||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not seeing tests in this PR. The tests should be ported to corefx too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'd keep them in Roslyn for now to make it easier to iterate (I need to keep these sources in sync). Be assured that the entire Roslyn test suite is testing this code.