FtoBuffer
Bun

function

ffi.toBuffer

function toBuffer(
pointer: bigint,
length: number,
copy?: boolean
): NonSharedBuffer;

Creates a Buffer from native memory.

When copy is true, the returned Buffer owns its own copied memory. When copy is false, the returned Buffer references the original native memory directly.

Using copy: false is a zero-copy escape hatch. The returned Buffer is a writable view onto foreign memory, so writes in JavaScript update the original native memory directly. The caller must guarantee that:

  • pointer remains valid for the entire lifetime of the returned Buffer.
  • length stays within the allocated native region.
  • no native code frees or repurposes that memory while JavaScript still uses the Buffer.
  • Memory protection is observed. For example, read-only memory pages must not be written to.

If these guarantees are not met, reading or writing the Buffer can corrupt memory or crash the process.

@param copy

When false, creates a zero-copy view. Default: true.