ceifa / wasmoon

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

Support async functions throughout [WIP] #16

Closed timstableford closed 3 years ago

timstableford commented 3 years ago

This takes on board your feedback about doString and makes it so functions returned from Lua can call async functions and generally makes it possible for any function call to yield and return the results on the caller thread.

ceifa commented 3 years ago

I think I didn't quite understand the problem you solved, I tried to handle promises inside lua and still don't get it, and things like pass a function back doesn't work anymore:

engine.global.set('asyncCallback', async (input) => {
    return Promise.resolve(input * 2)
})

const [asyncCallback] = await engine.doString(`
    return asyncCallback
`)

console.log(await asyncCallback(10)) // []
timstableford commented 3 years ago

Yeah, that is definitely a bug. The purpose is that if you had something like:

engine.global.set('sleep', (input) => {
    return new Promise(resolve => setTimeout(resolve, input))
})

const [asyncMultiplier] = await engine.doString(`
    return function (val)
        sleep(1)
        return val * 2
    end
`)

await asyncMultiplier(5)

So it will be possible to yield from within callbacks.

timstableford commented 3 years ago

In attempting to integrate it with your then/catch/await syntax I've found there's a lot that will have to change from this PR. I'll keep going out of interest and see where this ends up but yours may well end up better

timstableford commented 3 years ago

Closing in favour of https://github.com/ceifa/wasmoon/pull/15