lua-users home
lua-l archive

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

On Tue, Sep 29, 2015 at 09:57:09PM -0300, Soni L. wrote:
> On 29/09/15 09:48 PM, Luiz Henrique de Figueiredo wrote:
> >>The registry, a table, is SLOW. Can we use upvalues instead?
> >Define SLOW.
> >
> >What problem are you trying to solve?
> >
> It also has name conflicts.

I've never encountered naming conflicts when using the registry. IME most of
the time people either use luaL_ref or use a lightuserdata pointer derived
from a static- or file-scoped object. Both of those are guaranteed
unique.[1] Example:

	static int myregkey;

	void setmystate(lua_State *L) {
		luaL_checkany(L, -1);
		lua_pushlightuserdata(L, &myregkey);
		lua_pushvalue(L, -2);
		lua_settable(L, LUA_REGISTRYINDEX);
		lua_pop(L, 1);

	void pushmystate(lua_State *L) {
		lua_pushlightuserdata(L, &myregkey);
		lua_gettable(L, LUA_REGISTRYINDEX);

[1] Well, excluding the case of your module being unloaded and another
module loaded in the same memory region, in which case unrelated code could
derive the same pointer value. But you could always arrange for a destructor
to delete the registry reference before your module is unloaded.