rquickjs/
lib.rs

1#![allow(clippy::doc_lazy_continuation)]
2
3//! # High-level bindings to QuickJS
4//!
5//! The `rquickjs` crate provides safe high-level bindings to the
6//! [QuickJS](https://bellard.org/quickjs/) JavaScript engine. This crate is heavily inspired by
7//! the [rlua](https://crates.io/crates/rlua) crate.
8//!
9//! # The `Runtime` and `Context` objects
10//!
11//! The main entry point of this library is the [`Runtime`] struct. It represents the interpreter
12//! state and is used to create [`Context`] objects. As the QuickJS library does not support
13//! threading the runtime is locked behind a mutex. Multiple threads cannot run a script or create
14//! objects from the same runtime at the same time. The [`Context`] object represents a global
15//! environment. Contexts of the same runtime can share JavaScript objects like in the browser between
16//! frames of the same origin.
17//!
18//! As both [`Runtime`] and [`Context`] use a lock it is discouraged to use them in a async
19//! environment. Instead, when the `futures` feature is enabled this library also exposes
20//! [`AsyncRuntime`] and [`AsyncContext`] which use a future aware lock.
21//!
22//! # Converting Values
23//!
24//! This library has multiple traits for converting to and from JavaScript. The [`IntoJs`] trait is
25//! used for taking Rust values and turning them into JavaScript values. The [`FromJs`] is for
26//! converting JavaScript values to Rust. Note that this trait does not perform automatic
27//! coercion but [`Coerced`] can be used to convert the values with coercion.
28//!
29//! For values which represent the name of variables or indices, the trait [`IntoAtom`] is
30//! available to convert values to the representation QuickJS requires.
31//!
32//! # Optional features
33//!
34//! ## Advanced
35//!
36//! The following features may be enabled to get an extra functionality:
37//!
38//! - `rust-alloc` forces using Rust's global allocator by default instead of libc's one.
39//!
40//! - `loader` adds support for custom ES6 modules resolvers and loaders. The resolvers and loaders
41//! should implements [`Resolver`](loader::Resolver) and [Loader](loader::Loader) traits
42//! respectively and can be plugged in already existing [`Runtime`] before loading modules via
43//! [`Runtime::set_loader`]. The resolvers and loaders can be easily combined via tuples. When the
44//! previous resolver or loader failed the next one will be applied.
45//!
46//! - `dyn-load` adds support for loadable native modules (so/dll/dylib).
47//!
48//! - `futures` adds support for async Rust. When enabled the library exports [`AsyncRuntime`] and
49//! [`AsyncContext`]. These are the asynchronous variants of the normal runtime and context. In
50//! order to ensure that QuickJS is used properly the runtime is placed behind a lock. For the
51//! normal runtime this is a normal mutex. You should avoid blocking threads in asynchronous Rust
52//! so the async runtime uses a future aware mutex. In the async runtime Rust futures can be passed
53//! to JS as [ES6
54//! Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
55//! and ES6 Promises can be given back as Rust futures.
56//!
57//! - `macro` enables some useful procedural macros which gets Rust/JS interop much easy. An
58//! [attribute](#attributes) macros can be applied to functions, constants and modules. An
59//! [derive](#derives) macros can be used with structs and enums.
60//!
61//! - `phf` enables using Perfect Hash Function for builtin modules lookup
62//!
63//! - `disable-assertions` compiles QuickJS with assertions stripped out.
64//!
65//! ## Extra types
66//!
67//! This crate has support for conversion of many Rust types like [`Option`],
68//! [`Result`](std::result::Result), [`Vec`] and other collections. In addition an extra types
69//! support can be enabled via features:
70//!
71//! - `either` adds [`FromJs`]/[`IntoJs`] implementations for [`Either`](`either::Either`)
72//!
73//! - `indexmap` adds [`FromJs`]/[`IntoJs`] implementations for [`IndexSet`](`indexmap::IndexSet`)
74//! and [`IndexMap`](`indexmap_rs::IndexMap`)
75//!
76//! ## Bindings
77//!
78//! The bindings are pre-generated for the following platforms:
79//!
80//! - Linux:
81//!   - `aarch64-unknown-linux-musl`
82//!   - `i686-unknown-linux-gnu`
83//!   - `x86_64-unknown-linux-gnu`
84//!   - `x86_64-unknown-linux-musl`
85//!   - `loongarch64-unknown-linux-gnu`
86//!   - `loongarch64-unknown-linux-musl`
87//! - macOS:
88//!   - `aarch64-apple-darwin`
89//!   - `x86_64-apple-darwin`
90//! - Windows:
91//!   - `i686-pc-windows-gnu`
92//!   - `x86_64-pc-windows-gnu`
93//!   - `i686-pc-windows-msvc`
94//!   - `x86_64-pc-windows-msvc`
95//!
96//! To build the crate for any other target you must enable the `bindgen` feature.
97//!
98//! ## Experimental
99//!
100//! - `parallel` enables multithreading support.
101//!
102//! Note that the experimental features which may not works as expected. Use it for your own risk.
103//!
104//! ## Debugging
105//!
106//! QuickJS can be configured to output some info which can help debug. The following features
107//! enable that:
108//!
109//! - `dump-bytecode`
110//! - `dump-gc`
111//! - `dump-gc-free`
112//! - `dump-free`
113//! - `dump-leaks`
114//! - `dump-mem`
115//! - `dump-objects`
116//! - `dump-atoms`
117//! - `dump-shapes`
118//! - `dump-module-resolve`
119//! - `dump-promise`
120//! - `dump-read-object`
121
122#![cfg_attr(feature = "doc-cfg", feature(doc_cfg))]
123#![cfg_attr(not(test), no_std)]
124
125pub use rquickjs_core::*;
126
127#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "macro")))]
128#[cfg(feature = "macro")]
129pub use rquickjs_macro::{class, embed, function, methods, module, JsLifetime};
130
131pub mod class {
132    //! JavaScript classes defined from Rust.
133
134    pub use rquickjs_core::class::*;
135    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "macro")))]
136    #[cfg(feature = "macro")]
137    pub use rquickjs_macro::Trace;
138}
139
140// The following imports needed to linking docs
141
142#[cfg(feature = "either")]
143#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "either")))]
144extern crate either_rs as either;
145
146#[cfg(feature = "indexmap")]
147#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "indexmap")))]
148extern crate indexmap_rs as indexmap;