summaryrefslogtreecommitdiff
path: root/src/loadlib.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-12-28 17:38:12 +1100
committerdaurnimator <quae@daurnimator.com>2017-12-28 17:44:24 +1100
commit2fb328badf87294a412c0430772461644a080b8c (patch)
tree2d955dbc16a802f9daa986cbc98f8f3c53c9f98a /src/loadlib.js
parente0e5d05626fcd6cb4622abcaae9fc951d9ae91b1 (diff)
downloadfengari-2fb328badf87294a412c0430772461644a080b8c.tar.gz
fengari-2fb328badf87294a412c0430772461644a080b8c.tar.bz2
fengari-2fb328badf87294a412c0430772461644a080b8c.zip
src/loadlib.js: Use non-strict mode to find global env in unknown environments
Diffstat (limited to 'src/loadlib.js')
-rw-r--r--src/loadlib.js29
1 files changed, 22 insertions, 7 deletions
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