From 0447737eaa41565c8ffddbe3ec5650ce148c4be8 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 4 May 2017 11:21:40 +1000 Subject: package library doesn't need to be for just node --- src/loadlib.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'src/loadlib.js') diff --git a/src/loadlib.js b/src/loadlib.js index c22fee5..15b835d 100644 --- a/src/loadlib.js +++ b/src/loadlib.js @@ -3,9 +3,6 @@ const lua = require('./lua.js'); const lauxlib = require('./lauxlib.js'); -const fs = require('fs'); - - const LUA_IGMARK = ["-".charCodeAt(0)]; const CLIBS = lua.to_luastring("__CLIBS__", true); @@ -78,19 +75,34 @@ const noenv = function(L) { return b; }; -const readable = function(filename) { - let fd = false; +let readable = function(filename) { + return false; +}; +// Only with Node +if (typeof require === "function") { + let fs = false; try { - fd = fs.openSync(lua.to_jsstring(filename), 'r'); - } catch (e) { - return false; - } + fs = require('fs'); + } catch (e) {} - fs.closeSync(fd); + if (fs) { + readable = function(filename) { + let fd = false; + + try { + fd = fs.openSync(lua.to_jsstring(filename), 'r'); + } catch (e) { + return false; + } + + fs.closeSync(fd); + + return true; + }; + } +} - return true; -}; /* error codes for 'lookforfunc' */ const ERRLIB = 1; -- cgit v1.2.3-54-g00ecf