Skip to content

Commit 6e17877

Browse files
committed
add standard library opening functions
Exposes the luaopen_* and luaL_openlibs. Also adds a Ziggified wrapper function that takes a packed struct as input to specify which libs to load. After looking at rlua (a rust lua library) and the lua reference manual, it seems that these library loading functions have the possibility of failure. Not sure how to address this yet, but have left a todo.
1 parent aa686e3 commit 6e17877

File tree

1 file changed

+115
-2
lines changed

1 file changed

+115
-2
lines changed

src/zlua.zig

+115-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ const Lua = struct {
1515
allocator: ?*Allocator = null,
1616
state: *c.lua_State,
1717

18-
// Ziggified wrapper functions
19-
2018
/// Allows Lua to allocate memory using a Zig allocator passed in via data.
2119
/// See https://www.lua.org/manual/5.4/manual.html#lua_Alloc for more details
2220
fn alloc(data: ?*anyopaque, ptr: ?*anyopaque, osize: usize, nsize: usize) callconv(.C) ?*anyopaque {
@@ -102,6 +100,89 @@ const Lua = struct {
102100
}
103101

104102
// Standard library loading functions
103+
// TODO: opening libs can run arbitrary Lua code and can throw any error
104+
105+
pub const Libs = packed struct {
106+
base: bool = false,
107+
coroutine: bool = false,
108+
package: bool = false,
109+
string: bool = false,
110+
utf8: bool = false,
111+
table: bool = false,
112+
math: bool = false,
113+
io: bool = false,
114+
os: bool = false,
115+
debug: bool = false,
116+
};
117+
118+
/// Opens the specified standard library functions
119+
pub fn open(lua: *Lua, libs: Libs) void {
120+
if (libs.base) lua.openBase();
121+
if (libs.coroutine) lua.openCoroutine();
122+
if (libs.package) lua.openPackage();
123+
if (libs.string) lua.openString();
124+
if (libs.utf8) lua.openUtf8();
125+
if (libs.table) lua.openTable();
126+
if (libs.math) lua.openMath();
127+
if (libs.io) lua.openIO();
128+
if (libs.os) lua.openOS();
129+
if (libs.debug) lua.openDebug();
130+
}
131+
132+
/// Open all standard libraries
133+
pub fn openLibs(lua: *Lua) void {
134+
c.luaL_openlibs(lua.state);
135+
}
136+
137+
/// Open the basic standard library
138+
pub fn openBase(lua: *Lua) void {
139+
_ = c.luaopen_base(lua.state);
140+
}
141+
142+
/// Open the coroutine standard library
143+
pub fn openCoroutine(lua: *Lua) void {
144+
_ = c.luaopen_coroutine(lua.state);
145+
}
146+
147+
/// Open the package standard library
148+
pub fn openPackage(lua: *Lua) void {
149+
_ = c.luaopen_package(lua.state);
150+
}
151+
152+
/// Open the string standard library
153+
pub fn openString(lua: *Lua) void {
154+
_ = c.luaopen_string(lua.state);
155+
}
156+
157+
/// Open the UTF-8 standard library
158+
pub fn openUtf8(lua: *Lua) void {
159+
_ = c.luaopen_utf8(lua.state);
160+
}
161+
162+
/// Open the table standard library
163+
pub fn openTable(lua: *Lua) void {
164+
_ = c.luaopen_table(lua.state);
165+
}
166+
167+
/// Open the math standard library
168+
pub fn openMath(lua: *Lua) void {
169+
_ = c.luaopen_math(lua.state);
170+
}
171+
172+
/// Open the io standard library
173+
pub fn openIO(lua: *Lua) void {
174+
_ = c.luaopen_io(lua.state);
175+
}
176+
177+
/// Open the os standard library
178+
pub fn openOS(lua: *Lua) void {
179+
_ = c.luaopen_os(lua.state);
180+
}
181+
182+
/// Open the debug standard library
183+
pub fn openDebug(lua: *Lua) void {
184+
_ = c.luaopen_debug(lua.state);
185+
}
105186
};
106187

107188
// Tests
@@ -136,3 +217,35 @@ test "initialization" {
136217
lua = try Lua.auxNewState();
137218
lua.close();
138219
}
220+
221+
test "standard library loading" {
222+
// open a subset of standard libraries with Zig wrapper
223+
{
224+
var lua = try Lua.init(testing.allocator);
225+
defer lua.deinit();
226+
lua.open(.{ .base = true, .utf8 = true, .string = true });
227+
}
228+
229+
// open all standard libraries
230+
{
231+
var lua = try Lua.init(testing.allocator);
232+
defer lua.deinit();
233+
lua.openLibs();
234+
}
235+
236+
// open all standard libraries with individual functions
237+
{
238+
var lua = try Lua.init(testing.allocator);
239+
defer lua.deinit();
240+
lua.openBase();
241+
lua.openCoroutine();
242+
lua.openPackage();
243+
lua.openString();
244+
lua.openUtf8();
245+
lua.openTable();
246+
lua.openMath();
247+
lua.openIO();
248+
lua.openOS();
249+
lua.openDebug();
250+
}
251+
}

0 commit comments

Comments
 (0)