From 8e4f56d1477b33a9c1eb66a68f8dd8534d446c88 Mon Sep 17 00:00:00 2001 From: Squibid Date: Wed, 22 Oct 2025 22:40:19 -0400 Subject: [PATCH] add a basic filesystem library and use it for path concat --- runtime/share/mezzaluna/init.lua | 6 ++--- src/lua/fs.zig | 43 ++++++++++++++++++++++++++++++++ src/lua/lua.zig | 6 +++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index 9976a90..cfef496 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -4,8 +4,8 @@ if not env_conf then if not env_conf then error("Couldn't determine potential config directory is $HOME set?") end - env_conf = env_conf.."/.config/" + env_conf = mez.fs.joinpath(env_conf, ".config") end -mez.path.config = env_conf.."/mez/init.lua" -package.path = package.path..";"..env_conf.."/mez/lua/?.lua" +mez.path.config = mez.fs.joinpath(env_conf, "mez", "init.lua") +package.path = package.path..";"..mez.fs.joinpath(env_conf, "mez", "lua", "?.lua") diff --git a/src/lua/fs.zig b/src/lua/fs.zig index e69de29..3e5ea2c 100644 --- a/src/lua/fs.zig +++ b/src/lua/fs.zig @@ -0,0 +1,43 @@ +const Fs = @This(); + +const std = @import("std"); +const zlua = @import("zlua"); + +const Lua = @import("lua.zig"); + +const gpa = std.heap.c_allocator; + +pub fn joinpath(L: *zlua.Lua) i32 { + const nargs: i32 = L.getTop(); + if (nargs < 2) { + L.raiseErrorStr("Expected at least two paths to join", .{}); + return 0; + } + + var paths = std.ArrayList([:0]const u8).initCapacity(gpa, @intCast(nargs)) catch { + return 0; + }; + defer paths.deinit(gpa); + + var i: u8 = 1; + while (i <= nargs) : (i += 1) { + if (!L.isString(i)) { + L.raiseErrorStr("Expected string at argument %d", .{i}); + return 0; + } + + const partial_path = L.toString(i) catch unreachable; + paths.append(gpa, partial_path) catch { + // TODO: tell lua? + return 0; + }; + } + + const final_path: []const u8 = std.fs.path.join(gpa, paths.items) catch { + // TODO: tell lua? + return 0; + }; + _ = L.pushString(final_path); + + return 1; +} diff --git a/src/lua/lua.zig b/src/lua/lua.zig index ae5c23b..7a3e6cd 100644 --- a/src/lua/lua.zig +++ b/src/lua/lua.zig @@ -5,6 +5,7 @@ const config = @import("config"); const zlua = @import("zlua"); const Bridge = @import("bridge.zig"); +const Fs = @import("fs.zig"); const gpa = std.heap.c_allocator; @@ -51,6 +52,11 @@ pub fn init(self: *Lua) !void { self.state.newTable(); defer _ = self.state.setField(-2, "path"); } + { + const funcs = zlua.fnRegsFromType(Fs); + self.state.newLib(funcs); + self.state.setField(-2, "fs"); + } } loadRuntimeDir(self) catch |err| {