lua-users home
lua-l archive

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


Hi,

another possibility is/was assigning values with the cclosure used
to register functions (haven't had a close enough look at the
newer versions and don't know if this still works with the newest
Lua version)

lua_pushusertag( L, pMyCPointer, LUA_ANYTAG );
lua_pushcclosure( L, MyCFunction, 1 );
lua_setglobal( L, "Do" );

So with each call to the registered MyFunction, the pMyPointer
will be pushed on the stack too.


With kind regards,

Michael Flad

--
Fantastic Realms Interactive GmbH - Germany
Phone: +49 (0)7121 / 947 999 0
Fax:   +49 (0)7121 / 947 999 9


> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br
> [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of Benoit Germain
> Sent: Friday, June 14, 2002 9:37
> To: Multiple recipients of list
> Subject: RE: Lua state as a C++ class ...
>
>
> 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.
>