aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-05-15 19:50:41 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-15 19:50:41 +1000
commit4da6637af3dca403995b61e47159605fa272b0a9 (patch)
treecd4970f3a1af5673a5e6e23d444f03697cdc53e1
parentf1274016f284ad3c1d5f9e87db6cbedb86fef18d (diff)
downloadfengari-4da6637af3dca403995b61e47159605fa272b0a9.tar.gz
fengari-4da6637af3dca403995b61e47159605fa272b0a9.tar.bz2
fengari-4da6637af3dca403995b61e47159605fa272b0a9.zip
src/liolib.js: Add io.type
-rw-r--r--README.md2
-rw-r--r--src/liolib.js13
2 files changed, 14 insertions, 1 deletions
diff --git a/README.md b/README.md
index b27dc58..6d94e2e 100644
--- a/README.md
+++ b/README.md
@@ -34,6 +34,7 @@
- [x] `io.stderr`
- [x] `io.stdin`
- [x] `io.stdout`
+ - [x] `io.type()`
- [x] `io.write()`
- [ ] `io.flush()`
- [ ] `io.input()`
@@ -43,7 +44,6 @@
- [ ] `io.popen()`
- [ ] `io.read()`
- [ ] `io.tmpfile()`
- - [ ] `io.type()`
- [ ] `file:flush()`
- [ ] `file:lines()`
- [ ] `file:read()`
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
};