From fd997e82194639f830a9f225bfefa20435d6cfa4 Mon Sep 17 00:00:00 2001 From: Squibid Date: Tue, 3 Mar 2026 08:39:18 -0500 Subject: [PATCH] clean up the ziglua code to make what's happening slightly clearer --- src/RemoteLua.zig | 2 +- src/lua/Lua.zig | 99 ++++++++++++++++++++++++----------------------- src/main.zig | 4 +- 3 files changed, 53 insertions(+), 52 deletions(-) diff --git a/src/RemoteLua.zig b/src/RemoteLua.zig index 772e5cb..039c4c8 100644 --- a/src/RemoteLua.zig +++ b/src/RemoteLua.zig @@ -37,7 +37,7 @@ pub fn create(client: *wl.Client, version: u32, id: u32) !void { }; errdefer node.L.deinit(); node.L.openLibs(); - Lua.openLibs(node.L); + Lua.openMezLibs(node.L); Lua.loadRuntimeDir(node.L) catch |err| if (err == error.LuaRuntime) { std.log.warn("{s}", .{try node.L.toString(-1)}); }; diff --git a/src/lua/Lua.zig b/src/lua/Lua.zig index 90d5bd5..5642c9d 100644 --- a/src/lua/Lua.zig +++ b/src/lua/Lua.zig @@ -28,8 +28,9 @@ pub fn loadRuntimeDir(self: *zlua.Lua) !void { { _ = try self.getGlobal("mez"); + defer self.pop(1); _ = self.getField(-1, "path"); - defer self.pop(2); + defer self.pop(1); _ = self.pushString(path_dir); 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 { { _ = try self.getGlobal("mez"); + defer self.pop(1); _ = 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" }); defer gpa.free(new_path); _ = self.pushString(new_path); @@ -58,8 +60,9 @@ pub fn setBaseConfig(self: *zlua.Lua, path: []const u8) !void { } { _ = try self.getGlobal("mez"); + defer self.pop(1); _ = self.getField(-1, "path"); - defer self.pop(2); + defer self.pop(1); const cur_path = self.toString(-1) catch ""; 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 { const lua_path = "mez.path.base_config"; 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; } const path = self.toString(-1) catch |err| { @@ -91,7 +94,7 @@ fn loadBaseConfig(self: *zlua.Lua) !void { fn loadConfigDir(self: *zlua.Lua) !void { const lua_path = "mez.path.config"; 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; } const path = self.toString(-1) catch |err| { @@ -102,54 +105,52 @@ fn loadConfigDir(self: *zlua.Lua) !void { 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(); - defer _ = self.setGlobal("mez"); - { - self.newTable(); - defer _ = self.setField(-2, "path"); - } - { - 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 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 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 remote_funcs = zlua.fnRegsFromType(Remote); - LuaUtils.newLib(self, remote_funcs); - self.setField(-2, "remote"); - } + defer _ = self.setField(-2, "path"); + } + { + 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 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 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 remote_funcs = zlua.fnRegsFromType(Remote); + LuaUtils.newLib(self, remote_funcs); + self.setField(-2, "remote"); } } pub const Config = struct { - str: ?[]const u8, + path: ?[]const u8, enabled: bool, }; pub fn init(self: *Lua, cfg: Config) !void { @@ -157,7 +158,7 @@ pub fn init(self: *Lua, cfg: Config) !void { errdefer self.state.deinit(); self.state.openLibs(); - openLibs(self.state); + openMezLibs(self.state); if (!cfg.enabled) try setBaseConfig(self.state, ""); 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)}); }; - if (cfg.str) |path| { + if (cfg.path) |path| { defer gpa.free(path); try setBaseConfig(self.state, path); } diff --git a/src/main.zig b/src/main.zig index ceb9d78..97c8753 100644 --- a/src/main.zig +++ b/src/main.zig @@ -51,13 +51,13 @@ pub fn main() !void { 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) { std.debug.panic("You cannot set both -u and --clean", .{}); } else if (res.args.u != null) { // this is freed in lua/lua.zig 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) { lua_config.enabled = false; }