lua-users home
lua-l archive

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


First off, I'm using pre-3.0 lua, It's 2.0 or 2.1 or whatever the latest non-3.0
version was... If it matters, I can figure out exactly which version.

I have a C code snippit which is supposed to traverse a table, if the VALUE of a
given slot in the table is a table, then it recurses. 

The problem is, that in this particular case something is blowing away the value
of my index variable, and there is not much code there, just a few lua_istable,
and lua_getstring calls... At least that's what it looks like. Right after I do
lua_getparam(1), the value is NOT nil, but at the end of the loop (where it
tests it again) the value IS nil.

Of course, the recurse code will make calls into lua and blow away the lua stack
and such, so I'm trying to figure out what I'm doing wrong here. I tried not
recursing and it didn't seem to fix my problem.

Here is the code snippit..

temp = LUA_NOOBJECT; // set to nil
curindex = 0;
int reached_end = 0;

do {
  
     // Start or continue the traverse of the table...

     lua_pushobject(a_table);
     if (temp == LUA_NOOBJECT) {
        lua_pushnil();
     } else {
        lua_pushobject(temp);
     }

     if (lua_callfunction(next_fn)) {
        // an error occured!!!
	lua_error("error calling next() builtin Lua function");
     }
			
     // now get off the two return values
			
     tbl_idx = lua_getresult(1);  // index, should be a string or number
     tbl_val = lua_getresult(2);  // value, should be a table, unless it's the 
                                     // indexname
			
     if (lua_isstring(tbl_idx)) {
        reached_end = lua_isnil(tbl_idx);
	fprintf(stderr,"handle element(%d/%d/%d):%s",curindex,table_len,
                 reached_end,lua_getstring(tbl_idx));
     } 

     if (lua_istable(tbl_val)) {
        // the value is another table, so store and count it
        if (curindex < table_len) {
           if (lua_isstring(tbl_idx)) {
              strcpy(a_list->list[curindex].name,lua_getstring(tbl_idx)); 
                       // should size check that string !!

              this->parseSpriteTable(&(a_list->list[curindex].ptr),tbl_val); 
                       // recurse
            } else {
              consoleView->addText("non-string index on sprite chunk!");
              lua_error("can't allow sprite chunk with non-string index currently");
            }
         }
         curindex++;	
      }

      temp = tbl_idx;
	
      if (lua_isnil(tbl_idx)) {    // something blew away tbl_idx!!
         consoleView->addText("reached table end");
      }

   } while ((!reached_end) && (curindex < table_len));



My questions are this:

1) If I call something in lua (like the above lua_callfunction(next_fn)) will it
blow away something I stored in a local variable off a lua_getresult()? For
example, will the above tbl_idx go out of scope if I call back into lua?

2) Even when I remove the recurse to this->parseSpriteTable(...), tbl_idx still
comes up as nil at the end of this loop... So every time the loop executes lua
gives it the first element. Are lua_isstring, luagetstring, lua_istable, and
lua_isnil safe? 

3) what else might be going wrong here? The strcpy() is not doing the corruption
either, and there isn't anything else going on here except a bunch of calls to
lua macros.

I can comment out both the strcpy() and the parseSpriteTable (i.e. which
recurses to the same function this snippit is from) and there is no change. 



-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net