lua-users home
lua-l archive

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


On Sun, Oct 11, 2020 at 9:53 PM 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
>In the C API, there is lua_createtable
>This is not exposed on the Lua side, though it would be trivial to write a small extension module to make it available.
So, it's possible to expose such a interface by ourselves? Right?

Yes, and in fact I wrote the code for it in 5 minutes (having not touched the C API in several months), wrote a rockspec for easily building it in another 5, and tested it.

createtable.c:

#include "lua.h"
#include "lauxlib.h"

int myCreateTable(lua_State* L) {
    int narr = luaL_checkinteger(L, 1);
    int nrec = luaL_optinteger(L, 2, 0);
    lua_createtable(L, narr, nrec);
    return 1;
}

LUA_API int luaopen_createtable(lua_State* L) {
    lua_pushcfunction(L, myCreateTable);
    return 1;
}



createtable-0.1.0-1.rockspec:

package = "createtable"
version = "0.1.0-1"

source = {
    url = "" (use luarocks make)",
}

build = {
    type = "builtin",
    modules = {
        createtable = "createtable.c"
    }
}



Place both files together in an empty directory and run "luarocks make" (assuming you have LuaRocks installed and configured). Alternatively, for an embedding case you can build this into the Lua sources themselves (it should be simple to add to the standard table library).

(I release the code above into the public domain via the CC0 license: https://creativecommons.org/publicdomain/zero/1.0/)