Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding store import cleanup #53

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/instance.zig
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,17 @@ pub const FunctionImport = struct {
return copy;
}

fn deinit(import: *FunctionImport, allocator: std.mem.Allocator) void {
allocator.free(import.name);

switch (import.data) {
.Host => |*data| {
data.func_def.types.deinit();
},
.Wasm => {},
}
}

pub fn isTypeSignatureEql(import: *const FunctionImport, type_signature: *const FunctionTypeDefinition) bool {
var type_comparer = FunctionTypeDefinition.SortContext{};
switch (import.data) {
Expand All @@ -420,6 +431,10 @@ pub const TableImport = struct {
copy.name = try allocator.dupe(u8, copy.name);
return copy;
}

fn deinit(import: *TableImport, allocator: std.mem.Allocator) void {
allocator.free(import.name);
}
};

pub const MemoryImport = struct {
Expand All @@ -434,6 +449,10 @@ pub const MemoryImport = struct {
copy.name = try allocator.dupe(u8, copy.name);
return copy;
}

fn deinit(import: *MemoryImport, allocator: std.mem.Allocator) void {
allocator.free(import.name);
}
};

pub const GlobalImport = struct {
Expand All @@ -448,6 +467,10 @@ pub const GlobalImport = struct {
copy.name = try allocator.dupe(u8, copy.name);
return copy;
}

fn deinit(import: *GlobalImport, allocator: std.mem.Allocator) void {
allocator.free(import.name);
}
};

pub const ModuleImportPackage = struct {
Expand Down Expand Up @@ -535,6 +558,7 @@ pub const Store = struct {
memories: std.ArrayList(MemoryImport),
globals: std.ArrayList(GlobalImport),
},
allocator: std.mem.Allocator,

fn init(allocator: std.mem.Allocator) Store {
const store = Store{
Expand All @@ -548,6 +572,7 @@ pub const Store = struct {
.memories = std.ArrayList(MemoryInstance).init(allocator),
.globals = std.ArrayList(GlobalInstance).init(allocator),
.elements = std.ArrayList(ElementInstance).init(allocator),
.allocator = allocator,
};

return store;
Expand All @@ -566,6 +591,23 @@ pub const Store = struct {

self.globals.deinit();
self.elements.deinit();

for (self.imports.functions.items) |*item| {
item.deinit(self.allocator);
}
self.imports.functions.deinit();
for (self.imports.tables.items) |*item| {
item.deinit(self.allocator);
}
self.imports.tables.deinit();
for (self.imports.memories.items) |*item| {
item.deinit(self.allocator);
}
self.imports.memories.deinit();
for (self.imports.globals.items) |*item| {
item.deinit(self.allocator);
}
self.imports.globals.deinit();
}

pub fn getTable(self: *Store, index: usize) *TableInstance {
Expand Down
Loading