lua-users home
lua-l archive

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


2011/11/10 Miles Bader <miles@gnu.org>:
> In Lua 5.2, we can't call "setfenv", so ... what do we do?

_ENV is the first upvalue of a chunk, all you have to do is set that
upvalue before running it.

> I see that the "load" function takes an "env" (4th) parameter now, but
> loadfile doesn't; does this mean we can't use loadfile if we care about
> the loaded file's environment?  That seems a bit annoying, as "load" in
> Lua is kind of primitive, and can't just be used easily with files like
> loadfile can...

local chunk = loadfile(filename)
debug.setupvalue(chunk, 1, environ)
chunk()

> Also, what about from C?  lua_load there _doesn't_ take an env
> parameter, so what should we do then...?

if (luaL_loadfile(L, filename))
    return lua_error(L);
lua_getglobal(L, "environ);
lua_setupvalue(L, -2, 1);
lua_call(L, 0, 0);