From b8c63f726eda7b9cfe740a2a2249e1e7813d7b1f Mon Sep 17 00:00:00 2001 From: ktkk Date: Sun, 5 Jul 2026 20:17:10 +0200 Subject: [PATCH] Format local variable table attribute --- src/Class/AttributeInfo.zig | 55 ++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/Class/AttributeInfo.zig b/src/Class/AttributeInfo.zig index f7a8e59..3403136 100644 --- a/src/Class/AttributeInfo.zig +++ b/src/Class/AttributeInfo.zig @@ -411,7 +411,20 @@ pub const AttributeInfo = union(enum) { try line_number.indentedFormat(w, depth, line_number_indent + 1); } }, - //.local_variable_table, + .local_variable_table => |attr| { + try w.splatByteAll(' ', depth * indent); + _ = try w.write("local_variable_table:\n"); + var local_variable_indent = indent; + for (attr.local_variable_table, 0..) |*local_variable, i| { + local_variable_indent += 1; + defer local_variable_indent -= 1; + + try w.splatByteAll(' ', depth * local_variable_indent); + try w.print("{d}:\n", .{ i }); + + try local_variable.indentedFormat(w, depth, local_variable_indent + 1, constant_pool); + } + }, //.local_variable_type_table, .deprecated => {}, //.runtime_visible_annotations, @@ -1128,6 +1141,46 @@ pub const LocalVariableTable = struct { name_index: u16, descriptor_index: u16, index: u16, + + pub fn indentedFormat( + self: *const LocalVariable, + 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("length: {d}\n", .{ self.length }); + + const name_ = self.name(constant_pool) catch return error.WriteFailed; + try w.splatByteAll(' ', depth * indent); + _ = try w.write("name:\n"); + try name_.indentedFormat(w, depth, indent + 1, constant_pool); + + const descriptor_ = self.descriptor(constant_pool) catch return error.WriteFailed; + try w.splatByteAll(' ', depth * indent); + _ = try w.write("descriptor:\n"); + try descriptor_.indentedFormat(w, depth, indent + 1, constant_pool); + } + + pub fn name(self: *const LocalVariable, constant_pool: []const ?ConstantPoolInfo) ResolveError!ConstantPoolInfo { + const name_index = self.name_index - 1; + if (name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex; + const cp_info = constant_pool[name_index]; + if (cp_info == null or @as(ConstantPoolInfo.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType; + return cp_info.?; + } + + pub fn descriptor(self: *const LocalVariable, constant_pool: []const ?ConstantPoolInfo) ResolveError!ConstantPoolInfo { + const descriptor_index = self.descriptor_index - 1; + if (descriptor_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex; + const cp_info = constant_pool[descriptor_index]; + if (cp_info == null or @as(ConstantPoolInfo.Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType; + return cp_info.?; + } }; pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Self {