lua-users home
lua-l archive

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


On Tue, Jul 10, 2012 at 2:06 PM, John Dunn <John_Dunn@qscaudio.com> wrote:
>> Do you know there is a value for an invalid reference (actually two) LUA_NOREF and LUA_REFNIL?
> Do you mean instead of using -1? Nope. I'll switch to using that.

It would be cleaner (and might fix your problem incidentally) if you
used a static variable address (with light userdata) for keeping track
of the reference, e.g.:

static const int timer;

static int SetHandler( lua_State* L ) {
  luaL_checktype(L, 1, LUA_TFUNCTION);
  lua_pushlightuserdata(L, &timer);
  lua_pushvalue(L, 1)
  lua_rawset(L, LUA_REGISTRYINDEX);
  return 0;
}

static const struct luaL_reg TestLibMethods[] = {
  { "SetHandler", SetHandler },
  {NULL, NULL}
};

static void CallHandler(lua_State* L) {
  lua_pushlightuserdata(L, &timer);
  lua_rawget(L, LUA_REGISTRYINDEX);
  if (lua_isfunction(L, -1)) {
    lua_pcall(L, 0, 0, 0);
  }
}

-- 
- Patrick Donnelly