3
votes

I am investigating a strange problem: on Windows, lua_rawgeti() does not return back the value to which I have created reference, but a nil. Code:

lua_State *L = luaL_newstate();
luaL_requiref(L, "_G", luaopen_base, 1);
lua_pop(L, 1);

lua_getglobal(L, toStringz("_G"));
int t1 = lua_type(L, -1);
auto r = luaL_ref(L, LUA_REGISTRYINDEX);

lua_rawgeti(L, LUA_REGISTRYINDEX, r);
int t2 = lua_type(L, -1);

lua_close(L);

writefln("Ref: %d, types: %d, %d", r, t1, t2);

assert(r != LUA_REFNIL);
assert((t1 != LUA_TNIL) && (t1 == t2));

Full source and build bat: https://github.com/mkoskim/games/tree/master/tests/luaref


Compile & run:

rdmd -I<path>/DerelictLua/source/ -I<path>/DerelictUtil/source/ testref.d

64-bit Linux (_G is table, and rawgeti places a table to stack):

$ build.bat
Ref: 3, types: 5, 5

32-bit Windows (_G is table, but rawgeti places nil to stack):

$ build.bat
Ref: 3, types: 5, 0
<assertion fail>

So, either luaL_ref() fails to store reference to _G correctly, or lua_rawgeti() fails to retrieve _G correctly.

Update: I compiled Lua library from sources, and added printf() to lua_rawgeti() (lapi.c:660) to print out the reference:

printf("lua_rawgeti(%d)\n", n);

I also added writeln() to test.d to tell me at which point we call lua_rawgeti(). It shows that D sends the reference number correctly:

lua_rawgeti(2)
lua_rawgeti(0)
Dereferencing:
lua_rawgeti(3)
Ref: 3, types: 5, 0

On Windows, I use:

  • DMD 2.086.0 (32-bit Windows)
  • lua53.dll (32-bit Windows, I have tried both lua-5.3.4 and lua-5.3.5), from here: http://luabinaries.sourceforge.net/download.html
  • DerelictLua newest version (commit 5549c1a)
  • DerelictUtil newest version (commit 8dda339)

Questions:

Is there any bug in the code I just don't catch? Is there any known "quirks" or such to use 32-bit D and Lua on Windows? There can't be any big problems with my compiler and libraries, because they compile and link together without any errors, and lua calls mostly work (e.g. opening lua state, pushing _G to stack and such).

I was not able to find anything related when googling, so I am pretty sure there is something wrong in my setup (something is mismatching). It is hard to me to suspect problems in Lua libraries, because they have been stable quite some long time (even 32-bit versions).

I would like to know, if people have used 64-bit Windows DMD + Lua successfully. Of course, I would appreciate to hear if people use 32-bit Windows DMD + Lua successfully.

I am bit out of ideas where to look for solution. Any ideas what to try next?

Thanks in advance!

1
I have a stupid suggestion: replace auto with intEgor Skriptunoff
Did that, but it didn't help :/MaKo
Run Lua test suite on your system.Egor Skriptunoff
Build Lua with /DLUA_USE_APICHECKEgor Skriptunoff
I ran the basic tests (lua -e"_U=true" all.lua) without any problems. Do I really need to start to compile Lua from sources now... Gosh, I have tried to avoid that...MaKo

1 Answers

3
votes

I got an answer from lua mailing list: http://lua-users.org/lists/lua-l/2019-05/msg00076.html

I suspect this is a bug in DerelictLua.

Lua defines lua_rawgeti thus:

int lua_rawgeti (lua_State *L, int index, lua_Integer n);

While DerelictLua defines its binding thus:

alias da_lua_rawgeti = int function(lua_State*, int, int);

I fixed that and created pull request to DerelictLua.