summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <ast@2e8.dk>2020-07-22 20:14:31 +0000
committerAsbjørn Sloth Tønnesen <ast@2e8.dk>2020-07-30 22:56:14 +0000
commit02cc7c6bf355b48b08298aa355f9bd89b3ddf52d (patch)
tree50a034c6d41869a94a3ec299956c01faff6c5983
parenta9c562c0b8aa9cd3a0cea4169628e96ae7839c43 (diff)
downloadlem-02cc7c6bf355b48b08298aa355f9bd89b3ddf52d.tar.gz
lem-02cc7c6bf355b48b08298aa355f9bd89b3ddf52d.tar.xz
lem-02cc7c6bf355b48b08298aa355f9bd89b3ddf52d.zip
lem.io: add :print() mirroring Lua print()
Signed-off-by: Asbjørn Sloth Tønnesen <ast@2e8.dk>
-rw-r--r--lem/io/core.c23
-rw-r--r--lem/io/file.c7
-rw-r--r--lem/io/stream.c7
3 files changed, 37 insertions, 0 deletions
diff --git a/lem/io/core.c b/lem/io/core.c
index 4844bac..5917b64 100644
--- a/lem/io/core.c
+++ b/lem/io/core.c
@@ -104,6 +104,23 @@ error:
return luaL_argerror(T, idx, "invalid permissions");
}
+static void
+io_print(lua_State *T)
+{
+ int orig_top = lua_gettop(T);
+ luaL_checktype(T, 1, LUA_TUSERDATA);
+ lua_checkstack(T, (orig_top - 1) * 3 + 1);
+ for (int i=2;i<=orig_top;i++) {
+ if (i>2) lua_pushliteral(T, "\t");
+ (void)luaL_tolstring(T, i, NULL);
+ }
+ lua_pushliteral(T, "\n");
+ if (orig_top > 1) {
+ lua_rotate(T, 2, 1 - orig_top);
+ lua_settop(T, (orig_top - 1) * 2 + 1);
+ }
+}
+
#include "file.c"
#include "stream.c"
#include "server.c"
@@ -600,6 +617,9 @@ luaopen_lem_io_core(lua_State *L)
/* mt.lock = <file_lock> */
lua_pushcfunction(L, file_lock);
lua_setfield(L, -2, "lock");
+ /* mt.print = <file_print> */
+ lua_pushcfunction(L, file_print);
+ lua_setfield(L, -2, "print");
/* insert table */
lua_setfield(L, -2, "File");
@@ -637,6 +657,9 @@ luaopen_lem_io_core(lua_State *L)
/* mt.sendfile = <stream_sendfile> */
lua_pushcfunction(L, stream_sendfile);
lua_setfield(L, -2, "sendfile");
+ /* mt.print = <stream_print> */
+ lua_pushcfunction(L, stream_print);
+ lua_setfield(L, -2, "print");
/* insert io.stdin stream */
push_stdstream(L, STDIN_FILENO);
lua_setfield(L, -3, "stdin");
diff --git a/lem/io/file.c b/lem/io/file.c
index 9d54fcb..347414a 100644
--- a/lem/io/file.c
+++ b/lem/io/file.c
@@ -519,3 +519,10 @@ file_lock(lua_State *T)
lua_settop(T, 1);
return lua_yield(T, 1);
}
+
+static int
+file_print(lua_State *T)
+{
+ io_print(T);
+ return file_write(T);
+}
diff --git a/lem/io/stream.c b/lem/io/stream.c
index 47a5775..db23e91 100644
--- a/lem/io/stream.c
+++ b/lem/io/stream.c
@@ -559,3 +559,10 @@ stream_sendfile(lua_State *T)
lua_settop(T, 2);
return lua_yield(T, 2);
}
+
+static int
+stream_print(lua_State *T)
+{
+ io_print(T);
+ return stream_write(T);
+}