use Zig errors for load return values

The function can error, so use Zig errors to indicate that
This commit is contained in:
Nathan Craddock 2022-06-06 20:28:16 -06:00
parent 705e764fe8
commit 1c1aa58161
No known key found for this signature in database
GPG Key ID: ABE41A31B52E9DA7

View File

@ -511,8 +511,7 @@ pub const Lua = struct {
}
/// Loads a Lua chunk without running it
/// TODO: revisit this wrt return codes & docs
pub fn load(lua: *Lua, reader: Reader, data: *anyopaque, chunk_name: [:0]const u8, mode: ?Mode) i32 {
pub fn load(lua: *Lua, reader: Reader, data: *anyopaque, chunk_name: [:0]const u8, mode: ?Mode) !void {
const mode_str = blk: {
if (mode == null) break :blk "bt";
@ -522,7 +521,14 @@ pub const Lua = struct {
.binary_text => "bt",
};
};
return c.lua_load(lua.state, reader, data, chunk_name, mode_str);
const ret = c.lua_load(lua.state, reader, data, chunk_name, mode_str);
switch (ret) {
Status.ok => return,
Status.err_syntax => return error.Syntax,
Status.err_memory => return error.Memory,
// NOTE: the docs mention possible other return types, but I couldn't figure them out
else => panic("load returned an unexpected status: `{d}`", .{ret}),
}
}
/// Creates a new independent state and returns its main thread
@ -1517,7 +1523,6 @@ pub const Buffer = struct {
}
};
// Helper functions to make the ziglua API easier to use
pub const ZigFunction = fn (lua: *Lua) i32;