aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-05-03 17:52:42 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-03 18:28:28 +1000
commit134f6bd221ad8880cb1b8514bb84e3b41d2b0d61 (patch)
tree0d7367198c6cfa8193db0c4161d3b48c93430636
parent5525f63f02352a3347911d0d179657983ea5171a (diff)
downloadfengari-134f6bd221ad8880cb1b8514bb84e3b41d2b0d61.tar.gz
fengari-134f6bd221ad8880cb1b8514bb84e3b41d2b0d61.tar.bz2
fengari-134f6bd221ad8880cb1b8514bb84e3b41d2b0d61.zip
src/liolib.js: add io.close()
-rw-r--r--README.md2
-rw-r--r--src/liolib.js16
2 files changed, 17 insertions, 1 deletions
diff --git a/README.md b/README.md
index d62d8d4..2c6cbba 100644
--- a/README.md
+++ b/README.md
@@ -38,7 +38,7 @@
- [ ] `io.tmpfile()`
- [ ] `io.type()`
- [x] `io.write()`
- - [ ] `io.close()`
+ - [x] `io.close()`
- [ ] `file:flush()`
- [ ] `file:lines()`
- [ ] `file:read()`
diff --git a/src/liolib.js b/src/liolib.js
index 9940583..2136755 100644
--- a/src/liolib.js
+++ b/src/liolib.js
@@ -44,6 +44,20 @@ const newprefile = function(L) {
return p;
};
+const aux_close = function(L) {
+ let p = tolstream(L);
+ let cf = p.closef;
+ p.closef = null;
+ return cf(L);
+};
+
+const io_close = function(L) {
+ if (lua.lua_isnone(L, 1)) /* no argument? */
+ lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
+ tofile(L); /* make sure argument is an open stream */
+ return aux_close(L);
+};
+
const getiofile = function(L, findex) {
lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, findex);
let p = lua.lua_touserdata(L, -1);
@@ -80,10 +94,12 @@ const f_write = function(L) {
};
const iolib = {
+ "close": io_close,
"write": io_write
};
const flib = {
+ "close": io_close,
"write": f_write,
"__tostring": f_tostring
};