[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: the gc
- From: David Jones <drj@...>
- Date: Fri, 11 Aug 2006 13:35:04 +0100
On 11 Aug 2006, at 12:32, D Burgess wrote:
. It is a bit confusing maybe...
Ok I am confused. The LUA_ENVIRONINDEX according to the manual is
"The environment of the running C function is always at pseudo-index
LUA_ENVIRONINDEX." (You will note that this the one refernce to it in
the
manual).
Given that the code never sets LUA_ENVIRONINDEX I assume
that it is equal to LUA_GLOBALINDEX.
In this case it was a running *Lua* function in another thread..
A (lua) function only has one environment. If you remove it then
that affects all activations of that function which may or may not be
running in multiple threads.
Don't forget that execution a function definition will get you
another copy of what is essentially the same function (the same
function prototype). Two such copies can have different environments.
So perhaps your threads need to execute all your function defining
scripts each time one starts. You can do this without loading the
script again (just executing it) as long as you set the environment
of the script before you execute it (which may require an interlock).
The confusing thing about LUA_ENVIRONINDEX is that it really doesn't
affect the environment of much else. It just a thing that C
functions can access.
In the following program, 'x' is in the C function's environment but
that doesn't affect where globals come from.
#include <assert.h>
#include "lua.h"
#include "lauxlib.h"
int f(lua_State *);
int
main(void)
{
lua_State *l = luaL_newstate();
assert(l);
lua_pushcfunction(l, f);
lua_call(l, 0, 0);
return 0;
}
int
f(lua_State *l)
{
lua_newtable(l); /* t */
lua_pushstring(l, "x"); /* t 'x' */
lua_pushnumber(l, 7); /* t 'x' 7 */
lua_settable(l, -3); /* t */
lua_replace(l, LUA_ENVIRONINDEX);
luaL_dostring(l, "return x");
printf("type(x): %d\n", lua_type(l, -1));
lua_pushstring(l, "x"); /* 'x' */
lua_gettable(l, LUA_ENVIRONINDEX); /* v */
printf("type(x): %d\n", lua_type(l, -1));
return 0;
}
the output is:
type(x): 0
type(x): 3
x is nil in the global table, but is a number (7 hopefully) in the
function's environment.
Cheers,
drj