Refactor AttributeInfo
This commit is contained in:
parent
5f23482b1b
commit
a18ae9c265
4 changed files with 1122 additions and 905 deletions
|
|
@ -4,7 +4,7 @@ const EnumFlags = @import("EnumFlags.zig").EnumFlags;
|
|||
|
||||
pub const FieldInfo = @import("Class/FieldInfo.zig");
|
||||
pub const MethodInfo = @import("Class/MethodInfo.zig");
|
||||
pub const AttributeInfo = @import("Class/AttributeInfo.zig");
|
||||
pub const AttributeInfo = @import("Class/AttributeInfo.zig").AttributeInfo;
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
|
|
@ -45,7 +45,7 @@ pub const ResolveError = error {
|
|||
InvalidConstantType,
|
||||
};
|
||||
|
||||
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 || ResolveError)!Self {
|
||||
const magic = try input.takeInt(u32, .big);
|
||||
if (magic != 0xCAFEBABE) {
|
||||
return ParseError.InvalidMagicNumber;
|
||||
|
|
@ -70,7 +70,7 @@ pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Sel
|
|||
const interfaces = try parseInterfaces(input, allocator);
|
||||
errdefer allocator.free(interfaces);
|
||||
|
||||
const fields = try parseFields(input, allocator);
|
||||
const fields = try parseFields(input, constant_pool, allocator);
|
||||
errdefer {
|
||||
for (fields) |*field| {
|
||||
field.deinit(allocator);
|
||||
|
|
@ -78,7 +78,7 @@ pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Sel
|
|||
allocator.free(fields);
|
||||
}
|
||||
|
||||
const methods = try parseMethods(input, allocator);
|
||||
const methods = try parseMethods(input, constant_pool, allocator);
|
||||
errdefer {
|
||||
for (methods) |*method| {
|
||||
method.deinit(allocator);
|
||||
|
|
@ -86,7 +86,7 @@ pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Sel
|
|||
allocator.free(methods);
|
||||
}
|
||||
|
||||
const attributes = try parseAttributes(input, allocator);
|
||||
const attributes = try parseAttributes(input, constant_pool, allocator);
|
||||
errdefer {
|
||||
for (attributes) |*attribute| {
|
||||
attribute.deinit(allocator);
|
||||
|
|
@ -146,37 +146,37 @@ fn parseInterfaces(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseErr
|
|||
return interfaces;
|
||||
}
|
||||
|
||||
fn parseFields(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError![]FieldInfo {
|
||||
fn parseFields(input: *std.Io.Reader, constant_pool: []const ?ConstantPoolInfo, allocator: std.mem.Allocator) (ParseError || ResolveError)![]FieldInfo {
|
||||
const fields_count = try input.takeInt(u16, .big);
|
||||
const fields = try allocator.alloc(FieldInfo, fields_count);
|
||||
errdefer allocator.free(fields);
|
||||
|
||||
for (0..fields_count) |i| {
|
||||
fields[i] = try FieldInfo.parse(input, allocator);
|
||||
fields[i] = try FieldInfo.parse(input, constant_pool, allocator);
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
fn parseMethods(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError![]MethodInfo {
|
||||
fn parseMethods(input: *std.Io.Reader, constant_pool: []const ?ConstantPoolInfo, allocator: std.mem.Allocator) (ParseError || ResolveError)![]MethodInfo {
|
||||
const methods_count = try input.takeInt(u16, .big);
|
||||
const methods = try allocator.alloc(MethodInfo, methods_count);
|
||||
errdefer allocator.free(methods);
|
||||
|
||||
for (0..methods_count) |i| {
|
||||
methods[i] = try MethodInfo.parse(input, allocator);
|
||||
methods[i] = try MethodInfo.parse(input, constant_pool, allocator);
|
||||
}
|
||||
|
||||
return methods;
|
||||
}
|
||||
|
||||
pub fn parseAttributes(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError![]AttributeInfo {
|
||||
pub fn parseAttributes(input: *std.Io.Reader, constant_pool: []const ?ConstantPoolInfo, allocator: std.mem.Allocator) (ParseError || ResolveError)![]AttributeInfo {
|
||||
const attributes_count = try input.takeInt(u16, .big);
|
||||
const attributes = try allocator.alloc(AttributeInfo, attributes_count);
|
||||
errdefer allocator.free(attributes);
|
||||
|
||||
for (0..attributes_count) |i| {
|
||||
attributes[i] = try AttributeInfo.parse(input, allocator);
|
||||
attributes[i] = try AttributeInfo.parse(input, constant_pool, allocator);
|
||||
}
|
||||
|
||||
return attributes;
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -28,11 +28,11 @@ pub const FieldFlags = enum(u16) {
|
|||
|
||||
pub const FieldAccessFlags = EnumFlags(FieldFlags);
|
||||
|
||||
pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) !Self {
|
||||
pub fn parse(input: *std.Io.Reader, constant_pool: []const ?ConstantPoolInfo, allocator: std.mem.Allocator) !Self {
|
||||
const access_flags: FieldAccessFlags = .{ .mask = try input.takeInt(u16, .big) };
|
||||
const name_index = try input.takeInt(u16, .big);
|
||||
const descriptor_index = try input.takeInt(u16, .big);
|
||||
const attributes = try Class.parseAttributes(input, allocator);
|
||||
const attributes = try Class.parseAttributes(input, constant_pool, allocator);
|
||||
return .{
|
||||
.access_flags = access_flags,
|
||||
.name_index = name_index,
|
||||
|
|
|
|||
|
|
@ -31,11 +31,11 @@ pub const MethodFlags = enum(u16) {
|
|||
|
||||
pub const MethodAccessFlags = EnumFlags(MethodFlags);
|
||||
|
||||
pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) !Self {
|
||||
pub fn parse(input: *std.Io.Reader, constant_pool: []const ?ConstantPoolInfo, allocator: std.mem.Allocator) !Self {
|
||||
const access_flags: MethodAccessFlags = .{ .mask = try input.takeInt(u16, .big) };
|
||||
const name_index = try input.takeInt(u16, .big);
|
||||
const descriptor_index = try input.takeInt(u16, .big);
|
||||
const attributes = try Class.parseAttributes(input, allocator);
|
||||
const attributes = try Class.parseAttributes(input, constant_pool, allocator);
|
||||
return .{
|
||||
.access_flags = access_flags,
|
||||
.name_index = name_index,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue