Skip to content

Commit

Permalink
feat: add checkInt() and optInt() to 5.1 api
Browse files Browse the repository at this point in the history
  • Loading branch information
natecraddock committed Feb 17, 2023
1 parent 53fc706 commit 9b40b9c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/ziglua-5.1/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,12 @@ pub const Lua = struct {
c.luaL_checkany(lua.state, arg);
}

/// Checks whether the function argument `arg` is an integer (or can be converted to an integer) and returns the integer
/// Checks whether the function argument `arg` is a number and returns this number cast to an i32
/// See https://www.lua.org/manual/5.1/manual.html#luaL_checkint
pub fn checkInt(lua: *Lua, arg: i32) i32 {
return c.luaL_checkint(lua.state, arg);
}

pub fn checkInteger(lua: *Lua, arg: i32) Integer {
return c.luaL_checkinteger(lua.state, arg);
}
Expand Down Expand Up @@ -1280,6 +1285,13 @@ pub const Lua = struct {

// luaL_opt (a macro) really isn't that useful, so not going to implement for now

/// If the function argument `arg` is a number, returns this number cast to an i32.
/// If the argument is absent or nil returns `default`
/// /// See https://www.lua.org/manual/5.1/manual.html#luaL_optint
pub fn optInt(lua: *Lua, arg: i32, default: i32) i32 {
return c.luaL_optint(lua.state, arg, default);
}

/// If the function argument `arg` is an integer, returns the integer
/// If the argument is absent or nil returns `default`
pub fn optInteger(lua: *Lua, arg: i32, default: Integer) Integer {
Expand Down
2 changes: 2 additions & 0 deletions src/ziglua-5.1/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ test "aux check functions" {
const function = ziglua.wrap(struct {
fn inner(l: *Lua) i32 {
l.checkAny(1);
_ = l.checkInt(2);
_ = l.checkInteger(2);
_ = l.checkBytes(3);
_ = l.checkNumber(4);
Expand Down Expand Up @@ -921,6 +922,7 @@ test "aux opt functions" {

const function = ziglua.wrap(struct {
fn inner(l: *Lua) i32 {
expectEqual(@as(i32, 10), l.optInt(1, 10)) catch unreachable;
expectEqual(@as(Integer, 10), l.optInteger(1, 10)) catch unreachable;
expectEqualStrings("zig", l.optBytes(2, "zig")) catch unreachable;
expectEqual(@as(Number, 1.23), l.optNumber(3, 1.23)) catch unreachable;
Expand Down

0 comments on commit 9b40b9c

Please sign in to comment.