[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: individual globals for coroutines.
- From: "Marcus Brown" <mrbrown@...>
- Date: Sun, 9 Oct 2005 22:27:18 -0700
* 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.