wasmoon 基于webassembly 的lua 虚拟机

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

posted on   荣锋亮  阅读(217)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-11-30 试用solace 消息平台
2020-11-30 mqtt5 share subscription 简单说明
2019-11-30 testcontainers 方便的db测试框架
2019-11-30 tuned linux 性能调优工具
2019-11-30 streamsets 测试框架说明
2019-11-30 streamsets 源码构建
2018-11-30 hasura graphql-engine集成pgbouncer 连接池工具

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示