lua-users home
lua-l archive

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


Am 22.04.11 15:56, schrieb Marc Balmer:
> Am 22.04.11 15:35, schrieb Peter Cawley:
>> On Fri, Apr 22, 2011 at 2:33 PM, Marc Balmer <marc@msys.ch> wrote:
>>> I have a table on the stack, then I call lua_load() to load a function.
>>>  After lua_load() completes without an error, I have a function on top
>>> of the stack.  But my table, which should now be at position -2, is
>>> gone.  There is a nil value there.
>>>
>>> Hwo can that happen?  Is that some memory corruption in my lua_Reader
>>> function or are there situations where lua_load() modifies the stack in
>>> such a way?
>>
>> What is your reader function doing? It shares the view of the stack
>> with the caller of lua_load, it doesn't get its own stack frame.
> 
> The lua_Reader function does not touch the stack, it merely returns Lua
> source code piece by piece from a static buffer.

I replaced my reader with a bare-minimum reader, and I get the same
behaviour:


const char *
lt_reader(lua_State *L, void *data, size_t *size)
{
	static int state = 0;
	static char b[BUFSIZ];

	if (state == 0) {
		bzero(b, sizeof b);
		*size = strlcat(b, "print('hello, world!')\n",
		    sizeof b);
		state = 1;
		return b;
	} else {
		*size = 0L;
		state = 0;
		return NULL;
	}
}

Is there anything wrong with the reader (besides that it does not do
very much...)?