DelSkayn / rquickjs

High level bindings to the quickjs javascript engine
MIT License
434 stars 59 forks source link

Await in javascript throws error #235

Closed azam closed 8 months ago

azam commented 8 months ago

This code throws an Err with message "Exception generated by quickjs". Is this code wrong, or is there a limitation of some sort? Tested on rquickjs = { version = "0.3.1", features = ["full-async"] } with async-std and tokio

let rt = AsyncRuntime::new().unwrap();
let ctx = AsyncContext::full(&rt).await.unwrap();
async_with!(ctx => |ctx| {
    // Expecting Ok(69)
    let val = ctx.eval::<u64, _>("await (async () => 69)()"); // or just `await 69`
    match val {
        Ok(v) => eprintln!("Ok: {}", v),
        Err(v) => eprintln!("Err: {}", v), // Err: Exception generated by quickjs
    };
})
.await;
DelSkayn commented 8 months ago

QuickJS doesn't support top level await, so it generates an error for await (async() => 69).

This error isn't displayed because of some design choices we had to make in the library. If you want to see what the specific error is that is raise you need to first retrieve it from the context. If you use CatchResultExt::catch you can retrieve the error from the context and you will see that QuickJS returns an error.

azam commented 8 months ago

Got it. Thank you for the explanation! For others that may encounter this, my error was: Exception generated by quickjs: 'await' is not defined

zamfofex commented 4 months ago

Hello, I arrived at this repository from LLRT!

In case you want to add support for top level await, I wanted to say that I have made a patch that I submitted to the QuickJS mailing list about a year ago.

The patch was eventually added to txiki.js, and I’ve also just uploaded my original patch to sr.ht.

notes on the future of QuickJS Also, QuickJS seems to be mostly abandoned by its main developers currently, but there seems to be efforts to revive the project by the developer(s) of txiki.js, called [“QuickJS‐ng”][QuickJS-ng]. Maybe it would make sense to keep future work on QuickJS in a single repository, to avoid accidental duplication of efforts. (And I think it would be better to choose an option that seems to be willing to accept changes and updates, rather than upstream QuickJS.) [QuickJS-ng]: