Compare commits

...
Sign in to create a new pull request.

3 commits

Author SHA1 Message Date
bc8d525a3e Don't store allocator 2026-07-16 08:34:33 +00:00
10c8c9b98f Update record attribute formatting 2026-07-16 08:19:34 +00:00
39fcd51a7c Add EnumFlags.from 2026-07-14 10:13:26 +00:00
7 changed files with 36 additions and 31 deletions

View file

@ -7,8 +7,6 @@ pub const MethodInfo = @import("Class/MethodInfo.zig");
pub const AttributeInfo = @import("Class/AttributeInfo.zig").AttributeInfo; pub const AttributeInfo = @import("Class/AttributeInfo.zig").AttributeInfo;
pub const ConstantPool = @import("Class/ConstantPool.zig"); pub const ConstantPool = @import("Class/ConstantPool.zig");
allocator: std.mem.Allocator,
magic: u32, magic: u32,
minor_version: u16, minor_version: u16,
major_version: u16, major_version: u16,
@ -57,7 +55,7 @@ pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) (ParseError ||
var constant_pool: ConstantPool = try .parse(input, allocator); var constant_pool: ConstantPool = try .parse(input, allocator);
errdefer constant_pool.deinit(allocator); errdefer constant_pool.deinit(allocator);
const access_flags: ClassAccessFlags = .{ .mask = try input.takeInt(u16, .big) }; const access_flags: ClassAccessFlags = .from(try input.takeInt(u16, .big));
const this_class = try input.takeInt(u16, .big); const this_class = try input.takeInt(u16, .big);
const super_class = try input.takeInt(u16, .big); const super_class = try input.takeInt(u16, .big);
@ -90,7 +88,6 @@ pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) (ParseError ||
} }
return .{ return .{
.allocator = allocator,
.magic = magic, .magic = magic,
.minor_version = minor_version, .minor_version = minor_version,
.major_version = major_version, .major_version = major_version,
@ -153,37 +150,37 @@ pub fn parseAttributes(input: *std.Io.Reader, constant_pool: ConstantPool, alloc
return attributes; return attributes;
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
self.constant_pool.deinit(self.allocator); self.constant_pool.deinit(allocator);
self.freeInterfaces(); self.freeInterfaces(allocator);
self.freeFields(); self.freeFields(allocator);
self.freeMethods(); self.freeMethods(allocator);
self.freeAttributes(); self.freeAttributes(allocator);
} }
fn freeInterfaces(self: *Self) void { fn freeInterfaces(self: *Self, allocator: std.mem.Allocator) void {
self.allocator.free(self.interfaces); allocator.free(self.interfaces);
} }
fn freeFields(self: *Self) void { fn freeFields(self: *Self, allocator: std.mem.Allocator) void {
for (self.fields) |*field_info| { for (self.fields) |*field_info| {
field_info.deinit(self.allocator); field_info.deinit(allocator);
} }
self.allocator.free(self.fields); allocator.free(self.fields);
} }
fn freeMethods(self: *Self) void { fn freeMethods(self: *Self, allocator: std.mem.Allocator) void {
for (self.methods) |*method_info| { for (self.methods) |*method_info| {
method_info.deinit(self.allocator); method_info.deinit(allocator);
} }
self.allocator.free(self.methods); allocator.free(self.methods);
} }
fn freeAttributes(self: *Self) void { fn freeAttributes(self: *Self, allocator: std.mem.Allocator) void {
for (self.attributes) |*attr| { for (self.attributes) |*attr| {
attr.deinit(self.allocator); attr.deinit(allocator);
} }
self.allocator.free(self.attributes); allocator.free(self.attributes);
} }
pub fn jsonStringify(self: *const Self, jws: *std.json.Stringify) !void { pub fn jsonStringify(self: *const Self, jws: *std.json.Stringify) !void {

View file

@ -625,7 +625,7 @@ pub const AttributeInfo = union(enum) {
try w.splatByteAll(' ', depth * component_indent); try w.splatByteAll(' ', depth * component_indent);
try w.print("{d}:\n", .{ i }); try w.print("{d}:\n", .{ i });
try component.indentedFormat(w, depth, component_indent, constant_pool); try component.indentedFormat(w, depth, component_indent + 1, constant_pool);
} }
}, },
.permitted_subclasses => |attr| { .permitted_subclasses => |attr| {
@ -1200,7 +1200,7 @@ pub const InnerClasses = struct {
.inner_class_info_index = try input.takeInt(u16, .big), .inner_class_info_index = try input.takeInt(u16, .big),
.outer_class_info_index = try input.takeInt(u16, .big), .outer_class_info_index = try input.takeInt(u16, .big),
.inner_name_index = try input.takeInt(u16, .big), .inner_name_index = try input.takeInt(u16, .big),
.inner_class_access_flags = .{ .mask = try input.takeInt(u16, .big) }, .inner_class_access_flags = .from(try input.takeInt(u16, .big)),
}; };
} }
@ -2491,7 +2491,7 @@ pub const MethodParameters = struct {
for (parameters) |*parameter| { for (parameters) |*parameter| {
parameter.* = .{ parameter.* = .{
.name_index = try input.takeInt(u16, .big), .name_index = try input.takeInt(u16, .big),
.access_flags = .{ .mask = try input.takeInt(u16, .big) }, .access_flags = .from(try input.takeInt(u16, .big)),
}; };
} }
@ -2695,7 +2695,7 @@ pub const Module = struct {
pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Self { pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Self {
const module_name_index = try input.takeInt(u16, .big); const module_name_index = try input.takeInt(u16, .big);
const module_flags: ModuleFlags = .{ .mask = try input.takeInt(u16, .big) }; const module_flags: ModuleFlags = .from(try input.takeInt(u16, .big));
const module_version_index = try input.takeInt(u16, .big); const module_version_index = try input.takeInt(u16, .big);
const requires_count = try input.takeInt(u16, .big); const requires_count = try input.takeInt(u16, .big);
@ -2703,7 +2703,7 @@ pub const Module = struct {
errdefer allocator.free(requires); errdefer allocator.free(requires);
for (requires) |*r| { for (requires) |*r| {
const requires_index = try input.takeInt(u16, .big); const requires_index = try input.takeInt(u16, .big);
const requires_flags: Requires.RequiresFlags = .{ .mask = try input.takeInt(u16, .big) }; const requires_flags: Requires.RequiresFlags = .from(try input.takeInt(u16, .big));
const requires_version_index = try input.takeInt(u16, .big); const requires_version_index = try input.takeInt(u16, .big);
r.* = .{ r.* = .{
.requires_index = requires_index, .requires_index = requires_index,
@ -2717,7 +2717,7 @@ pub const Module = struct {
errdefer allocator.free(exports); errdefer allocator.free(exports);
for (exports) |*e| { for (exports) |*e| {
const exports_index = try input.takeInt(u16, .big); const exports_index = try input.takeInt(u16, .big);
const exports_flags: Exports.ExportsFlags = .{ .mask = try input.takeInt(u16, .big) }; const exports_flags: Exports.ExportsFlags = .from(try input.takeInt(u16, .big));
const exports_to_count = try input.takeInt(u16, .big); const exports_to_count = try input.takeInt(u16, .big);
const exports_to_indeces = try allocator.alloc(u16, exports_to_count); const exports_to_indeces = try allocator.alloc(u16, exports_to_count);
errdefer allocator.free(exports_to_indeces); errdefer allocator.free(exports_to_indeces);
@ -2736,7 +2736,7 @@ pub const Module = struct {
errdefer allocator.free(opens); errdefer allocator.free(opens);
for (opens) |*o| { for (opens) |*o| {
const opens_index = try input.takeInt(u16, .big); const opens_index = try input.takeInt(u16, .big);
const opens_flags: Opens.OpensFlags = .{ .mask = try input.takeInt(u16, .big) }; const opens_flags: Opens.OpensFlags = .from(try input.takeInt(u16, .big));
const opens_to_count = try input.takeInt(u16, .big); const opens_to_count = try input.takeInt(u16, .big);
const opens_to_indeces = try allocator.alloc(u16, opens_to_count); const opens_to_indeces = try allocator.alloc(u16, opens_to_count);
errdefer allocator.free(opens_to_indeces); errdefer allocator.free(opens_to_indeces);

View file

@ -27,7 +27,7 @@ pub const FieldAccessFlags = EnumFlags(enum(u16) {
}); });
pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) !Self { pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) !Self {
const access_flags: FieldAccessFlags = .{ .mask = try input.takeInt(u16, .big) }; const access_flags: FieldAccessFlags = .from(try input.takeInt(u16, .big));
const name_index = try input.takeInt(u16, .big); const name_index = try input.takeInt(u16, .big);
const descriptor_index = try input.takeInt(u16, .big); const descriptor_index = try input.takeInt(u16, .big);
const attributes = try Class.parseAttributes(input, constant_pool, allocator); const attributes = try Class.parseAttributes(input, constant_pool, allocator);

View file

@ -30,7 +30,7 @@ pub const MethodAccessFlags = EnumFlags(enum(u16) {
}); });
pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) !Self { pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) !Self {
const access_flags: MethodAccessFlags = .{ .mask = try input.takeInt(u16, .big) }; const access_flags: MethodAccessFlags = .from(try input.takeInt(u16, .big));
const name_index = try input.takeInt(u16, .big); const name_index = try input.takeInt(u16, .big);
const descriptor_index = try input.takeInt(u16, .big); const descriptor_index = try input.takeInt(u16, .big);
const attributes = try Class.parseAttributes(input, constant_pool, allocator); const attributes = try Class.parseAttributes(input, constant_pool, allocator);

View file

@ -10,6 +10,12 @@ pub fn EnumFlags(comptime E: type) type {
pub const empty: Self = .{ .data = 0 }; pub const empty: Self = .{ .data = 0 };
pub fn from(mask: BackingInt) Self {
return .{
.mask = mask,
};
}
pub fn contains(self: Self, flag: E) bool { pub fn contains(self: Self, flag: E) bool {
return (self.mask & @intFromEnum(flag)) != 0; return (self.mask & @intFromEnum(flag)) != 0;
} }

View file

@ -50,7 +50,7 @@ pub fn main(init: std.process.Init) !void {
}, },
else => return err, else => return err,
}; };
defer class.deinit(); defer class.deinit(allocator);
try stdout.print("{f}", .{ class }); try stdout.print("{f}", .{ class });

View file

@ -0,0 +1,2 @@
public record Record(String name, int age) {}