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:

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.)

example: | void *ptr; duk_size_t sz; duk_eval_string(ctx, "new Uint16Array(16)"); ptr = duk_get_buffer_data(ctx, -1, &sz); /* Here 'ptr' would be non-NULL and sz would be 32 (bytes). */ tags: - stack - buffer - bufferobject seealso: - duk_require_buffer_data - duk_get_buffer - duk_require_buffer introduced: 1.3.0