DelSkayn / rquickjs

High level bindings to the quickjs javascript engine
MIT License
504 stars 63 forks source link

Panic when throwing non-object exception in javascript #140

Closed holly-hacker closed 1 year ago

holly-hacker commented 1 year ago

Minimal sample to reproduce, uses rquickjs v0.1.7:

use rquickjs::{Context, Module, Runtime};

fn main() {
    let script_source = "throw `test`";

    // initialize a js runtime
    let runtime = Runtime::new().expect("create quickjs runtime");
    let context = Context::full(&runtime).expect("create quickjs context");

    // load module
    context.with(|ctx| {
        let module = Module::new(ctx, "<startup_module>", script_source).expect("create module");
        _ = module.eval();
    });
}

If this code is run, rquickjs panics because Error::from_js calls JS_IsError internally and returns an error which is unwrapped in get_exception in result.rs.

D:\Projects\Rust\rquickjs-repro> cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target\debug\rquickjs-repro.exe`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromJs { from: "string", to: "object", message: None }', C:\Users\HoLLy\.cargo\registry\src\index.crates.io-6f17d22bba15001f\rquickjs-core-0.1.7\src\result.rs:480:36
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\std\src\panicking.rs:579
   1: core::panicking::panic_fmt
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\core\src\panicking.rs:64
   2: core::result::unwrap_failed
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\core\src\result.rs:1750
   3: enum2$<core::result::Result<enum2$<rquickjs_core::result::Error>,enum2$<rquickjs_core::result::Error> > >::unwrap<enum2$<rquickjs_core::result::Error>,enum2$<rquickjs_core::result::Error> >
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc\library\core\src\result.rs:1090
   4: rquickjs_core::result::get_exception
             at C:\Users\HoLLy\.cargo\registry\src\index.crates.io-6f17d22bba15001f\rquickjs-core-0.1.7\src\result.rs:480
   5: rquickjs_core::result::handle_exception
             at C:\Users\HoLLy\.cargo\registry\src\index.crates.io-6f17d22bba15001f\rquickjs-core-0.1.7\src\result.rs:468
   6: rquickjs_core::value::module::Module<rquickjs_core::value::module::Loaded<rquickjs_core::value::module::Script> >::eval<rquickjs_core::value::module::Script>
             at C:\Users\HoLLy\.cargo\registry\src\index.crates.io-6f17d22bba15001f\rquickjs-core-0.1.7\src\value\module.rs:379
   7: rquickjs_repro::main::closure$0
             at .\src\main.rs:13
   8: rquickjs_core::context::Context::with<rquickjs_repro::main::closure_env$0,tuple$<> >
             at C:\Users\HoLLy\.cargo\registry\src\index.crates.io-6f17d22bba15001f\rquickjs-core-0.1.7\src\context.rs:131
   9: rquickjs_repro::main
             at .\src\main.rs:11
  10: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc\library\core\src\ops\function.rs:250
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\rquickjs-repro.exe` (exit code: 101)
holly-hacker commented 1 year ago

It looks like this is already tracked in #138.