Search code examples
clualua-api

Select nested value with selector string with the Lua C API


Assumed I have a value defined in nested tables: tab["m"]["b"] = {}. In Lua, I can just define it with the previous statement.

Is that possible with the C API too? Specifically instead of pushing tab, m, etc. individually, selecting the value with a single string tab["m"]["b"].

Pushing and selecting it, like done with single values, (like in the code below) does not work.

lua_pushstring(state, "tab[\"m\"][\"b\"]");
lua_gettable(state, LUA_GLOBALSINDEX);

Solution

  • This is not possible in the C API. If you need this functionality, you can add a helper function to do it:

    /* Pushes onto the stack the element t[k_1][...][k_n]
     * where t is the value at the given index and 
     * k_1, ..., k_n are the elements at the top of the stack
     * (k_1 being furthest from the top of the stack and
     *  k_n being at very the top of the stack).
     */
    void recursive_gettable(lua_State *L, int index, int n) /*[-n,+1,e]*/ {
        luaL_checkstack(L, 2, NULL);           /*[k_1,...,k_n]*/
        lua_pushvalue(L, index);               /*[k_1,...,k_n,t]*/
        for (int i = 1; i <= n; ++i) {
            lua_pushvalue(L, -(n+1)+(i-1));    /*[k_1,...,k_n,t[...][k_(i-1)],k_i]*/
            lua_gettable(L, -2);               /*[k_1,...,k_n,t[...][k_i]]*/
        }
        lua_replace(L, -1, -(n+1));            /*[t[...][k_n],k_2,...,k_n]*/
        lua_pop(L, n-1);                       /*[t[...][k_n]] */
    }
    
    /*usage:*/
    luaL_checkstack(L, 3, NULL);
    lua_pushstring(L, "tab");
    lua_pushstring(L, "m");
    lua_pushstring(L, "b");
    recursive_gettable(L, LUA_GLOBALSINDEX, 3);