Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ jobs:
components: clippy
- name: Cargo clippy
run: cargo clippy --all --all-targets --features full-async,bindgen
- name: Check no_std feature sets
run: |
cargo check -p rquickjs --no-default-features
cargo check -p rquickjs --no-default-features --features full-nostd

msrv:
# Check to see if rquickjs builds on minimal supported Rust version.
Expand Down
22 changes: 20 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
edition = "2021"
rust-version = "1.87"
license = "MIT"
readme = "README.md"

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / doc

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / check

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-latest, nightly, wasm32-wasip2, full-async-wasi)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-latest, nightly, wasm32-wasip2, full-async-wasi)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-latest, nightly, wasm32-wasip2, full-async-wasi)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (channels, ubuntu-latest, nightly, x86_64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (channels, ubuntu-latest, nightly, x86_64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (channels, ubuntu-latest, nightly, x86_64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-musl, full-async)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-musl, full-async)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-musl, full-async)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 8 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred
description = "High level bindings to the QuickJS JavaScript engine"
keywords = ["quickjs", "ecmascript", "javascript", "es6", "es2020"]
categories = ["api-bindings"]
Expand All @@ -13,6 +13,7 @@


[workspace]
exclude = ["examples/zephyr"]
members = [
"sys",
"core",
Expand All @@ -33,8 +34,8 @@
rquickjs = { version = "0.11.0", path = "./", default-features = false }

[dependencies]
indexmap-rs = { package = "indexmap", version = "2", optional = true }
either-rs = { package = "either", version = "1", optional = true }
indexmap-rs = { package = "indexmap", version = "2", optional = true, default-features = false }
either-rs = { package = "either", version = "1", optional = true, default-features = false }
rquickjs-core = { workspace = true }
rquickjs-macro = { workspace = true, optional = true }

Expand All @@ -60,6 +61,23 @@
# A version of full designed for wasm32-unknown-unknown and similar hosts
full-wasm = ["full-wasi"]

# Portable feature set for no_std hosts with a global allocator.
full-nostd = [
"chrono",
"loader",
"either",
"indexmap",
"macro",
"phf",
"half",
"futures",
"parallel",
"rust-alloc",
]

# Build QuickJS against Zephyr's C headers and configuration.
zephyr = ["full-nostd", "bindgen", "rquickjs-core/zephyr"]

# Almost all features excluding "parallel"
full-async = ["full", "futures"]

Expand Down Expand Up @@ -88,7 +106,7 @@
# otherwise bundled bindings will be used
bindgen = ["rquickjs-core/bindgen", "rquickjs-macro?/bindgen"]

# Enable support of parallel execution

Check warning on line 109 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

unused workspace dependency `rquickjs`

Check warning on line 109 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

unused workspace dependency `rquickjs`

Check warning on line 109 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

unused workspace dependency `rquickjs`

Check warning on line 109 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

unused workspace dependency `rquickjs`

Check warning on line 109 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

unused workspace dependency `rquickjs`

Check warning on line 109 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

unused workspace dependency `rquickjs`

Check warning on line 109 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (channels, ubuntu-latest, nightly, x86_64-unknown-linux-gnu, full-async)

unused workspace dependency `rquickjs`

Check warning on line 109 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-musl, full-async)

unused workspace dependency `rquickjs`

Check warning on line 109 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-gnu, full-async)

unused workspace dependency `rquickjs`
parallel = ["rquickjs-core/parallel"]

# Enable user-defined module loader support
Expand All @@ -103,7 +121,7 @@


# Enable helper macros
macro = ["rquickjs-macro"]

Check warning on line 124 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

unused workspace dependency `rquickjs-sys`

Check warning on line 124 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

unused workspace dependency `rquickjs-sys`

Check warning on line 124 in Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

unused workspace dependency `rquickjs-sys`

Check warning on line 124 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (channels, ubuntu-latest, nightly, x86_64-unknown-linux-gnu, full-async)

unused workspace dependency `rquickjs-sys`

Check warning on line 124 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-musl, full-async)

unused workspace dependency `rquickjs-sys`

Check warning on line 124 in Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-gnu, full-async)

unused workspace dependency `rquickjs-sys`

# Allows transferring objects between different contexts of the same runtime.
# Disabled for now as it can be used to create unsound code.
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,35 @@ See below for a list of supported platforms.
| wasm32-wasip1 | ✅ | ✅ | ✅ |
| wasm32-wasip2 | ✅ | ✅ | ✅ |
| wasm32-unknown-unknown | ✅ | ✅* | ✅ |
| thumbv7m-none-eabi (Zephyr) | ❌ | ✅** | ✅ |
| other | ❌ | ❌ | Unknown |

\* `wasm32-unknown-unknown` is validated with workspace builds and a dedicated smoke example compile. Runtime embedding still depends on the host providing the generated wasm imports.

\** Zephyr is validated on the `mps2/an385` QEMU board. Bindings are generated during the Zephyr build.

## `no_std` and Zephyr

Disable default features to use rquickjs with `no_std + alloc`. The `full-nostd` feature enables the
portable high-level APIs, including async/futures support, macros, in-memory module loaders,
`parallel`, and the Rust global allocator. The default feature set continues to use `std`.

```toml
rquickjs = { version = "0.11", default-features = false, features = ["full-nostd"] }
```

For Zephyr, use the `zephyr` feature. It also enables bindgen and the C integration required to use
Zephyr's generated configuration, libc, threading primitives, and compiler toolchain.

```toml
rquickjs = { path = "../rquickjs", default-features = false, features = ["zephyr"] }
```

The [Zephyr example](examples/zephyr/README.md) runs two Zephyr threads against one runtime and
checks promises, the pending-job queue, SharedArrayBuffer, and 8/16/32/64-bit Atomics on QEMU.
Filesystem and dynamic-library module loading remain host facilities; use in-memory or custom
resolvers and loaders on embedded targets.

## WebAssembly notes

For browser-style or freestanding wasm hosts, prefer the `full-wasm` or `full-async-wasm` feature sets:
Expand Down
21 changes: 13 additions & 8 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
authors = ["Mees Delzenne <mees.delzenne@gmail.com>", "K. <kayo@illumium.org>"]
edition = "2021"
license = "MIT"
readme = "README.md"

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / doc

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / check

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / check

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / check

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / sanitizer

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-latest, nightly, wasm32-wasip2, full-async-wasi)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-latest, nightly, wasm32-wasip2, full-async-wasi)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-latest, nightly, wasm32-wasip2, full-async-wasi)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (channels, ubuntu-latest, nightly, x86_64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (channels, ubuntu-latest, nightly, x86_64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (channels, ubuntu-latest, nightly, x86_64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (channels, ubuntu-latest, nightly, x86_64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-musl, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-musl, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-musl, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-musl, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred

Check warning on line 7 in core/Cargo.toml

View workflow job for this annotation

GitHub Actions / test (bindings, ubuntu-24.04, nightly, loongarch64-unknown-linux-gnu, full-async)

explicit `package.readme` can be inferred
description = "High level bindings to the QuickJS JavaScript engine"
keywords = ["quickjs", "ecmascript", "javascript", "es6", "es2020"]
categories = ["api-bindings"]
Expand All @@ -15,16 +15,17 @@

[dependencies]
rquickjs-sys = { workspace = true }
hashbrown = { version = "0.17" }
phf = { version = "0.13", optional = true }
indexmap = { version = "2", optional = true }
either = { version = "1", optional = true }
hashbrown = { version = "0.17", default-features = false, features = ["default-hasher"] }
phf = { version = "0.13", optional = true, default-features = false }
indexmap = { version = "2", optional = true, default-features = false }
either = { version = "1", optional = true, default-features = false }
async-lock = { version = "3", optional = true, default-features = false }
chrono = { version = "0.4", optional = true }
chrono = { version = "0.4", optional = true, default-features = false, features = ["alloc"] }
relative-path = { version = "2.0", optional = true, default-features = false, features = [
"alloc",
] }
half = { version = "2.7.1", optional = true }
half = { version = "2.7.1", optional = true, default-features = false }
spin = { version = "0.10", optional = true, default-features = false, features = ["mutex", "spin_mutex"] }

[target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dependencies]
dlopen2 = { version = "0.8", optional = true }
Expand All @@ -44,14 +45,17 @@
[features]
default = ["std"]

std = ["relative-path?/std"]
std = ["relative-path?/std", "chrono?/clock"]

# Almost all features excluding "parallel" and support for async runtimes
full = ["std", "chrono", "loader", "dyn-load", "either", "indexmap", "half"]

# A version of full designed for wasm hosts without a native filesystem or dynamic loading.
full-wasm = ["std", "chrono", "loader", "either", "indexmap", "half"]

# Portable feature set for no_std hosts with a global allocator.
full-nostd = ["chrono", "loader", "either", "indexmap", "half", "futures", "parallel", "rust-alloc"]

# Almost all features excluding "parallel"
full-async = ["full", "futures"]

Expand All @@ -64,9 +68,10 @@
# Use bindgen to generate bindings at compile-type
# otherwise bundled bindings will be used
bindgen = ["rquickjs-sys/bindgen"]
zephyr = ["rquickjs-sys/zephyr"]

# Enable support of parallel execution
parallel = ["std", "tokio/rt-multi-thread"]
parallel = ["dep:spin", "tokio/rt-multi-thread"]

# Enable user-defined module loader support
loader = ["relative-path"]
Expand Down
6 changes: 3 additions & 3 deletions core/src/context/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use super::{
owner::{ContextOwner, DropContext},
ContextBuilder, Intrinsic,
};
#[cfg(feature = "parallel")]
use crate::runtime::DropContextPtr;
use crate::{markers::ParallelSend, qjs, runtime::AsyncRuntime, Ctx, Error, Result};
use core::{mem, ptr::NonNull};

Expand Down Expand Up @@ -100,9 +102,7 @@ impl DropContext for AsyncRuntime {
}
#[cfg(feature = "parallel")]
{
self.drop_send
.send(ctx)
.expect("runtime should be alive while contexts life");
self.drop_queue.lock().push(DropContextPtr(ctx));
return;
}
}
Expand Down
17 changes: 13 additions & 4 deletions core/src/context/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ use core::ptr::NonNull;
use crate::qjs;

#[cfg(feature = "parallel")]
use std::sync::Arc;
use alloc::sync::Arc;

#[cfg(feature = "parallel")]
struct ParallelContext(NonNull<qjs::JSContext>);

// Access to the context is serialized by the owning runtime lock.
#[cfg(feature = "parallel")]
unsafe impl Send for ParallelContext {}
#[cfg(feature = "parallel")]
unsafe impl Sync for ParallelContext {}

/// Trait to specify how to drop a context once it goes out of scope.
/// Implemented on Runtime and AsyncRuntime.
Expand All @@ -19,7 +28,7 @@ pub(crate) struct ContextOwner<R: DropContext> {
#[cfg(not(feature = "parallel"))]
ctx: NonNull<qjs::JSContext>,
#[cfg(feature = "parallel")]
pub(crate) ctx: Arc<NonNull<qjs::JSContext>>,
ctx: Arc<ParallelContext>,
pub(crate) rt: R,
}

Expand All @@ -31,7 +40,7 @@ impl<R: DropContext> ContextOwner<R> {
#[cfg(feature = "parallel")]
pub(crate) unsafe fn new(ctx: NonNull<qjs::JSContext>, rt: R) -> Self {
Self {
ctx: Arc::new(ctx),
ctx: Arc::new(ParallelContext(ctx)),
rt,
}
}
Expand All @@ -43,7 +52,7 @@ impl<R: DropContext> ContextOwner<R> {

#[cfg(feature = "parallel")]
pub(crate) fn ctx(&self) -> NonNull<qjs::JSContext> {
*self.ctx
self.ctx.0
}

pub(crate) fn rt(&self) -> &R {
Expand Down
2 changes: 2 additions & 0 deletions core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use alloc::boxed::Box;
pub use base::{Runtime, WeakRuntime};
pub use userdata::{UserDataError, UserDataGuard};

#[cfg(all(feature = "futures", feature = "parallel"))]
pub(crate) use r#async::DropContextPtr;
#[cfg(feature = "futures")]
pub(crate) use r#async::InnerRuntime;
#[cfg(feature = "futures")]
Expand Down
52 changes: 36 additions & 16 deletions core/src/runtime/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ use core::{ptr::NonNull, result::Result as StdResult, task::Poll};
#[cfg(feature = "std")]
use std::println;

#[cfg(feature = "parallel")]
use std::sync::mpsc::{self, Receiver, Sender};

use async_lock::Mutex;

use super::{
Expand All @@ -21,22 +18,35 @@ use crate::allocator::Allocator;
use crate::loader::{Loader, Resolver};
#[cfg(feature = "parallel")]
use crate::util::{AssertSendFuture, AssertSyncFuture};
#[cfg(feature = "parallel")]
use crate::Mut;
use crate::{
context::AsyncContext, qjs, result::AsyncJobException, util::ManualPoll, Ctx, Exception, Result,
};

#[cfg(feature = "parallel")]
#[derive(Debug)]
pub(crate) struct DropContextPtr(pub(crate) NonNull<qjs::JSContext>);

// Context destruction is deferred until the runtime lock is held.
#[cfg(feature = "parallel")]
unsafe impl Send for DropContextPtr {}

#[cfg(feature = "parallel")]
type DropQueue = Arc<Mut<Vec<DropContextPtr>>>;

#[derive(Debug)]
pub(crate) struct InnerRuntime {
pub runtime: RawRuntime,
#[cfg(feature = "parallel")]
pub drop_recv: Receiver<NonNull<qjs::JSContext>>,
pub drop_queue: DropQueue,
}

impl InnerRuntime {
pub fn drop_pending(&self) {
#[cfg(feature = "parallel")]
while let Ok(x) = self.drop_recv.try_recv() {
unsafe { qjs::JS_FreeContext(x.as_ptr()) }
for ptr in self.drop_queue.lock().drain(..) {
unsafe { qjs::JS_FreeContext(ptr.0.as_ptr()) }
}
}
}
Expand All @@ -58,15 +68,15 @@ unsafe impl Send for InnerRuntime {}
pub struct AsyncWeakRuntime {
inner: Weak<Mutex<InnerRuntime>>,
#[cfg(feature = "parallel")]
drop_send: Sender<NonNull<qjs::JSContext>>,
drop_queue: DropQueue,
}

impl AsyncWeakRuntime {
pub fn try_ref(&self) -> Option<AsyncRuntime> {
self.inner.upgrade().map(|inner| AsyncRuntime {
inner,
#[cfg(feature = "parallel")]
drop_send: self.drop_send.clone(),
drop_queue: self.drop_queue.clone(),
})
}
}
Expand All @@ -78,7 +88,7 @@ pub struct AsyncRuntime {
// use Arc instead of Ref so we can use OwnedLock
pub(crate) inner: Arc<Mutex<InnerRuntime>>,
#[cfg(feature = "parallel")]
pub(crate) drop_send: Sender<NonNull<qjs::JSContext>>,
pub(crate) drop_queue: DropQueue,
}

// Since all functions which use runtime are behind a mutex
Expand Down Expand Up @@ -110,16 +120,16 @@ impl AsyncRuntime {
let runtime = unsafe { RawRuntime::new(opaque) }?;

#[cfg(feature = "parallel")]
let (drop_send, drop_recv) = mpsc::channel();
let drop_queue = Arc::new(Mut::new(Vec::new()));

Ok(Self {
inner: Arc::new(Mutex::new(InnerRuntime {
runtime,
#[cfg(feature = "parallel")]
drop_recv,
drop_queue: drop_queue.clone(),
})),
#[cfg(feature = "parallel")]
drop_send,
drop_queue,
})
}

Expand All @@ -136,16 +146,16 @@ impl AsyncRuntime {
let runtime = unsafe { RawRuntime::new_with_allocator(opaque, allocator) }?;

#[cfg(feature = "parallel")]
let (drop_send, drop_recv) = mpsc::channel();
let drop_queue = Arc::new(Mut::new(Vec::new()));

Ok(Self {
inner: Arc::new(Mutex::new(InnerRuntime {
runtime,
#[cfg(feature = "parallel")]
drop_recv,
drop_queue: drop_queue.clone(),
})),
#[cfg(feature = "parallel")]
drop_send,
drop_queue,
})
}

Expand All @@ -154,7 +164,7 @@ impl AsyncRuntime {
AsyncWeakRuntime {
inner: Arc::downgrade(&self.inner),
#[cfg(feature = "parallel")]
drop_send: self.drop_send.clone(),
drop_queue: self.drop_queue.clone(),
}
}

Expand Down Expand Up @@ -192,6 +202,16 @@ impl AsyncRuntime {
}
}

/// Configure whether JavaScript Atomics operations may block this thread.
///
/// This must be enabled before using `Atomics.wait`.
#[inline]
pub async fn set_can_block(&self, can_block: bool) {
unsafe {
self.inner.lock().await.runtime.set_can_block(can_block);
}
}

/// Set the module loader
#[cfg(feature = "loader")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "loader")))]
Expand Down
Loading
Loading