lua-users home
lua-l archive

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



I’ve been using lua_ref as it is recommended to keep a reference to a table.

But my problem is that I’d like to find the C++ holder of the ref from another call. That is find which ref matches the table. I’ve been using this code:


// find table

  lua_pushvalue(L, 1);

  lua_gettable(L, LUA_REGISTRYINDEX);

  int ref;

if lua_isnil(L, -1) {

// not found, create ref

    lua_pop(L, 1);

    lua_pushvalue(L, 1);

    ref = luaL_ref(L, LUA_REGISTRYINDEX);

// LUA_REGISTRYINDEX.table = ref

    lua_pushvalue(L, 1);

    lua_pushnumber(L, ref);

    lua_settable(L, LUA_REGISTRYINDEX);

} else {

// found : retrieve ref

    ref = (int) lua_tonumber(L, -1);

    lua_pop(L, 1);



But could it some better mechanisms ? And by the way does LUA_REGISTRYINDEX behaves as a weak table?