Skip to content

Commit

Permalink
More.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Jul 28, 2024
1 parent 8f0041d commit d150bdc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
Expand Down
47 changes: 26 additions & 21 deletions src/IKVM.ByteCode/Writing/ConstantBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ConstantBuilder

readonly MUTF8Encoding _mutf8;
readonly BlobBuilder _builder = new();
ushort _count = 0;
ushort _next = 1;

readonly Dictionary<string, Utf8ConstantHandle> _utf8cache = new();

Expand All @@ -26,6 +26,11 @@ public ConstantBuilder(ClassFormatVersion version)
_mutf8 = MUTF8Encoding.GetMUTF8(version.Major);
}

/// <summary>
/// Gets the number of constants currently added.
/// </summary>
public int Count => _next - 1;

/// <summary>
/// Adds a new UTF8 constant value.
/// </summary>
Expand All @@ -38,7 +43,7 @@ public Utf8ConstantHandle AddUtf8Constant(ReadOnlySpan<byte> value)
w.TryWriteU1((byte)ConstantTag.Utf8);
w.TryWriteU2((ushort)value.Length);
_builder.WriteBytes(value);
return new(_count++);
return new(_next++);
}

/// <summary>
Expand Down Expand Up @@ -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++);
}

/// <summary>
Expand All @@ -88,7 +93,7 @@ public FloatConstantHandle AddFloatConstant(float value)
#else
w.TryWriteU4(BitConverter.SingleToUInt32Bits(value));
#endif
return new(_count++);
return new(_next++);
}

/// <summary>
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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++);
}

/// <summary>
Expand All @@ -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);
}

Expand Down

0 comments on commit d150bdc

Please sign in to comment.