From 303328bd921c7b21f4fba27fe15922783dca671c Mon Sep 17 00:00:00 2001 From: Jerome Haltom Date: Tue, 10 Dec 2024 11:21:07 -0600 Subject: [PATCH] Fix nil lookup. --- .../Encoding/CodeBuilderTests.cs | 3 +- src/IKVM.ByteCode/Decoding/ConstantTable.cs | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/IKVM.ByteCode.Tests/Encoding/CodeBuilderTests.cs b/src/IKVM.ByteCode.Tests/Encoding/CodeBuilderTests.cs index f9ec7e9..0d27087 100644 --- a/src/IKVM.ByteCode.Tests/Encoding/CodeBuilderTests.cs +++ b/src/IKVM.ByteCode.Tests/Encoding/CodeBuilderTests.cs @@ -221,6 +221,7 @@ public void CanEncodeException() { var code = new BlobBuilder(); var excp = new BlobBuilder(); + var excpEncoder = new ExceptionTableEncoder(excp); new CodeBuilder(code) .DefineLabel(out var end) .BeginExceptionBlock(ClassConstantHandle.Nil, out var handlerLabel) @@ -233,7 +234,7 @@ public void CanEncodeException() .Goto(end) .MarkLabel(end) .Return() - .SerializeExceptions(excp); + .WriteExceptionsTo(ref excpEncoder); var codeReader = new SequenceReader(new ReadOnlySequence(code.ToArray())); var excpReader = new SequenceReader(new ReadOnlySequence(excp.ToArray())); diff --git a/src/IKVM.ByteCode/Decoding/ConstantTable.cs b/src/IKVM.ByteCode/Decoding/ConstantTable.cs index 5800f14..346e519 100644 --- a/src/IKVM.ByteCode/Decoding/ConstantTable.cs +++ b/src/IKVM.ByteCode/Decoding/ConstantTable.cs @@ -953,8 +953,13 @@ public readonly PackageConstant Get(PackageConstantHandle handle) /// readonly IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + #region IConstantPool + readonly ConstantHandle IConstantPool.Get(in Constant value) { + if (value.IsNil) + return ConstantHandle.Nil; + return value.Kind switch { ConstantKind.Utf8 => ((IConstantPool)this).Get((Utf8Constant)value), @@ -980,6 +985,9 @@ readonly ConstantHandle IConstantPool.Get(in Constant value) readonly Utf8ConstantHandle IConstantPool.Get(in Utf8Constant value) { + if (value.IsNil) + return Utf8ConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Utf8) @@ -996,6 +1004,9 @@ readonly Utf8ConstantHandle IConstantPool.Get(in Utf8Constant value) readonly IntegerConstantHandle IConstantPool.Get(in IntegerConstant value) { + if (value.IsNil) + return IntegerConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Integer) @@ -1012,6 +1023,9 @@ readonly IntegerConstantHandle IConstantPool.Get(in IntegerConstant value) readonly FloatConstantHandle IConstantPool.Get(in FloatConstant value) { + if (value.IsNil) + return FloatConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Float) @@ -1028,6 +1042,9 @@ readonly FloatConstantHandle IConstantPool.Get(in FloatConstant value) readonly LongConstantHandle IConstantPool.Get(in LongConstant value) { + if (value.IsNil) + return LongConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Long) @@ -1044,6 +1061,9 @@ readonly LongConstantHandle IConstantPool.Get(in LongConstant value) readonly DoubleConstantHandle IConstantPool.Get(in DoubleConstant value) { + if (value.IsNil) + return DoubleConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Double) @@ -1060,6 +1080,9 @@ readonly DoubleConstantHandle IConstantPool.Get(in DoubleConstant value) readonly ClassConstantHandle IConstantPool.Get(in ClassConstant value) { + if (value.IsNil) + return ClassConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Class) @@ -1076,6 +1099,9 @@ readonly ClassConstantHandle IConstantPool.Get(in ClassConstant value) readonly StringConstantHandle IConstantPool.Get(in StringConstant value) { + if (value.IsNil) + return StringConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.String) @@ -1092,6 +1118,9 @@ readonly StringConstantHandle IConstantPool.Get(in StringConstant value) readonly FieldrefConstantHandle IConstantPool.Get(in FieldrefConstant value) { + if (value.IsNil) + return FieldrefConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Fieldref) @@ -1108,6 +1137,9 @@ readonly FieldrefConstantHandle IConstantPool.Get(in FieldrefConstant value) readonly MethodrefConstantHandle IConstantPool.Get(in MethodrefConstant value) { + if (value.IsNil) + return MethodrefConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Methodref) @@ -1124,6 +1156,9 @@ readonly MethodrefConstantHandle IConstantPool.Get(in MethodrefConstant value) readonly InterfaceMethodrefConstantHandle IConstantPool.Get(in InterfaceMethodrefConstant value) { + if (value.IsNil) + return InterfaceMethodrefConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.InterfaceMethodref) @@ -1140,6 +1175,9 @@ readonly InterfaceMethodrefConstantHandle IConstantPool.Get(in InterfaceMethodre readonly NameAndTypeConstantHandle IConstantPool.Get(in NameAndTypeConstant value) { + if (value.IsNil) + return NameAndTypeConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.NameAndType) @@ -1156,6 +1194,9 @@ readonly NameAndTypeConstantHandle IConstantPool.Get(in NameAndTypeConstant valu readonly MethodHandleConstantHandle IConstantPool.Get(in MethodHandleConstant value) { + if (value.IsNil) + return MethodHandleConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.MethodHandle) @@ -1172,6 +1213,9 @@ readonly MethodHandleConstantHandle IConstantPool.Get(in MethodHandleConstant va readonly MethodTypeConstantHandle IConstantPool.Get(in MethodTypeConstant value) { + if (value.IsNil) + return MethodTypeConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.MethodType) @@ -1188,6 +1232,9 @@ readonly MethodTypeConstantHandle IConstantPool.Get(in MethodTypeConstant value) readonly DynamicConstantHandle IConstantPool.Get(in DynamicConstant value) { + if (value.IsNil) + return DynamicConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Dynamic) @@ -1204,6 +1251,9 @@ readonly DynamicConstantHandle IConstantPool.Get(in DynamicConstant value) readonly InvokeDynamicConstantHandle IConstantPool.Get(in InvokeDynamicConstant value) { + if (value.IsNil) + return InvokeDynamicConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.InvokeDynamic) @@ -1220,6 +1270,9 @@ readonly InvokeDynamicConstantHandle IConstantPool.Get(in InvokeDynamicConstant readonly ModuleConstantHandle IConstantPool.Get(in ModuleConstant value) { + if (value.IsNil) + return ModuleConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Module) @@ -1236,6 +1289,9 @@ readonly ModuleConstantHandle IConstantPool.Get(in ModuleConstant value) readonly PackageConstantHandle IConstantPool.Get(in PackageConstant value) { + if (value.IsNil) + return PackageConstantHandle.Nil; + foreach (var i in this) { if (ReadData(i).Kind == ConstantKind.Package) @@ -1250,6 +1306,8 @@ readonly PackageConstantHandle IConstantPool.Get(in PackageConstant value) throw new ByteCodeException("Unknown constant for value."); } + #endregion + } }