Skip to content

Commit

Permalink
EncodeTo -> CopyTo
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Dec 9, 2024
1 parent 9f3458c commit 5aaf36a
Show file tree
Hide file tree
Showing 83 changed files with 214 additions and 214 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/IKVM.ByteCode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ jobs:
with:
dotnet-version: 8.0.x
- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v1
uses: gittools/actions/gitversion/setup@v3
with:
versionSpec: 5.x
- name: Execute GitVersion
uses: gittools/actions/gitversion/execute@v1
uses: gittools/actions/gitversion/execute@v3
with:
useConfigFile: true
configFilePath: GitVersion.yml
Expand Down Expand Up @@ -215,17 +215,17 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup .NET 7.0
- name: Setup .NET 8.0
uses: actions/setup-dotnet@v4
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x
- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v1
uses: gittools/actions/gitversion/setup@v3
with:
versionSpec: 5.x
- name: Execute GitVersion
id: GitVersion
uses: gittools/actions/gitversion/execute@v1
uses: gittools/actions/gitversion/execute@v3
with:
useConfigFile: true
- name: Download NuGet Packages
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Each of the field-only record structures can be individually used to parse or en

`TryRead` actually reads the data. In both of these cases, `false` is returned if the end of the memory has been reached. Exceptions are thrown for parsing errors involving valid data.

Each of the field-only record structures also contains `EncodeTo` and `WriteTo`. `EncodeTo` processes the structure, along with a `IConstantHandleMap`, and reemits it to an Encoder. This allows you to copy structures read from one class file into another, either creating the necessary constants in the new class, or reencode it to an output stream using `IdentityConstantMap`. `EncodeTo` requires acccess to a `IConstantHandleMap` to read constant data in order to navigate into dynamic components that require constant data to reason about (for instance `Attribute`s). `WriteTo` is much lighter weight: it simply emits the structure as is without consideration as to the constants. They must already be present in whatever constant table is used with the resulting class file.
Each of the field-only record structures also contains `CopyTo` and `WriteTo`. `CopyTo` processes the structure, along with a `IConstantHandleMap`, and reemits it to an Encoder. This allows you to copy structures read from one class file into another, either creating the necessary constants in the new class, or reencode it to an output stream using `IdentityConstantMap`. `CopyTo` requires acccess to a `IConstantHandleMap` to read constant data in order to navigate into dynamic components that require constant data to reason about (for instance `Attribute`s). `WriteTo` is much lighter weight: it simply emits the structure as is without consideration as to the constants. They must already be present in whatever constant table is used with the resulting class file.

### Constants

Expand Down
4 changes: 2 additions & 2 deletions src/IKVM.ByteCode/Decoding/Annotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public static bool TryRead(ref ClassFormatReader reader, out Annotation annotati
/// </summary>
/// <param name="map"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, ref AnnotationEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref AnnotationEncoder encoder)
where TConstantMap : IConstantMap
{
var self = this;
encoder.Annotation(map.Map(Type), e => self.Elements.EncodeTo(map, ref e));
encoder.Annotation(map.Map(Type), e => self.Elements.CopyTo(map, ref e));
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/IKVM.ByteCode/Decoding/AnnotationDefaultAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public static bool TryRead(ref ClassFormatReader reader, out AnnotationDefaultAt
/// <param name="map"></param>
/// <param name="attributeName"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, Utf8ConstantHandle attributeName, ref AttributeTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, Utf8ConstantHandle attributeName, ref AttributeTableEncoder encoder)
where TConstantMap : IConstantMap
{
var self = this;
encoder.AnnotationDefault(attributeName, e => self.DefaultValue.EncodeTo(map, ref e));
encoder.AnnotationDefault(attributeName, e => self.DefaultValue.CopyTo(map, ref e));
}

}
Expand Down
4 changes: 2 additions & 2 deletions src/IKVM.ByteCode/Decoding/AnnotationTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ readonly ref readonly Annotation GetItem(int index)
/// </summary>
/// <param name="map"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, ref AnnotationTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref AnnotationTableEncoder encoder)
where TConstantMap : IConstantMap
{
foreach (var i in this)
encoder.Annotation(e => i.EncodeTo(map, ref e));
encoder.Annotation(e => i.CopyTo(map, ref e));
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/IKVM.ByteCode/Decoding/AppendStackMapFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public static bool TryRead(ref ClassFormatReader reader, byte frameType, out App
/// </summary>
/// <param name="map"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, ref StackMapTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref StackMapTableEncoder encoder)
where TConstantMap : IConstantMap
{
var self = this;
encoder.Append(FrameType, OffsetDelta, e => self.Locals.EncodeTo(map, ref e));
encoder.Append(FrameType, OffsetDelta, e => self.Locals.CopyTo(map, ref e));
}

public readonly byte FrameType = FrameType;
Expand Down
2 changes: 1 addition & 1 deletion src/IKVM.ByteCode/Decoding/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static bool TryRead(ref ClassFormatReader reader, out Attribute attribute
/// </summary>
/// <param name="map"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, ref AttributeTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref AttributeTableEncoder encoder)
where TConstantMap : IConstantMap
{
EncodeSelfTo(map, ref encoder);
Expand Down
60 changes: 30 additions & 30 deletions src/IKVM.ByteCode/Decoding/Attribute.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,94 +345,94 @@ readonly void EncodeSelfTo<TConstantMap>(TConstantMap map, ref AttributeTableEnc
switch (map.Get(Name).Value)
{
case AttributeName.ConstantValue:
((ConstantValueAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((ConstantValueAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.Code:
((CodeAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((CodeAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.StackMapTable:
((StackMapTableAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((StackMapTableAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.Exceptions:
((ExceptionsAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((ExceptionsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.InnerClasses:
((InnerClassesAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((InnerClassesAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.EnclosingMethod:
((EnclosingMethodAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((EnclosingMethodAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.Synthetic:
((SyntheticAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((SyntheticAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.Signature:
((SignatureAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((SignatureAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.SourceFile:
((SourceFileAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((SourceFileAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.SourceDebugExtension:
((SourceDebugExtensionAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((SourceDebugExtensionAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.LineNumberTable:
((LineNumberTableAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((LineNumberTableAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.LocalVariableTable:
((LocalVariableTableAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((LocalVariableTableAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.LocalVariableTypeTable:
((LocalVariableTypeTableAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((LocalVariableTypeTableAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.Deprecated:
((DeprecatedAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((DeprecatedAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.RuntimeVisibleAnnotations:
((RuntimeVisibleAnnotationsAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((RuntimeVisibleAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.RuntimeInvisibleAnnotations:
((RuntimeInvisibleAnnotationsAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((RuntimeInvisibleAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.RuntimeVisibleParameterAnnotations:
((RuntimeVisibleParameterAnnotationsAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((RuntimeVisibleParameterAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.RuntimeInvisibleParameterAnnotations:
((RuntimeInvisibleParameterAnnotationsAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((RuntimeInvisibleParameterAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.RuntimeVisibleTypeAnnotations:
((RuntimeVisibleTypeAnnotationsAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((RuntimeVisibleTypeAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.RuntimeInvisibleTypeAnnotations:
((RuntimeInvisibleTypeAnnotationsAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((RuntimeInvisibleTypeAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.AnnotationDefault:
((AnnotationDefaultAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((AnnotationDefaultAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.BootstrapMethods:
((BootstrapMethodsAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((BootstrapMethodsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.MethodParameters:
((MethodParametersAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((MethodParametersAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.Module:
((ModuleAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((ModuleAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.ModulePackages:
((ModulePackagesAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((ModulePackagesAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.ModuleMainClass:
((ModuleMainClassAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((ModuleMainClassAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.NestHost:
((NestHostAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((NestHostAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.NestMembers:
((NestMembersAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((NestMembersAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.Record:
((RecordAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((RecordAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
case AttributeName.PermittedSubclasses:
((PermittedSubclassesAttribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((PermittedSubclassesAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
default:
throw new ByteCodeException("Cannot encode unknown attribute. Attribute layout is unknown.");
Expand Down
2 changes: 1 addition & 1 deletion src/IKVM.ByteCode/Decoding/Attribute.g.tt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ foreach (var i in File.ReadAllLines(Host.ResolvePath(Path.Combine("..", "Attribu
var name = i.Trim();
#>
case AttributeName.<#= name #>:
((<#= name #>Attribute)this).EncodeTo(map, map.Map(Name), ref encoder);
((<#= name #>Attribute)this).CopyTo(map, map.Map(Name), ref encoder);
break;
<#
}
Expand Down
4 changes: 2 additions & 2 deletions src/IKVM.ByteCode/Decoding/AttributeTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ readonly ref readonly Attribute GetItem(int index)
/// </summary>
/// <param name="map"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, ref AttributeTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref AttributeTableEncoder encoder)
where TConstantMap : IConstantMap
{
foreach (var i in this)
i.EncodeTo(map, ref encoder);
i.CopyTo(map, ref encoder);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/IKVM.ByteCode/Decoding/BootstrapMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public static bool TryRead(ref ClassFormatReader reader, out BootstrapMethod met
/// </summary>
/// <param name="map"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, ref BootstrapMethodTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref BootstrapMethodTableEncoder encoder)
where TConstantMap : IConstantMap
{
var self = this;
encoder.Method(map.Map(Method), e => self.Arguments.EncodeTo(map, ref e));
encoder.Method(map.Map(Method), e => self.Arguments.CopyTo(map, ref e));
}

public readonly MethodHandleConstantHandle Method = Method;
Expand Down
4 changes: 2 additions & 2 deletions src/IKVM.ByteCode/Decoding/BootstrapMethodTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ readonly ref readonly BootstrapMethod GetItem(int index)
/// </summary>
/// <param name="map"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, ref BootstrapMethodTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref BootstrapMethodTableEncoder encoder)
where TConstantMap : IConstantMap
{
foreach (var i in this)
i.EncodeTo(map, ref encoder);
i.CopyTo(map, ref encoder);
}

/// <inheritdoc />
Expand Down
4 changes: 2 additions & 2 deletions src/IKVM.ByteCode/Decoding/BootstrapMethodsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public static bool TryRead(ref ClassFormatReader reader, out BootstrapMethodsAtt
/// <param name="map"></param>
/// <param name="attributeName"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, Utf8ConstantHandle attributeName, ref AttributeTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, Utf8ConstantHandle attributeName, ref AttributeTableEncoder encoder)
where TConstantMap : IConstantMap
{
var self = this;
encoder.BootstrapMethods(attributeName, e => self.Methods.EncodeTo(map, ref e));
encoder.BootstrapMethods(attributeName, e => self.Methods.CopyTo(map, ref e));
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/IKVM.ByteCode/Decoding/ChopStackMapFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static bool TryRead(ref ClassFormatReader reader, byte frameType, out Cho
/// </summary>
/// <param name="map"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, ref StackMapTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref StackMapTableEncoder encoder)
where TConstantMap : IConstantMap
{
encoder.Chop(FrameType, OffsetDelta);
Expand Down
2 changes: 1 addition & 1 deletion src/IKVM.ByteCode/Decoding/ClassConstantHandleTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ readonly ref readonly ClassConstantHandle GetItem(int index)
/// </summary>
/// <param name="map"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, ref ClassConstantTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref ClassConstantTableEncoder encoder)
where TConstantMap : IConstantMap
{
foreach (var i in this)
Expand Down
2 changes: 1 addition & 1 deletion src/IKVM.ByteCode/Decoding/CodeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static bool TryRead(ref ClassFormatReader reader, out CodeAttribute attri
/// <param name="map"></param>
/// <param name="attributeName"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, Utf8ConstantHandle attributeName, ref AttributeTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, Utf8ConstantHandle attributeName, ref AttributeTableEncoder encoder)
where TConstantMap : IConstantMap
{
var self = this;
Expand Down
2 changes: 1 addition & 1 deletion src/IKVM.ByteCode/Decoding/ConstantHandleTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ readonly ref readonly ConstantHandle GetItem(int index)
/// </summary>
/// <param name="map"></param>
/// <param name="encoder"></param>
public readonly void EncodeTo<TConstantMap>(TConstantMap map, ref ConstantTableEncoder encoder)
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref ConstantTableEncoder encoder)
where TConstantMap : IConstantMap
{
foreach (var i in this)
Expand Down
Loading

0 comments on commit 5aaf36a

Please sign in to comment.