second-state / wasmedge-quickjs

A high-performance, secure, extensible, and OCI-complaint JavaScript runtime for WasmEdge.
Apache License 2.0
477 stars 59 forks source link

How to progress promises #124

Open lastmjs opened 7 months ago

lastmjs commented 7 months ago

I see that promise_loop_poll is deprecated, though the examples show its usage. I am running into problems with promises, I don't see how to get them to progress. I have wasmedge-quickjs embedded in my own Rust program compiled to Wasm.

Any help would be appreciated, thank you! And it would be great for those examples to show the up-to-date usage.

lastmjs commented 7 months ago

From the discord I have learned that promise_loop_poll was removed in lieu of a Future integration. While that's nice for some use cases, this is making it difficult for my use case where I have a custom Futures implementation and use RefCell to access a global runtime. If I could just poll the promises manually I believe I would be able to proceed easily, but because I can't I have to somehow figure out how to get an async closure working, but using RefCells we generally must get everything to work in a non-async closure.

It would be wonderful to be able to poll manually again.

lastmjs commented 7 months ago

I went back in time to find the implementation body of promise_loop_poll and added it into my fork of wasmedge-quickjs, so far it does indeed work!

    #[deprecated]
    pub fn promise_loop_poll(&mut self) {
        unsafe {
            let rt = self.rt();
            let mut pctx: *mut JSContext = 0 as *mut JSContext;

            loop {
                let err = JS_ExecutePendingJob(rt, (&mut pctx) as *mut *mut JSContext);
                if err <= 0 {
                    if err < 0 {
                        js_std_dump_error(pctx);
                    }
                    break;
                }
            }
        }
    }

I ask for this method to be put back and not removed.

L-jasmine commented 7 months ago

Ok, I will restore it.