daurnimator / lua.vm.js

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

One argument not being passed. #39

Closed jaredallard closed 8 years ago

jaredallard commented 8 years ago

Observe:

script

  inject_js(this.l, function() {
    console.log(arguments);
  }, "test_args")

inject_js

function inject_js(state, code, global) {
  state.pushjs(code);
  state.setglobal(global);
  state.pop(0); // may or may not be needed.
}

lua

test_args("hello", "world")

output:

{ '0': 'world' }

Hope that sums this up!

daurnimator commented 8 years ago

the first argument to a javascript function appears as this. Try instead:

  inject_js(this.l, function() { "use strict";
    console.log(this, arguments);
  }, "test_args")

Otherwise; pop(0) does nothing: it removes 0 items from the lua stack :P

jaredallard commented 8 years ago

Thank you! On another question, how would I convert a JS array to a Lua table?

EDIT

Also, how would I pass two return values from js?

daurnimator commented 8 years ago

Thank you! On another question, how would I convert a JS array to a Lua table?

See https://github.com/kripken/lua.vm.js/issues/22