Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

PEBuilder implementation #5354

Merged
merged 1 commit into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,48 @@
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<PackageTargetFramework>dotnet5.2</PackageTargetFramework>
<LangVersion>6</LangVersion>
<DefineConstants>$(DefineConstants);SRM</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<Optimize>false</Optimize>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<Optimize>true</Optimize>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Reflection\Blob.cs" />
<Compile Include="System\Reflection\BlobWriter.cs" />
<Compile Include="System\Reflection\BlobWriterImpl.cs" />
<Compile Include="System\Reflection\BlobBuilder.cs" />
<Compile Include="System\Reflection\BlobBuilder.Enumerators.cs" />
<Compile Include="System\Reflection\Internal\Utilities\ByteSequenceComparer.cs" />
<Compile Include="System\Reflection\Internal\Utilities\DecimalUtilities.cs" />
<Compile Include="System\Reflection\Internal\Utilities\EnumerableExtensions.cs" />
<Compile Include="System\Reflection\Metadata\Ecma335\Blobs\ExceptionRegionEncoder.cs" />
<Compile Include="System\Reflection\Metadata\Ecma335\Blobs\InstructionEncoder.cs" />
<Compile Include="System\Reflection\Metadata\ILOpCode.cs" />
<Compile Include="System\Reflection\Metadata\Ecma335\CodedIndex.cs" />
<Compile Include="System\Reflection\Metadata\Ecma335\MetadataSerializer.cs" />
<Compile Include="System\Reflection\Metadata\Ecma335\MetadataBuilder.Tables.cs" />
<Compile Include="System\Reflection\Metadata\Ecma335\Blobs\BlobEncoders.cs" />
<Compile Include="System\Reflection\Metadata\Ecma335\Blobs\MethodBodyEncoder.cs" />
<Compile Include="System\Reflection\Metadata\Ecma335\MetadataSizes.cs" />
<Compile Include="System\Reflection\Metadata\Ecma335\MetadataBuilder.Heaps.cs" />
<Compile Include="System\Reflection\Metadata\Internal\HeapSizeFlag.cs" />
<Compile Include="System\Reflection\Metadata\Internal\MetadataWriterUtilities.cs" />
<Compile Include="System\Reflection\PortableExecutable\ContentId.cs" />
<Compile Include="System\Reflection\PortableExecutable\ManagedPEBuilder.cs" />
<Compile Include="System\Reflection\PortableExecutable\ManagedTextSection.cs" />
<Compile Include="System\Reflection\PortableExecutable\PEBuilder.cs" />
<Compile Include="System\Reflection\PortableExecutable\PEDirectoriesBuilder.cs" />
<Compile Include="System\Reflection\PortableExecutable\PESectionLocation.cs" />
<Compile Include="System\Reflection\Internal\Utilities\BlobUtilities.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\InternalsVisibleTo.cs" />
<Compile Include="System\Reflection\Internal\MemoryBlocks\AbstractMemoryBlock.cs" />
Expand Down
32 changes: 32 additions & 0 deletions src/System.Reflection.Metadata/src/System/Reflection/Blob.cs
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);
}
}
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
Copy link
Contributor

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.

Copy link
Member Author

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.

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();
}
}
}
Loading