From 0aa68af3d73f115e584e35013d3db9df6b1199a1 Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Sun, 24 Mar 2024 20:22:06 +1300 Subject: [PATCH] extra: init function "printfmt" Roughly equivalent to: io.stdout:write(string.format(fstring, ...), '\n') Only possible because of the newly bundled string.format replacement. --- lextra.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lextra.c b/lextra.c index fe995f5..8114733 100644 --- a/lextra.c +++ b/lextra.c @@ -12,11 +12,37 @@ * @module extra */ +#include #include +#include #include #include +#include "util.h" + +static int +extra_printfmt(lua_State *L) +{ + const char *outstring; + int i, nargs; + + nargs = lua_gettop(L); + + lua_pushcfunction(L, lstrfmt); + for (i = 1; i <= nargs; i++) { + lua_pushvalue(L, i); + } + lua_call(L, nargs, 1); + + outstring = lua_tostring(L, -1); + lua_pop(L, 1); + write(1, outstring, strlen(outstring)); + write(1, "\n", 1); + + return 0; +} + /*** * Waits the specified amount of seconds. * @@ -57,7 +83,8 @@ extra_sleep(lua_State *L) /* clang-format off */ static const luaL_Reg extlib[] = { - {"sleep", extra_sleep}, + {"printfmt", extra_printfmt}, + {"sleep", extra_sleep}, {NULL, NULL} };