From 8d67e4400906cdef2a33fb948dbbe50a0fb549b7 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Sun, 12 Jan 2025 03:50:46 +0300 Subject: [PATCH] /apps/twnlua: use lua_numberx for slightly more optimized defaults --- apps/twnlua/bindgen.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/apps/twnlua/bindgen.py b/apps/twnlua/bindgen.py index b6c487d..5ac6ea1 100755 --- a/apps/twnlua/bindgen.py +++ b/apps/twnlua/bindgen.py @@ -8,9 +8,6 @@ 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": @@ -72,13 +69,18 @@ for procedure, procedure_desc in api["procedures"].items(): binding += " %s %s;\n" % (parameter["type"], parameter["name"]) binding += " lua_getfield(L, 1, \"%s\");\n" % parameter["name"] - if "default" in parameter: + if "default" in parameter and parameter["type"] != "float": binding += " if (lua_isnoneornil(L, -1))\n" binding += " %s = %s;\n" % (parameter["name"], default(parameter)) binding += " else\n " if parameter["type"] == "float": - binding += " %s = (float)lua_tonumber(L, -1);\n" % (parameter["name"]); + if "default" in parameter: + binding += " int is_%s_num;\n" % parameter["name"] + binding += " %s = (float)lua_tonumberx(L, -1, &is_%s_num);\n" % (parameter["name"], parameter["name"]); + binding += " if (!is_%s_num) %s = %s;\n" % (parameter["name"], parameter["name"], default(parameter)) + else: + binding += " %s = (float)lua_tonumber(L, -1);\n" % (parameter["name"]); elif parameter["type"] == "bool": binding += " %s = lua_toboolean(L, -1);\n" % (parameter["name"]); elif parameter["type"] == "char *":