Skip to content

Commit

Permalink
More assistive methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Aug 3, 2024
1 parent 6cfa7f8 commit 01b3178
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/IKVM.ByteCode/Parsing/ModuleAttributeExportsRecord.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace IKVM.ByteCode.Parsing
{

public readonly record struct ModuleAttributeExportsRecord(ushort Index, ModuleExportsFlag Flags, ushort[] Modules);
public readonly record struct ModuleAttributeExportsRecord(PackageConstantHandle Package, ModuleExportsFlag Flags, ModuleConstantHandle[] Modules);

}
2 changes: 1 addition & 1 deletion src/IKVM.ByteCode/Parsing/ModuleAttributeOpensRecord.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace IKVM.ByteCode.Parsing
{

public readonly record struct ModuleAttributeOpensRecord(ushort Index, ModuleOpensFlag Flags, ushort[] Modules);
public readonly record struct ModuleAttributeOpensRecord(PackageConstantHandle Package, ModuleOpensFlag Flags, ModuleConstantHandle[] Modules);

}
2 changes: 1 addition & 1 deletion src/IKVM.ByteCode/Parsing/ModuleAttributeProvidesRecord.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace IKVM.ByteCode.Parsing
{

public readonly record struct ModuleAttributeProvidesRecord(ushort Index, ushort[] Modules);
public readonly record struct ModuleAttributeProvidesRecord(ClassConstantHandle Class, ClassConstantHandle[] Classes);

}
2 changes: 1 addition & 1 deletion src/IKVM.ByteCode/Parsing/ModuleAttributeRequiresRecord.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace IKVM.ByteCode.Parsing
{

public readonly record struct ModuleAttributeRequiresRecord(ushort Index, ModuleRequiresFlag Flag, ushort VersionIndex);
public readonly record struct ModuleAttributeRequiresRecord(ModuleConstantHandle Module, ModuleRequiresFlag Flag, Utf8ConstantHandle Version);

}
80 changes: 79 additions & 1 deletion src/IKVM.ByteCode/Writing/AttributeTableBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@ public AttributeTableBuilder ConstantValue(string value)
/// <param name="attributes"></param>
public AttributeTableBuilder Code(ushort maxStack, ushort maxLocals, BlobBuilder code, Action<ExceptionTableEncoder> exceptions, AttributeTableBuilder attributes)
{
if (code is null)
throw new ArgumentNullException(nameof(code));
if (exceptions is null)
throw new ArgumentNullException(nameof(exceptions));
if (attributes is null)
throw new ArgumentNullException(nameof(attributes));

var b = new BlobBuilder();
var w = new ClassFormatWriter(b.ReserveBytes(ClassFormatWriter.U2 + ClassFormatWriter.U2 + ClassFormatWriter.U4).GetBytes());
w.TryWriteU2(maxStack);
Expand All @@ -270,6 +277,9 @@ public AttributeTableBuilder Code(ushort maxStack, ushort maxLocals, BlobBuilder
/// </summary>
public AttributeTableBuilder StackMapTable(Action<StackMapTableEncoder> stackMapTable)
{
if (stackMapTable is null)
throw new ArgumentNullException(nameof(stackMapTable));

var b = new BlobBuilder();
stackMapTable(new StackMapTableEncoder(b));
return Attribute("StackMapTable", b);
Expand All @@ -280,6 +290,9 @@ public AttributeTableBuilder StackMapTable(Action<StackMapTableEncoder> stackMap
/// </summary>
public AttributeTableBuilder Exceptions(Action<ClassConstantTableEncoder> exceptions)
{
if (exceptions is null)
throw new ArgumentNullException(nameof(exceptions));

var b = new BlobBuilder();
exceptions(new ClassConstantTableEncoder(b));
return Attribute("Exceptions", b);
Expand All @@ -291,6 +304,9 @@ public AttributeTableBuilder Exceptions(Action<ClassConstantTableEncoder> except
/// <param name="classes"></param>
public AttributeTableBuilder InnerClasses(Action<InnerClassesTableEncoder> classes)
{
if (classes is null)
throw new ArgumentNullException(nameof(classes));

var b = new BlobBuilder();
classes(new InnerClassesTableEncoder(b));
return Attribute("InnerClasses", b);
Expand Down Expand Up @@ -336,6 +352,9 @@ public AttributeTableBuilder Signature(Utf8ConstantHandle signature)
/// <param name="signature"></param>
public AttributeTableBuilder Signature(string signature)
{
if (signature is null)
throw new ArgumentNullException(nameof(signature));

return Signature(_constants.GetOrAddUtf8Constant(signature));
}

Expand All @@ -357,6 +376,9 @@ public AttributeTableBuilder SourceFile(Utf8ConstantHandle sourceFile)
/// <param name="sourceFile"></param>
public AttributeTableBuilder SourceFile(string sourceFile)
{
if (sourceFile is null)
throw new ArgumentNullException(nameof(sourceFile));

return SourceFile(_constants.GetOrAddUtf8Constant(sourceFile));
}

Expand All @@ -374,6 +396,9 @@ public AttributeTableBuilder SourceDebugExtension(BlobBuilder debugExtension)
/// </summary>
public AttributeTableBuilder LineNumberTable(Action<LineNumberTableEncoder> lineNumbers)
{
if (lineNumbers is null)
throw new ArgumentNullException(nameof(lineNumbers));

var b = new BlobBuilder();
lineNumbers(new LineNumberTableEncoder(b));
return Attribute("LineNumberTable", b);
Expand All @@ -384,6 +409,9 @@ public AttributeTableBuilder LineNumberTable(Action<LineNumberTableEncoder> line
/// </summary>
public AttributeTableBuilder LocalVariableTable(Action<LocalVariableTableEncoder> localVars)
{
if (localVars is null)
throw new ArgumentNullException(nameof(localVars));

var b = new BlobBuilder();
localVars(new LocalVariableTableEncoder(b));
return Attribute("LocalVariableTable", b);
Expand All @@ -394,6 +422,9 @@ public AttributeTableBuilder LocalVariableTable(Action<LocalVariableTableEncoder
/// </summary>
public AttributeTableBuilder LocalVariableTypeTable(Action<LocalVariableTypeTableEncoder> localVarTypes)
{
if (localVarTypes is null)
throw new ArgumentNullException(nameof(localVarTypes));

var b = new BlobBuilder();
localVarTypes(new LocalVariableTypeTableEncoder(b));
return Attribute("LocalVariableTypeTable", b);
Expand All @@ -413,6 +444,9 @@ public AttributeTableBuilder Deprecated()
/// <param name="annotations"></param>
public AttributeTableBuilder RuntimeVisibleAnnotations(Action<AnnotationTableEncoder> annotations)
{
if (annotations is null)
throw new ArgumentNullException(nameof(annotations));

var b = new BlobBuilder();
annotations(new AnnotationTableEncoder(b));
return Attribute("RuntimeVisibleAnnotations", b);
Expand All @@ -423,6 +457,9 @@ public AttributeTableBuilder RuntimeVisibleAnnotations(Action<AnnotationTableEnc
/// </summary>
public AttributeTableBuilder RuntimeInvisibleAnnotations(Action<AnnotationTableEncoder> annotations)
{
if (annotations is null)
throw new ArgumentNullException(nameof(annotations));

var b = new BlobBuilder();
annotations(new AnnotationTableEncoder(b));
return Attribute("RuntimeInvisibleAnnotations", b);
Expand All @@ -433,6 +470,9 @@ public AttributeTableBuilder RuntimeInvisibleAnnotations(Action<AnnotationTableE
/// </summary>
public AttributeTableBuilder RuntimeVisibleParameterAnnotations(Action<ParameterAnnotationTableEncoder> parameterAnnotations)
{
if (parameterAnnotations is null)
throw new ArgumentNullException(nameof(parameterAnnotations));

var b = new BlobBuilder();
parameterAnnotations(new ParameterAnnotationTableEncoder(b));
return Attribute("RuntimeVisibleParameterAnnotations", b);
Expand All @@ -443,6 +483,9 @@ public AttributeTableBuilder RuntimeVisibleParameterAnnotations(Action<Parameter
/// </summary>
public AttributeTableBuilder RuntimeInvisibleParametersAnnotations(Action<ParameterAnnotationTableEncoder> parameterAnnotations)
{
if (parameterAnnotations is null)
throw new ArgumentNullException(nameof(parameterAnnotations));

var b = new BlobBuilder();
parameterAnnotations(new ParameterAnnotationTableEncoder(b));
return Attribute("RuntimeInvisibleParameterAnnotations", b);
Expand All @@ -454,6 +497,9 @@ public AttributeTableBuilder RuntimeInvisibleParametersAnnotations(Action<Parame
/// <param name="typeAnnotations"></param>
public AttributeTableBuilder RuntimeVisibleTypeAnnotations(Action<TypeAnnotationTableEncoder> typeAnnotations)
{
if (typeAnnotations is null)
throw new ArgumentNullException(nameof(typeAnnotations));

var b = new BlobBuilder();
typeAnnotations(new TypeAnnotationTableEncoder(b));
return Attribute("RuntimeVisibleTypeAnnotations", b);
Expand All @@ -465,6 +511,9 @@ public AttributeTableBuilder RuntimeVisibleTypeAnnotations(Action<TypeAnnotation
/// <param name="typeAnnotations"></param>
public AttributeTableBuilder RuntimeInvisibleTypeAnnotations(Action<TypeAnnotationTableEncoder> typeAnnotations)
{
if (typeAnnotations is null)
throw new ArgumentNullException(nameof(typeAnnotations));

var b = new BlobBuilder();
typeAnnotations(new TypeAnnotationTableEncoder(b));
return Attribute("RuntimeInvisibleTypeAnnotations", b);
Expand All @@ -476,6 +525,9 @@ public AttributeTableBuilder RuntimeInvisibleTypeAnnotations(Action<TypeAnnotati
/// <param name="defaultValue"></param>
public AttributeTableBuilder AnnotationDefault(Action<ElementValueEncoder> defaultValue)
{
if (defaultValue is null)
throw new ArgumentNullException(nameof(defaultValue));

var b = new BlobBuilder();
defaultValue(new ElementValueEncoder(b));
return Attribute("AnnotationDefault", b);
Expand All @@ -487,6 +539,9 @@ public AttributeTableBuilder AnnotationDefault(Action<ElementValueEncoder> defau
/// <param name="bootstrapMethods"></param>
public AttributeTableBuilder BootstrapMethods(Action<BootstrapMethodsTableEncoder> bootstrapMethods)
{
if (bootstrapMethods is null)
throw new ArgumentNullException(nameof(bootstrapMethods));

var b = new BlobBuilder();
bootstrapMethods(new BootstrapMethodsTableEncoder(b));
return Attribute("BootstrapMethods", b);
Expand All @@ -497,6 +552,9 @@ public AttributeTableBuilder BootstrapMethods(Action<BootstrapMethodsTableEncode
/// </summary>
public AttributeTableBuilder MethodParameters(Action<MethodParametersTableEncoder> parameters)
{
if (parameters is null)
throw new ArgumentNullException(nameof(parameters));

var b = new BlobBuilder();
parameters(new MethodParametersTableEncoder(b));
return Attribute("MethodParameters", b);
Expand All @@ -505,9 +563,29 @@ public AttributeTableBuilder MethodParameters(Action<MethodParametersTableEncode
/// <summary>
/// Adds a new Module attribute.
/// </summary>
/// <param name="module"></param>
/// <param name="name"></param>
/// <param name="flags"></param>
/// <param name="version"></param>
/// <param name="requires"></param>
/// <param name="exports"></param>
/// <param name="opens"></param>
/// <param name="uses"></param>
/// <param name="provides"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public AttributeTableBuilder Module(ModuleConstantHandle name, ModuleFlag flags, Utf8ConstantHandle version, Action<ModuleRequiresTableEncoder> requires, Action<ModuleExportsTableEncoder> exports, Action<ModuleOpensTableEncoder> opens, Action<ClassConstantTableEncoder> uses, Action<ModuleProvidesTableEncoder> provides)
{
if (requires is null)
throw new ArgumentNullException(nameof(requires));
if (exports is null)
throw new ArgumentNullException(nameof(exports));
if (opens is null)
throw new ArgumentNullException(nameof(opens));
if (uses is null)
throw new ArgumentNullException(nameof(uses));
if (provides is null)
throw new ArgumentNullException(nameof(provides));

var b = new BlobBuilder();
var w = new ClassFormatWriter(_builder.ReserveBytes(ClassFormatWriter.U2 + ClassFormatWriter.U2 + ClassFormatWriter.U2).GetBytes());
w.TryWriteU2(name.Index);
Expand Down
37 changes: 37 additions & 0 deletions src/IKVM.ByteCode/Writing/LocalVariableTypeTableEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

using IKVM.ByteCode.Buffers;
using IKVM.ByteCode.Parsing;
Expand All @@ -24,6 +25,42 @@ public LocalVariableTypeTableEncoder(BlobBuilder builder)
_count = 0;
}

/// <summary>
/// Adds an existing local variable type.
/// </summary>
/// <param name="record"></param>
/// <returns></returns>
public LocalVariableTypeTableEncoder Add(LocalVariableTypeTableAttributeItemRecord record)
{
return LocalVarType(record.CodeOffset, record.CodeLength, record.Name, record.Signature, record.Index);
}

/// <summary>
/// Adds many existing local variable types.
/// </summary>
/// <param name="records"></param>
/// <returns></returns>
public LocalVariableTypeTableEncoder AddMany(ReadOnlySpan<LocalVariableTypeTableAttributeItemRecord> records)
{
foreach (var i in records)
Add(i);

return this;
}

/// <summary>
/// Adds many existing local variable types.
/// </summary>
/// <param name="records"></param>
/// <returns></returns>
public LocalVariableTypeTableEncoder AddMany(IEnumerable<LocalVariableTypeTableAttributeItemRecord> records)
{
foreach (var i in records)
Add(i);

return this;
}

/// <summary>
/// Adds a new local variable type.
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions src/IKVM.ByteCode/Writing/MethodParametersTableEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

using IKVM.ByteCode.Buffers;
using IKVM.ByteCode.Parsing;
Expand Down Expand Up @@ -27,6 +28,42 @@ public MethodParametersTableEncoder(BlobBuilder builder)
_count = 0;
}

/// <summary>
/// Adds an existing method parameter.
/// </summary>
/// <param name="record"></param>
/// <returns></returns>
public MethodParametersTableEncoder Add(MethodParametersAttributeParameterRecord record)
{
return Parameter(record.Name, record.AccessFlags);
}

/// <summary>
/// Adds many existing method parameters.
/// </summary>
/// <param name="records"></param>
/// <returns></returns>
public MethodParametersTableEncoder AddMany(ReadOnlySpan<MethodParametersAttributeParameterRecord> records)
{
foreach (var i in records)
Add(i);

return this;
}

/// <summary>
/// Adds many existing method parameters.
/// </summary>
/// <param name="records"></param>
/// <returns></returns>
public MethodParametersTableEncoder AddMany(IEnumerable<MethodParametersAttributeParameterRecord> records)
{
foreach (var i in records)
Add(i);

return this;
}

/// <summary>
/// Adds a parameter.
/// </summary>
Expand Down
Loading

0 comments on commit 01b3178

Please sign in to comment.