lua-users home
lua-l archive

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

On 2-Dec-05, at 9:46 PM, David Burgess wrote:

On 12/3/05, Rici Lake <> wrote:

As far as I know, lua_replace will not work with the LUA_ENVIRONINDEX
pseudo-index, [...]
This can be done, as it is in loadlib.c and liolib.c. It requires that the C function that is doing the lua_replace has its own lua stack (i.e. has
been lua_call()ed).

Right you are; there is a special check in lua_replace for that very case. So I take it back, you can set your own environment with LUA_ENVIRONINDEX

 I do not understand why.

The io library uses an environment table for data shared between the various io methods: specifically, the current input and output file descriptors. I imagine that the package system has a similar need.

The 5.0.2 io library uses the "FILE" metatable for this purpose, but that makes the data non-private:

Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> f ="/dev/null"
> =f._input
file (0x8072170)
> =f._output
file (0x80721d0)
> =f
file (0x8072b10)
> io.output(f)
> =f._output
file (0x8072b10)

--> contrast (note that the fenv wouldn't be visible without the debug library, which is one of the reasons you shouldn't include the debug library in production builds)

Lua 5.1 (beta)  Copyright (C) 1994-2005, PUC-Rio
> for k, v in pairs(debug.getfenv( do print(k,v) end
1       file (0x281eaf00)
2       file (0x281eaf58)
__close function: 0x8082900
> =io.stdin
file (0x281eaf00)
> =io.stdout
file (0x281eaf58)