Skip to content

Commit

Permalink
Name
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Dec 9, 2024
1 parent d676187 commit f785e0f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/IKVM.ByteCode.Tests/Decoding/CodeAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Buffers;
using System.Buffers.Binary;

using FluentAssertions;

using IKVM.ByteCode.Buffers;
using IKVM.ByteCode.Decoding;
using IKVM.ByteCode.Encoding;
using IKVM.ByteCode.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IKVM.ByteCode.Tests.Decoding
{

[TestClass]
public class CodeAttributeTests
{

[TestMethod]
public void CanRewriteLoadConstant()
{
var int32 = new byte[4];
BinaryPrimitives.WriteInt32BigEndian(int32, 1234);

var codeName = new byte[6];
BinaryPrimitives.WriteInt16BigEndian(codeName, 4);
MUTF8Encoding.GetMUTF8(50).GetBytes("Code").CopyTo(codeName, 2);

var constants = new ConstantTable(new ClassFormatVersion(50, 0), [
new(ConstantKind.Unknown, new ReadOnlySequence<byte>()),
new(ConstantKind.Integer, new ReadOnlySequence<byte>(int32)),
new(ConstantKind.Utf8, new ReadOnlySequence<byte>(codeName)),
]);

var codeBuffer1 = new BlobBuilder();
var excpBuffer1 = new BlobBuilder();
new CodeBuilder(codeBuffer1)
.DefineLabel(out var end)
.BeginExceptionBlock(ClassConstantHandle.Nil, out var handler)
.LoadConstant(new IntegerConstantHandle(1))
.Pop()
.EndExceptionBlock()
.MarkLabel(handler)
.MarkLabel(end)
.Return()
.SerializeExceptions(excpBuffer1);

// fake up a code attribute
var excpReader1 = new ClassFormatReader(excpBuffer1.ToArray());
ExceptionHandlerTable.TryRead(ref excpReader1, out var excpTable1).Should().BeTrue();
var codeAttr1 = new CodeAttribute(10, 10, new ReadOnlySequence<byte>(codeBuffer1.ToArray()), excpTable1, new AttributeTable([]));

// copy code attribute to new attribute table encoder
var attrTable2 = new BlobBuilder();
var attrEncoder2 = new AttributeTableEncoder(attrTable2);
codeAttr1.CopyTo(constants, constants, ref attrEncoder2);
}

}

}
1 change: 1 addition & 0 deletions src/IKVM.ByteCode/Encoding/ConstantBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;

using IKVM.ByteCode.Buffers;
using IKVM.ByteCode.Decoding;
using IKVM.ByteCode.Text;

namespace IKVM.ByteCode.Encoding
Expand Down

0 comments on commit f785e0f

Please sign in to comment.