ceifa / wasmoon

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

How to execute a coroutine from javascript #64

Closed jude90 closed 1 year ago

jude90 commented 1 year ago

I wrote a lua function with yield , then create a coroutine in lua . Now I want to execute it step by step (yield by yield) in javascript , how should I do this job ?

fermuch commented 1 year ago

I think I just found the way. I am still unsure if there is any problem with this approach, just wanted to share with you:

await lua.doString(`
  co = coroutine.create(function ()
    print("hi")
  end)
`)
const co = lua.global.get('co');
lua.global.get('coroutine').resume(co)
ceifa commented 1 year ago

You don't need to get entire coroutine object, since wasmoon already converts a coroutine to a thread object:

await lua.doString(`
  co = coroutine.create(function ()
    print("hi")
  end)
`)
const co = lua.global.get('co');
const { result, resultCount } = co.resume(0)