lua-users home
lua-l archive

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


On Thu, Jun 13, 2002 at 12:57:16PM -0400, Siome Klein Goldenstein wrote:
> Abbreviating parts of the code:
> 
> void
> MarkerHandler::read_file(std::string fname)
> {
>   lua_State *lua_s = lua_open(0);
> 
>   lua_pushusertag(lua_s, static_cast<void*>(this), LUA_ANYTAG);
>   lua_setglobal (lua_s, "MARKERHANDLERPTR");
> 
>   lua_register(lua_s,"markers", MarkerHandler::general_frame_marker);
> 
>   lua_dofile(lua_s, fname.c_str());
> 
>   lua_close(lua_s);
> }


You can avoid polluting the global lua namespace by using the
registry API, as follows:

lua_getregistry(lua_s);
lua_pushstring(lua_s, "MARKERHANDLERPTR");
lua_pushusertag(lua_s, static_cast<void*>(this), LUA_ANYTAG);
lua_settable(lua_s, -3);
lua_pop(lua_s, 1);


and accessing it:

lua_getregistry(lua_s);
lua_pushstring(lua_s, "MARKERHANDLERPTR");
lua_gettable(lua_s, -2);
MarkerHandler *mh = static_cast<MarkerHandler*>(lua_touserdata  (lua_s, -1));
lua_pop(lua_s, 2);

- Christian

P.S. I know that it is a dead horse, but the then-new API in Lua 4.0
really *is* hard to use.