name: duk_get_buffer_data proto: | void *duk_get_buffer_data(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size); stack: | [ ... val! ... ] summary: |
Get the data pointer for a plain buffer or a buffer object (ArrayBuffer,
Node.js Buffer, DataView, or TypedArray view) value at idx
without
modifying or coercing the value. Return a non-NULL
pointer if the
value is a valid buffer with a non-zero size. For a zero-size buffer, may return
a NULL
or a non-NULL
pointer. Returns NULL
if the value is not a buffer, the value is a buffer object whose "backing buffer"
doesn't fully cover the buffer object's apparent size, or the index is invalid.
If out_size
is non-NULL
, the size of the buffer is
written to *out_size
; 0 is written if the return value is
NULL
.
The data area indicated by the return pointer and length is the full
buffer for a plain buffer value, and the active "slice" for a buffer object.
The length returned is expressed in bytes (instead of elements), so that you
can always access ptr[0]
to ptr[len - 1]
.
For example:
new Uint8Array(16)
the return pointer would point to
start of the array and length would be 16.new Uint32Array(16)
the return pointer would point to
start of the array and length would be 64.new Uint32Array(16).subarray(2, 6)
the return pointer
would point to the start of the subarray (2 x 4 = 8 bytes from the start
of the Uint32Array) and length would be 16 ((6-2) x 4).If the target value or the underlying buffer value of a buffer object is a
fixed buffer, the returned pointer is stable and won't change during the
lifetime of the buffer. For dynamic and external buffers the pointer may
change as the buffer is resized or reconfigured; the caller is responsible for
ensuring pointer values returned from this API call are not used after the
buffer is resized/reconfigured. Buffers created from Ecmascript code directly
e.g. as new Buffer(8)
are backed by an automatic fixed buffer, so
their pointers are always stable.
In special cases it's possible that a buffer object is backed by a plain
buffer which is smaller than the buffer object's apparent size. In these
cases a NULL
is returned; this ensures that the pointer and size
returned can always be used safely. (This behavior may change even in minor
versions.)