[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Lua state as a C++ class ...
- From: Benoit Germain <bgermain@...>
- Date: Fri, 14 Jun 2002 09:37:10 +0200
does it still work work when lua code changes the globals table ? I suppose
the registry is a table outside the lua state itself as seen by lua code ?
-- swap globals table
sometable = {}
local prevglobals = globals(sometable)
-- declare some stuff that goes into sometable
-- restore globals
globals ( prevglobals )
The last Titmouse public release stores a pointer as a global userdata
inside the lua state, but crashes on a null pointer when it tries to
retrieve it after the lua code changed the globals table.
-----Original Message-----
From: Christian Vogler [mailto:cvogler@gradient.cis.upenn.edu]
Sent: jeudi 13 juin 2002 19:29
To: Multiple recipients of list
Subject: Re: Lua state as a C++ class ...
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.