daurnimator / lua.vm.js

The project is superceded by Fengari. See https://fengari.io/
MIT License
836 stars 101 forks source link

js.global is not initialized correctly when used within node.js #31

Closed gianpiero closed 8 years ago

gianpiero commented 9 years ago

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:

function executeLua(L,code) {
  try {
    L.execute(code);
  } catch(e) {
    console.log(e.toString());
  }
}

function test1(){
  var LuaVM = require('lua.vm.js');
  var L = new LuaVM.Lua.State();
  var theCode =
  "print(js.global:eval('[0,1,2,3,4,5][3]'))"
  var result = executeLua(L,theCode)
}

function test2(){
  var LuaVM = require('lua.vm.js');
  var L = new LuaVM.Lua.State();
  var theCode =
  "print(js.global.console:log('hello'))"
  var result = executeLua(L,theCode)
}

test1()
//test2()

and execute it by running:

node test.js

you will get the following exception:

Lua.Error: [string "?"]:1: attempt to call method 'eval' (a nil value)

or, for test2:

Lua.Error: [string "?"]:1: attempt to index field 'console' (a nil value)

Issue https://github.com/kripken/lua.vm.js/issues/30 started the discussion about this issue

daurnimator commented 9 years ago

It's not easy to get a reference to the global environment.

Options:

th-yoo commented 9 years ago

lua 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)

  1. Move this.execute("dofile'js.lua'") from Lua.init() to Lua.State()
this.gc(Lua.defines.GC.RESTART, 0);
+this.execute("dofile'js.lua'");
  1. Change the last line
-})(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

daurnimator commented 9 years ago

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....

  1. 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)

daurnimator commented 9 years ago

Is anyone more involved in the javascript community able to weigh in here? I'm leaning towards option 2. (which assumes sloppy mode).

jaredallard commented 8 years ago

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)