clean up the ziglua code to make what's happening slightly clearer

This commit is contained in:
Squibid 2026-03-03 08:39:18 -05:00
parent edd0956b25
commit fd997e8219
3 changed files with 53 additions and 52 deletions

View file

@ -37,7 +37,7 @@ pub fn create(client: *wl.Client, version: u32, id: u32) !void {
}; };
errdefer node.L.deinit(); errdefer node.L.deinit();
node.L.openLibs(); node.L.openLibs();
Lua.openLibs(node.L); Lua.openMezLibs(node.L);
Lua.loadRuntimeDir(node.L) catch |err| if (err == error.LuaRuntime) { Lua.loadRuntimeDir(node.L) catch |err| if (err == error.LuaRuntime) {
std.log.warn("{s}", .{try node.L.toString(-1)}); std.log.warn("{s}", .{try node.L.toString(-1)});
}; };

View file

@ -28,8 +28,9 @@ pub fn loadRuntimeDir(self: *zlua.Lua) !void {
{ {
_ = try self.getGlobal("mez"); _ = try self.getGlobal("mez");
defer self.pop(1);
_ = self.getField(-1, "path"); _ = self.getField(-1, "path");
defer self.pop(2); defer self.pop(1);
_ = self.pushString(path_dir); _ = self.pushString(path_dir);
self.setField(-2, "runtime"); self.setField(-2, "runtime");
} }
@ -49,8 +50,9 @@ pub fn loadRuntimeDir(self: *zlua.Lua) !void {
pub fn setBaseConfig(self: *zlua.Lua, path: []const u8) !void { pub fn setBaseConfig(self: *zlua.Lua, path: []const u8) !void {
{ {
_ = try self.getGlobal("mez"); _ = try self.getGlobal("mez");
defer self.pop(1);
_ = self.getField(-1, "path"); _ = self.getField(-1, "path");
defer self.pop(2); defer self.pop(1);
const new_path = try std.fs.path.join(gpa, &[_][]const u8{ path, "init.lua" }); const new_path = try std.fs.path.join(gpa, &[_][]const u8{ path, "init.lua" });
defer gpa.free(new_path); defer gpa.free(new_path);
_ = self.pushString(new_path); _ = self.pushString(new_path);
@ -58,8 +60,9 @@ pub fn setBaseConfig(self: *zlua.Lua, path: []const u8) !void {
} }
{ {
_ = try self.getGlobal("mez"); _ = try self.getGlobal("mez");
defer self.pop(1);
_ = self.getField(-1, "path"); _ = self.getField(-1, "path");
defer self.pop(2); defer self.pop(1);
const cur_path = self.toString(-1) catch ""; const cur_path = self.toString(-1) catch "";
const unsentinel: []const u8 = std.mem.span(cur_path.ptr); const unsentinel: []const u8 = std.mem.span(cur_path.ptr);
@ -77,7 +80,7 @@ pub fn setBaseConfig(self: *zlua.Lua, path: []const u8) !void {
fn loadBaseConfig(self: *zlua.Lua) !void { fn loadBaseConfig(self: *zlua.Lua) !void {
const lua_path = "mez.path.base_config"; const lua_path = "mez.path.base_config";
if (!Bridge.getNestedField(self, @constCast(lua_path[0..]))) { if (!Bridge.getNestedField(self, @constCast(lua_path[0..]))) {
std.log.err("Base config path not found. is your runtime dir setup?", .{}); std.log.err("Base config path not found. Is your runtime dir setup?", .{});
return; return;
} }
const path = self.toString(-1) catch |err| { const path = self.toString(-1) catch |err| {
@ -91,7 +94,7 @@ fn loadBaseConfig(self: *zlua.Lua) !void {
fn loadConfigDir(self: *zlua.Lua) !void { fn loadConfigDir(self: *zlua.Lua) !void {
const lua_path = "mez.path.config"; const lua_path = "mez.path.config";
if (!Bridge.getNestedField(self, @constCast(lua_path[0..]))) { if (!Bridge.getNestedField(self, @constCast(lua_path[0..]))) {
std.log.err("Config path not found. is your runtime dir setup?", .{}); std.log.err("Config path not found. Is your runtime dir setup?", .{});
return; return;
} }
const path = self.toString(-1) catch |err| { const path = self.toString(-1) catch |err| {
@ -102,54 +105,52 @@ fn loadConfigDir(self: *zlua.Lua) !void {
try self.doFile(path); try self.doFile(path);
} }
pub fn openLibs(self: *zlua.Lua) void { pub fn openMezLibs(self: *zlua.Lua) void {
self.newTable();
defer _ = self.setGlobal("mez");
{ {
self.newTable(); self.newTable();
defer _ = self.setGlobal("mez"); defer _ = self.setField(-2, "path");
{ }
self.newTable(); {
defer _ = self.setField(-2, "path"); const fs_funcs = zlua.fnRegsFromType(Fs);
} LuaUtils.newLib(self, fs_funcs);
{ self.setField(-2, "fs");
const fs_funcs = zlua.fnRegsFromType(Fs); }
LuaUtils.newLib(self, fs_funcs); {
self.setField(-2, "fs"); const input_funcs = zlua.fnRegsFromType(Input);
} LuaUtils.newLib(self, input_funcs);
{ self.setField(-2, "input");
const input_funcs = zlua.fnRegsFromType(Input); }
LuaUtils.newLib(self, input_funcs); {
self.setField(-2, "input"); const hook_funcs = zlua.fnRegsFromType(Hook);
} LuaUtils.newLib(self, hook_funcs);
{ self.setField(-2, "hook");
const hook_funcs = zlua.fnRegsFromType(Hook); }
LuaUtils.newLib(self, hook_funcs); {
self.setField(-2, "hook"); const api_funcs = zlua.fnRegsFromType(Api);
} LuaUtils.newLib(self, api_funcs);
{ self.setField(-2, "api");
const api_funcs = zlua.fnRegsFromType(Api); }
LuaUtils.newLib(self, api_funcs); {
self.setField(-2, "api"); const view_funcs = zlua.fnRegsFromType(View);
} LuaUtils.newLib(self, view_funcs);
{ self.setField(-2, "view");
const view_funcs = zlua.fnRegsFromType(View); }
LuaUtils.newLib(self, view_funcs); {
self.setField(-2, "view"); const output_funcs = zlua.fnRegsFromType(Output);
} LuaUtils.newLib(self, output_funcs);
{ self.setField(-2, "output");
const output_funcs = zlua.fnRegsFromType(Output); }
LuaUtils.newLib(self, output_funcs); {
self.setField(-2, "output"); const remote_funcs = zlua.fnRegsFromType(Remote);
} LuaUtils.newLib(self, remote_funcs);
{ self.setField(-2, "remote");
const remote_funcs = zlua.fnRegsFromType(Remote);
LuaUtils.newLib(self, remote_funcs);
self.setField(-2, "remote");
}
} }
} }
pub const Config = struct { pub const Config = struct {
str: ?[]const u8, path: ?[]const u8,
enabled: bool, enabled: bool,
}; };
pub fn init(self: *Lua, cfg: Config) !void { pub fn init(self: *Lua, cfg: Config) !void {
@ -157,7 +158,7 @@ pub fn init(self: *Lua, cfg: Config) !void {
errdefer self.state.deinit(); errdefer self.state.deinit();
self.state.openLibs(); self.state.openLibs();
openLibs(self.state); openMezLibs(self.state);
if (!cfg.enabled) try setBaseConfig(self.state, ""); if (!cfg.enabled) try setBaseConfig(self.state, "");
loadRuntimeDir(self.state) catch |err| if (err == error.LuaRuntime) { loadRuntimeDir(self.state) catch |err| if (err == error.LuaRuntime) {
@ -168,7 +169,7 @@ pub fn init(self: *Lua, cfg: Config) !void {
std.log.warn("{s}", .{try self.state.toString(-1)}); std.log.warn("{s}", .{try self.state.toString(-1)});
}; };
if (cfg.str) |path| { if (cfg.path) |path| {
defer gpa.free(path); defer gpa.free(path);
try setBaseConfig(self.state, path); try setBaseConfig(self.state, path);
} }

View file

@ -51,13 +51,13 @@ pub fn main() !void {
std.process.exit(0); std.process.exit(0);
} }
var lua_config: Lua.Config = .{ .enabled = true, .str = null }; var lua_config: Lua.Config = .{ .enabled = true, .path = null };
if (res.args.u != null and res.args.clean == 1) { if (res.args.u != null and res.args.clean == 1) {
std.debug.panic("You cannot set both -u and --clean", .{}); std.debug.panic("You cannot set both -u and --clean", .{});
} else if (res.args.u != null) { } else if (res.args.u != null) {
// this is freed in lua/lua.zig // this is freed in lua/lua.zig
const path = try std.fs.cwd().realpathAlloc(gpa, res.args.u.?); const path = try std.fs.cwd().realpathAlloc(gpa, res.args.u.?);
lua_config.str = path; lua_config.path = path;
} else if (res.args.clean == 1) { } else if (res.args.clean == 1) {
lua_config.enabled = false; lua_config.enabled = false;
} }