@@ -2424,7 +2424,7 @@ pub const Lua = opaque {
2424
2424
pub fn checkOption (lua : * Lua , comptime T : type , arg : i32 , default : ? T ) T {
2425
2425
const name = blk : {
2426
2426
if (default ) | defaultName | {
2427
- break :blk lua .optString (arg , @tagName (defaultName ) );
2427
+ break :blk lua .optString (arg ) orelse @tagName (defaultName );
2428
2428
} else {
2429
2429
break :blk lua .checkString (arg );
2430
2430
}
@@ -2734,43 +2734,44 @@ pub const Lua = opaque {
2734
2734
// luaL_opt (a macro) really isn't that useful, so not going to implement for now
2735
2735
2736
2736
/// 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
2738
2738
/// See https://www.lua.org/manual/5.2/manual.html#luaL_optint
2739
2739
/// 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 );
2742
2743
}
2743
2744
2744
2745
/// 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
2746
2747
/// 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 );
2749
2751
}
2750
2752
2751
2753
/// 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
2753
2755
/// 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 );
2756
2759
}
2757
2760
2758
2761
/// 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
2760
2763
/// 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 );
2767
2767
}
2768
2768
2769
2769
/// 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
2771
2771
/// 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 );
2774
2775
}
2775
2776
2776
2777
/// Pushes the fail value onto the stack
0 commit comments