Skip to content

Commit 67fc4a9

Browse files
committed
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
1 parent cc292f1 commit 67fc4a9

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/lib.zig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,57 +2957,57 @@ pub const Lua = struct {
29572957

29582958
/// Open the basic standard library
29592959
pub fn openBase(lua: *Lua) void {
2960-
_ = c.luaopen_base(lua.state);
2960+
lua.requireF("_G", c.luaopen_base, true);
29612961
}
29622962

29632963
/// Open the coroutine standard library
29642964
pub fn openCoroutine(lua: *Lua) void {
2965-
_ = c.luaopen_coroutine(lua.state);
2965+
lua.requireF(c.LUA_COLIBNAME, c.luaopen_coroutine, true);
29662966
}
29672967

29682968
/// Open the package standard library
29692969
pub fn openPackage(lua: *Lua) void {
2970-
_ = c.luaopen_package(lua.state);
2970+
lua.requireF(c.LUA_LOADLIBNAME, c.luaopen_package, true);
29712971
}
29722972

29732973
/// Open the string standard library
29742974
pub fn openString(lua: *Lua) void {
2975-
_ = c.luaopen_string(lua.state);
2975+
lua.requireF(c.LUA_STRLIBNAME, c.luaopen_string, true);
29762976
}
29772977

29782978
/// Open the UTF-8 standard library
29792979
pub fn openUtf8(lua: *Lua) void {
2980-
_ = c.luaopen_utf8(lua.state);
2980+
lua.requireF(c.LUA_UTF8LIBNAME, c.luaopen_utf8, true);
29812981
}
29822982

29832983
/// Open the table standard library
29842984
pub fn openTable(lua: *Lua) void {
2985-
_ = c.luaopen_table(lua.state);
2985+
lua.requireF(c.LUA_TABLIBNAME, c.luaopen_table, true);
29862986
}
29872987

29882988
/// Open the math standard library
29892989
pub fn openMath(lua: *Lua) void {
2990-
_ = c.luaopen_math(lua.state);
2990+
lua.requireF(c.LUA_MATHLIBNAME, c.luaopen_math, true);
29912991
}
29922992

29932993
/// Open the io standard library
29942994
pub fn openIO(lua: *Lua) void {
2995-
_ = c.luaopen_io(lua.state);
2995+
lua.requireF(c.LUA_IOLIBNAME, c.luaopen_io, true);
29962996
}
29972997

29982998
/// Open the os standard library
29992999
pub fn openOS(lua: *Lua) void {
3000-
_ = c.luaopen_os(lua.state);
3000+
lua.requireF(c.LUA_OSLIBNAME, c.luaopen_os, true);
30013001
}
30023002

30033003
/// Open the debug standard library
30043004
pub fn openDebug(lua: *Lua) void {
3005-
_ = c.luaopen_debug(lua.state);
3005+
lua.requireF(c.LUA_DBLIBNAME, c.luaopen_debug, true);
30063006
}
30073007

30083008
/// Open the bit32 standard library
30093009
pub fn openBit32(lua: *Lua) void {
3010-
_ = c.luaopen_bit32(lua.state);
3010+
lua.requireF(c.LUA_BITLIBNAME, c.luaopen_bit32, true);
30113011
}
30123012

30133013
/// Returns if given typeinfo is a string type

src/tests.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ test "standard library loading" {
168168
lua.openOS();
169169
lua.openDebug();
170170

171-
// TODO: why do these fail in lua51? Debugger shows it is on line with LUA_ENVIRONINDEX
172-
if (ziglua.lang != .luau and ziglua.lang != .lua51 and ziglua.lang != .luajit) {
171+
if (ziglua.lang != .luau) {
173172
lua.openPackage();
174173
lua.openIO();
175174
}

0 commit comments

Comments
 (0)