From cc3e3c93585d81304954131a9a3982f0786426b7 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 21 May 2017 22:41:17 +1000 Subject: src/liolib.js: Do more argument checking before bailing out of opening a file --- src/liolib.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/liolib.js') diff --git a/src/liolib.js b/src/liolib.js index 51e18ee..a87309a 100644 --- a/src/liolib.js +++ b/src/liolib.js @@ -80,7 +80,14 @@ const getiofile = function(L, findex) { 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")); + let filename = lua.lua_tostring(L, 1); + if (filename) + lauxlib.luaL_error(L, lua.to_luastring("opening files not yet implemented")); + else { + tofile(L); /* check that it's a valid file handle */ + lua.lua_pushvalue(L, 1); + } + lua.lua_setfield(L, lua.LUA_REGISTRYINDEX, f); } /* return current value */ lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, f); -- cgit v1.2.3-54-g00ecf