lua-users home
lua-l archive

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


This is a program completely doen by myself. For the curious: it implements the same API as the standard io-module, but can dynamically choose between various backends, e.g. reading/writing compressed, to a memory file,etc.

I kept the filehandle userdata at hand with
	lua_pushlightuserdata(L, p); /* p = my filehandle userdata */
	lua_rawseti(L, LUA_ENVIRONINDEX, stdindex); /* register in environment */

and also to make stderr globally available by this code in the C-module:
	lua_pushlightuserdata(L, p); /* p = my filehandle userdata */
	lua_setglobal(L, "stdin"); /* register global under its name */

I verified that the top of the stack has my filehandle before the rawset and setglobal.
But neither 'io.stderr:write' nor 'stderr:write' have a known stderr.

Could it be that I misunderstand the effect of the setglobal?

Hans van der Meer

On 31 dec 2010, at 21:22, Rob Hoelz wrote:

> Is this a program you're working on yourself, or are you extending someone else's work?
> 
> Some people like to do this little trick:
> 
> local io = {
>  write = write,
> }
> 
> Do you have access to _G, or getfenv?  Try comparing io to _G.io or getfenv(0).io.
> 
> -Rob
> 
> On Dec 31, 2010, at 3:17 PM, Hans van der Meer wrote:
> 
>> In Lua I can write
>>  io.stderr:write(msg)
>> and get the msg on the stderr output stream.
>> Therefore I conclude io.stderr is in the globally accessible space.
>> 
>> In an alternative, special output module I want to duplicate this behaviour. Although I have managed to get working:
>>   io.write(stderr, msg)
>> The equivalent of the above io.stderr:write(msg) does not know stderr: "attempt to index field 'stderr' (a nil value)"
>> 
>> I have in earnest sought in the documentations as well as in the sources, but could not find how to accomplish this. Is there someone who will enlighten me and point in the right direction?
>> 
>> Thanks in advance,
>> Hans van der Meer
>> 
>> 
> 
>