Dump constant pool in debug mode

This commit is contained in:
ktkk 2026-07-04 01:43:11 +02:00
parent a18ae9c265
commit 07d43f2fe7
5 changed files with 77 additions and 42 deletions

View file

@ -246,11 +246,11 @@ pub fn format(self: *const Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
const this_class = self.thisClass() catch return error.WriteFailed;
_ = try w.write("this class:\n");
try this_class.format(w, depth, indent + 1, self.constant_pool);
try this_class.indentedFormat(w, depth, indent + 1, self.constant_pool);
_ = try w.write("super class:\n");
if (self.superClass() catch return error.WriteFailed) |super_class| {
try super_class.format(w, depth, indent + 1, self.constant_pool);
try super_class.indentedFormat(w, depth, indent + 1, self.constant_pool);
}
_ = try w.write("interfaces:\n");
@ -264,7 +264,7 @@ pub fn format(self: *const Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("{d}:\n", .{ i });
try cp_info.?.format(w, depth, indent + 1, self.constant_pool);
try cp_info.?.indentedFormat(w, depth, indent + 1, self.constant_pool);
}
_ = try w.write("fields:\n");
@ -274,7 +274,7 @@ pub fn format(self: *const Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("{d}:\n", .{ i });
try field.format(w, depth, indent + 1, self.allocator, self.constant_pool);
try field.indentedFormat(w, depth, indent + 1, self.allocator, self.constant_pool);
}
_ = try w.write("methods:\n");
@ -284,7 +284,7 @@ pub fn format(self: *const Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("{d}:\n", .{ i });
try method.format(w, depth, indent + 1, self.allocator, self.constant_pool);
try method.indentedFormat(w, depth, indent + 1, self.allocator, self.constant_pool);
}
_ = try w.write("attributes:\n");
@ -294,7 +294,7 @@ pub fn format(self: *const Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("{d}:\n", .{ i });
try attribute.format(w, depth, indent + 1, self.allocator, self.constant_pool);
try attribute.indentedFormat(w, depth, indent + 1, self.allocator, self.constant_pool);
}
}
@ -656,7 +656,26 @@ pub const ConstantPoolInfo = union(ConstantPoolInfo.Tag) {
}
}
pub fn format(self: *const ConstantPoolInfo, w: *std.Io.Writer, depth: usize, indent: u8, constant_pool: []const ?ConstantPoolInfo) std.Io.Writer.Error!void {
pub fn format(self: *const ConstantPoolInfo, w: *std.Io.Writer) std.Io.Writer.Error!void {
switch (self.*) {
.class => |class| try w.print("#{d}", .{ class.name_index }),
.field_ref => |field_ref| try w.print("#{d}.#{d}", .{ field_ref.class_index, field_ref.name_and_type_index }),
.method_ref => |method_ref| try w.print("#{d}.#{d}", .{ method_ref.class_index, method_ref.name_and_type_index }),
.interface_method_ref => |interface_method_ref| try w.print("#{d}.#{d}", .{ interface_method_ref.class_index, interface_method_ref.name_and_type_index }),
.string => |string| try w.print("#{d}", .{ string.string_index }),
.integer => |integer| try w.print("{d}", .{ integer.bytes }),
.float => |float| try w.print("{d}", .{ @as(f32, @bitCast(float.bytes)) }),
.long => |long| try w.print("{d}", .{ (@as(u64, long.high_bytes) << 32) | long.low_bytes }),
.double => |double| try w.print("{d}", .{ @as(f64, @bitCast((@as(u64, double.high_bytes) << 32) | double.low_bytes)) }),
.name_and_type => |name_and_type| try w.print("#{d}:#{d}", .{ name_and_type.name_index, name_and_type.descriptor_index }),
.utf8 => |utf8| try w.print("\"{s}\"", .{ utf8.bytes }),
.method_handle => |method_handle| try w.print("{s} #{d}", .{ @tagName(method_handle.reference_kind), method_handle.reference_index }),
.method_type => |method_type| try w.print("#{d}", .{ method_type.descriptor_index }),
.invoke_dynamic => |invoke_dynamic| try w.print("#{d} #{d}", .{ invoke_dynamic.bootstrap_method_attr_index, invoke_dynamic.name_and_type_index }),
}
}
pub fn indentedFormat(self: *const ConstantPoolInfo, w: *std.Io.Writer, depth: usize, indent: u8, constant_pool: []const ?ConstantPoolInfo) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("constant_value_type: {s}\n", .{ @tagName(self.*) });
@ -665,44 +684,44 @@ pub const ConstantPoolInfo = union(ConstantPoolInfo.Tag) {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name:\n");
const class_name = class.name(constant_pool) catch return error.WriteFailed;
try class_name.format(w, depth, indent + 1, constant_pool);
try class_name.indentedFormat(w, depth, indent + 1, constant_pool);
},
.field_ref => |field_ref| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("class:\n");
const class = field_ref.class(constant_pool) catch return error.WriteFailed;
try class.format(w, depth, indent + 1, constant_pool);
try class.indentedFormat(w, depth, indent + 1, constant_pool);
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name_and_type:\n");
const name_and_type = field_ref.nameAndType(constant_pool) catch return error.WriteFailed;
try name_and_type.format(w, depth, indent + 1, constant_pool);
try name_and_type.indentedFormat(w, depth, indent + 1, constant_pool);
},
.method_ref => |method_ref| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("class:\n");
const class = method_ref.class(constant_pool) catch return error.WriteFailed;
try class.format(w, depth, indent + 1, constant_pool);
try class.indentedFormat(w, depth, indent + 1, constant_pool);
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name_and_type:\n");
const name_and_type = method_ref.nameAndType(constant_pool) catch return error.WriteFailed;
try name_and_type.format(w, depth, indent + 1, constant_pool);
try name_and_type.indentedFormat(w, depth, indent + 1, constant_pool);
},
.interface_method_ref => |interface_method_ref| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("class:\n");
const class = interface_method_ref.class(constant_pool) catch return error.WriteFailed;
try class.format(w, depth, indent + 1, constant_pool);
try class.indentedFormat(w, depth, indent + 1, constant_pool);
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name_and_type:\n");
const name_and_type = interface_method_ref.nameAndType(constant_pool) catch return error.WriteFailed;
try name_and_type.format(w, depth, indent + 1, constant_pool);
try name_and_type.indentedFormat(w, depth, indent + 1, constant_pool);
},
.string => |string| {
const s = string.string(constant_pool) catch return error.WriteFailed;
try s.format(w, depth, indent + 1, constant_pool);
try s.indentedFormat(w, depth, indent + 1, constant_pool);
},
.integer => |integer| {
try w.splatByteAll(' ', depth * indent);
@ -731,12 +750,12 @@ pub const ConstantPoolInfo = union(ConstantPoolInfo.Tag) {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name:\n");
const name = name_and_type.name(constant_pool) catch return error.WriteFailed;
try name.format(w, depth, indent + 1, constant_pool);
try name.indentedFormat(w, depth, indent + 1, constant_pool);
try w.splatByteAll(' ', depth * indent);
_ = try w.write("descriptor:\n");
const descriptor = name_and_type.descriptor(constant_pool) catch return error.WriteFailed;
try descriptor.format(w, depth, indent + 1, constant_pool);
try descriptor.indentedFormat(w, depth, indent + 1, constant_pool);
},
.utf8 => |utf8| {
try w.splatByteAll(' ', depth * indent);
@ -749,19 +768,19 @@ pub const ConstantPoolInfo = union(ConstantPoolInfo.Tag) {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("reference:\n");
const reference = method_handle.reference(constant_pool) catch return error.WriteFailed;
try reference.format(w, depth, indent + 1, constant_pool);
try reference.indentedFormat(w, depth, indent + 1, constant_pool);
},
.method_type => |method_type| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("descriptor:\n");
const descriptor = method_type.descriptor(constant_pool) catch return error.WriteFailed;
try descriptor.format(w, depth, indent + 1, constant_pool);
try descriptor.indentedFormat(w, depth, indent + 1, constant_pool);
},
.invoke_dynamic => |invoke_dynamic| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name_and_type:\n");
const name_and_type = invoke_dynamic.nameAndType(constant_pool) catch return error.WriteFailed;
try name_and_type.format(w, depth, indent + 1, constant_pool);
try name_and_type.indentedFormat(w, depth, indent + 1, constant_pool);
},
}
}