The goals are:
- Use JS to manage the lifetime of Rust data.
- Allow references to JS managed data to be freely copied and discarded, relying on the garbage collector for safety.
- Maintain Rust memory safety (for example no mutable aliasing), without requiring additional static analysis such as a lint.
- Allow mutable and immutable access to Rust data via JS managed references, so we do not need to rely on interior mutability.
- Provide a rooting API to ensure that JS managed data is not garbage collected while it is being used.
To support safe access to JS managed data, the API uses a JS context, which is used as an access token to allow JS managed data to be accessed, allocated or deallocated. Mutable access to JS managed data requires mutable access to the JS context, which is how the API achieves memory safety even though JS managed references can be copied and discarded freely.
JS managed memory is split into compartments, which are separately garbage collected, so the garbage collector can avoid scanning the entire heap. The API statically tracks compartments, to ensure that there are no direct references between compartments.
The josephine crate is based on the
rust-mozjs crate for the SpiderMonkey engine,
which uses nightly features, so
josephine also requires nightly.
[dependencies] josephine = "0.1"
cargo +nightly build.
A minimal example, which uses JS to manage the lifetime of a string:
Larger examples are:
- Doubly linked lists: an implementation of a simple cyclic data structure.
cargo +nightly run --example dbllist.
- A fragment of the DOM: A tiny subset of the HTML Document Object Model.
cargo +nightly run --example minidom.