Skip to main content

Module buffers

Module buffers 

Source
Expand description

ArrayBuffer and the typed-array views, from C.

§Ownership, which is the whole difficulty here

JS_NewArrayBuffer(ctx, buf, len, free_func, opaque, is_shared) takes ownership of a C block and calls free_func when the buffer dies. JS_NewUint8Array does the same for a view. So a buffer built this way cannot wrap an engine Vec<u8> without one of two losses: either the C block is copied (and free_func has to be called immediately, which contradicts the header) or the buffer holds a foreign pointer and every read goes through it.

This layer takes the first option and is explicit about it: the bytes are copied into the engine’s storage. What it does not do any more is call free_func right away — the block is released when the buffer is collected, which is what the header says and what a host that counts its own allocations can observe. The copy stays, because the alternative — a foreign-pointer storage kind threaded through every typed-array operation — would be a second representation of the same thing, and the callers of JS_NewArrayBuffer in the acceptance surface (tests/bjson.c) hand over a block they never touch again.

The one difference left: a host that keeps writing into its block after the call does not see those writes in script, because script reads the copy. JS_NewArrayBufferCopy is the spelling that admits the copy; the plain one now at least releases at the documented time.

§Detachment

JS_DetachArrayBuffer zeroes the storage and sets the flag, which every view reads: a detached view answers 0 bytes and undefined elements. That is what structuredClone’s transfer path and postMessage rely on.

Modules§

typed_array_kind
The JSTypedArrayEnum values, in the header’s order.

Functions§

JS_AtomicsPollWaiters⚠
JS_AtomicsPollWaiters: how many waiters a SharedArrayBuffer has.
JS_AtomicsSetAsyncWaker⚠
JS_AtomicsSetAsyncWaker: register a wake-up callback for Atomics.waitAsync.
JS_DetachArrayBuffer⚠
JS_DetachArrayBuffer: take the storage away, leaving an empty detached buffer.
JS_GetTypedArrayBuffer⚠
JS_GetTypedArrayBuffer: the view’s buffer and its geometry.
JS_GetTypedArrayType⚠
JS_GetTypedArrayType: the view’s JSTypedArrayEnum, or -1.
JS_JSONStringify⚠
JS_JSONStringify: JSON.stringify(obj, replacer, space).
JS_NewArrayBuffer⚠
JS_NewArrayBuffer: an ArrayBuffer over buf, taking ownership of it.
JS_NewTypedArray⚠
JS_NewTypedArray: a view, built the way new Uint8Array(...) would be.
JS_NewUint8Array⚠
JS_NewUint8Array: a Uint8Array over a C block, taking ownership of it.
JS_NewUint8ArrayCopy⚠
JS_NewUint8ArrayCopy: a Uint8Array holding a copy of buf.
JS_NewUint8ArrayCopy2⚠
JS_NewUint8ArrayCopy2: the same, optionally immutable.
JS_ParseJSON⚠
JS_ParseJSON: parse buf_len bytes as JSON.
JS_ParseJSON2⚠
JS_ParseJSON2: parse JSON, optionally as extended JSON.
JS_ParseJSON3⚠
JS_ParseJSON3: the same, plus a record of where each value came from.
JS_SameValue⚠
JS_SameValue: Object.is(a, b).
JS_SameValueZero⚠
JS_SameValueZero: SameValueZero — Object.is except that ±0 are equal.
JS_SetSharedArrayBufferFunctions⚠
JS_SetSharedArrayBufferFunctions: install the host’s shared-memory allocator.
JS_StrictEq⚠
JS_StrictEq: a === b.

Type Aliases§

FreeArrayBufferDataFunc
JSFreeArrayBufferDataFunc: called when a C-supplied block is released.