lua-users home
lua-l archive

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


Ok. I've got it mostly implemented (rewriting a lot of stuff in the process), and there's a theoretical question that I've missed.

I set a new global's table for the new thread. That is, boiled down, this:

m_SubLuaState = lua_newthread( m_MasterLuaState );
int thread = lua_gettop( m_MasterLuaState);

lua_newtable( m_MasterLuaState );
int newGlobal = lua_gettop( m_MasterLuaState );

lua_setfenv( m_MasterLuaState, thread );

At that point, i believe, the subState's thread has a new globals. Now, if I try to get something from it, by using:

lua_getfield( m_SubLuaState, LUA_GLOBALSINDEX, "Something" );

Does LUA_GLOBALSINDEX refer to the real global, or to the new global that i just created? Or am i getting this all wrong?

Cheers!



Marcus Brown wrote:

* David Morris-Oliveros <david@teambondi.com> on Mon, Oct 10, 2005:

Hi,

I'm on my next stage of integrating lua into our engine, and have come to a point where it would be wise to ask for advice.

I want each coroutine to have their own individual global table. I know that you can do this by creating a new table, filling it with whatever you want, and doing a lua_setfenv().

Now, when I bind my code into lua, i create all sorts of tables and cfunctions in the global table. I still want these new coroutines to be able access my bound tables and cfunctions.


I'm doing the same exact thing in our project - creating a seperate global
namespace for threads with access to the real global table.

Just create your thread's global table, and give it a metatable with the
__index metamethod pointing to the global table:

   namespace = setmetatable({}, {__index = _G})

In C this would look something like:

   lua_newtable(L);
   int metatable = lua_gettop(L);

   lua_pushvalue(L, LUA_GLOBALSINDEX);
   lua_setfield(L, metatable, "__index");    /* {__index = _G} */

Then set this as the metatable of your namespace.

I believe this was mentioned in PIL, section 14.3.



--
// David Morris-Oliveros
// Camera & Lua Coder
// Team Bondi


------------------------------------------------------------------------
Contact:
Team Bondi Pty Ltd
Level 2, 608 Harris Street
Ultimo, NSW 2007
Australia
Tel: +61 (0)2 8218 1500
Fax: +61 (0)2 8218 1507
Web: http://www.teambondi.com
------------------------------------------------------------------------
This email may contain confidential information.  If you are not
the intended recipient, you may not copy or deliver this message to
anyone. In such case, you should destroy this message and kindly
notify the sender by reply email. Opinions, conclusions and other
information in this message that do not relate to the official business
of our firm shall be understood as neither given nor endorsed by it.
------------------------------------------------------------------------