Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

registerFns doesn't build when lang=lua54 #107

Open
nurpax opened this issue Oct 10, 2024 · 4 comments
Open

registerFns doesn't build when lang=lua54 #107

nurpax opened this issue Oct 10, 2024 · 4 comments

Comments

@nurpax
Copy link
Contributor

nurpax commented Oct 10, 2024

Looks like there's no luaL_findtable in Lua 5.4 so the registerFns function fails to build.

_ = c.luaL_findtable(@ptrCast(lua), registry_index, "_LOADED", 1);

@nurpax
Copy link
Contributor Author

nurpax commented Oct 10, 2024

Actually this was already failing to build when I chose lang=lua53 but I thought luaL_findtable was indeed removed in 5.4 and I was suspecting I saw some kind of an incremental build bug with Zig.

@nurpax
Copy link
Contributor Author

nurpax commented Oct 10, 2024

Yeah, I think it was still there in 5.3 but only when built for compat:

C:\Users\janne\dev\zig-ast\t>rg findtable
lua-5.3.6\src\lauxlib.c
855:static const char *luaL_findtable (lua_State *L, int idx,
899:  luaL_findtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE, 1);
904:    if (luaL_findtable(L, 0, modname, sizehint) != NULL)
/*
** {======================================================
** Compatibility with 5.1 module functions
** =======================================================
*/
#if defined(LUA_COMPAT_MODULE)

static const char *luaL_findtable (lua_State *L, int idx,
                                   const char *fname, int szhint) {

@natecraddock
Copy link
Owner

This seems closely related to #42

I think the issue here is that older versions of Lua use luaL_register while newer ones use luaL_newlib. Maybe what ziglua needs is a higher-level function that works the same for all versions of Lua?

Also makes me wonder how we should handle LUA_COMPAT_MODULE (if at all)

@nurpax
Copy link
Contributor Author

nurpax commented Oct 11, 2024

I'm not sure what to do about LUA_COMPAT_MODULE. I did notice though that the luaL_findtable is not present anymore even under LUA_COMPAT_MODULE in Lua 5.4.

In my code it was easy to work around that the function didn't compile, I just copied it into my own project:

fn registerFns2(lua: *Lua, funcs: []const ziglua.FnReg) void {
    for (funcs) |f| {
        lua.pushFunction(f.func.?);
        lua.setField(-2, f.name);
    }
}
...
fn register_L_Player(lua: *Lua) !void {
    try lua.newMetatable("L_Player");
    lua.pushValue(-1);
    lua.setField(-2, "__index");
    const methods = [_]ziglua.FnReg{
        .{ .name = "incScore", .func = ziglua.wrap(L_Player_incScore) },
        .{ .name = "setScore", .func = ziglua.wrap(L_Player_setScore) },
        .{ .name = "getScore", .func = ziglua.wrap(L_Player_getScore) },
    };
    registerFns2(lua, &methods);
    lua.register("Player", ziglua.wrap(L_Player_create));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants