1

I have a table as follows below.

Table = {
        1,      2,      3,
        6,      7,      8,
        11,     12,     13,...
}

I want to read that table and say how many values were read, the more I'm having trouble with my code below, always the following error occurs when reading PANIC: unprotected error in call to Lua API

I am using lua 5.2, the error is pointed out here

lua_getfield (L, -1, "Table");

can someone help me with this reading, I'm new in lua.

void read_table(void) {

    int var1;
    long double var2;
    lua_State *L;
    L = = luaL_newstate();
    luaL_openlibs(L);

    if (luaL_loadfile(L, "table.lua") || lua_pcall(L, 0, 0, 0)) {
        printf("cannot run cofig file: %s\n");
        return;
    }
    else {
        lua_getfield(L, -1, "Table");

        if (lua_type(L, -1) == 5) {
            var1 = 1;
            while (1)
            {
                lua_pushnumber(L, var2);
                lua_gettable(L, -2);

                if (!lua_isnumber(L, -1))
                break;
                lua_tonumber(L, -1);
                lua_settop(L, -2);
                ++var1;

                lua_close(L);
            }
            printf("table %d invalid\n", var1);
        }
        else {
            printf("table should be a table\n");
        }
    }
    printf("Reading %d numbers\n", var1);
}
greatwolf
  • 20,287
  • 13
  • 71
  • 105
carolzinha
  • 101
  • 1
  • 7
  • What do you think is at stack index `-1` at the point where you call `lua_getfield(L, -1, "Table")`? – Etan Reisner Mar 24 '15 at 16:14
  • The lua_getfield was giving error, as I am using lua 5.2 I switched to `lua_getglobal (L, "Table");` – carolzinha Mar 24 '15 at 17:09
  • That's not a 5.2 thing but sure. Did that solve your problem? – Etan Reisner Mar 24 '15 at 17:12
  • reading is made, only 1 value is read, but should read all. – carolzinha Mar 24 '15 at 17:30
  • Where does `var2` get given a value? You are pushing an uninitialized value onto the lua stack. – Etan Reisner Mar 24 '15 at 17:36
  • I'm having a little trouble for that I'm using as a basis a debugging made by IDA, here's the link, I'm trying to write like is in debugging. http://pastebin.com/kTbraLp4 – carolzinha Mar 24 '15 at 17:49
  • First question, does "table.lua" actually return a table back when executed? Does that table contain a `Table` key as one of its field? – greatwolf Mar 24 '15 at 21:58
  • Why is `lua_close` in the inner most loop? – greatwolf Mar 24 '15 at 22:03
  • Use [`lua_rawgeti`](http://pgl.yoyo.org/luai/i/lua_rawgeti) if you're trying to iterate through the array part of the table. If you just want to count the elements in it and there are no 'holes' you can use [`lua_objlen`](http://pgl.yoyo.org/luai/i/lua_objlen). – greatwolf Mar 24 '15 at 22:09
  • I managed to read, now how can I see the values that are being read? Another thing how can I start making the count while from 0 and not 1 e.g `for (i = 0; i < 1000; i++)` – carolzinha Mar 26 '15 at 06:06

0 Answers0