Skip to content

Commit bf9fc6e

Browse files
authored
feat: toNumeric and checkNumeric (#173)
* feat: add pushNumeric, toNumeric and checkNumeric These functions rely (lightly) on Zig's comptime to minimize some of the annoying overhead of writing `@intCast` and `@floatCast` all the time by making those builtins part of the function definition. However, since those builtins assert in builds with runtime safety enabled, these functions will crash the program if called with bad Lua input. More discussion is warranted about the tradeoffs of going this route before merging. Resolves #172. * fix: remove pushNumeric * fix: checkNumeric raises a lua error * fix: toNumeric raises a Zig error * fix: remove pushNumeric from test, typo in toNumeric * fix: error.IntegerCastFailed -> error.Overflow * fix: add test for checkNumeric * fix: error message inconsistency outside of test control * fix: toStringEx not available in 5.1
1 parent ef5af10 commit bf9fc6e

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/lib.zig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,19 @@ pub const Lua = opaque {
25942594
c.lua_toclose(@ptrCast(lua), index);
25952595
}
25962596

2597+
/// Converts the Lua value at the given `index` to a numeric type;
2598+
/// if T is an integer type, the Lua value is converted to an integer.
2599+
///
2600+
/// * Pops: `0`
2601+
/// * Pushes: `0`
2602+
/// * Errors: `error.Overflow` if `T` is an integer type and the value at index doesn't fit
2603+
pub fn toNumeric(lua: *Lua, comptime T: type, index: i32) !T {
2604+
if (@typeInfo(T) == .int) {
2605+
return std.math.cast(T, try lua.toInteger(index)) orelse error.Overflow;
2606+
}
2607+
return @floatCast(try lua.toNumber(index));
2608+
}
2609+
25972610
/// Converts the Lua value at the given `index` to a signed integer
25982611
/// The Lua value must be an integer, or a number, or a string convertible to an integer
25992612
/// Returns an error if the conversion failed
@@ -3342,6 +3355,32 @@ pub const Lua = opaque {
33423355
c.luaL_checkany(@ptrCast(lua), arg);
33433356
}
33443357

3358+
/// Checks whether the function argument `arg` is a numeric type and converts it to type T
3359+
///
3360+
/// Raises a Lua error if the argument is an integer type but std.math.cast fails
3361+
///
3362+
/// * Pops: `0`
3363+
/// * Pushes: `0`
3364+
/// * Errors: `explained in text / on purpose`
3365+
pub fn checkNumeric(lua: *Lua, comptime T: type, arg: i32) T {
3366+
if (comptime @typeInfo(T) != .int) return @floatCast(lua.checkNumber(arg));
3367+
return std.math.cast(T, lua.checkInteger(arg)) orelse {
3368+
const error_msg = comptime msg: {
3369+
var buf: [1024]u8 = undefined;
3370+
const info = @typeInfo(T).int;
3371+
const signedness = switch (info.signedness) {
3372+
.unsigned => "u",
3373+
.signed => "i",
3374+
};
3375+
const output = std.fmt.bufPrintZ(&buf, "integer argument doesn't fit inside {s}{d} range [{d}, {d}]", .{
3376+
signedness, info.bits, std.math.minInt(T), std.math.maxInt(T),
3377+
}) catch unreachable;
3378+
break :msg output[0..output.len :0].*;
3379+
};
3380+
lua.argError(arg, &error_msg);
3381+
};
3382+
}
3383+
33453384
/// Checks whether the function argument `arg` is a number and returns this number cast to an i32
33463385
///
33473386
/// Not available in Lua 5.3 and 5.4

src/tests.zig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,3 +3055,41 @@ test "error union for CFn" {
30553055
try expectEqualStrings("MissingInteger", try lua.toString(-1));
30563056
};
30573057
}
3058+
3059+
test "checkNumeric and toNumeric" {
3060+
const error_msg = "integer argument doesn't fit inside u8 range [0, 255]";
3061+
3062+
const lua: *Lua = try .init(testing.allocator);
3063+
defer lua.deinit();
3064+
3065+
lua.pushFunction(zlua.wrap(struct {
3066+
fn f(l: *Lua) i32 {
3067+
_ = l.checkNumeric(u8, 1);
3068+
return 1;
3069+
}
3070+
}.f));
3071+
const idx = lua.getTop();
3072+
3073+
lua.pushValue(idx);
3074+
lua.pushInteger(128);
3075+
try lua.protectedCall(.{
3076+
.args = 1,
3077+
.results = 1,
3078+
});
3079+
const val = lua.toNumeric(u8, lua.getTop());
3080+
try std.testing.expectEqual(128, val);
3081+
3082+
lua.pushValue(idx);
3083+
lua.pushInteger(256);
3084+
if (lua.protectedCall(.{
3085+
.args = 1,
3086+
.results = 0,
3087+
})) |_| {
3088+
return error.ExpectedError;
3089+
} else |_| {
3090+
const string = try lua.toString(lua.getTop());
3091+
errdefer std.log.err("expected error message to contain: {s}", .{error_msg});
3092+
errdefer std.log.err("error message: {s}", .{string});
3093+
_ = std.mem.indexOf(u8, string, error_msg) orelse return error.BadErrorMessage;
3094+
}
3095+
}

0 commit comments

Comments
 (0)