mirror of
https://github.com/MezzalunaWM/Mezzaluna.git
synced 2026-03-07 19:49:53 -05:00
clean up the ziglua code to make what's happening slightly clearer
This commit is contained in:
parent
edd0956b25
commit
fd997e8219
3 changed files with 53 additions and 52 deletions
|
|
@ -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)});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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,8 +105,7 @@ 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");
|
||||
{
|
||||
|
|
@ -146,10 +148,9 @@ pub fn openLibs(self: *zlua.Lua) void {
|
|||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue