lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hi list,

I'm doing C-code bindings to Lua for years but on Windows only.
Some time ago I started porting some of my existing bindings to Linux.
Below is part of such a binding (it is for Lua 5.1).

While it worked in a simple test program it failed when it was require()'d from a larger program. Namely, the metatable ReaderType didn't have any of keys listed in  methods[] array
(it had some garbage-like looking keys however).
I had a very hard time debugging this issue and it was eventually fixed by prepending
the word static to definitions of funcs and methods.

Now the question: whether the lack of the word 'static' is a bug here?
Does some GCC switch exist to prevent such behavior?
Thanks for the explanations.

const char ReaderType[] = "LFSearch.ChunkReader";

// [ skipped functions implementations ]

const luaL_Reg funcs[] = {
  { "new", NewReader },
  { NULL, NULL }

const luaL_Reg methods[] = {
  { "__gc",      Reader_delete },
  { "closefile", Reader_closefile },
  { "delete",    Reader_delete },
  { "ftell",     Reader_ftell },
  { "openfile",  Reader_openfile },
  { "get_next_overlapped_chunk", Reader_getnextchunk },
  { "getsize",   Reader_getsize },
  { NULL, NULL }

LUALIB_API int luaopen_reader (lua_State *L)
  luaL_newmetatable(L, ReaderType);
  luaL_register(L, NULL, methods);
  lua_pushvalue(L, -1);
  lua_setfield(L, -2, "__index");

  luaL_register(L, NULL, funcs);
  return 1;