/apps/twnlua: ctx uploading

This commit is contained in:
veclavtalica 2025-01-14 01:35:54 +03:00
parent e984e95fa8
commit b037d7a0b9
2 changed files with 28 additions and 5 deletions

View File

@ -43,7 +43,22 @@ def to_table(typedesc, variable, indent = 0):
else:
raise BaseException("Unhandled return field type '%s'" % (field["type"]))
binding += ' ' * indent + "lua_setfield(L, -2, \"%s\");\n" % field["name"]
# binding += ' ' * indent + "lua_pop(L, 1);\n"
return binding
def from_table(typedesc, variable, indent = 0):
binding = ""
for field in typedesc["fields"]:
binding += ' ' * indent + "lua_getfield(L, -1, \"%s\");\n" % field["name"]
if field["type"] == "float":
binding += ' ' * indent + "%s = (float)lua_tonumber(L, -1);\n" % (variable + ".%s" % field["name"])
elif field["type"] == "bool":
binding += ' ' * indent + "%s = lua_toboolean(L, -1);\n" % (variable + ".%s" % field["name"])
elif field["type"] in api["types"]:
binding += from_table(api["types"][field["type"]], variable + ".%s" % field["name"], indent + 4)
else:
raise BaseException("Unhandled return field type '%s'" % (field["type"]))
binding += ' ' * indent + "lua_pop(L, 1);\n"
return binding
@ -94,8 +109,6 @@ 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"]))
@ -178,7 +191,7 @@ print(loader)
unloader = "extern void bindgen_unload_%s(lua_State *L);\n" % api["name"]
unloader += "void bindgen_unload_%s(lua_State *L) {\n (void)L;\n" % api["name"]
unloader += '\n'.join(deinitializers)
unloader += "}\n"
unloader += "\n}\n"
print(unloader)
@ -188,5 +201,11 @@ if api["name"] == "twn":
contexter = "extern void bindgen_build_context(lua_State *L);\n"
contexter += "void bindgen_build_context(lua_State *L) {\n"
contexter += to_table(api["types"]["Context"], "ctx", 4)
contexter += "}\n\n"
contexter += "extern void bindgen_upload_context(lua_State *L);\n"
contexter += "void bindgen_upload_context(lua_State *L) {\n"
contexter += from_table(api["types"]["Context"], "ctx", 4)
contexter += "}"
print(contexter)

View File

@ -14,6 +14,7 @@
void bindgen_load_twn(lua_State *L);
void bindgen_unload_twn(lua_State *L);
void bindgen_build_context(lua_State *L);
void bindgen_upload_context(lua_State *L);
/* require will go through physicsfs exclusively so that scripts can be in the data dir */
@ -170,6 +171,9 @@ void game_tick(void) {
log_critical("%s", lua_tostring(state->L, -1));
lua_pop(state->L, 1);
}
lua_getglobal(state->L, "ctx");
bindgen_upload_context(state->L);
}