Permalink
Browse files

Added a few extension methods to make it easier to use the metadata t…

…able row writing API
  • Loading branch information...
1 parent 2720305 commit 90785b7f5096296438eb48f1813a9271064c0b2b @philiplaureano philiplaureano committed Dec 16, 2011
View
@@ -69,8 +69,25 @@
</node>
<node CREATED="1318023222984" ID="ID_50309323" MODIFIED="1318276090004" POSITION="right" TEXT="Metadata Table Heap Parsing">
<icon BUILTIN="full-2"/>
+<node CREATED="1318026262235" ID="ID_535889197" MODIFIED="1323259688839" TEXT="#Strings">
+<icon BUILTIN="button_ok"/>
+<node CREATED="1318028437964" ID="ID_128401096" MODIFIED="1318028444501" TEXT="Reading a string from the heap using a given index"/>
+</node>
+<node CREATED="1318026266682" ID="ID_1646397104" MODIFIED="1323259688839" TEXT="#Blob">
+<icon BUILTIN="button_ok"/>
+<node CREATED="1318026574984" ID="ID_1632315818" MODIFIED="1318026597658" TEXT="Should Be Able To Read Blob Bytes From Given Index"/>
+<node CREATED="1318026647704" ID="ID_1293416273" MODIFIED="1318026649424" TEXT="Should Automatically Encode Blob Size When Writing Blob From Given Bytes"/>
+</node>
+<node CREATED="1318026269963" ID="ID_399681676" MODIFIED="1323259688838" TEXT="#GUID">
+<icon BUILTIN="button_ok"/>
+</node>
+<node CREATED="1318026273859" ID="ID_1432849107" MODIFIED="1323259688838" TEXT="#US">
+<icon BUILTIN="button_ok"/>
+</node>
+<node CREATED="1318113990160" ID="ID_1148239234" MODIFIED="1318114005016" TEXT="Metadata Signatures"/>
+<node CREATED="1323990669131" ID="ID_203187106" MODIFIED="1323990678690" TEXT="Reading the raw table heap">
<node CREATED="1318025780966" ID="ID_1720433022" MODIFIED="1318028485057" TEXT="The Table Stream">
-<arrowlink DESTINATION="ID_1720433022" ENDARROW="Default" ENDINCLINATION="0;0;" ID="Arrow_ID_1325405762" STARTARROW="None" STARTINCLINATION="0;0;"/>
+<arrowlink DESTINATION="ID_1720433022" ENDARROW="Default" ENDINCLINATION="0;0;" ID="Arrow_ID_977056183" STARTARROW="None" STARTINCLINATION="0;0;"/>
<node CREATED="1318025819086" ID="ID_1569802689" MODIFIED="1318025824308" TEXT="Top level structure">
<node CREATED="1318025834966" ID="ID_499104783" MODIFIED="1323259712197" TEXT="MajorVersion">
<icon BUILTIN="button_ok"/>
@@ -101,7 +118,7 @@
</node>
<node CREATED="1318025908877" ID="ID_1740581809" MODIFIED="1318025914916" TEXT="Row counts"/>
</node>
-<node CREATED="1316298889934" FOLDED="true" ID="ID_216095167" MODIFIED="1323259665224" TEXT="Metadata Tables">
+<node CREATED="1316298889934" FOLDED="true" ID="ID_216095167" MODIFIED="1323990436282" TEXT="Metadata Tables">
<node CREATED="1316301621166" ID="ID_338298977" MODIFIED="1316301622987" TEXT="AssemblyOS"/>
<node CREATED="1316301611802" ID="ID_1960503287" MODIFIED="1316301614687" TEXT="AssemblyProcessor"/>
<node CREATED="1316301603046" ID="ID_180636830" MODIFIED="1316301605031" TEXT="AssemblyRef"/>
@@ -141,22 +158,7 @@
<node CREATED="1316300510960" ID="ID_1734411846" MODIFIED="1316300512499" TEXT="TypeSpec"/>
</node>
</node>
-<node CREATED="1318026262235" ID="ID_535889197" MODIFIED="1323259688839" TEXT="#Strings">
-<icon BUILTIN="button_ok"/>
-<node CREATED="1318028437964" ID="ID_128401096" MODIFIED="1318028444501" TEXT="Reading a string from the heap using a given index"/>
-</node>
-<node CREATED="1318026266682" ID="ID_1646397104" MODIFIED="1323259688839" TEXT="#Blob">
-<icon BUILTIN="button_ok"/>
-<node CREATED="1318026574984" ID="ID_1632315818" MODIFIED="1318026597658" TEXT="Should Be Able To Read Blob Bytes From Given Index"/>
-<node CREATED="1318026647704" ID="ID_1293416273" MODIFIED="1318026649424" TEXT="Should Automatically Encode Blob Size When Writing Blob From Given Bytes"/>
</node>
-<node CREATED="1318026269963" ID="ID_399681676" MODIFIED="1323259688838" TEXT="#GUID">
-<icon BUILTIN="button_ok"/>
-</node>
-<node CREATED="1318026273859" ID="ID_1432849107" MODIFIED="1323259688838" TEXT="#US">
-<icon BUILTIN="button_ok"/>
-</node>
-<node CREATED="1318113990160" ID="ID_1148239234" MODIFIED="1318114005016" TEXT="Metadata Signatures"/>
</node>
<node CREATED="1318026938398" ID="ID_374970568" MODIFIED="1318065194894" POSITION="left" TEXT="CLI Header Tests">
<node CREATED="1318027497122" ID="ID_1709687722" MODIFIED="1318027692649" TEXT="Always having a MethodDef or a FileToken as the entry point for the assembly"/>
@@ -305,5 +307,6 @@
</node>
</node>
</node>
+<node CREATED="1323990732130" ID="ID_240159592" MODIFIED="1323990758916" POSITION="right" TEXT="Tao&apos;s PE Verification"/>
</node>
</map>
@@ -11,14 +11,14 @@ namespace Tao
{
public interface IRowWriter[TRow]
{
- WriteStringIndex(value : uint) : void;
- WriteGuidIndex(value : uint) : void;
- WriteBlobIndex(value : uint) : void;
+ WriteStringIndex(value : void -> Index) : void;
+ WriteGuidIndex(value : void -> Index) : void;
+ WriteBlobIndex(value : void -> Index) : void;
WriteDword(value : uint) : void;
WriteWord(value : ushort) : void;
WriteByte(value : byte) : void;
- WriteIndex(tableId : TableId, value : uint) : void;
- WriteIndex(tokenType : CodedTokenType, value : uint) : void;
+ WriteIndex(tableId : TableId, value : void -> Index) : void;
+ WriteIndex(tokenType : CodedTokenType, value : void -> Index) : void;
}
}
@@ -0,0 +1,49 @@
+using Nemerle;
+using Nemerle.Assertions;
+using Nemerle.Collections;
+using Nemerle.Extensions;
+using Nemerle.Text;
+using Nemerle.Utility;
+
+using Tao;
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Tao
+{
+ public static class IndexExtensions
+ {
+ public ToUInt32(this index : Index) : uint
+ requires index != null
+ {
+ match(index)
+ {
+ |index is Index.Narrow =>
+ {
+ (index : Index.Narrow).Value;
+ }
+ |index is Index.Wide =>
+ {
+ (index : Index.Wide).Value;
+ }
+ }
+ }
+ public ToUInt16(this index : Index) : ushort
+ requires index != null
+ {
+ match(index)
+ {
+ |index is Index.Narrow =>
+ {
+ (index : Index.Narrow).Value;
+ }
+ |index is Index.Wide =>
+ {
+ (index : Index.Wide).Value :> ushort;
+ }
+ }
+ }
+ }
+}
@@ -46,7 +46,7 @@ namespace Tao
def rowWriter = RowWriter.[TRow](heapSizeFlags, rowCounts, BinaryWriter(outputStream));
foreach(row in Rows)
{
- _schemaWriter.WriteRow(rowCounts, rowWriter);
+ _schemaWriter.WriteRow(row, rowCounts, rowWriter);
}
}
@@ -1,4 +1,5 @@
using Nemerle;
+using Nemerle.Assertions;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
@@ -11,87 +12,102 @@ using System.Linq;
namespace Tao
{
public class RowWriter[TRow] : IRowWriter[TRow]
-{
- private _heapSizeFlags : ISet.[HeapSizeFlags];
- private _rowCounts : IDictionary.[TableId, uint];
- private _writer : BinaryWriter;
-
- public this(heapSizeFlags : IEnumerable.[HeapSizeFlags], rowCounts : IDictionary.[TableId, uint],
- writer : BinaryWriter)
{
- _heapSizeFlags = HashSet.[HeapSizeFlags](heapSizeFlags);
- _rowCounts = rowCounts;
- _writer = writer;
- }
+ private _heapSizeFlags : ISet.[HeapSizeFlags];
+ private _rowCounts : IDictionary.[TableId, uint];
+ private _writer : BinaryWriter;
- public WriteStringIndex(value : uint) : void
- {
- if (_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideStringIndex))
+ public this(heapSizeFlags : IEnumerable.[HeapSizeFlags], rowCounts : IDictionary.[TableId, uint],
+ writer : BinaryWriter)
{
- _writer.Write(value : uint);
+ _heapSizeFlags = HashSet.[HeapSizeFlags](heapSizeFlags);
+ _rowCounts = rowCounts;
+ _writer = writer;
}
- else
+
+ public WriteStringIndex(value : void -> Index) : void
+ requires (_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideStringIndex) && value() is Index.Wide) ||
+ (!_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideStringIndex) && value() is Index.Narrow)
{
- _writer.Write(value :> ushort);
+ def index = value();
+ if (_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideStringIndex))
+ {
+ _writer.Write(index.ToUInt32());
+ }
+ else
+ {
+ _writer.Write(index.ToUInt16());
+ }
}
- }
- public WriteGuidIndex(value : uint) : void
- {
- if (_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideGuidIndex))
+ public WriteGuidIndex(value : void -> Index) : void
+ requires (_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideGuidIndex) && value() is Index.Wide) ||
+ (!_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideGuidIndex) && value() is Index.Narrow)
{
- _writer.Write(value : uint);
+ def index = value();
+ if (_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideGuidIndex))
+ {
+ _writer.Write(index.ToUInt32());
+ }
+ else
+ {
+ _writer.Write(index.ToUInt16());
+ }
}
- else
+ public WriteBlobIndex(value : void -> Index) : void
+ requires (_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideBlobIndex) && value() is Index.Wide) ||
+ (!_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideBlobIndex) && value() is Index.Narrow)
{
- _writer.Write(value :> ushort);
+ def index = value();
+ if (_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideBlobIndex))
+ {
+ _writer.Write(index.ToUInt32());
+ }
+ else
+ {
+ _writer.Write(index.ToUInt16());
+ }
}
- }
- public WriteBlobIndex(value : uint) : void
- {
- if (_heapSizeFlags.Contains(HeapSizeFlags.RequiresWideBlobIndex))
+ public WriteDword(value : uint) : void
{
_writer.Write(value : uint);
}
- else
+ public WriteWord(value : ushort) : void
{
- _writer.Write(value :> ushort);
+ _writer.Write(value : ushort);
}
- }
- public WriteDword(value : uint) : void
- {
- _writer.Write(value : uint);
- }
- public WriteWord(value : ushort) : void
- {
- _writer.Write(value : ushort);
- }
- public WriteByte(value : byte) : void
- {
- _writer.Write(value : byte);
- }
- public WriteIndex(tableId : TableId, value : uint) : void
- {
- def useWideIndex = _rowCounts.RequiresWideIndices(tableId);
- if (useWideIndex)
+ public WriteByte(value : byte) : void
{
- WriteDword(value);
- }
- else
- {
- WriteWord(value :> ushort);
- }
- }
- public WriteIndex(tokenType : CodedTokenType, value : uint) : void
- {
- def useWideIndex = _rowCounts.RequiresWideIndices(tokenType);
- if (useWideIndex)
+ _writer.Write(value : byte);
+ }
+ public WriteIndex(tableId : TableId, value : void -> Index) : void
+ requires (_rowCounts.RequiresWideIndices(tableId) && value() is Index.Wide) ||
+ (!_rowCounts.RequiresWideIndices(tableId) && value() is Index.Narrow)
{
- WriteDword(value);
+ def useWideIndex = _rowCounts.RequiresWideIndices(tableId);
+ def index = value();
+ if (useWideIndex)
+ {
+ _writer.Write(index.ToUInt32());
+ }
+ else
+ {
+ _writer.Write(index.ToUInt16());
+ }
}
- else
+ public WriteIndex(tokenType : CodedTokenType, value : void -> Index) : void
+ requires (_rowCounts.RequiresWideIndices(tokenType) && value() is Index.Wide) ||
+ (!_rowCounts.RequiresWideIndices(tokenType) && value() is Index.Narrow)
{
- WriteWord(value :> ushort);
+ def useWideIndex = _rowCounts.RequiresWideIndices(tokenType);
+ def index = value();
+ if (useWideIndex)
+ {
+ _writer.Write(index.ToUInt32());
+ }
+ else
+ {
+ _writer.Write(index.ToUInt16());
+ }
}
}
}
-}
@@ -11,6 +11,6 @@ namespace Tao
{
public interface ISchemaWriter[TRow]
{
- WriteRow(rowCounts : IDictionary.[TableId, uint], rowWriter : IRowWriter.[TRow]) : void;
+ WriteRow(row : TRow, rowCounts : IDictionary.[TableId, uint], rowWriter : IRowWriter.[TRow]) : void;
}
}
@@ -11,7 +11,7 @@ namespace Tao
{
public class ModuleSchemaWriter : ISchemaWriter.[ModuleRow]
{
- public WriteRow(rowCounts : IDictionary.[TableId, uint], rowWriter : IRowWriter.[ModuleRow]) : void
+ public WriteRow(row : ModuleRow, rowCounts : IDictionary.[TableId, uint], rowWriter : IRowWriter.[ModuleRow]) : void
{
throw NotImplementedException();
}
@@ -11,7 +11,7 @@ namespace Tao
{
public class TypeDefSchemaWriter : ISchemaWriter.[TypeDefRow]
{
- public WriteRow(rowCounts : IDictionary.[TableId, uint], rowWriter : IRowWriter.[TypeDefRow]) : void
+ public WriteRow(row : TypeDefRow, rowCounts : IDictionary.[TableId, uint], rowWriter : IRowWriter.[TypeDefRow]) : void
{
throw NotImplementedException();
}
View
@@ -229,6 +229,9 @@
<Compile Include="Metadata\Tables\IMetadataTableReader.n">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Metadata\Tables\IndexExtensions.n">
+ <SubType>Code</SubType>
+ </Compile>
<Compile Include="Metadata\Tables\IRowReader.n">
<SubType>Code</SubType>
</Compile>

0 comments on commit 90785b7

Please sign in to comment.