fengari-lua / fengari-interop

Fengari <=> JS Interop
MIT License
98 stars 13 forks source link

Passing arguments from JS to Lua functions #9

Closed cristiano-belloni closed 7 years ago

cristiano-belloni commented 7 years ago

So if I have this code in lua:

js = require "js"
function foo(x)
    print(x)
  end
js.global.foo = foo

And I call global.foo in my js script with an argument:

global.foo(88)

print(x) outputs [object global], which is the same object as js.global (printing the key/value pairs in x confirms that).

Is there a way to pass args from js to Lua with interop?

daurnimator commented 7 years ago

print(x) outputs [object global], which is the same object as js.global (printing the key/value pairs in x confirms that).

The first argument passed by JS is this. Try:

function foo(this, x)
    print(x)
end
giann commented 7 years ago

@cristiano-belloni consider joining #fengari on freenode for those quick questions

cristiano-belloni commented 7 years ago

@daurnimator Thanks! @giann Will do.