add a getter for a given views size and add some safety when setting the size

This commit is contained in:
Squibid 2025-12-13 07:58:58 -05:00
parent 12aa7b6448
commit 54c1a976c8
Signed by: squibid
GPG key ID: BECE5684D3C4005D
2 changed files with 50 additions and 2 deletions

26
src/lua/LuaUtils.zig Normal file
View file

@ -0,0 +1,26 @@
const LuaUtils = @This();
const std = @import("std");
const zlua = @import("zlua");
pub fn coerceNumber(comptime x: type, number: zlua.Number) error{InvalidNumber}!x {
if (number < std.math.minInt(x) or number > std.math.maxInt(x) or std.math.isNan(number)) {
return error.InvalidNumber;
}
switch (@typeInfo(x)) {
.int => return @as(x, @intFromFloat(number)),
.float => return @floatCast(number),
else => @compileError("unsupported type"),
}
}
pub fn coerceInteger(comptime x: type, number: zlua.Integer) error{InvalidInteger}!x {
if (number < std.math.minInt(x) or number > std.math.maxInt(x) or std.math.isNan(number)) {
return error.InvalidInteger;
}
switch (@typeInfo(x)) {
.int => return @intCast(number),
.float => return @as(x, @floatFromInt(number)),
else => @compileError("unsupported type"),
}
}