Skip to content

Commit

Permalink
EncodeTo renamed to CopyTo all over. IConstantMap gone. This just doe…
Browse files Browse the repository at this point in the history
…sn't help.
  • Loading branch information
wasabii committed Dec 9, 2024
1 parent 889d682 commit 81c30f4
Show file tree
Hide file tree
Showing 91 changed files with 3,748 additions and 3,173 deletions.
10 changes: 7 additions & 3 deletions src/IKVM.ByteCode.Tests/Decoding/CodeDecoderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Buffers;
using System.Buffers.Binary;

using FluentAssertions;

Expand Down Expand Up @@ -148,9 +149,12 @@ public void CanDecodeLookupSwitch()
[TestMethod]
public void CanCopyToWithConstant()
{
var int32 = new byte[4];
BinaryPrimitives.WriteInt32BigEndian(int32, 1234);

var constants = new ConstantTable(new ClassFormatVersion(50, 0), [
new(ConstantKind.String, new ReadOnlySequence<byte>(MUTF8Encoding.GetMUTF8(50).GetBytes("test"))),
new(ConstantKind.String, new ReadOnlySequence<byte>(MUTF8Encoding.GetMUTF8(50).GetBytes("test"))),
new(ConstantKind.Unknown, new ReadOnlySequence<byte>()),
new(ConstantKind.Integer, new ReadOnlySequence<byte>(int32)),
]);

var buffer1 = new BlobBuilder();
Expand All @@ -159,7 +163,7 @@ public void CanCopyToWithConstant()

// copy from decoder into new builder
var buffer2 = new BlobBuilder();
code1.CopyTo(new IdentityConstantMap<ConstantTable>(constants), new CodeBuilder(buffer2));
code1.CopyTo(constants, constants, new CodeBuilder(buffer2));
var code2 = new CodeDecoder(buffer2.ToArray());

code2.TryReadNext(out var inst1).Should().BeTrue();
Expand Down
14 changes: 9 additions & 5 deletions src/IKVM.ByteCode/Decoding/Annotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ public static bool TryRead(ref ClassFormatReader reader, out Annotation annotati
}

/// <summary>
/// Encodes this data class to the encoder.
/// Copies this annotation to the encoder.
/// </summary>
/// <param name="map"></param>
/// <typeparam name="TConstantView"></typeparam>
/// <typeparam name="TConstantPool"></typeparam>
/// <param name="constantView"></param>
/// <param name="constantPool"></param>
/// <param name="encoder"></param>
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref AnnotationEncoder encoder)
where TConstantMap : IConstantMap
public readonly void CopyTo<TConstantView, TConstantPool>(TConstantView constantView, TConstantPool constantPool, ref AnnotationEncoder encoder)
where TConstantView : IConstantView
where TConstantPool : IConstantPool
{
var self = this;
encoder.Annotation(map.Map(Type), e => self.Elements.CopyTo(map, ref e));
encoder.Annotation(constantPool.Get(constantView.Get(Type)), e => self.Elements.CopyTo(constantView, constantPool, ref e));
}

/// <summary>
Expand Down
15 changes: 9 additions & 6 deletions src/IKVM.ByteCode/Decoding/AnnotationDefaultAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ public static bool TryRead(ref ClassFormatReader reader, out AnnotationDefaultAt
public readonly bool IsNotNil => _isNotNil;

/// <summary>
/// Encodes this data class to the encoder.
/// Copies this attribute to the encoder.
/// </summary>
/// <param name="map"></param>
/// <param name="attributeName"></param>
/// <typeparam name="TConstantView"></typeparam>
/// <typeparam name="TConstantPool"></typeparam>
/// <param name="constantView"></param>
/// <param name="constantPool"></param>
/// <param name="encoder"></param>
public readonly void CopyTo<TConstantMap>(TConstantMap map, Utf8ConstantHandle attributeName, ref AttributeTableEncoder encoder)
where TConstantMap : IConstantMap
public readonly void CopyTo<TConstantView, TConstantPool>(TConstantView constantView, TConstantPool constantPool, ref AttributeTableEncoder encoder)
where TConstantView : IConstantView
where TConstantPool : IConstantPool
{
var self = this;
encoder.AnnotationDefault(attributeName, e => self.DefaultValue.CopyTo(map, ref e));
encoder.AnnotationDefault(constantPool.Get(AttributeName.AnnotationDefault), e => self.DefaultValue.CopyTo(constantView, constantPool, ref e));
}

}
Expand Down
14 changes: 9 additions & 5 deletions src/IKVM.ByteCode/Decoding/AnnotationTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,19 @@ readonly ref readonly Annotation GetItem(int index)
public readonly Enumerator GetEnumerator() => new Enumerator(_items);

/// <summary>
/// Encodes this data class to the encoder.
/// Copies this table to the encoder.
/// </summary>
/// <param name="map"></param>
/// <typeparam name="TConstantView"></typeparam>
/// <typeparam name="TConstantPool"></typeparam>
/// <param name="constantView"></param>
/// <param name="constantPool"></param>
/// <param name="encoder"></param>
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref AnnotationTableEncoder encoder)
where TConstantMap : IConstantMap
public readonly void CopyTo<TConstantView, TConstantPool>(TConstantView constantView, TConstantPool constantPool, ref AnnotationTableEncoder encoder)
where TConstantView : IConstantView
where TConstantPool : IConstantPool
{
foreach (var i in this)
encoder.Annotation(e => i.CopyTo(map, ref e));
encoder.Annotation(e => i.CopyTo(constantView, constantPool, ref e));
}

/// <summary>
Expand Down
14 changes: 9 additions & 5 deletions src/IKVM.ByteCode/Decoding/AppendStackMapFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,19 @@ public static bool TryRead(ref ClassFormatReader reader, byte frameType, out App
}

/// <summary>
/// Encodes this data class to the encoder.
/// Copies this frame to the specified encoder.
/// </summary>
/// <param name="map"></param>
/// <typeparam name="TConstantView"></typeparam>
/// <typeparam name="TConstantPool"></typeparam>
/// <param name="constantView"></param>
/// <param name="constantPool"></param>
/// <param name="encoder"></param>
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref StackMapTableEncoder encoder)
where TConstantMap : IConstantMap
public readonly void CopyTo<TConstantView, TConstantPool>(TConstantView constantView, TConstantPool constantPool, ref StackMapTableEncoder encoder)
where TConstantView : IConstantView
where TConstantPool : IConstantPool
{
var self = this;
encoder.Append(FrameType, OffsetDelta, e => self.Locals.CopyTo(map, ref e));
encoder.Append(FrameType, OffsetDelta, e => self.Locals.CopyTo(constantView, constantPool, ref e));
}

public readonly byte FrameType = FrameType;
Expand Down
14 changes: 9 additions & 5 deletions src/IKVM.ByteCode/Decoding/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,18 @@ public static bool TryRead(ref ClassFormatReader reader, out Attribute attribute
public readonly bool IsNotNil => _isNotNil;

/// <summary>
/// Encodes this data class to the encoder.
/// Copies this attribute to the specified attribute encoder.
/// </summary>
/// <param name="map"></param>
/// <typeparam name="TConstantView"></typeparam>
/// <typeparam name="TConstantPool"></typeparam>
/// <param name="constantView"></param>
/// <param name="constantPool"></param>
/// <param name="encoder"></param>
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref AttributeTableEncoder encoder)
where TConstantMap : IConstantMap
public readonly void CopyTo<TConstantView, TConstantPool>(TConstantView constantView, TConstantPool constantPool, ref AttributeTableEncoder encoder)
where TConstantView : IConstantView
where TConstantPool : IConstantPool
{
EncodeSelfTo(map, ref encoder);
EncodeSelfTo(constantView, constantPool, ref encoder);
}

/// <summary>
Expand Down
67 changes: 34 additions & 33 deletions src/IKVM.ByteCode/Decoding/Attribute.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,100 +339,101 @@ public PermittedSubclassesAttribute AsPermittedSubclasses()
return value;
}

readonly void EncodeSelfTo<TConstantMap>(TConstantMap map, ref AttributeTableEncoder encoder)
where TConstantMap : IConstantMap
readonly void EncodeSelfTo<TConstantView, TConstantPool>(TConstantView constantView, TConstantPool constantPool, ref AttributeTableEncoder encoder)
where TConstantView : IConstantView
where TConstantPool : IConstantPool
{
switch (map.Get(Name).Value)
switch (constantView.Get(Name).Value)
{
case AttributeName.ConstantValue:
((ConstantValueAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((ConstantValueAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.Code:
((CodeAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((CodeAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.StackMapTable:
((StackMapTableAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((StackMapTableAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.Exceptions:
((ExceptionsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((ExceptionsAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.InnerClasses:
((InnerClassesAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((InnerClassesAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.EnclosingMethod:
((EnclosingMethodAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((EnclosingMethodAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.Synthetic:
((SyntheticAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((SyntheticAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.Signature:
((SignatureAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((SignatureAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.SourceFile:
((SourceFileAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((SourceFileAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.SourceDebugExtension:
((SourceDebugExtensionAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((SourceDebugExtensionAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.LineNumberTable:
((LineNumberTableAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((LineNumberTableAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.LocalVariableTable:
((LocalVariableTableAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((LocalVariableTableAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.LocalVariableTypeTable:
((LocalVariableTypeTableAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((LocalVariableTypeTableAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.Deprecated:
((DeprecatedAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((DeprecatedAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.RuntimeVisibleAnnotations:
((RuntimeVisibleAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((RuntimeVisibleAnnotationsAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.RuntimeInvisibleAnnotations:
((RuntimeInvisibleAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((RuntimeInvisibleAnnotationsAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.RuntimeVisibleParameterAnnotations:
((RuntimeVisibleParameterAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((RuntimeVisibleParameterAnnotationsAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.RuntimeInvisibleParameterAnnotations:
((RuntimeInvisibleParameterAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((RuntimeInvisibleParameterAnnotationsAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.RuntimeVisibleTypeAnnotations:
((RuntimeVisibleTypeAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((RuntimeVisibleTypeAnnotationsAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.RuntimeInvisibleTypeAnnotations:
((RuntimeInvisibleTypeAnnotationsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((RuntimeInvisibleTypeAnnotationsAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.AnnotationDefault:
((AnnotationDefaultAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((AnnotationDefaultAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.BootstrapMethods:
((BootstrapMethodsAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((BootstrapMethodsAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.MethodParameters:
((MethodParametersAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((MethodParametersAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.Module:
((ModuleAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((ModuleAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.ModulePackages:
((ModulePackagesAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((ModulePackagesAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.ModuleMainClass:
((ModuleMainClassAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((ModuleMainClassAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.NestHost:
((NestHostAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((NestHostAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.NestMembers:
((NestMembersAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((NestMembersAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.Record:
((RecordAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((RecordAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
case AttributeName.PermittedSubclasses:
((PermittedSubclassesAttribute)this).CopyTo(map, map.Map(Name), ref encoder);
((PermittedSubclassesAttribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
default:
throw new ByteCodeException("Cannot encode unknown attribute. Attribute layout is unknown.");
Expand Down
9 changes: 5 additions & 4 deletions src/IKVM.ByteCode/Decoding/Attribute.g.tt
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,19 @@ foreach (var i in File.ReadAllLines(Host.ResolvePath(Path.Combine("..", "Attribu
}
#>

readonly void EncodeSelfTo<TConstantMap>(TConstantMap map, ref AttributeTableEncoder encoder)
where TConstantMap : IConstantHandleMap
readonly void EncodeSelfTo<TConstantView, TConstantPool>(TConstantView constantView, TConstantPool constantPool, ref AttributeTableEncoder encoder)
where TConstantView : IConstantView
where TConstantPool : IConstantPool
{
switch (map.Get(Name).Value)
switch (constantView.Get(Name).Value)
{
<#
foreach (var i in File.ReadAllLines(Host.ResolvePath(Path.Combine("..", "Attribute.txt"))))
{
var name = i.Trim();
#>
case AttributeName.<#= name #>:
((<#= name #>Attribute)this).CopyTo(map, map.Map(Name), ref encoder);
((<#= name #>Attribute)this).CopyTo(constantView, constantPool, ref encoder);
break;
<#
}
Expand Down
29 changes: 24 additions & 5 deletions src/IKVM.ByteCode/Decoding/AttributeTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,34 @@ readonly ref readonly Attribute GetItem(int index)
public readonly Enumerator GetEnumerator() => new Enumerator(_items);

/// <summary>
/// Encodes this data class to the encoder.
/// Copies this attribute table to the specified builder.
/// </summary>
/// <typeparam name="TConstantView"></typeparam>
/// <typeparam name="TConstantPool"></typeparam>
/// <param name="constantView"></param>
/// <param name="constantPool"></param>
/// <param name="builder"></param>
public readonly void CopyTo<TConstantView, TConstantPool>(TConstantView constantView, TConstantPool constantPool, AttributeTableBuilder builder)
where TConstantView : IConstantView
where TConstantPool : IConstantPool
{
CopyTo(constantView, constantPool, ref builder.Encoder);
}

/// <summary>
/// Copies this attribute table to the specified encoder.
/// </summary>
/// <param name="map"></param>
/// <typeparam name="TConstantView"></typeparam>
/// <typeparam name="TConstantPool"></typeparam>
/// <param name="constantView"></param>
/// <param name="constantPool"></param>
/// <param name="encoder"></param>
public readonly void CopyTo<TConstantMap>(TConstantMap map, ref AttributeTableEncoder encoder)
where TConstantMap : IConstantMap
public readonly void CopyTo<TConstantView, TConstantPool>(TConstantView constantView, TConstantPool constantPool, ref AttributeTableEncoder encoder)
where TConstantView : IConstantView
where TConstantPool : IConstantPool
{
foreach (var i in this)
i.CopyTo(map, ref encoder);
i.CopyTo(constantView, constantPool, ref encoder);
}

/// <summary>
Expand Down
Loading

0 comments on commit 81c30f4

Please sign in to comment.