Skip to content

Commit

Permalink
Add InnerClassesTableEncoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Jul 31, 2024
1 parent 4e3f50f commit 3d6646e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/IKVM.ByteCode/Writing/AttributeTableBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,11 @@ public AttributeTableBuilder Exceptions(Action<ClassConstantTableEncoder> except
/// Adds a new InnerClasses attribute.
/// </summary>
/// <param name="classes"></param>
public AttributeTableBuilder InnerClasses(BlobBuilder classes)
public AttributeTableBuilder InnerClasses(Action<InnerClassesTableEncoder> classes)
{
return Attribute("InnerClasses", classes);
var b = new BlobBuilder();
classes(new InnerClassesTableEncoder(b));
return Attribute("InnerClasses", b);
}

/// <summary>
Expand Down
44 changes: 44 additions & 0 deletions src/IKVM.ByteCode/Writing/InnerClassesTableEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;

using IKVM.ByteCode.Buffers;
using IKVM.ByteCode.Parsing;

namespace IKVM.ByteCode.Writing
{

public struct InnerClassesTableEncoder
{

readonly BlobBuilder _builder;
readonly Blob _countBlob;
ushort _count;

/// <summary>
/// Intializes a new instance.
/// </summary>
/// <param name="builder"></param>
public InnerClassesTableEncoder(BlobBuilder builder)
{
_builder = builder ?? throw new ArgumentNullException(nameof(builder));
_countBlob = _builder.ReserveBytes(ClassFormatWriter.U2);
_count = 0;
}

/// <summary>
/// Adds a package to the table.
/// </summary>
/// <param name="package"></param>
public InnerClassesTableEncoder Add(ClassConstantHandle innerClass, ClassConstantHandle outerClass, Utf8ConstantHandle innerName, AccessFlag innerAccessFlags)
{
var w = new ClassFormatWriter(_builder.ReserveBytes(ClassFormatWriter.U2 + ClassFormatWriter.U2 + ClassFormatWriter.U2 + ClassFormatWriter.U2).GetBytes());
w.TryWriteU2(innerClass.Index);
w.TryWriteU2(outerClass.Index);
w.TryWriteU2(innerName.Index);
w.TryWriteU2((ushort)innerAccessFlags);
new ClassFormatWriter(_countBlob.GetBytes()).TryWriteU2(++_count);
return this;
}

}

}

0 comments on commit 3d6646e

Please sign in to comment.