load a runtime file where the prefix is determined at comptime

This commit is contained in:
Squibid 2025-10-20 22:53:17 -04:00
parent ba4d403119
commit 86a01bbcf2
Signed by: squibid
GPG key ID: BECE5684D3C4005D
4 changed files with 59 additions and 8 deletions

43
src/lua.zig Normal file
View file

@ -0,0 +1,43 @@
const Lua = @This();
const std = @import("std");
const config = @import("config");
const zlua = @import("zlua");
const gpa = std.heap.c_allocator;
state: *zlua.Lua,
fn loadRuntimeDir(self: *Lua) !void {
const tmppath = try std.fs.path.join(gpa, &[_][]const u8{
config.runtime_path_prefix,
"share",
"mezzaluna",
"init.lua",
});
var path_buffer = try gpa.alloc(u8, tmppath.len + 1);
std.mem.copyForwards(u8, path_buffer[0..tmppath.len], tmppath);
path_buffer[tmppath.len] = 0;
const path: [:0]u8 = path_buffer[0..tmppath.len :0];
try self.state.doFile(path);
}
pub fn init(self: *Lua) !void {
self.state = try zlua.Lua.init(gpa);
errdefer self.state.deinit();
self.state.openLibs();
{
self.state.newTable();
defer _ = self.state.pushString("mez");
}
try loadRuntimeDir(self);
std.log.debug("Loaded lua", .{});
}
pub fn deinit(self: *Lua) void {
self.state.deinit();
}

View file

@ -1,25 +1,20 @@
const std = @import("std");
const wlr = @import("wlroots");
const zlua = @import("zlua");
const Lua = zlua.Lua;
const Server = @import("server.zig");
const Lua = @import("lua.zig");
const gpa = std.heap.c_allocator;
pub var server: Server = undefined;
pub var lua: Lua = undefined;
pub fn main() !void {
wlr.log.init(.err, null);
var lua = try Lua.init(gpa);
defer lua.deinit();
lua.openLibs();
lua.doString("print('Hello from Lua embedded in Zig!')") catch {};
std.log.info("Starting mezzaluna", .{});
try lua.init();
try server.init();
defer server.deinit();