From 0422224def06db85f16d24a73b71b8d75e6f74a4 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 16 May 2017 17:42:11 +1000 Subject: src/liolib.js: Partial implementation of io.input and io.output --- README.md | 4 ++-- src/liolib.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4b78239..75959f2 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,10 @@ - [x] `io.type()` - [x] `io.write()` - [x] `io.flush()` - - [ ] `io.input()` + - [ ] `io.input()`: partially implemented - [ ] `io.lines()` - [ ] `io.open()` - - [ ] `io.output()` + - [ ] `io.output()`: partially implemented - [ ] `io.popen()` - [ ] `io.read()` - [ ] `io.tmpfile()` diff --git a/src/liolib.js b/src/liolib.js index 6c7d945..51e18ee 100644 --- a/src/liolib.js +++ b/src/liolib.js @@ -78,6 +78,23 @@ const getiofile = function(L, findex) { return p.f; }; +const g_iofile = function(L, f, mode) { + if (!lua.lua_isnoneornil(L, 1)) { + lauxlib.luaL_error(L, lua.to_luastring("opening files not yet implemented")); + } + /* return current value */ + lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, f); + return 1; +}; + +const io_input = function(L) { + return g_iofile(L, IO_INPUT, "r"); +}; + +const io_output = function(L) { + return g_iofile(L, IO_OUTPUT, "w"); +}; + const g_write = function(L, f, arg) { let nargs = lua.lua_gettop(L) - arg; let status = true; @@ -120,6 +137,8 @@ const f_flush = function (L) { const iolib = { "close": io_close, "flush": io_flush, + "input": io_input, + "output": io_output, "type": io_type, "write": io_write }; -- cgit v1.2.3-70-g09d2