From f0780d8f384035651c7381e86969d55749667455 Mon Sep 17 00:00:00 2001 From: Jerome Haltom Date: Wed, 31 Jul 2024 21:23:49 -0500 Subject: [PATCH] Fix up some more encodings. --- .../LocalVariableTargetTableItemRecord.cs | 6 +++--- .../BootstrapMethodsAttributeMethodReader.cs | 4 ++-- .../Reading/ConstantValueAttributeReader.cs | 2 +- .../LocalVariableTargetTableEncoder.cs | 20 ++++++++++++++++++- .../Writing/TypeAnnotationEncoder.cs | 4 ++-- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/IKVM.ByteCode/Parsing/LocalVariableTargetTableItemRecord.cs b/src/IKVM.ByteCode/Parsing/LocalVariableTargetTableItemRecord.cs index 74f5624..42cb435 100644 --- a/src/IKVM.ByteCode/Parsing/LocalVariableTargetTableItemRecord.cs +++ b/src/IKVM.ByteCode/Parsing/LocalVariableTargetTableItemRecord.cs @@ -1,14 +1,14 @@ namespace IKVM.ByteCode.Parsing { - public readonly record struct LocalVariableTargetTableItemRecord(ushort Offset, ushort Length, ushort Index) + public readonly record struct LocalVariableTargetTableItemRecord(ushort Start, ushort Length, ushort Index) { public static bool TryRead(ref ClassFormatReader reader, out LocalVariableTargetTableItemRecord record) { record = default; - if (reader.TryReadU2(out ushort offset) == false) + if (reader.TryReadU2(out ushort start) == false) return false; if (reader.TryReadU2(out ushort length) == false) @@ -17,7 +17,7 @@ record = default; if (reader.TryReadU2(out ushort index) == false) return false; - record = new LocalVariableTargetTableItemRecord(offset, length, index); + record = new LocalVariableTargetTableItemRecord(start, length, index); return true; } diff --git a/src/IKVM.ByteCode/Reading/BootstrapMethodsAttributeMethodReader.cs b/src/IKVM.ByteCode/Reading/BootstrapMethodsAttributeMethodReader.cs index 35a7bab..acc64e3 100644 --- a/src/IKVM.ByteCode/Reading/BootstrapMethodsAttributeMethodReader.cs +++ b/src/IKVM.ByteCode/Reading/BootstrapMethodsAttributeMethodReader.cs @@ -10,7 +10,7 @@ namespace IKVM.ByteCode.Reading public sealed class BootstrapMethodsAttributeMethodReader : ReaderBase { - MethodHandleConstantReader methodref; + MethodHandleConstantReader method; IReadOnlyList arguments; /// @@ -27,7 +27,7 @@ internal BootstrapMethodsAttributeMethodReader(ClassReader declaringClass, Boots /// /// Gets the method being referenced. /// - public MethodHandleConstantReader Method => LazyGet(ref methodref, () => DeclaringClass.Constants.Get(Record.MethodRef)); + public MethodHandleConstantReader Method => LazyGet(ref method, () => DeclaringClass.Constants.Get(Record.Method)); /// /// Gets the arguments bound to the method reference. diff --git a/src/IKVM.ByteCode/Reading/ConstantValueAttributeReader.cs b/src/IKVM.ByteCode/Reading/ConstantValueAttributeReader.cs index 528fae5..1f8c65d 100644 --- a/src/IKVM.ByteCode/Reading/ConstantValueAttributeReader.cs +++ b/src/IKVM.ByteCode/Reading/ConstantValueAttributeReader.cs @@ -22,7 +22,7 @@ internal ConstantValueAttributeReader(ClassReader declaringClass, AttributeInfoR public object Value => value ??= ResolveValue(); - object ResolveValue() => DeclaringClass.Constants.Get(Record.Handle) switch + object ResolveValue() => DeclaringClass.Constants.Get(Record.Value) switch { LongConstantReader l => l.Value, FloatConstantReader f => f.Value, diff --git a/src/IKVM.ByteCode/Writing/LocalVariableTargetTableEncoder.cs b/src/IKVM.ByteCode/Writing/LocalVariableTargetTableEncoder.cs index 33c3e8e..62fcce6 100644 --- a/src/IKVM.ByteCode/Writing/LocalVariableTargetTableEncoder.cs +++ b/src/IKVM.ByteCode/Writing/LocalVariableTargetTableEncoder.cs @@ -24,6 +24,25 @@ public LocalVariableTargetTableEncoder(BlobBuilder builder) _count = 0; } + /// + /// Encodes an existing local variable target table record. + /// + /// + public void Encode(LocalVariableTargetTableRecord table) + { + foreach (var i in table.Items) + Encode(i); + } + + /// + /// Encodes an existing local variable target. + /// + /// + public void Encode(LocalVariableTargetTableItemRecord target) + { + LocalVar(target.Start, target.Length, target.Index); + } + /// /// Adds a new local variable. /// @@ -36,7 +55,6 @@ public LocalVariableTargetTableEncoder LocalVar(ushort start, ushort length, ush new ClassFormatWriter(_countBlob.GetBytes()).TryWriteU2(++_count); return this; } - } } diff --git a/src/IKVM.ByteCode/Writing/TypeAnnotationEncoder.cs b/src/IKVM.ByteCode/Writing/TypeAnnotationEncoder.cs index 66a9463..7c70952 100644 --- a/src/IKVM.ByteCode/Writing/TypeAnnotationEncoder.cs +++ b/src/IKVM.ByteCode/Writing/TypeAnnotationEncoder.cs @@ -51,7 +51,7 @@ public void Encode(TypeAnnotationRecord record) ThrowsTarget(record.TargetType, target.ThrowsTypeIndex, e => e.Encode(record.TargetPath), record.Type, e => e.Encode(record.Elements)); break; case LocalVariableTargetTableRecord target: - LocalVarTarget(record.TargetType, e => e.Encode(target), record.Type, e => e.Encode(record.Elements)); + LocalVarTarget(record.TargetType, e => e.Encode(target), e => e.Encode(record.TargetPath), record.Type, e => e.Encode(record.Elements)); break; case CatchTargetRecord target: CatchTarget(record.TargetType, target.ExceptionTableIndex, e => e.Encode(record.TargetPath), record.Type, e => e.Encode(record.Elements)); @@ -133,7 +133,7 @@ public void MethodTypeParameter(byte typeParameterIndex, Action /// /// /// - void SuperTypeTarget(TypeAnnotationTargetType targetType, byte superTypeIndex, Action targetPath, Utf8ConstantHandle type, Action elementValues) + void SuperTypeTarget(TypeAnnotationTargetType targetType, ushort superTypeIndex, Action targetPath, Utf8ConstantHandle type, Action elementValues) { if (_count > 0) throw new InvalidOperationException("Encoder can only encode a single type annotation.");