[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Current environment
- From: Nodir Temirhodzhaev <tnodir@...>
- Date: Tue, 25 May 2004 09:37:45 +0400 (MSD)
Hello!
I suggest to use by default environment of "current" thread or of function, if it specially setted by setfenv.
lua5.0: ldo.c: f_parser {
...
cl = luaF_newLclosure(L, 0, gt(L));
cl->l.g.tt = LUA_TNIL; //+
...
}
lua5.0: lvm.c: luaV_execute {
...
case OP_GETGLOBAL: {
const TObject *g = ttistable(&cl->g) ? &cl->g : gt(L); //+
TObject *rb = KBx(i);
const TObject *v;
lua_assert(ttisstring(rb) && ttistable(g)); //&cl->g
v = luaH_getstr(hvalue(g), tsvalue(rb)); //&cl->g
if (!ttisnil(v)) { setobj2s(ra, v); }
else
setobj2s(XRA(i), luaV_index(L, g, rb, 0)); //&cl->g
break;
}
...
case OP_SETGLOBAL: {
const TObject *g = ttistable(&cl->g) ? &cl->g : gt(L); //+
lua_assert(ttisstring(KBx(i)) && ttistable(g)); //&cl->g
luaV_settable(L, g, KBx(i), ra); //&cl->g
break;
}
...
}
=====
local function inc() print(out) end
--setfenv(inc, setmetatable({out = "fenv"}, {__index = _G}))
local function th(arg)
setfenv(0, setmetatable({out = arg}, {__index = _G}))
coroutine.yield()
inc()
end
local a = coroutine.create(th)
local b = coroutine.create(th)
coroutine.resume(a, 'a')
coroutine.resume(b, 'b')
coroutine.resume(a)
coroutine.resume(b)
=====
Prints:
a
b
If uncomment 3 line:
fenv
fenv