From 3c288b5775370e069bf13dbced0e3230ce9e7c8e Mon Sep 17 00:00:00 2001 From: robin Date: Fri, 12 Jun 2026 17:44:30 +0200 Subject: [PATCH] lua: enable package lib --- src/lua.zig | 81 +++++++++++------------------------------------------ 1 file changed, 16 insertions(+), 65 deletions(-) diff --git a/src/lua.zig b/src/lua.zig index cd46311..4c294b9 100644 --- a/src/lua.zig +++ b/src/lua.zig @@ -14,9 +14,6 @@ const helpers = maivi.helpers; pub var lua: *Lua = undefined; -var arena: std.heap.ArenaAllocator = undefined; -var chunk_allocator: std.mem.Allocator = undefined; -var packages: std.StringHashMap([]const u8) = undefined; const embed = @import("embed.zig"); const lua_display = @import("lua_display.zig"); @@ -24,10 +21,6 @@ const lua_display = @import("lua_display.zig"); pub fn init() !void { fern.trace().ctx("lua.init").log(); - arena = .init(maivi.allocator); - errdefer arena.deinit(); - chunk_allocator = arena.allocator(); - packages = .init(chunk_allocator); lua = try lua_init(); } @@ -43,8 +36,22 @@ pub fn lua_init() !*Lua { l.openMath(); l.openBit32(); + // package ================================================================ + + l.openPackage(); + + _ = l.getGlobal("package"); + + const path = try std.fmt.allocPrint(maivi.allocator, "{s}/?.lua;{s}/?/init.lua", .{ maivi.config.luaroot, maivi.config.luaroot }); + defer maivi.allocator.free(path); + _ = l.pushString(path); + l.setField(-2, "path"); + + l.pop(1); // pops "package" + + // global fn ============================================================== + l.register("print", zlua.wrap(lua_print)); - l.register("require", zlua.wrap(lua_require)); l.register("read", zlua.wrap(lua_read)); // maivi ====================================================================== @@ -97,7 +104,6 @@ pub fn lua_init() !*Lua { pub fn deinit() void { lua.deinit(); - arena.deinit(); } /// * Pops from Stack: `0` @@ -110,7 +116,7 @@ fn do_require(l: *Lua, modname: []const u8) !void { pub fn setup() !void { // explicitly import `init.lua` - try do_require(lua, "init.lua"); + try do_require(lua, "init"); if (lua.isNil(-1) or !lua.isFunction(-1)) { fern.err().msg("lua/init.lua does not return an init function").log(); return error.Unexpected; @@ -210,58 +216,3 @@ fn lua_read(l: *Lua) !i32 { return 1; } -fn loadchunk(l: *Lua, modname: []const u8) !void { - const buffer = packages.get(modname) orelse return error.PackageNotFound; - - const modnameZ = try maivi.allocator.dupeZ(u8, modname); - defer maivi.allocator.free(modnameZ); - - fern.debug().ctx("lua.loadchunk") - .msg("loading buffer") - .str("modname", modname).log(); - try l.loadBuffer(buffer, modnameZ); - try pcall(l, .{ .args = 0, .results = 1 }); -} - -fn loadfile(modname: []const u8) !void { - const dir = try std.Io.Dir.openDirAbsolute(maivi.io, maivi.config.luaroot, .{}); - const contents = helpers.read_file(chunk_allocator, dir, modname) catch |err| switch (err) { - error.IsDirectory => blk: { - const new_path = try std.mem.concat(maivi.allocator, u8, &.{ modname, "/init.lua" }); - defer maivi.allocator.free(new_path); - break :blk try helpers.read_file(chunk_allocator, dir, new_path); - }, - error.FileNotFound => blk: { - const new_path = try std.mem.concat(maivi.allocator, u8, &.{ modname, ".lua" }); - defer maivi.allocator.free(new_path); - break :blk try helpers.read_file(chunk_allocator, dir, new_path); - }, - else => return err, - }; - errdefer chunk_allocator.free(contents); - - fern.debug().ctx("lua.loadfile") - .msg("saving contents") - .str("modname", modname).log(); - const modnameZ = try maivi.allocator.dupeZ(u8, modname); - defer maivi.allocator.free(modnameZ); - - if (packages.contains(modname)) return error.Clobber; - try packages.put(modname, contents); -} - -pub fn lua_require(l: *Lua) !i32 { - const modname = try l.toString(-1); - l.pop(1); - fern.debug().ctx("lua.require").str("modname", modname).log(); - - loadchunk(l, modname) catch |err| switch (err) { - error.PackageNotFound => { - try loadfile(modname); - try loadchunk(l, modname); - }, - else => return err, - }; - - return 1; -} -- 2.51.2