lua-users home
lua-l archive

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


Yep, that got it.  Thanks for the tip.  

But you know.  Thinking about lua as an embedded language, I think it should be more straight forward to replace functions that write output to stdout or 
stderr.  My .02.  This is something that many/most embedded programs probably want to do.  

For many reasons, I hate mucking up the original libraries with one-off solutions - so my other option was copy io_write() and all its supporting functions 
elsewhere, modify it as needed (didnt need to change much), register it using the method described below.  This is somewhat awkward... wonder if there is 
a better way.

Thanks.
Terry

On Mon, 13 Aug 2001 16:50:32 -0500, Rici Lake wrote:

>I think you'll find that io_write (and other io helper functions) are
>actually registered as closures with a special structure which contains
>persistent information necessary to the functioning of the io library.
>If you look at liolib.c, you'll see that there are two groups of
>functions registered, the first set registered in the "normal" fashion
>and the second set with the IOCtrl structure as an upvalue.
>
>This is a pretty useful technique and ought to be written up somewhere
>:-)
>
>R.
>
>
>Terry Bayne wrote:
>> 
>> Hi,
>> 
>> In trying to create a replacement for the standard write() function call, the first thing I did was copy io_write() and its static helper functions and definitions
>> from the distribution, modified the calls that actually do the output.  But I am running into a strange, problem....
>> 
>> static int io_write (lua_State *L) {
>>   int lastarg = lua_gettop(L) - 1;
>>   IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
>>   int arg = 1;
>>   int status = 1;
>>   FILE *f = gethandle(L, ctrl, arg);
>>   if (f) arg++;
>>   else f = getfilebyref(L, ctrl, OUTFILE);  /* get _OUTPUT */
>> 
>> The call to getfilebyref() never returns.... debugging further I see that inside of there we get stuck on the line:
>> 
>>   lua_getref(L, ctrl->ref[inout]);
>> 
>> Now, up to this poing, the variable ctrl is NULL, so it is not surprising this doesnt work correctly.  The question is, why is it NULL?  Or am I doing 
something
>> just basically wrong?
>> 
>> Guess I should stipulate that I am using version 4.0
>> 
>> Thanks.
>> Terry Bayne
>