slot based allocator for lua, usage of lua_createtable

This commit is contained in:
veclavtalica
2025-01-12 02:44:41 +03:00
parent 46e077ba63
commit e8b02570a2
3 changed files with 60 additions and 6 deletions

View File

@ -8,6 +8,9 @@ with open(sys.argv[1], 'r') if sys.argv[1] != "-" else sys.stdin as f:
api = json.loads(api_source)
# TODO: use lua_to--x functions for defaults, so that fewer function calls are made
# TODO: use lua_createtable for preallocation
def default(parameter):
basetype = parameter["type"].rsplit(' *', 1)[0]
if parameter["type"] == "float":
@ -32,7 +35,7 @@ def default(parameter):
def to_table(typedesc, variable, indent = 0):
binding = ' ' * indent + "lua_newtable(L);\n"
binding = ' ' * indent + "lua_createtable(L, 0, %i);\n" % len(typedesc["fields"])
for field in typedesc["fields"]:
if field["type"] == "float":
binding += ' ' * indent + "lua_pushnumber(L, (double)(%s));\n" % (variable + ".%s" % field["name"])
@ -48,7 +51,8 @@ def to_table(typedesc, variable, indent = 0):
print('#include "twn_game_api.h"\n')
print('#define STB_DS_IMPLEMENTATION') # TODO: reuse implementation from the engine
# TODO: reuse implementation from the engine, this also breaks with statically compiled build
print('#define STB_DS_IMPLEMENTATION')
print('#include <stb_ds.h>')
print('#include <lua.h>')
print('#include <lualib.h>')
@ -88,11 +92,13 @@ for procedure, procedure_desc in api["procedures"].items():
else:
raise BaseException("Unhandled parameter type '%s'" % (parameter["type"]))
# binding += " lua_pop(L, %i);\n" % (1 + len(procedure_desc["params"]) if "params" in procedure_desc else 0)
if "return" in procedure_desc:
if procedure_desc["return"] == "bool":
binding += " lua_pushboolean(L, (int)%s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
binding += " lua_pushboolean(L, (int)(%s(%s)));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
elif procedure_desc["return"] == "float":
binding += " lua_pushnumber(L, (double)%s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
binding += " lua_pushnumber(L, (double)(%s(%s)));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
elif procedure_desc["return"] == "char *":
binding += " lua_pushstring(L, %s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
elif type(procedure_desc["return"]) is dict or procedure_desc["return"] in api["types"]:
@ -159,7 +165,7 @@ loader = "void bindgen_load_%s(lua_State *L) {\n" % api["name"]
modules = set(api["procedures"][procedure]["module"] for procedure in api["procedures"])
for module in modules:
loader += " bindgen_init();\n"
loader += " lua_newtable(L);\n"
loader += " lua_createtable(L, 0, %i);\n" % len(api["procedures"])
for procedure, procedure_desc in api["procedures"].items():
if procedure_desc["module"] == module:
loader += " lua_pushcfunction(L, binding_%s);\n" % procedure