ceifa / wasmoon

A real lua 5.4 VM with JS bindings made with webassembly
MIT License
495 stars 32 forks source link

How to execute wasmoon with lua module? #124

Closed JNY0606 closed 1 week ago

JNY0606 commented 1 month ago

-- test.lua --

require 'a'

function getAA()
    local aa = aa()

    return aa
end

-- a.lua --

function aa()
    return 'aa'
end

-- test.js --

import { LuaFactory } from 'wasmoon'
import fs from 'fs'

async function main() {
    const factory = new LuaFactory()
    const lua = await factory.createEngine()

    try {
        const lua_str = fs.readFileSync('test.lua', 'utf-8')
        await lua.doString(lua_str)

        const getAA = lua.global.get('getAA')
        console.log(getAA())

    } finally {
        lua.global.close()
    }
}
main()

-- error report -- Uncaught Error Error: [string "require 'a'..."]:1: module 'a' not found

ceifa commented 1 week ago

Wasmoon uses a sandboxed file system. For this to work you have to mount the file into that FS.