pub struct BufferStorage {
pub bytes: Vec<u8>,
pub detached: bool,
pub max_byte_length: Option<usize>,
pub release: Option<Box<dyn FnOnce()>>,
}Expand description
The bytes an ArrayBuffer owns, plus whether they have been detached.
transfer() moves the bytes to a new buffer and leaves this one empty and detached; a
view of a detached buffer reads undefined from every index.
Fields§
§bytes: Vec<u8>The bytes, in memory order.
bytes.len() is the byte length: resizing is Vec::resize, which is what keeps the
length, the capacity and the pointer from ever disagreeing.
detached: boolWhether transfer() has taken the bytes away.
max_byte_length: Option<usize>The most bytes this buffer may grow to, or None when it cannot grow at all.
Some(max) is what new ArrayBuffer(n, {maxByteLength: max}) asks for: resizable
answers true, maxByteLength answers max, and resize may move the length
anywhere up to it. A fixed-length buffer answers false/the byte length.
release: Option<Box<dyn FnOnce()>>A host’s “release this block” callback, run when the storage dies.
This is what lets the C API’s JS_NewArrayBuffer honour its free_func contract: the
header says the block is released when the buffer is collected, not when it is built,
and a host that counts allocations sees the difference. The bytes themselves are
copied — this engine’s storage is a Vec<u8> and cannot alias a foreign pointer
without a second representation of every typed-array operation — so the callback
releases the host’s original block while the storage keeps its own copy. Writes the
host makes to its block after the call are therefore not visible to script, which is
the one documented difference.
Implementations§
Source§impl BufferStorage
impl BufferStorage
Sourcepub fn fixed(bytes: Vec<u8>) -> BufferStorage
pub fn fixed(bytes: Vec<u8>) -> BufferStorage
Storage for a fixed-length buffer, which is what every caller but a resizable constructor wants.
Sourcepub fn fixed_with_release(
bytes: Vec<u8>,
release: Box<dyn FnOnce()>,
) -> BufferStorage
pub fn fixed_with_release( bytes: Vec<u8>, release: Box<dyn FnOnce()>, ) -> BufferStorage
Storage for a fixed-length buffer whose origin block has to be released at collection.
The C API’s JS_NewArrayBuffer(ctx, buf, len, free_func, opaque, …): the bytes are a
copy, and release is the closure that calls free_func(opaque, buf) when this
storage is dropped. Keeping the closure here rather than calling it at construction is
the whole difference between honouring the header and approximating it.
Sourcepub fn resizable(bytes: Vec<u8>, max: usize) -> BufferStorage
pub fn resizable(bytes: Vec<u8>, max: usize) -> BufferStorage
Storage for a resizable buffer of bytes bytes that may grow to max.
Sourcepub fn is_resizable(&self) -> bool
pub fn is_resizable(&self) -> bool
Whether resize can change this buffer’s length.
Sourcepub fn max_byte_length(&self) -> usize
pub fn max_byte_length(&self) -> usize
What maxByteLength reports: the ceiling for a resizable buffer, and the current
length for a fixed one — which is what the getter’s IsDetachedBuffer/IsFixedLength
split in the spec amounts to once the length stops changing.