lua-users home
lua-l archive

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


Damn, I'll have to keep that in mind, I don't think that is what this
is, it dies on a step over lua_next, so im not letting it run rampant.


On 1/19/07, Rici Lake <lua@ricilake.net> wrote:

On 19-Jan-07, at 9:31 PM, Jérôme VUARAND wrote:

> 2007/1/19, Raymond Jacobs <raymondj@gmail.com>:
>> int idx=LUA_GLOBALSINDEX;
>> lua_getfenv(l,idx);
>>
>> while(lua_next(l,idx)!=0)
>> {
>>                 const char* key=lua_tostring(l,-2);
>>
>>                 //get value at -1 index and check type of it
>>
>>                 //do somthing with key and value based on type of
>> value
>>                 //if value is a table call this function again to
>> traverse it, then
>> continue here
>>
>>
>>                 lua_pop(l,1);
>> }
>
> You forget to push an initial nil on the stack before entering you
> while loop (see lua_next documentation example).

He did push a nil, although I suspect it was not by design:

>> int idx=LUA_GLOBALSINDEX;
>> lua_getfenv(l,idx);

Since tables don't have environments, lua_getfenv() will do the job.

By the way, be careful with recursively enumerating tables; if there's
a circular reference, you'll find yourself running off the end of the C
stack. (And, since _G._G == _G, there is such a circular reference in
the standard globals table.)