Skip to content

Commit 74e7cf7

Browse files
committed
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.
1 parent ef5af10 commit 74e7cf7

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/lib.zig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,18 @@ pub const Lua = opaque {
20352035
c.lua_pushnil(@ptrCast(lua));
20362036
}
20372037

2038+
/// Pushes a numeric type with value `n` onto the stack
2039+
/// The conversion from the type of `n` to `Integer` or `Number`
2040+
/// is performed with `@_Cast` so will assert in modes with runtime safety enabled.
2041+
///
2042+
/// * Pops: `0`
2043+
/// * Pushes: `1`
2044+
/// * Errors: `never`
2045+
pub fn pushNumeric(lua: *Lua, n: anytype) void {
2046+
if (@typeInfo(@TypeOf(n)) == .int) return lua.pushInteger(@intCast(n));
2047+
lua.pushNumber(@floatCast(n));
2048+
}
2049+
20382050
/// Pushes a float with value `n` onto the stack
20392051
///
20402052
/// * Pops: `0`
@@ -2594,6 +2606,19 @@ pub const Lua = opaque {
25942606
c.lua_toclose(@ptrCast(lua), index);
25952607
}
25962608

2609+
/// Converts the Lua value at the given `index` to a numeric type;
2610+
/// if T is an integer type, the Lua value is converted to an integer.
2611+
/// The conversion from `Integer` or `Number` to T is performed with `@_Cast`,
2612+
/// which will assert in builds with runtime safety enabled
2613+
///
2614+
/// * Pops: `0`
2615+
/// * Pushes: `0`
2616+
/// * Errors: `never`
2617+
pub fn toNumeric(lua: *Lua, comptime T: type, index: i32) !T {
2618+
if (@typeInfo(T) == .int) return @intCast(try lua.toInteger(index));
2619+
return @floatCast(try lua.toNumber(index));
2620+
}
2621+
25972622
/// Converts the Lua value at the given `index` to a signed integer
25982623
/// The Lua value must be an integer, or a number, or a string convertible to an integer
25992624
/// Returns an error if the conversion failed
@@ -3342,6 +3367,19 @@ pub const Lua = opaque {
33423367
c.luaL_checkany(@ptrCast(lua), arg);
33433368
}
33443369

3370+
/// Checks whether the function argument `arg` is a numeric type and converts it to type T
3371+
///
3372+
/// The conversion is done with `@intCast` for numeric types,
3373+
/// so causes an assertion in modes with runtime safety enabled.
3374+
///
3375+
/// * Pops: `0`
3376+
/// * Pushes: `0`
3377+
/// * Errors: `explained in text / on purpose`
3378+
pub fn checkNumeric(lua: *Lua, comptime T: type, arg: i32) T {
3379+
if (@typeInfo(T) == .int) return @intCast(lua.checkInteger(arg));
3380+
return @floatCast(lua.checkNumber(arg));
3381+
}
3382+
33453383
/// Checks whether the function argument `arg` is a number and returns this number cast to an i32
33463384
///
33473385
/// Not available in Lua 5.3 and 5.4

src/tests.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,3 +3055,13 @@ test "error union for CFn" {
30553055
try expectEqualStrings("MissingInteger", try lua.toString(-1));
30563056
};
30573057
}
3058+
3059+
test "pushNumeric and toNumeric" {
3060+
const lua: *Lua = try .init(testing.allocator);
3061+
defer lua.deinit();
3062+
3063+
const num: u32 = 100;
3064+
lua.pushNumeric(num);
3065+
const pull = lua.toNumeric(u32, lua.getTop());
3066+
try std.testing.expectEqual(num, pull);
3067+
}

0 commit comments

Comments
 (0)