From 9a24308b0f8bb04d533d9abae39bd7546cf92377 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 26 Apr 2017 23:30:30 +1000 Subject: src/iolib.js: Add file:__tostring() --- README.md | 2 +- src/liolib.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 56f3496..02110dd 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ - [ ] `file:setvbuf()` - [ ] `file:write()` - [ ] `file:__gc()` - - [ ] `file:__tostring()` + - [x] `file:__tostring()` - [ ] C API - [x] ... - [ ] lua_arith diff --git a/src/liolib.js b/src/liolib.js index 247baef..f753f91 100644 --- a/src/liolib.js +++ b/src/liolib.js @@ -12,6 +12,19 @@ const tolstream = function(L) { return lauxlib.luaL_checkudata(L, 1, lauxlib.LUA_FILEHANDLE); }; +const isclosed = function(p) { + return p.closef === null; +}; + +const f_tostring = function(L) { + let p = tolstream(L); + if (isclosed(p)) + lua.lua_pushliteral(L, "file (closed)"); + else + lua.lua_pushstring(L, lua.to_luastring(`file (${p.f.toString()})`)); + return 1; +}; + const newprefile = function(L) { let p = lua.lua_newuserdata(L); p.f = null; @@ -24,6 +37,7 @@ const iolib = { }; const flib = { + "__tostring": f_tostring }; const createmeta = function(L) { -- cgit v1.2.3-70-g09d2