Closed gianpiero closed 8 years ago
It's not easy to get a reference to the global environment.
Options:
global
: node.js specific(function() { return this }).call(null)
: relies on sloppy mode(Function('return this'))()
: relies on eval-like behaviour, which is disallowed in places like chrome extensionslua object "js" is initialized in the functions Lua.init() and Lua.State().
I fixed the problem in "lua.js" as below. (Is this the correct way? I'm not sure)
this.gc(Lua.defines.GC.RESTART, 0);
+this.execute("dofile'js.lua'");
-})(typeof module !== 'undefined' ? module.exports : this, this, Module);
+})(typeof module !=='undefined' ? module.exports : this, ENVIRONMENT_IS_NODE? global : this, Module);
I confirmed the code below works fine.
var lua = require('lua.vm.js');
var l = new lua.Lua.State();
l.execute('for k, v in js.pairs(js.global) do print(k, v) end');
My environment is Ubuntu 14.04 x64, node v0.12.7
I fixed the problem in "lua.js" as below. (Is this the correct way? I'm not sure)
Move this.execute("dofile'js.lua'") from Lua.init() to Lua.State() this.gc(Lua.defines.GC.RESTART, 0); +this.execute("dofile'js.lua'");
You shouldn't do that; sometimes you want to create states without the javascript compat stuff loaded. Leaving it in Lua.init
should be fine....
- Change the last line -})(typeof module !== 'undefined' ? module.exports : this, this, Module); +})(typeof module !=='undefined' ? module.exports : this, ENVIRONMENT_IS_NODE? global : this, Module);
Yes, this is the first option I mentioned above (https://github.com/kripken/lua.vm.js/issues/31#issuecomment-127145403)
Is anyone more involved in the javascript community able to weigh in here? I'm leaning towards option 2. (which assumes sloppy mode).
Can we get @th-yoo's fixes into a separate node.js release? This fixes all the issues I've been experiencing and makes it usable from my standpoint. (I also have been unable to build this project, but that's mostly my lack of time put into it I'm sure)
js.global works fine if used into the browser, but if you try to access it from a node.js application it generate an exception.
To reproduce the error, create a
test.js
file:and execute it by running:
you will get the following exception:
or, for test2:
Issue https://github.com/kripken/lua.vm.js/issues/30 started the discussion about this issue