lua-users home
lua-l archive

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


Peter Pimley wrote:

Hello Lua enthusiasts :)

I've been making quite light use of lua (from C/C++) on and off for a
couple of years now and I love it to bits :)

There are two things that I always wanted, but have never quite figured
out how to do.  I'm sure they're probably quite easy.  Here's the first;
I'll put the other in a separate mail.

Quite often I'll create a lua_State, and at the same time I'll create
some C structures or C++ objects.  Something along the lines of:

lua_State * lua = luaL_newstate ();
MyThing * thing = new MyThing ();

Semantically, the C++ objects 'belong' to the lua_State.  I have more
than one lua_State in my program, and I'd like each one to carry its
associated C++ structures around with it.  However (and this is the
important bit) I don't want any scripts that the lua_State executes to
be able to access or modify the objects.  The C++ objects should be
invisible to any lua scripts.

EHm, if I understood right, you want to access MyThing objects only from C functions called by Lua. You can have it available as an upvalue of the function. You do that when registering the functions in Lua:

lua_pushlightuserdata(L, thing);
lua_pushcclosure(L, your_C_function, 1);
lua_setglobal(L, "yourFunctionName");


Regards,
Ignacio Burgueño