wasmoon 是基于webassembly 开发的lua 虚拟机
包含的特性
- 可以嵌入到node,deno,web app
- 运行lua 在如何操作系统中
- js 与lua的交互不用担心内存泄漏
参考使用
- app.js
const { LuaFactory } = require('wasmoon')
async function main() {
// Initialize a new lua environment factory
// You can pass the wasm location as the first argument, useful if you are using wasmoon on a web environment and want to host the file by yourself
const factory = new LuaFactory()
// Create a standalone lua environment from the factory
const lua = await factory.createEngine()
// 基于虚拟文件系统的文件操作
await factory.mountFile('app.lua',`
local app = {
name = "app",
age = 333
}
local function demo()
return app
end
return {
demo = demo
}
`)
try {
// Set a JS function to be a global lua function
lua.global.set('sum', (x, y) => x + y)
// Run a lua string
await lua.doString(`
print(sum(10, 10))
function multiply(x, y)
return x * y
end
`);
await lua.doFile('app.lua')
// Get a global lua function as a JS function
const multiply = lua.global.get('multiply')
console.log(multiply(10, 10))
// 加载模块
const value = await lua.doString(`local appmy = require('app'); return appmy;`)
// 调用模块的方法
console.log(value.demo())
} finally {
// Close the lua environment, so it can be freed
lua.global.close()
}
}
main().catch(console.error)说明
wasmoon 是一个很不错的项目,同时性能也很不错,似乎mac 版的qq 也使用了此包
参考资料
https://github.com/ceifa/wasmoon
标签:function,webassembly,const,虚拟机,global,lua,app,wasmoon From: https://www.cnblogs.com/rongfengliang/p/17866612.html