Skip to content

Commit cc292f1

Browse files
committed
breaking: remove lua.open() function
This function's implementation and tests are messy and the function has no real benefit. Instead of using lua.open(.{}), use the individual functions like lua.openBase() or lua.openOS(). lua.openLibs() opens all libraries.
1 parent 6150f0f commit cc292f1

File tree

3 files changed

+4
-50
lines changed

3 files changed

+4
-50
lines changed

examples/zig-fn.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn main() anyerror!void {
5050
lua.setGlobal("add");
5151

5252
// We need to open the base library so the global print() is available
53-
lua.open(.{ .base = true });
53+
lua.openBase();
5454

5555
// Our "program" is an inline string
5656
lua.doString(

src/lib.zig

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,6 @@ pub const Libs53 = packed struct {
435435
debug: bool = false,
436436
};
437437

438-
/// Bitflag for the Lua standard libraries
439-
pub const Libs = switch (lang) {
440-
.lua51, .luajit => Libs51,
441-
.lua52 => Libs52,
442-
.lua53, .lua54, .luau => Libs53,
443-
};
444-
445438
/// The type of the opaque structure that points to a thread and the state of a Lua interpreter
446439
pub const LuaState = c.lua_State;
447440

@@ -2956,30 +2949,6 @@ pub const Lua = struct {
29562949

29572950
// Standard library loading functions
29582951

2959-
/// Opens the specified standard library functions
2960-
/// Behaves like openLibs, but allows specifying which libraries
2961-
/// to expose to the global table rather than all of them
2962-
/// See https://www.lua.org/manual/5.4/manual.html#luaL_openlibs
2963-
pub fn open(lua: *Lua, libs: Libs) void {
2964-
if (libs.base) lua.requireF("_G", c.luaopen_base, true);
2965-
if (libs.string) lua.requireF(c.LUA_STRLIBNAME, c.luaopen_string, true);
2966-
if (libs.table) lua.requireF(c.LUA_TABLIBNAME, c.luaopen_table, true);
2967-
if (libs.math) lua.requireF(c.LUA_MATHLIBNAME, c.luaopen_math, true);
2968-
if (libs.os) lua.requireF(c.LUA_OSLIBNAME, c.luaopen_os, true);
2969-
if (libs.debug) lua.requireF(c.LUA_DBLIBNAME, c.luaopen_debug, true);
2970-
2971-
if (lang != .luau) {
2972-
if (libs.io) lua.requireF(c.LUA_IOLIBNAME, c.luaopen_io, true);
2973-
if (libs.package) lua.requireF(c.LUA_LOADLIBNAME, c.luaopen_package, true);
2974-
}
2975-
2976-
if (lang != .lua51 and lang != .luajit and libs.coroutine) lua.requireF(c.LUA_COLIBNAME, c.luaopen_coroutine, true);
2977-
2978-
if ((lang == .lua53 or lang == .lua54) and libs.utf8) lua.requireF(c.LUA_UTF8LIBNAME, c.luaopen_utf8, true);
2979-
2980-
if (lang == .lua52 and libs.bit) lua.requireF(c.LUA_BITLIBNAME, c.luaopen_bit32, true);
2981-
}
2982-
29832952
/// Open all standard libraries
29842953
/// See https://www.lua.org/manual/5.4/manual.html#luaL_openlibs
29852954
pub fn openLibs(lua: *Lua) void {

src/tests.zig

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,6 @@ test "standard library loading" {
154154
lua.openLibs();
155155
}
156156

157-
// open a subset of standard libraries with Zig wrapper
158-
{
159-
var lua = try Lua.init(&testing.allocator);
160-
defer lua.deinit();
161-
162-
switch (ziglua.lang) {
163-
.lua51 => lua.open(.{ .base = true, .package = true, .string = true, .table = true, .math = true, .io = true, .os = true, .debug = true }),
164-
.lua52 => lua.open(.{ .base = true, .coroutine = true, .package = true, .string = true, .table = true, .math = true, .io = true, .os = true, .debug = true, .bit = true }),
165-
.lua53, .lua54 => lua.open(.{ .base = true, .coroutine = true, .package = true, .string = true, .utf8 = true, .table = true, .math = true, .io = true, .os = true, .debug = true }),
166-
.luau => lua.open(.{ .base = true, .coroutine = true, .string = true, .utf8 = true, .table = true, .math = true, .io = true, .os = true, .debug = true }),
167-
.luajit => {
168-
// TODO: why do tests crash?
169-
},
170-
}
171-
}
172-
173157
// open all standard libraries with individual functions
174158
// these functions are only useful if you want to load the standard
175159
// packages into a non-standard table
@@ -631,7 +615,8 @@ test "global table" {
631615
defer lua.deinit();
632616

633617
// open some libs so we can inspect them
634-
lua.open(.{ .math = true, .base = true });
618+
lua.openBase();
619+
lua.openMath();
635620
lua.pushGlobalTable();
636621

637622
// find the print function
@@ -1143,7 +1128,7 @@ test "closing vars" {
11431128
var lua = try Lua.init(&testing.allocator);
11441129
defer lua.deinit();
11451130

1146-
lua.open(.{ .base = true });
1131+
lua.openBase();
11471132

11481133
// do setup in Lua for ease
11491134
try lua.doString(

0 commit comments

Comments
 (0)