Skip to content

pushInteger, checkInteger, pushNumber, checkNumber typing annoyances #172

@nurpax

Description

@nurpax

As Lua floats are f64 and as checkInteger returns c_longlong, I notice that code using ziglua ends up being a little annoying with @intCasts and @floatCasts:

    fn sheetSprite(lua: *Lua) i32 {
        const globals = lua_common.getZigObjects(lua);
        const self = lua_common.checkUserdataWithMeta(lua, sprites.Renderer, 1);
        const sheet_id: usize = @intCast(lua.checkInteger(2));
        const x = lua.checkNumber(3);
        const y = lua.checkNumber(4);
        const frame_idx: usize = @intCast(lua.checkInteger(5) - 1);

        const sheet: *const sprites.SpriteSheet = &globals.sprite_sheets.items[sheet_id];
        const image_id = sheet.image_id;
        const frame = sheet.frames[frame_idx % sheet.frames.len];

        const src_x: f32 = frame.x;
        const src_y: f32 = frame.y;
        const src_xy = Vec2.T{ src_x, src_y };
        const src_wh = Vec2.T{ frame.w, frame.h };
        self.sprite(.{ @floatCast(x), @floatCast(y) }, .{ frame.w, frame.h }, sprites.RenderMode{ .image = .{ .image_id = @intCast(image_id) } }, src_xy, src_wh);
        return 0;
    }

I wonder if it'd be a good idea to add something like this into ziglua (written in browser, didn't check if it compiles):

    pub fn checkNum(comptime T: type, lua: *Lua, arg: i32) Number {
        const lv = c.luaL_checknumber(@ptrCast(lua), arg)
        const v: T = @floatCast(lv);
        return v;
    }

And similarly for checkInteger and also for pushInteger, pushNumber.

I suppose I could add my own functions that do the same, but then the won't be usable as "methods" like lua.checkNumber(...) but rather myCheckNumber(lua, ...).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions