0

Im using the lua.vm.js javascript library in order to get some Lua code to execute in JavaScript.

The code works fine(its a very simple Lua example) but I cant get any variables declared in Lua into my JavaScript context.

The REPL from the project allows me to test the following Lua code:

local key = { k1 = value}
local data = {
[key] = "something",
a = { b = 3 },
}
local v1 = data [key]
local v2 = data.a.b
print(v1)
print(v2)

In the output window, I can see that v1 and v2 are being printed and with the correct value, but I cant find a way to assign the value from data, for example, into a JS variable.

A little more simple: Get the values from the key and data into javascript variables after executing the Lua code.

Is that possible?

I'm reading the code from lua.vm.js but I cant find that guides me in the right direction.

Thanks in advance!

2 Answers 2

0

X-posted from https://github.com/kripken/lua.vm.js/issues/18

Use my branch/fork; repl can be found at https://daurnimator.github.io/lua.vm.js/repl.html

When a Lua variable is exposed to javascript; it is exposed as a function object; with methods invoke/get/set/etc. See https://github.com/daurnimator/lua.vm.js/blob/master/lua.js#L523

Using your example; expose the object to javascript somehow. e.g. in lua:

window.mydata = data
window.mykey = key

Then in javascript:

v1 = window.mydata.get(window.mykey)
v2 = window.mydata.get("a").get("b")
console.log(v1)
console.log(v2)
Sign up to request clarification or add additional context in comments.

2 Comments

That did it! I had to use your branch because it doesn't work in the original
Could you please add example of invoke?
0
<script src="./lua.vm.js"></script>

<script type="text/lua">
  print(js.global:myShow("test"))
</script>

<script>
myShow = function show(value)
{
    return value + " me"
}
</script>

1 Comment

Kindly provide an explanation of your code. stackoverflow.com/help/how-to-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.