3 ms·
Thanks for the new method! In fengari we currently use https://github.com/fengari-lua/fengari-interop/blob/8e59efb7e25469ed1d56dcd065361a1481112e9e/src/js.js#L
by daurnimator 7y ago
Thanks for the new method!
In fengari we currently use https://github.com/fengari-lua/fengari-interop/blob/8e59efb7e25469ed1d56dcd065361a1481112e9e/src/js.js#L92-L107 https://github.com/fengari-lua/fengari-interop/blob/8e59efb7...
const global_env = (function() {
/* global WorkerGlobalScope */ /* see https://github.com/sindresorhus/globals/issues/127 */
if (typeof process !== "undefined") {
/* node */
return global;
} else if (typeof window !== "undefined") {
/* browser window */
return window;
} else if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
/* web worker */
return self;
} else {
/* unknown global env */
return (0, eval)('this'); /* use non-strict mode to get global env */
}
})();
I created https://github.com/fengari-lua/fengari-interop/issues/45 https://github.com/fengari-lua/fengari-interop/issues/45 to track.
- bfred_it 7y agoeval doesn’t work with some CSP settings, the article mentions this and other limits of previous solutions.
- codedokode 7y agoYou use "global_env" only inside luaopen_js(). Why don't you just add it as an argument, for example: function luaopen_js(L, global) { ... } instead of writing this ugly hack?
- daurnimator 7y ago`luaopen_js` is the entry point for the library with a set API. (that follows https://www.lua.org/manual/5.3/manual.html#lua_CFunction https://www.lua.org/manual/5.3/manual.html#lua_CFunction ). The caller cannot pass more arguments.
- Technetium_Hat 7y agoWhy the comma operator with the eval?
- daurnimator 7y agoHave a look at https://stackoverflow.com/a/46397327/282536 https://stackoverflow.com/a/46397327/282536