From 97e01ee1e9d778bcf744ceca752c9dbb78232560 Mon Sep 17 00:00:00 2001 From: Guilherme Janczak Date: Fri, 20 Dec 2024 20:52:24 +0000 Subject: [PATCH] Get closer to the Lua 5.2 C API Aegisub requires Luajit implementing Lua 5.2, but the code was making a mixture of Lua 5.1 and Lua 5.2 C API calls which happened to work because luajit's standards compliance is loose. A call to `lua_objlen()` remains because Luajit doesn't implement Lua 5.2 fully and is missing `lua_rawlen()`. Tested with some out of tree patches to make Aegisub work on an unsupported platform (OpenBSD, official Lua). --- libaegisub/lua/modules/lpeg.c | 8 ++------ src/auto4_lua.cpp | 3 +-- src/auto4_lua_assfile.cpp | 4 ++-- subprojects/luabins/src/luabins.c | 5 ++++- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/libaegisub/lua/modules/lpeg.c b/libaegisub/lua/modules/lpeg.c index 5261c20617..07f42a3b37 100644 --- a/libaegisub/lua/modules/lpeg.c +++ b/libaegisub/lua/modules/lpeg.c @@ -39,10 +39,6 @@ #undef lua_objlen #define lua_objlen lua_rawlen -#undef luaL_register -#define luaL_register(L,n,f) \ - { if ((n) == NULL) luaL_setfuncs(L,f,0); else luaL_newlib(L,f); } - #endif @@ -2379,8 +2375,8 @@ int luaopen_lpeg (lua_State *L) { luaL_newmetatable(L, PATTERN_T); lua_pushnumber(L, MAXBACK); lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); - luaL_register(L, NULL, metapattreg); - luaL_register(L, "lpeg", pattreg); + luaL_setfuncs(L, metapattreg, 0); + luaL_newlib(L, pattreg); lua_pushliteral(L, "__index"); lua_pushvalue(L, -2); lua_settable(L, -4); diff --git a/src/auto4_lua.cpp b/src/auto4_lua.cpp index 21a7d2858e..693fcceeb5 100644 --- a/src/auto4_lua.cpp +++ b/src/auto4_lua.cpp @@ -466,7 +466,6 @@ namespace { stackcheck.check_stack(0); // make "aegisub" table - lua_pushstring(L, "aegisub"); lua_createtable(L, 0, 13); set_field(L, "register_macro"); @@ -487,7 +486,7 @@ namespace { set_field(L, "set_status_text"); // store aegisub table to globals - lua_settable(L, LUA_GLOBALSINDEX); + lua_setglobal(L, "aegisub"); stackcheck.check_stack(0); // load user script diff --git a/src/auto4_lua_assfile.cpp b/src/auto4_lua_assfile.cpp index f783952299..4ca7ac1ed6 100644 --- a/src/auto4_lua_assfile.cpp +++ b/src/auto4_lua_assfile.cpp @@ -72,7 +72,7 @@ namespace { std::string get_string_field(lua_State *L, const char *name, const char *line_class) { get_field(L, name, line_class, lua_isstring); - std::string ret(lua_tostring(L, -1), lua_strlen(L, -1)); + std::string ret = lua_tostring(L, -1); lua_pop(L, 1); return ret; } @@ -80,7 +80,7 @@ namespace { agi::Color get_color_field(lua_State *L, const char *name, const char *line_class) { get_field(L, name, line_class, lua_isstring); - agi::Color ret(std::string_view(lua_tostring(L, -1), lua_strlen(L, -1))); + agi::Color ret = std::string_view(lua_tostring(L, -1)); lua_pop(L, 1); return ret; } diff --git a/subprojects/luabins/src/luabins.c b/subprojects/luabins/src/luabins.c index 35b1776293..c818483fc5 100644 --- a/subprojects/luabins/src/luabins.c +++ b/subprojects/luabins/src/luabins.c @@ -82,7 +82,10 @@ LUALIB_API int luaopen_luabins(lua_State * L) /* * Register module */ - luaL_register(L, "luabins", R); + lua_newtable(L); + luaL_setfuncs(L, R, 0); + lua_pushvalue(L, -1); + lua_setglobal(L, "mylib"); /* * Register module information