Commit 2fd1e70
committed
feat: allow backing an ArrayBuffer/SharedArrayBuffer with embedder-owned memory
Adds a public API to create \ArrayBuffer\s and \SharedArrayBuffer\s whose
bytes alias an embedder-supplied memory region instead of a Boa-owned
allocation, enabling zero-copy sharing of byte regions (Wasm linear
memories, mmap'd files, GPU-mapped buffers) between JavaScript and native
host code:
- \ArrayBuffer::from_external_data\ (safe) / \rom_external_ptr\ (unsafe):
constructors over a \&'static mut [u8]\ region. The engine accesses
non-shared external regions with normal (non-atomic) loads, stores and
copies - exactly like Boa-owned memory - so zero-copy workloads stay as
fast as native \memcpy\.
- \SharedArrayBuffer::from_external_data\ (safe) / \rom_external_ptr\
(unsafe): constructors over a \&'static [AtomicU8]\ region. Shared
buffers keep the atomic access path, since cross-thread visibility is
required for \SharedArrayBuffer\ semantics.
- the same constructor pairs on \JsArrayBuffer\ / \JsSharedArrayBuffer\
- \is_external()\ on all four types
The \&'static mut\ borrow given to a non-shared \ArrayBuffer\ is what makes
non-atomic access sound: the embedder hands over exclusive access to the
region. Embedders that must keep accessing the region use the raw-pointer
constructor and synchronize with the JavaScript code that may access the
buffer. Regions concurrently accessed from other threads (e.g. shared
WebAssembly linear memories) back a \SharedArrayBuffer\, which keeps atomic
access.
Externally-backed buffers are always fixed-length and cannot be resized,
grown nor transferred. Non-shared external buffers can be detached, which
returns a copy of the region's contents and drops the engine's reference
into the region, giving embedders a hard guarantee that the engine can no
longer access it (e.g. before unmapping it). Cloning an \ArrayBuffer\
deep-copies external regions into a Boa-owned allocation, so clones never
alias the original region. External regions must be 8-byte aligned
(asserted at construction) to keep the wider typed-array views
(\Float64Array\, \BigInt64Array\, ...) properly aligned.
Closes #54471 parent 257bc30 commit 2fd1e70
7 files changed
Lines changed: 900 additions & 71 deletions
File tree
- core
- engine/src
- builtins/array_buffer
- object/builtins
- wintertc/src/store
0 commit comments