1

I have an inquiry about the binding between lua and c languages.

lua usage example)

a[1].b.c[1].d = 1
a[1].b.c[2].d = 2
a[2].b.c[1].d = 3
a[2].b.c[2].d = 4

I am wondering how to create this with lua C api. This is the code I used in C

    lua_createtable(L, 2, 0);
lua_pushnumber(L, 1);
{
    lua_createtable(L, 0, 1);
    {
        lua_newtable(L);
        lua_pushnumber(L, 1);
        {
            lua_pushnumber(L, 49);
            lua_setfield(L, -2, "d");
            lua_settable(L, -3);
        }
        lua_pushnumber(L, 2);
        {
            lua_pushstring(L, "old");
            lua_setfield(L, -2, "d");
            lua_settable(L, -3);
        }
        lua_setfield(L, -2, "c");
    }
    lua_setfield(L, -2, "b");
}
lua_pushnumber(L, 2);
{
    lua_createtable(L, 0, 1);
    {
        lua_newtable(L);
        lua_pushnumber(L, 1);
        {
            lua_pushnumber(L, 49);
            lua_setfield(L, -2, "d");
            lua_settable(L, -3);
        }
        lua_pushnumber(L, 2);
        {
            lua_pushstring(L, "old");
            lua_setfield(L, -2, "d");
            lua_settable(L, -3);
        }
        lua_setfield(L, -2, "c");
    }
    lua_setfield(L, -2, "b");
}
lua_setglobal(L, "a");

By the way PANIC: unprotected error in call to Lua API (attempt to index a number value) I got an error like this

Thank you so much for telling me this.

P.Kwon
  • 21
  • 2
  • Why are you doing `lua_pushnumber` after every `lua_newtable`? Also, why are you combining `lua_setfield` and `lua_settable` like that? Maybe you could add some comments to your code to clarify, otherwise it's just very hard to read. – DarkWiiPlayer Nov 13 '20 at 07:46

1 Answers1

0
lua_createtable(L, 2, 0); // Stack: {}
lua_pushnumber(L, 1); // Stack: {}, 1
{
    lua_createtable(L, 0, 1); // Stack: {}, 1, {}
    {
        lua_newtable(L); // Stack: {}, 1, {}, {}
        lua_pushnumber(L, 1); // Stack: {}, 1, {}, {}, 1
        {
            lua_pushnumber(L, 49); // Stack: {}, 1, {}, {}, 1, 49
            lua_setfield(L, -2, "d"); // !!!!!!!
            // You currently have two numbers at the top of the stack.
            // the index -2 thus points to the number 1
            // Trying to lua_setfield on that index fails because it expects
            // a table but finds an integer instead.
            lua_settable(L, -3);
            // ...
        }
        // ...
    }
    // ...
}
DarkWiiPlayer
  • 6,871
  • 3
  • 23
  • 38
  • Thanks for the answer. What should I do to do it like lua code in the question? Thank you for showing me an example. I've only been in contact with lua for two weeks, so I don't know well, so please understand. – P.Kwon Nov 13 '20 at 08:38
  • You're generally doing it right, but you were indexing the wrong items on the stack. It's easier if you start with a simple table, compile, see if it works and then add complexity little by little so when it breaks you know where :D @P.Kwon – DarkWiiPlayer Nov 13 '20 at 10:53
  • a[1].b.c.d = 1 Expressing this way was a success. By the way, I do not know how to express a[1].b.c[1].d = 1 like this. How can we express a[1].b.c[1].d = 1? It's really urgent. Can I see an example? – P.Kwon Nov 16 '20 at 13:40