From d150bdcf1420be3e758e4518ac6dbcf155821e9d Mon Sep 17 00:00:00 2001 From: Jerome Haltom Date: Sat, 27 Jul 2024 20:09:36 -0500 Subject: [PATCH] More. --- .../Parsing/TypeAnnotationRecordTests.cs | 2 +- src/IKVM.ByteCode/Writing/ConstantBuilder.cs | 47 ++++++++++--------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/IKVM.ByteCode.Tests/Parsing/TypeAnnotationRecordTests.cs b/src/IKVM.ByteCode.Tests/Parsing/TypeAnnotationRecordTests.cs index d9e21eb..c3c9038 100644 --- a/src/IKVM.ByteCode.Tests/Parsing/TypeAnnotationRecordTests.cs +++ b/src/IKVM.ByteCode.Tests/Parsing/TypeAnnotationRecordTests.cs @@ -20,7 +20,7 @@ public void CanRoundTripTypeAnnotation() TypeAnnotationTargetType.Field, new TypeAnnotationEmptyTargetRecord(), new TypePathRecord(new TypePathItemRecord(TypePathKind.ArrayType, 0)), - 1, + new Utf8ConstantHandle(1), new ElementValuePairRecord(2, new ElementValueRecord(ElementValueTag.Integer, new ElementValueConstantValueRecord(3)))); var b1 = new byte[a.GetSize()]; diff --git a/src/IKVM.ByteCode/Writing/ConstantBuilder.cs b/src/IKVM.ByteCode/Writing/ConstantBuilder.cs index 39f391a..b12e370 100644 --- a/src/IKVM.ByteCode/Writing/ConstantBuilder.cs +++ b/src/IKVM.ByteCode/Writing/ConstantBuilder.cs @@ -13,7 +13,7 @@ public class ConstantBuilder readonly MUTF8Encoding _mutf8; readonly BlobBuilder _builder = new(); - ushort _count = 0; + ushort _next = 1; readonly Dictionary _utf8cache = new(); @@ -26,6 +26,11 @@ public ConstantBuilder(ClassFormatVersion version) _mutf8 = MUTF8Encoding.GetMUTF8(version.Major); } + /// + /// Gets the number of constants currently added. + /// + public int Count => _next - 1; + /// /// Adds a new UTF8 constant value. /// @@ -38,7 +43,7 @@ public Utf8ConstantHandle AddUtf8Constant(ReadOnlySpan value) w.TryWriteU1((byte)ConstantTag.Utf8); w.TryWriteU2((ushort)value.Length); _builder.WriteBytes(value); - return new(_count++); + return new(_next++); } /// @@ -71,7 +76,7 @@ public IntegerConstantHandle AddIntegerConstant(int value) var w = new ClassFormatWriter(_builder.ReserveBytes(ClassFormatWriter.U1 + ClassFormatWriter.U4).GetBytes()); w.TryWriteU1((byte)ConstantTag.Integer); w.TryWriteU4((uint)value); - return new(_count++); + return new(_next++); } /// @@ -88,7 +93,7 @@ public FloatConstantHandle AddFloatConstant(float value) #else w.TryWriteU4(BitConverter.SingleToUInt32Bits(value)); #endif - return new(_count++); + return new(_next++); } /// @@ -103,8 +108,8 @@ public LongConstantHandle AddLongConstant(long value) w.TryWriteU4((uint)(value >> 32)); w.TryWriteU4(unchecked((uint)value)); - var n = _count; - _count += 2; + var n = _next; + _next += 2; return new(n); } @@ -121,8 +126,8 @@ public DoubleConstantHandle AddDoubleConstant(double value) w.TryWriteU4((uint)(v >> 32)); w.TryWriteU4(unchecked((uint)v)); - var n = _count; - _count += 2; + var n = _next; + _next += 2; return new(n); } @@ -136,7 +141,7 @@ public ClassConstantHandle AddClassConstant(Utf8ConstantHandle name) var w = new ClassFormatWriter(_builder.ReserveBytes(ClassFormatWriter.U1 + ClassFormatWriter.U2).GetBytes()); w.TryWriteU1((byte)ConstantTag.Class); w.TryWriteU2(name.Value); - return new(_count++); + return new(_next++); } /// @@ -149,7 +154,7 @@ public StringConstantHandle AddStringConstant(Utf8ConstantHandle name) var w = new ClassFormatWriter(_builder.ReserveBytes(ClassFormatWriter.U1 + ClassFormatWriter.U2).GetBytes()); w.TryWriteU1((byte)ConstantTag.String); w.TryWriteU2(name.Value); - return new(_count++); + return new(_next++); } /// @@ -164,7 +169,7 @@ public FieldrefConstantHandle AddFieldrefConstant(ClassConstantHandle clazz, Nam w.TryWriteU1((byte)ConstantTag.Fieldref); w.TryWriteU2(clazz.Value); w.TryWriteU2(nameAndType.Value); - return new(_count++); + return new(_next++); } /// @@ -179,7 +184,7 @@ public MethodrefConstantHandle AddMethodrefConstant(ClassConstantHandle clazz, N w.TryWriteU1((byte)ConstantTag.Methodref); w.TryWriteU2(clazz.Value); w.TryWriteU2(nameAndType.Value); - return new(_count++); + return new(_next++); } /// @@ -194,7 +199,7 @@ public InterfaceMethodrefConstantHandle AddInterfaceMethodrefConstant(ClassConst w.TryWriteU1((byte)ConstantTag.InterfaceMethodref); w.TryWriteU2(clazz.Value); w.TryWriteU2(nameAndType.Value); - return new(_count++); + return new(_next++); } /// @@ -209,7 +214,7 @@ public NameAndTypeConstantHandle AddNameAndTypeConstant(Utf8ConstantHandle name, w.TryWriteU1((byte)ConstantTag.NameAndType); w.TryWriteU2(name.Value); w.TryWriteU2(type.Value); - return new(_count++); + return new(_next++); } /// @@ -224,7 +229,7 @@ public MethodHandleConstantHandle AddMethodHandleConstant(ReferenceKind kind, Re w.TryWriteU1((byte)ConstantTag.MethodHandle); w.TryWriteU1((byte)kind); w.TryWriteU2(reference.Value); - return new(_count++); + return new(_next++); } /// @@ -237,7 +242,7 @@ public MethodTypeConstantHandle AddMethodTypeConstant(Utf8ConstantHandle type) var w = new ClassFormatWriter(_builder.ReserveBytes(ClassFormatWriter.U1 + ClassFormatWriter.U2).GetBytes()); w.TryWriteU1((byte)ConstantTag.MethodType); w.TryWriteU2(type.Value); - return new(_count++); + return new(_next++); } /// @@ -252,7 +257,7 @@ public DynamicConstantHandle AddDynamicConstant(ushort bootstrapMethodAttrIndex, w.TryWriteU1((byte)ConstantTag.Dynamic); w.TryWriteU2(bootstrapMethodAttrIndex); w.TryWriteU2(nameAndType.Value); - return new(_count++); + return new(_next++); } /// @@ -267,7 +272,7 @@ public InvokeDynamicConstantHandle AddInvokeDynamicConstant(ushort bootstrapMeth w.TryWriteU1((byte)ConstantTag.InvokeDynamic); w.TryWriteU2(bootstrapMethodAttrIndex); w.TryWriteU2(nameAndType.Value); - return new(_count++); + return new(_next++); } /// @@ -280,7 +285,7 @@ public ModuleConstantHandle AddModuleConstant(Utf8ConstantHandle name) var w = new ClassFormatWriter(_builder.ReserveBytes(ClassFormatWriter.U1 + ClassFormatWriter.U2).GetBytes()); w.TryWriteU1((byte)ConstantTag.Module); w.TryWriteU2(name.Value); - return new(_count++); + return new(_next++); } /// @@ -293,7 +298,7 @@ public ModuleConstantHandle AddPackageConstant(Utf8ConstantHandle name) var w = new ClassFormatWriter(_builder.ReserveBytes(ClassFormatWriter.U1 + ClassFormatWriter.U2).GetBytes()); w.TryWriteU1((byte)ConstantTag.Package); w.TryWriteU2(name.Value); - return new(_count++); + return new(_next++); } /// @@ -303,7 +308,7 @@ public ModuleConstantHandle AddPackageConstant(Utf8ConstantHandle name) public void Serialize(BlobBuilder builder) { var w = new ClassFormatWriter(_builder.ReserveBytes(ClassFormatWriter.U2).GetBytes()); - w.TryWriteU2(_count); + w.TryWriteU2((ushort)Count); builder.LinkSuffix(_builder); }