Skip to content

Commit

Permalink
Fix nil lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Dec 10, 2024
1 parent 3516735 commit 303328b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/IKVM.ByteCode.Tests/Encoding/CodeBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -233,7 +234,7 @@ public void CanEncodeException()
.Goto(end)
.MarkLabel(end)
.Return()
.SerializeExceptions(excp);
.WriteExceptionsTo(ref excpEncoder);

var codeReader = new SequenceReader<byte>(new ReadOnlySequence<byte>(code.ToArray()));
var excpReader = new SequenceReader<byte>(new ReadOnlySequence<byte>(excp.ToArray()));
Expand Down
58 changes: 58 additions & 0 deletions src/IKVM.ByteCode/Decoding/ConstantTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -953,8 +953,13 @@ public readonly PackageConstant Get(PackageConstantHandle handle)
/// <inheritdoc />
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),
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -1250,6 +1306,8 @@ readonly PackageConstantHandle IConstantPool.Get(in PackageConstant value)
throw new ByteCodeException("Unknown constant for value.");
}

#endregion

}

}

0 comments on commit 303328b

Please sign in to comment.