Skip to main content

shared_array_buffer_view

Function shared_array_buffer_view 

Source
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 / DataView views silently no-op (strict mode: TypeError) — aliased consumers can’t corrupt the source.
  • buffer.transfer() throws TypeError: ArrayBuffer is immutable — so a consumer can’t detach the view and drop the opaque pointer that keeps the source alive. Without this guard the free_func would later fire with opaque=NULL (QuickJS strips opaque on transfer; see js_array_buffer_constructor3) and panic in Box::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).