pub fn shared_array_buffer_view<'js>(
ctx: &Ctx<'js>,
source: &ArrayBuffer<'js>,
offset: usize,
len: usize,
) -> Result<ArrayBuffer<'js>>Expand description
Create a fresh, immutable ArrayBuffer that shares storage with
source at [offset..offset+len] without copying any bytes. The
returned buffer holds a dup’d reference to the source’s JSValue, so
the backing allocation stays alive exactly as long as any view (or
transferred descendant of it) is reachable.
Immutability is what makes this sound:
- Writes through
Uint8Array/DataViewviews silently no-op (strict mode:TypeError) — aliased consumers can’t corrupt the source. buffer.transfer()throwsTypeError: ArrayBuffer is immutable— so a consumer can’t detach the view and drop theopaquepointer that keeps the source alive. Without this guard thefree_funcwould later fire withopaque=NULL(QuickJS stripsopaqueon transfer; seejs_array_buffer_constructor3) and panic inBox::from_raw(null). Because immutability blocks transfer at the JS layer, that path is unreachable.
If a future caller wants a mutable shared view, they need a different cleanup strategy (ptr-keyed side table, upstream QuickJS patch, or accepting a per-transfer leak).