使用lua.vm.js从Javascript读取Lua变量 [英] Reading Lua variables from Javascript using lua.vm.js

查看:642
本文介绍了使用lua.vm.js从Javascript读取Lua变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 lua.vm.js javascript库以获取一些lua要在javascript中执行的代码. 该代码可以正常工作(它是一个非常简单的lua示例),但是我无法将Lua中声明的任何变量放入我的JavaScript上下文中.

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.

项目中的 REPL 允许我测试以下Lua代码:

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)

在输出窗口中,我看到正在打印v1和v2并具有正确的值,但是我找不到从数据分配值的方法,例如,将其分配给js变量.

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.

更简单:执行Lua代码后,将键和数据中的值转换为javascript变量.

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

有可能吗?

我正在从lua.vm.js中读取代码,但找不到正确的方向.

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

提前谢谢!

推荐答案

X发布于使用我的分支/叉子;可以在 https://daurnimator.github.io/lua.vm中找到repl. js/repl.html

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

当Lua变量暴露给javascript时;它作为一个函数对象公开;使用方法invoke/get/set/etc.参见 https://github.com/daurnimator/lua. vm.js/blob/master/lua.js#L523

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

使用您的示例;以某种方式将对象暴露给javascript.例如在lua中:

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

window.mydata = data
window.mykey = key

然后使用javascript:

Then in javascript:

v1 = window.mydata.get(window.mykey)
v2 = window.mydata.get("a").get("b")
console.log(v1)
console.log(v2)

这篇关于使用lua.vm.js从Javascript读取Lua变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆