aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-04-27 03:02:41 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-03 18:28:26 +1000
commit5525f63f02352a3347911d0d179657983ea5171a (patch)
tree503c19c11cb27858f4e5332adfcf859c3e9a4157
parent290ddad546117d6b55639faa7ad943d96f8e224c (diff)
downloadfengari-5525f63f02352a3347911d0d179657983ea5171a.tar.gz
fengari-5525f63f02352a3347911d0d179657983ea5171a.tar.bz2
fengari-5525f63f02352a3347911d0d179657983ea5171a.zip
src/liolib: Add file:write()
-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 804136a..d62d8d4 100644
--- a/README.md
+++ b/README.md
@@ -44,7 +44,7 @@
- [ ] `file:read()`
- [ ] `file:read()`
- [ ] `file:setvbuf()`
- - [ ] `file:write()`
+ - [x] `file:write()`
- [ ] `file:__gc()`
- [x] `file:__tostring()`
- [ ] C API
diff --git a/src/liolib.js b/src/liolib.js
index f581323..9940583 100644
--- a/src/liolib.js
+++ b/src/liolib.js
@@ -1,5 +1,6 @@
"use strict";
+const assert = require('assert');
const fs = require('fs');
const lua = require('./lua.js');
@@ -27,6 +28,14 @@ const f_tostring = function(L) {
return 1;
};
+const tofile = function(L) {
+ let p = tolstream(L);
+ if (isclosed(p))
+ lauxlib.luaL_error(L, "attempt to use a closed file");
+ assert(p.f);
+ return p.f;
+};
+
const newprefile = function(L) {
let p = lua.lua_newuserdata(L);
p.f = null;
@@ -64,11 +73,18 @@ const io_write = function(L) {
return g_write(L, getiofile(L, IO_OUTPUT), 1);
};
+const f_write = function(L) {
+ let f = tofile(L);
+ lua.lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
+ return g_write(L, f, 2);
+};
+
const iolib = {
"write": io_write
};
const flib = {
+ "write": f_write,
"__tostring": f_tostring
};