pkulchenko / wxlua

wxlua: Lua bindings for wxWidgets cross-platform GUI toolkit; supports Lua 5.1, 5.2, 5.3, 5.4, LuaJIT and wxWidgets 3.x
304 stars 59 forks source link

Cannot build wxLua for Lua 5.2 #54

Closed LesNewell-SheetCam closed 4 years ago

LesNewell-SheetCam commented 4 years ago

If you try to build for Lua5.2 the build fails in bit.c, line 180. It expects a return value from lua_rawget but lua_rawget does not return a value in 5.2. This appears to only be available in 5.3+ I have attached a diff to fix the issue (bit.c.txt):

diff --git a/wxLua/modules/wxlua/bit.c b/wxLua/modules/wxlua/bit.c
index 1c37f86..bc9abe1 100644
--- a/wxLua/modules/wxlua/bit.c
+++ b/wxLua/modules/wxlua/bit.c
@@ -168,7 +168,7 @@ static const struct luaL_Reg bit_funcs[] = {
 */
 #define BAD_SAR        (bsar(-8, 2) != (SBits)-2)

-#if LUA_VERSION_NUM >= 502
+#if LUA_VERSION_NUM > 502
 static int libsize (const luaL_Reg *l) {
   int size = 0;
   for (; l && l->name; l++) size++;
@@ -212,7 +212,7 @@ LUALIB_API int luaopen_bit(lua_State *L)
       msg = "arithmetic right-shift broken";
     luaL_error(L, "bit library self-test failed (%s)", msg);
   }
-#if LUA_VERSION_NUM < 502
+#if LUA_VERSION_NUM <= 502
   luaL_register(L, "bit", bit_funcs);
 #else
   pushmodule(L, "bit", libsize(bit_funcs));  /* get/create library table */
pkulchenko commented 4 years ago

@sheetcam, what's the actual error you are getting? There is no luaL_register in lua 5.2 unless you turn the compatibility settings on, so the change may not work in some of the cases.

I think the following patch should work with both Lua 5.2 and Lua 5.3:

diff --git a/wxLua/modules/wxlua/bit.c b/wxLua/modules/wxlua/bit.c
index 1c37f86..377d544 100644
--- a/wxLua/modules/wxlua/bit.c
+++ b/wxLua/modules/wxlua/bit.c
@@ -177,7 +177,8 @@ static int libsize (const luaL_Reg *l) {
 static void pushmodule (lua_State *L, const char *modname, int sizehint) {
   lua_pushglobaltable(L);
   lua_pushstring(L, modname);
-  if (lua_rawget(L, -2) != LUA_TNIL) {  /* no such field? */
+  lua_rawget(L, -2);
+  if (!lua_isnil(L, -1)) {
     luaL_error(L, "name conflict for module '%s'", modname);
   }
   lua_pop(L, 1);  /* remove this nil */

Can you apply it and let me know?

LesNewell-SheetCam commented 4 years ago

I just built wxWidgets 3.1.3 with 2.8 compatibility enabled then used the cmake defaults apart from setting the Lua version to 5.2 and the build type to MinSizeRel. Your change appears to build. I'll run some more tests later.

pkulchenko commented 4 years ago

@sheetcam, sounds good. I pushed the changes, but let me know if you run into any issues.