Skip to main content

create_buffer_object

Function create_buffer_object 

Source
pub unsafe fn create_buffer_object(
    cx: *mut JSContext,
    bytes: &[u8],
) -> *mut JSObject
Expand description

Build a Buffer instance wrapping the given bytes and return the raw object pointer. The caller owns the returned reference. Returns null on OOM.

The instance is a real SM Uint8Array (typed array backed by an inline ArrayBuffer). This makes instanceof Uint8Array true, gives native length/indexed element access/subarray/slice/set, and crucially makes Buffer.allocUnsafe(64 MiB) an O(1) allocation instead of a per-byte JS_DefineElement storm that would take minutes.

Shared by globals.rs Buffer methods, node_crypto.rs (randomBytes) and node_fs.rs (readFileSync without encoding) so that all paths return real Buffer instances (Buffer.isBuffer(x) === true).