Skip to content

Commit 9b40b9c

Browse files
committed
feat: add checkInt() and optInt() to 5.1 api
1 parent 53fc706 commit 9b40b9c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Diff for: src/ziglua-5.1/lib.zig

+13-1
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,12 @@ pub const Lua = struct {
11161116
c.luaL_checkany(lua.state, arg);
11171117
}
11181118

1119-
/// Checks whether the function argument `arg` is an integer (or can be converted to an integer) and returns the integer
1119+
/// Checks whether the function argument `arg` is a number and returns this number cast to an i32
1120+
/// See https://www.lua.org/manual/5.1/manual.html#luaL_checkint
1121+
pub fn checkInt(lua: *Lua, arg: i32) i32 {
1122+
return c.luaL_checkint(lua.state, arg);
1123+
}
1124+
11201125
pub fn checkInteger(lua: *Lua, arg: i32) Integer {
11211126
return c.luaL_checkinteger(lua.state, arg);
11221127
}
@@ -1280,6 +1285,13 @@ pub const Lua = struct {
12801285

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

1288+
/// If the function argument `arg` is a number, returns this number cast to an i32.
1289+
/// If the argument is absent or nil returns `default`
1290+
/// /// See https://www.lua.org/manual/5.1/manual.html#luaL_optint
1291+
pub fn optInt(lua: *Lua, arg: i32, default: i32) i32 {
1292+
return c.luaL_optint(lua.state, arg, default);
1293+
}
1294+
12831295
/// If the function argument `arg` is an integer, returns the integer
12841296
/// If the argument is absent or nil returns `default`
12851297
pub fn optInteger(lua: *Lua, arg: i32, default: Integer) Integer {

Diff for: src/ziglua-5.1/tests.zig

+2
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ test "aux check functions" {
805805
const function = ziglua.wrap(struct {
806806
fn inner(l: *Lua) i32 {
807807
l.checkAny(1);
808+
_ = l.checkInt(2);
808809
_ = l.checkInteger(2);
809810
_ = l.checkBytes(3);
810811
_ = l.checkNumber(4);
@@ -921,6 +922,7 @@ test "aux opt functions" {
921922

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

0 commit comments

Comments
 (0)