From 67fc4a93a7d1cd7ea593dab82894779c2fa902b1 Mon Sep 17 00:00:00 2001 From: Nathan Craddock Date: Mon, 18 Mar 2024 21:52:13 -0600 Subject: [PATCH] fix(#64): segfault when loading standard libraries The individual lua.load*() function where incorrectly loading the libraries. The docs mention to use lua_call or requireF instead of calling the function directly. Closes #64 --- src/lib.zig | 22 +++++++++++----------- src/tests.zig | 3 +-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/lib.zig b/src/lib.zig index 073479e..885cff1 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -2957,57 +2957,57 @@ pub const Lua = struct { /// Open the basic standard library pub fn openBase(lua: *Lua) void { - _ = c.luaopen_base(lua.state); + lua.requireF("_G", c.luaopen_base, true); } /// Open the coroutine standard library pub fn openCoroutine(lua: *Lua) void { - _ = c.luaopen_coroutine(lua.state); + lua.requireF(c.LUA_COLIBNAME, c.luaopen_coroutine, true); } /// Open the package standard library pub fn openPackage(lua: *Lua) void { - _ = c.luaopen_package(lua.state); + lua.requireF(c.LUA_LOADLIBNAME, c.luaopen_package, true); } /// Open the string standard library pub fn openString(lua: *Lua) void { - _ = c.luaopen_string(lua.state); + lua.requireF(c.LUA_STRLIBNAME, c.luaopen_string, true); } /// Open the UTF-8 standard library pub fn openUtf8(lua: *Lua) void { - _ = c.luaopen_utf8(lua.state); + lua.requireF(c.LUA_UTF8LIBNAME, c.luaopen_utf8, true); } /// Open the table standard library pub fn openTable(lua: *Lua) void { - _ = c.luaopen_table(lua.state); + lua.requireF(c.LUA_TABLIBNAME, c.luaopen_table, true); } /// Open the math standard library pub fn openMath(lua: *Lua) void { - _ = c.luaopen_math(lua.state); + lua.requireF(c.LUA_MATHLIBNAME, c.luaopen_math, true); } /// Open the io standard library pub fn openIO(lua: *Lua) void { - _ = c.luaopen_io(lua.state); + lua.requireF(c.LUA_IOLIBNAME, c.luaopen_io, true); } /// Open the os standard library pub fn openOS(lua: *Lua) void { - _ = c.luaopen_os(lua.state); + lua.requireF(c.LUA_OSLIBNAME, c.luaopen_os, true); } /// Open the debug standard library pub fn openDebug(lua: *Lua) void { - _ = c.luaopen_debug(lua.state); + lua.requireF(c.LUA_DBLIBNAME, c.luaopen_debug, true); } /// Open the bit32 standard library pub fn openBit32(lua: *Lua) void { - _ = c.luaopen_bit32(lua.state); + lua.requireF(c.LUA_BITLIBNAME, c.luaopen_bit32, true); } /// Returns if given typeinfo is a string type diff --git a/src/tests.zig b/src/tests.zig index 958209f..c86f2af 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -168,8 +168,7 @@ test "standard library loading" { lua.openOS(); lua.openDebug(); - // TODO: why do these fail in lua51? Debugger shows it is on line with LUA_ENVIRONINDEX - if (ziglua.lang != .luau and ziglua.lang != .lua51 and ziglua.lang != .luajit) { + if (ziglua.lang != .luau) { lua.openPackage(); lua.openIO(); }