Skip to main content

Module array_buffer

Module array_buffer 

Source
Expand description

Zero-copy ArrayBuffer helpers built on QuickJS-NG primitives.

rquickjs doesn’t yet ship safe wrappers for QuickJS-NG’s immutable ArrayBuffer support, so we go through rquickjs::qjs::* directly. The two capabilities exposed here are:

  • shared_array_buffer_view — create a fresh ArrayBuffer that borrows the bytes of an existing one (no memcpy), kept alive via a dup’d JSValue reference. The view is marked immutable, which is both a correctness guarantee (consumer mutations can’t leak into the source) and a hard safety rail (the only QuickJS code path that would lose our refcount handle is .transfer(), which immutability blocks at the JS layer).
  • set_immutable — flip the immutable flag on an existing ArrayBuffer (used when the source is a freshly-allocated buffer we own and want to seal before handing out).

These are used by Blob.stream() / Blob.slice() and by fetch’s Response.body / Request.body getters to hand out aliased, transfer-safe views into producer-owned storage.

Functions§

set_immutable
Mark an ArrayBuffer as immutable: subsequent writes through any Uint8Array / DataView view silently fail (or TypeError in strict mode), and .transfer() throws TypeError: ArrayBuffer is immutable.
shared_array_buffer_view
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.