@@ -2424,7 +2424,7 @@ pub const Lua = opaque {
24242424 pub fn checkOption (lua : * Lua , comptime T : type , arg : i32 , default : ? T ) T {
24252425 const name = blk : {
24262426 if (default ) | defaultName | {
2427- break :blk lua .optString (arg , @tagName (defaultName ) );
2427+ break :blk lua .optString (arg ) orelse @tagName (defaultName );
24282428 } else {
24292429 break :blk lua .checkString (arg );
24302430 }
@@ -2734,43 +2734,44 @@ pub const Lua = opaque {
27342734 // luaL_opt (a macro) really isn't that useful, so not going to implement for now
27352735
27362736 /// If the function argument `arg` is a number, returns this number cast to an i32.
2737- /// If the argument is absent or nil returns `default`
2737+ /// If the argument is absent or nil returns null
27382738 /// See https://www.lua.org/manual/5.2/manual.html#luaL_optint
27392739 /// TODO: just like checkInt, is this ever useful?
2740- pub fn optInt (lua : * Lua , arg : i32 , default : i32 ) i32 {
2741- return c .luaL_optint (@ptrCast (lua ), arg , default );
2740+ pub fn optInt (lua : * Lua , arg : i32 ) ? i32 {
2741+ if (lua .isNoneOrNil (arg )) return null ;
2742+ return lua .checkInt (arg );
27422743 }
27432744
27442745 /// If the function argument `arg` is an integer, returns the integer
2745- /// If the argument is absent or nil returns `default`
2746+ /// If the argument is absent or nil returns null
27462747 /// See https://www.lua.org/manual/5.4/manual.html#luaL_optinteger
2747- pub fn optInteger (lua : * Lua , arg : i32 , default : Integer ) Integer {
2748- return c .luaL_optinteger (@ptrCast (lua ), arg , default );
2748+ pub fn optInteger (lua : * Lua , arg : i32 ) ? Integer {
2749+ if (lua .isNoneOrNil (arg )) return null ;
2750+ return lua .checkInteger (arg );
27492751 }
27502752
27512753 /// If the function argument `arg` is a number, returns the number
2752- /// If the argument is absent or nil returns `default`
2754+ /// If the argument is absent or nil returns null
27532755 /// See https://www.lua.org/manual/5.4/manual.html#luaL_optnumber
2754- pub fn optNumber (lua : * Lua , arg : i32 , default : Number ) Number {
2755- return c .luaL_optnumber (@ptrCast (lua ), arg , default );
2756+ pub fn optNumber (lua : * Lua , arg : i32 ) ? Number {
2757+ if (lua .isNoneOrNil (arg )) return null ;
2758+ return lua .checkNumber (arg );
27562759 }
27572760
27582761 /// If the function argument `arg` is a string, returns the string
2759- /// If the argment is absent or nil returns `default`
2762+ /// If the argment is absent or nil returns null
27602763 /// See https://www.lua.org/manual/5.4/manual.html#luaL_optstring
2761- pub fn optString (lua : * Lua , arg : i32 , default : [:0 ]const u8 ) [:0 ]const u8 {
2762- var length : usize = 0 ;
2763- // will never return null because default cannot be null
2764- const ret : [* ]const u8 = c .luaL_optlstring (@ptrCast (lua ), arg , default .ptr , & length );
2765- if (ret == default .ptr ) return default ;
2766- return ret [0.. length :0 ];
2764+ pub fn optString (lua : * Lua , arg : i32 ) ? [:0 ]const u8 {
2765+ if (lua .isNoneOrNil (arg )) return null ;
2766+ return lua .checkString (arg );
27672767 }
27682768
27692769 /// If the function argument is a number, returns this number as an unsigned
2770- /// If the argument is absent or nil returns default , otherwise raises an error
2770+ /// If the argument is absent or nil returns null , otherwise raises an error
27712771 /// See https://www.lua.org/manual/5.2/manual.html#luaL_optunsigned
2772- pub fn optUnsigned (lua : * Lua , arg : i32 , default : Unsigned ) Unsigned {
2773- return c .luaL_optunsigned (@ptrCast (lua ), arg , default );
2772+ pub fn optUnsigned (lua : * Lua , arg : i32 ) ? Unsigned {
2773+ if (lua .isNoneOrNil (arg )) return null ;
2774+ return lua .checkUnsigned (arg );
27742775 }
27752776
27762777 /// Pushes the fail value onto the stack
0 commit comments