Skip to content
Open
Show file tree
Hide file tree
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
21 changes: 15 additions & 6 deletions src/Package.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ pub const State = struct {
};
}

pub fn deinit(self: *State, gpa: Allocator) void {
pub fn deinit(self: *State, gpa: Allocator, lua: *const zlua.State) void {
for (self.packages.items(.build_func_ref)) |build_ref| {
lua.unref(zlua.REGISTRYINDEX, build_ref);
}
self.package_table.deinit(gpa);
self.string_state.deinit(gpa);
self.packages.deinit(gpa);
Expand All @@ -119,8 +122,8 @@ id: Id = .none,
/// name of Package
name: String,
version: std.SemanticVersion,
/// Lua stack index of pkg from manifest
lua_idx: i32,
/// Lua registry reference to the package's build function
build_func_ref: zlua.Idx,
desc: String,
homepage: String,
license: String,
Expand Down Expand Up @@ -222,8 +225,14 @@ pub fn init(
});
lua.pop(1);

if (lua.getField(pkg, "build") != .function) return error.WrongLuaType;
lua.pop(1);
const build_func_ref = switch (lua.getField(pkg, "build")) {
.function => lua.ref(zlua.REGISTRYINDEX), // pops build
else => |kind| {
log.err("Package expected build to be a function, got {t}", .{kind});
return error.WrongLuaType;
},
};
errdefer lua.unref(zlua.REGISTRYINDEX, build_func_ref);

var runtime_deps: Deps = .{ .start = 0, .count = 0 };
var compile_deps: Deps = .{ .start = 0, .count = 0 };
Expand Down Expand Up @@ -295,7 +304,7 @@ pub fn init(
.desc = desc,
.homepage = homepage,
.license = license,
.lua_idx = pkg,
.build_func_ref = build_func_ref,
.compile_deps = compile_deps,
.runtime_deps = runtime_deps,
};
Expand Down
11 changes: 4 additions & 7 deletions src/actions/build_package.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub fn build(io: Io, gpa: Allocator, arena: Allocator, env: *std.process.Environ
try lua_helpers.setupState(&lua);

var state: Package.State = .empty;
defer state.deinit(arena, &lua);

const pkg_id = try Package.collect(io, arena, &state, packa_dir, args.package_name, &lua, true);
// TODO: fetch and install deps

Expand Down Expand Up @@ -99,13 +101,8 @@ pub fn build(io: Io, gpa: Allocator, arena: Allocator, env: *std.process.Environ
};
defer tar_root_dir.close(io);

switch (lua.getField(pkg.lua_idx, "build")) {
.function => {},
else => |kind| {
log.err("Expected build to be a function, got {t}", .{kind});
return error.WrongLuaType;
},
}
// Push package build func to stack for calling
assert(lua.rawGetI(zlua.REGISTRYINDEX, pkg.build_func_ref) == .function);

// create b = Build{}
lua.createTable(0, 8);
Expand Down
2 changes: 1 addition & 1 deletion src/actions/info.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn info(io: Io, gpa: Allocator, package_name: []const u8) !void {
try lua_helpers.setupState(&lua);

var state: Package.State = .empty;
defer state.deinit(gpa);
defer state.deinit(gpa, &lua);

const pkg_id = try Package.collect(io, gpa, &state, packa_dir, package_name, &lua, false);

Expand Down
2 changes: 1 addition & 1 deletion src/actions/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn install(
try lua_helpers.setupState(&lua);

var state: Package.State = .empty;
defer state.deinit(gpa);
defer state.deinit(gpa, &lua);

var package_ids: std.ArrayList(Package.Id) = try .initCapacity(gpa, args.package_names.len);
defer package_ids.deinit(gpa);
Expand Down