From 2fb328badf87294a412c0430772461644a080b8c Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 28 Dec 2017 17:38:12 +1100 Subject: src/loadlib.js: Use non-strict mode to find global env in unknown environments --- src/loadlib.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'src/loadlib.js') diff --git a/src/loadlib.js b/src/loadlib.js index 9748732..eb382c1 100644 --- a/src/loadlib.js +++ b/src/loadlib.js @@ -4,6 +4,19 @@ const fengari = require('./fengari.js'); const lua = require('./lua.js'); const lauxlib = require('./lauxlib.js'); +const global_env = (function() { + if (typeof process !== "undefined") { + /* node */ + return global; + } else if (typeof window !== "undefined") { + /* browser window */ + return window; + } else { + /* unknown global env */ + return eval('this'); /* use non-strict mode to get global env */ + } +})(); + const CLIBS = lua.to_luastring("__CLIBS__", true); const LUA_PATH_VAR = "LUA_PATH"; const LUA_CPATH_VAR = "LUA_CPATH"; @@ -63,7 +76,7 @@ if (typeof process === "undefined") { if (typeof res === "function" || (typeof res === "object" && res !== null)) { return res; } else if (res === void 0) { /* assume library added symbols to global environment */ - return window; + return global_env; } else { lua.lua_pushstring(L, lua.to_luastring(`library returned unexpected type (${typeof res})`)); return null; @@ -186,12 +199,14 @@ const ll_loadlib = function(L) { } }; -let env; -if (typeof process === "undefined") { - env = window; -} else { - env = process.env; -} +const env = (function() { + if (typeof process !== "undefined") { + /* node */ + return process.env; + } else { + return global_env; + } +})(); /* ** Set a path -- cgit v1.2.3-54-g00ecf