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
JSTypedArrayEnumvalues, in the header’s order.
Functions§
- JS_
Atomics ⚠Poll Waiters JS_AtomicsPollWaiters: how many waiters aSharedArrayBufferhas.- JS_
Atomics ⚠SetAsync Waker JS_AtomicsSetAsyncWaker: register a wake-up callback forAtomics.waitAsync.- JS_
Detach ⚠Array Buffer JS_DetachArrayBuffer: take the storage away, leaving an empty detached buffer.- JS_
GetTyped ⚠Array Buffer JS_GetTypedArrayBuffer: the view’s buffer and its geometry.- JS_
GetTyped ⚠Array Type JS_GetTypedArrayType: the view’sJSTypedArrayEnum, or-1.- JS_
JSON ⚠Stringify JS_JSONStringify:JSON.stringify(obj, replacer, space).- JS_
NewArray ⚠Buffer JS_NewArrayBuffer: anArrayBufferoverbuf, taking ownership of it.- JS_
NewTyped ⚠Array JS_NewTypedArray: a view, built the waynew Uint8Array(...)would be.- JS_
NewUint8 ⚠Array JS_NewUint8Array: aUint8Arrayover a C block, taking ownership of it.- JS_
NewUint8 ⚠Array Copy JS_NewUint8ArrayCopy: aUint8Arrayholding a copy ofbuf.- JS_
NewUint8 ⚠Array Copy2 JS_NewUint8ArrayCopy2: the same, optionally immutable.- JS_
ParseJSON ⚠ JS_ParseJSON: parsebuf_lenbytes as JSON.- JS_
ParseJSO ⚠N2 JS_ParseJSON2: parse JSON, optionally as extended JSON.- JS_
ParseJSO ⚠N3 JS_ParseJSON3: the same, plus a record of where each value came from.- JS_
Same ⚠Value JS_SameValue:Object.is(a, b).- JS_
Same ⚠Value Zero JS_SameValueZero:SameValueZero—Object.isexcept that±0are equal.- JS_
SetShared ⚠Array Buffer Functions JS_SetSharedArrayBufferFunctions: install the host’s shared-memory allocator.- JS_
Strict ⚠Eq JS_StrictEq:a === b.
Type Aliases§
- Free
Array Buffer Data Func JSFreeArrayBufferDataFunc: called when a C-supplied block is released.