From 6971dce705ca2c8e82ac607b957cc132536b66e0 Mon Sep 17 00:00:00 2001 From: ktkk Date: Sun, 5 Jul 2026 16:05:46 +0200 Subject: [PATCH] Format code attribute exception table --- src/Class/AttributeInfo.zig | 53 ++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/src/Class/AttributeInfo.zig b/src/Class/AttributeInfo.zig index 50fd96d..9e13aa0 100644 --- a/src/Class/AttributeInfo.zig +++ b/src/Class/AttributeInfo.zig @@ -308,18 +308,18 @@ pub const AttributeInfo = union(enum) { try w.print("{d}: 0x{x}\n", .{ i, instr }); } - //try w.splatByteAll(' ', depth * indent); - //_ = try w.write("exception_table:\n"); - //var exception_indent = indent; - //for (attr.exception_table, 0..) |*exception, i| { - // exception_indent += 1; - // defer exception_indent -= 1; + try w.splatByteAll(' ', depth * indent); + _ = try w.write("exception_table:\n"); + var exception_indent = indent; + for (attr.exception_table, 0..) |*exception, i| { + exception_indent += 1; + defer exception_indent -= 1; - // try w.splatByteAll(' ', depth * exception_indent); - // try w.print("{d}:\n", .{ i }); + try w.splatByteAll(' ', depth * exception_indent); + try w.print("{d}:\n", .{ i }); - // try self.formatExceptionHandler(w, depth, exception_indent + 1, exception); - //} + try exception.indentedFormat(w, depth, exception_indent + 1, constant_pool); + } try w.splatByteAll(' ', depth * indent); _ = try w.write("attributes:\n"); @@ -569,6 +569,39 @@ pub const Code = struct { end_pc: u16, handler_pc: u16, catch_type: u16, + + pub fn indentedFormat( + self: *const ExceptionHandler, + 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("start_pc: {d}\n", .{ self.start_pc }); + + try w.splatByteAll(' ', depth * indent); + try w.print("end_pc: {d}\n", .{ self.end_pc }); + + try w.splatByteAll(' ', depth * indent); + try w.print("handler_pc: {d}\n", .{ self.handler_pc }); + + const catch_type = self.catchType(constant_pool) catch return error.WriteFailed; + if (catch_type) |ct| { + try w.splatByteAll(' ', depth * indent); + _ = try w.write("catch_type:\n"); + try ct.indentedFormat(w, depth, indent + 1, constant_pool); + } + } + + pub fn catchType(self: *const ExceptionHandler, constant_pool: []const ?ConstantPoolInfo) ResolveError!?ConstantPoolInfo { + if (self.catch_type == 0) return null; + const catch_type = self.catch_type - 1; + if (catch_type >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex; + const cp_info = constant_pool[catch_type]; + if (cp_info == null or @as(ConstantPoolInfo.Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType; + return cp_info.?; + } }; pub fn parse(input: *std.Io.Reader, constant_pool: []const ?ConstantPoolInfo, allocator: std.mem.Allocator) (ParseError || ResolveError)!Self {