Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An attempt to call JS function from Rust causes panic #1

Closed
katyo opened this issue Nov 11, 2020 · 4 comments
Closed

An attempt to call JS function from Rust causes panic #1

katyo opened this issue Nov 11, 2020 · 4 comments

Comments

@katyo
Copy link
Collaborator

katyo commented Nov 11, 2020

Hi,
This project seems to implement the best of available bindings to QuickJS and best fits to my app.
Great thank you for that awesome crate!

When I try call function, which defined in JS, in Rust and it causes panic.

My Rust code looks like:

    ctx.with(|ctx| -> Result<()> {
        let module = ctx.compile(name, src)?;
        for (key, value) in module.export_list() {
            if key.to_string()? == "default" {
                let func = Function::from_js(ctx, value)?;
                func.call(())?;
            }
        }
        Ok(())
    })?;

And JS:

export default async () => 1;

Full backtrace:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `0`,
 right: `-1`', /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/rquickjs-0.0.5/src/value/rf.rs:32:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/std/src/panicking.rs:475
   1: std::panicking::begin_panic_fmt
             at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/std/src/panicking.rs:429
   2: rquickjs::value::rf::JsRef<Ty>::from_js_value
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/rquickjs-0.0.5/src/value/rf.rs:32
   3: rquickjs::context::ctx::Ctx::globals
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/rquickjs-0.0.5/src/context/ctx.rs:171
   4: rquickjs::value::function::Function::call
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/rquickjs-0.0.5/src/value/function/mod.rs:186
   5: geneshaft::run::{{closure}}::{{closure}}
             at ./src/main.rs:39
   6: rquickjs::context::Context::with
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/rquickjs-0.0.5/src/context/mod.rs:122
   7: geneshaft::run::{{closure}}
             at ./src/main.rs:34
   8: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:79
   9: geneshaft::main::main::{{closure}}
             at ./src/main.rs:11
  10: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:79
  11: geneshaft::main::{{closure}}
             at ./src/main.rs:9
  12: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:79
  13: <async_std::task::builder::SupportTaskLocals<F> as core::future::future::Future>::poll::{{closure}}
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.7.0/src/task/builder.rs:199
  14: async_std::task::task_locals_wrapper::TaskLocalsWrapper::set_current::{{closure}}
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.7.0/src/task/task_locals_wrapper.rs:60
  15: std::thread::local::LocalKey<T>::try_with
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:265
  16: std::thread::local::LocalKey<T>::with
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:241
  17: async_std::task::task_locals_wrapper::TaskLocalsWrapper::set_current
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.7.0/src/task/task_locals_wrapper.rs:55
  18: <async_std::task::builder::SupportTaskLocals<F> as core::future::future::Future>::poll
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.7.0/src/task/builder.rs:197
  19: <futures_lite::future::Or<F1,F2> as core::future::future::Future>::poll
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-lite-1.11.2/src/future.rs:526
  20: async_executor::Executor::run::{{closure}}
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-executor-1.4.0/src/lib.rs:236
  21: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:79
  22: async_executor::LocalExecutor::run::{{closure}}
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-executor-1.4.0/src/lib.rs:437
  23: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:79
  24: async_io::driver::block_on
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-io-1.2.0/src/driver.rs:142
  25: async_global_executor::reactor::block_on::{{closure}}
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-global-executor-1.4.3/src/lib.rs:48
  26: async_global_executor::reactor::block_on
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-global-executor-1.4.3/src/lib.rs:55
  27: async_global_executor::block_on::{{closure}}
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-global-executor-1.4.3/src/lib.rs:205
  28: std::thread::local::LocalKey<T>::try_with
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:265
  29: std::thread::local::LocalKey<T>::with
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:241
  30: async_global_executor::block_on
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-global-executor-1.4.3/src/lib.rs:205
  31: async_std::task::builder::Builder::blocking::{{closure}}::{{closure}}
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.7.0/src/task/builder.rs:171
  32: async_std::task::task_locals_wrapper::TaskLocalsWrapper::set_current::{{closure}}
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.7.0/src/task/task_locals_wrapper.rs:60
  33: std::thread::local::LocalKey<T>::try_with
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:265
  34: std::thread::local::LocalKey<T>::with
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:241
  35: async_std::task::task_locals_wrapper::TaskLocalsWrapper::set_current
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.7.0/src/task/task_locals_wrapper.rs:55
  36: async_std::task::builder::Builder::blocking::{{closure}}
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.7.0/src/task/builder.rs:168
  37: std::thread::local::LocalKey<T>::try_with
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:265
  38: std::thread::local::LocalKey<T>::with
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:241
  39: async_std::task::builder::Builder::blocking
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.7.0/src/task/builder.rs:161
  40: async_std::task::block_on::block_on
             at /home/kayo/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.7.0/src/task/block_on.rs:33
  41: geneshaft::main
             at ./src/main.rs:9
  42: core::ops::function::FnOnce::call_once
             at /home/kayo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
@katyo
Copy link
Collaborator Author

katyo commented Nov 11, 2020

Seems this happens when ctx.globals() requested.

Panic caused when I create context using builder like so:

    let ctx = Context::build(&rt)
        .none()
        .regex(true)
        .json(true)
        .date(true)
        .promises(true)
       .build()?;

This works when context is created by calling Context::full(&rt).

@DelSkayn
Copy link
Owner

Hi @katyo ,
Thanks for your interest!
As you might have noticed I haven't worked on this repository for a while since I had put this project somewhat on hold.
I will have a look at your pull requests, need to re-familiarize myself with the code.

Regarding using this project, what is your use case? Cause currently this library is not well tested and the parallel feature definitively has a race condition if actually used parallel because quickjs uses at least a single global variable which need to be patched. If you use it in parallel it might randomly crash currently.

@DelSkayn
Copy link
Owner

Seems this happens when ctx.globals() requested.

Panic caused when I create context using builder like so:

    let ctx = Context::build(&rt)
        .none()
        .regex(true)
        .json(true)
        .date(true)
        .promises(true)
       .build()?;

This works when context is created by calling Context::full(&rt).

Good catch, seems I forgot to initialize base intricsics objects when using the builder

@DelSkayn
Copy link
Owner

Fixed in 458d13b

katyo pushed a commit that referenced this issue Feb 27, 2023
Co-authored-by: richarddd <richarddd@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants