clean up lua stuff a bit and add mez.inspect

This commit is contained in:
Squibid 2025-12-10 21:03:03 -05:00
parent 1f2e333846
commit ec8b86cbb7
Signed by: squibid
GPG key ID: BECE5684D3C4005D
6 changed files with 698 additions and 309 deletions

View file

@ -37,6 +37,9 @@ pub fn create(client: *wl.Client, version: u32, id: u32) !void {
errdefer node.L.deinit();
node.L.openLibs();
Lua.openLibs(node.L);
Lua.loadRuntimeDir(node.L) catch |err| if (err == error.LuaRuntime) {
std.log.warn("{s}", .{try node.L.toString(-1)});
};
// TODO: replace stdout and stderr with buffers we can send to the clients
server.remote_lua_clients.prepend(&node.node);

View file

@ -1,25 +1,25 @@
const Bridge = @This();
const std = @import("std");
const Lua = @import("Lua.zig");
const zlua = @import("zlua");
const gpa = std.heap.c_allocator;
pub fn getNestedField(L: *Lua, path: []u8) bool {
pub fn getNestedField(L: *zlua.Lua, path: []u8) bool {
var tokens = std.mem.tokenizeScalar(u8, path, '.');
var first = true;
while (tokens.next()) |token| {
const tok = gpa.dupeZ(u8, token) catch return false;
if (first) {
_ = L.state.getGlobal(tok) catch return false;
_ = L.getGlobal(tok) catch return false;
first = false;
} else {
_ = L.state.getField(-1, tok);
L.state.remove(-2);
_ = L.getField(-1, tok);
L.remove(-2);
}
if (L.state.isNil(-1)) {
if (L.isNil(-1)) {
return false;
}
}

View file

@ -16,33 +16,60 @@ const gpa = std.heap.c_allocator;
state: *zlua.Lua,
fn loadRuntimeDir(self: *Lua) !void {
const tmppath = try std.fs.path.join(gpa, &[_][]const u8{
pub fn loadRuntimeDir(self: *zlua.Lua) !void {
const path_dir = try std.fs.path.joinZ(gpa, &[_][]const u8{
config.runtime_path_prefix,
"share",
"mezzaluna",
});
defer gpa.free(path_dir);
{
_ = try self.getGlobal("mez");
_ = self.getField(-1, "path");
defer self.pop(2);
_ = self.pushString(path_dir);
self.setField(-2, "runtime");
}
const path_full = try std.fs.path.joinZ(gpa, &[_][]const u8{
path_dir,
"init.lua",
});
const path = try gpa.dupeZ(u8, tmppath);
defer gpa.free(path_full);
self.state.doFile(path) catch {
const err = try self.state.toString(-1);
self.doFile(path_full) catch {
const err = try self.toString(-1);
std.log.debug("Failed to run lua file: {s}", .{err});
};
}
fn loadConfigDir(self: *Lua) !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?", .{});
return;
}
const path = self.toString(-1) catch |err| {
std.log.err("Failed to pop the base config path from the lua stack. {}", .{err});
return;
};
self.pop(-1);
try self.doFile(path);
}
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?", .{});
return;
}
const path = self.state.toString(-1) catch |err| {
const path = self.toString(-1) catch |err| {
std.log.err("Failed to pop the config path from the lua stack. {}", .{err});
return;
};
self.state.pop(-1);
try self.state.doFile(path);
self.pop(-1);
try self.doFile(path);
}
pub fn openLibs(self: *zlua.Lua) void {
@ -93,16 +120,16 @@ pub fn init(self: *Lua) !void {
openLibs(self.state);
loadRuntimeDir(self) catch |err| {
if (err == error.LuaRuntime) {
std.log.warn("{s}", .{try self.state.toString(-1)});
}
loadRuntimeDir(self.state) catch |err| if (err == error.LuaRuntime) {
std.log.warn("{s}", .{try self.state.toString(-1)});
};
loadConfigDir(self) catch |err| {
if (err == error.LuaRuntime) {
std.log.warn("{s}", .{try self.state.toString(-1)});
}
loadBaseConfig(self.state) catch |err| if (err == error.LuaRuntime) {
std.log.warn("{s}", .{try self.state.toString(-1)});
};
loadConfigDir(self.state) catch |err| if (err == error.LuaRuntime) {
std.log.warn("{s}", .{try self.state.toString(-1)});
};
std.log.debug("Loaded lua", .{});