From 4da6637af3dca403995b61e47159605fa272b0a9 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 15 May 2017 19:50:41 +1000 Subject: src/liolib.js: Add io.type --- src/liolib.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src') diff --git a/src/liolib.js b/src/liolib.js index 1557adf..575b3ec 100644 --- a/src/liolib.js +++ b/src/liolib.js @@ -19,6 +19,18 @@ const isclosed = function(p) { return p.closef === null; }; +const io_type = function(L) { + lauxlib.luaL_checkany(L, 1); + let p = lauxlib.luaL_testudata(L, 1, lauxlib.LUA_FILEHANDLE); + if (p === null) + lua.lua_pushnil(L); /* not a file */ + else if (isclosed(p)) + lua.lua_pushliteral(L, "closed file"); + else + lua.lua_pushliteral(L, "file"); + return 1; +}; + const f_tostring = function(L) { let p = tolstream(L); if (isclosed(p)) @@ -95,6 +107,7 @@ const f_write = function(L) { const iolib = { "close": io_close, + "type": io_type, "write": io_write }; -- cgit v1.2.3-54-g00ecf