Compare commits

..

No commits in common. "b037d7a0b9212933d3823280407f44709afef58d" and "4ed3764c1d10899bb7f247228e8a9b1bdf98c561" have entirely different histories.

3 changed files with 6 additions and 29 deletions

View File

@ -11,7 +11,7 @@ add_subdirectory($ENV{TWNROOT} $ENV{TWNBUILDDIR})
add_custom_command( add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/luabind.c OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/luabind.c
COMMAND ${PYTHON3} ${CMAKE_CURRENT_SOURCE_DIR}/bindgen.py $ENV{TWNROOT}/share/twn_api.json > ${CMAKE_CURRENT_SOURCE_DIR}/luabind.c COMMAND ${PYTHON3} ${CMAKE_CURRENT_SOURCE_DIR}/bindgen.py $ENV{TWNROOT}/share/twn_api.json > ${CMAKE_CURRENT_SOURCE_DIR}/luabind.c
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bindgen.py $ENV{TWNROOT}/share/twn_api.json DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bindgen.py
) )

View File

@ -43,22 +43,7 @@ def to_table(typedesc, variable, indent = 0):
else: else:
raise BaseException("Unhandled return field type '%s'" % (field["type"])) raise BaseException("Unhandled return field type '%s'" % (field["type"]))
binding += ' ' * indent + "lua_setfield(L, -2, \"%s\");\n" % field["name"] binding += ' ' * indent + "lua_setfield(L, -2, \"%s\");\n" % field["name"]
return binding # binding += ' ' * indent + "lua_pop(L, 1);\n"
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 return binding
@ -109,6 +94,8 @@ for procedure, procedure_desc in api["procedures"].items():
else: else:
raise BaseException("Unhandled parameter type '%s'" % (parameter["type"])) 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 "return" in procedure_desc:
if procedure_desc["return"] == "bool": 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"]))
@ -189,9 +176,9 @@ print(loader)
unloader = "extern void bindgen_unload_%s(lua_State *L);\n" % api["name"] 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 += "void bindgen_unload_%s(lua_State *L) {\n(void)L;\n" % api["name"]
unloader += '\n'.join(deinitializers) unloader += '\n'.join(deinitializers)
unloader += "\n}\n" unloader += "}\n"
print(unloader) print(unloader)
@ -201,11 +188,5 @@ if api["name"] == "twn":
contexter = "extern void bindgen_build_context(lua_State *L);\n" contexter = "extern void bindgen_build_context(lua_State *L);\n"
contexter += "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 += 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 += "}" contexter += "}"
print(contexter) print(contexter)

View File

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