Compare commits

...

2 Commits

Author SHA1 Message Date
veclavtalica
458b44d0b0 /docs/interop.md: limit parameter count to 8, specify enums 2025-01-28 23:51:18 +03:00
veclavtalica
8de4a1f09b /apps/twnlua: optimize default boolean and convertrer pops 2025-01-28 23:48:49 +03:00
3 changed files with 7 additions and 4 deletions

View File

@ -84,7 +84,7 @@ for procedure, procedure_desc in api["procedures"].items():
binding += " %s %s;\n" % (parameter["type"] if not parameter["type"].endswith("*") else 'const ' + parameter["type"], parameter["name"])
binding += " lua_getfield(L, 1, \"%s\");\n" % parameter["name"]
if "default" in parameter and parameter["type"] != "float":
if "default" in parameter and not parameter["type"] in ("float", "bool"):
binding += " if (lua_isnoneornil(L, -1))\n"
binding += " %s = %s;\n" % (parameter["name"], default(parameter))
binding += " else\n "
@ -141,15 +141,15 @@ for typename, typedesc in used_converters.items():
converter += " %s %s;\n" % (typename, typename.lower());
if "fields" in typedesc:
for field in typedesc["fields"]:
converter += " lua_getfield(L, -1, \"%s\");\n" % (field["name"]);
for idx, field in enumerate(typedesc["fields"]):
converter += " lua_getfield(L, -%i, \"%s\");\n" % (idx + 1, field["name"]);
if field["type"] == "float":
converter += " %s.%s = (float)lua_tonumber(L, -1);\n" % (typename.lower(), field["name"]);
elif field["type"] == "uint8_t":
converter += " %s.%s = (uint8_t)lua_tointeger(L, -1);\n" % (typename.lower(), field["name"]);
else:
raise BaseException("Unhandled converter field type '%s'" % (field["type"]))
converter += " lua_pop(L, 1);\n";
converter += " lua_pop(L, %i);\n" % len(typedesc["fields"]);
# TODO: wild idea: use compile time built hash table
elif "enums" in typedesc:

View File

@ -5,6 +5,7 @@ for that certain considerations are taken:
* number of public api calls is kept at the minimum
* procedure parameters can only use basic types, no aggregates, with exception of Vec/Matrix types and alike,
with no new additions, ever (see [/include/twn_types.h](../include/twn_types.h))
* enum types are allowed, as they decay to numeric type (but language-specific api can decide to provide simple ways to use them)
* optionals can be expressed via pointer passage of value primitives, assumed immutable, with the NULL expressing default value
* no opaque types, only keys if needed
* pointers to pointers aren't allowed
@ -15,3 +16,4 @@ for that certain considerations are taken:
* 32 bit floating point is the only numeric type
* [/include/twn_api.json](../share/twn_api.json) file is hand-kept with a schema to aid automatic binding generation and tooling
* parameter names should not collide with keywords of any language that is targetted; if so happens, parameter alias could be added
* any procedure can't have more than 8 parameters

View File

@ -8,6 +8,7 @@
#include <stdbool.h>
/* pushes a sprite onto the sprite render queue */
/* TODO: combine flip_x and flip_y into a flip_mask with enum */
TWN_API void draw_sprite(char const *texture,
Rect rect,
Rect const *texture_region, /* optional, default: NULL */