quickjs/value_helpers.rs
1//! The two directions of the bridge, as free functions.
2//!
3//! [`crate::convert`] owns the actual conversions; this module exists so that the exported
4//! API files can write `jsvalue_of(value)` and `value_of(js)` without each of them importing
5//! the same pair of names. The names read the way the C API does — a `JSValue` is what goes
6//! over the boundary, a `Value` is what the engine holds.
7
8use engine::Value;
9
10use crate::convert;
11use crate::jsvalue::JsValue;
12
13/// Box an engine value as a `JSValue` the caller owns.
14pub fn jsvalue_of(value: Value) -> JsValue {
15 convert::encode(&value)
16}
17
18/// The engine value behind a `JSValue`, when it is one this layer can decode.
19///
20/// # Safety
21///
22/// `value` must be a `JSValue` from this crate (or a primitive), and a reference-counted one
23/// must be alive.
24pub unsafe fn value_of(value: JsValue) -> Option<Value> {
25 // SAFETY: the caller's contract.
26 unsafe { convert::decode(value) }
27}