|
Ultralight C API 2.0.0
|
Functions for sharing raw binary memory with JavaScript through ArrayBuffers and typed arrays.
#include <Ultralight/CAPI/CAPI_JSBuffer.h>
These functions allow native code and page script to share raw binary memory without copying. You can pass native memory directly to JavaScript as an ArrayBuffer or typed array, and read binary buffers created by the page.
For conventions governing handle ownership, NULL handles, threading, and error reporting across the JavaScript C API, see CAPI_JSValue.h.
Share native memory with the page by wrapping a ULBuffer in an ArrayBuffer:
Native memory passes to JavaScript as an ArrayBuffer or typed array using a ULBuffer or an explicit copy:
Native code accesses bytes from an ArrayBuffer or typed array without copying:
Inspect a typed array and read its elements through ULJSTypedArrayInfo:
Call ulJSArrayBufferDetach() to take shared bytes back from the page early so JavaScript can no longer reach the memory.
Once native code reads the bytes (by calling ulJSArrayBufferGetBuffer(), ulJSArrayBufferGetBytes(), or ulJSTypedArrayGetInfo()), the ArrayBuffer can no longer be detached by native code or by the page. This restriction applies even to an ArrayBuffer created from your own ULBuffer. Calling ulJSArrayBufferDetach() then returns false.
A ULBuffer shared with the page releases its memory according to who holds it:
Classes | |
| struct | ULJSTypedArrayInfo |
| Description of a typed array, filled by ulJSTypedArrayGetInfo(). More... | |
Functions | |
| bool | ulJSValueIsArrayBuffer (ULJSValue value) |
| Whether or not a value is an ArrayBuffer (detached ones included). | |
| ULJSValue | ulCreateJSArrayBuffer (ULJSContext ctx, const void *bytes, size_t length) |
| Create an ArrayBuffer holding a copy of the given bytes. | |
| ULJSValue | ulCreateJSArrayBufferFromBuffer (ULJSContext ctx, ULBuffer buffer) |
| Create an ArrayBuffer that shares a Buffer's bytes with JavaScript without copying. | |
| ULBuffer | ulJSArrayBufferGetBuffer (ULJSValue array_buffer) |
| Get a Buffer that views an ArrayBuffer's bytes without copying. | |
| void * | ulJSArrayBufferGetBytes (ULJSValue array_buffer, size_t *length) |
| Get a pointer to an ArrayBuffer's bytes without copying. | |
| bool | ulJSArrayBufferDetach (ULJSValue array_buffer) |
| Detach an ArrayBuffer so JavaScript can no longer reach its bytes. | |
| ULJSTypedArrayType | ulJSValueGetTypedArrayType (ULJSValue value) |
| Get the element type of a typed array. | |
| ULJSValue | ulCreateJSTypedArray (ULJSContext ctx, ULJSTypedArrayType type, size_t length) |
| Create a typed array of zero-filled elements. | |
| ULJSValue | ulCreateJSTypedArrayFromBuffer (ULJSContext ctx, ULJSTypedArrayType type, ULBuffer buffer) |
| Create a typed array that shares a Buffer's bytes with JavaScript without copying. | |
| ULJSValue | ulCreateJSTypedArrayFromArrayBuffer (ULJSContext ctx, ULJSTypedArrayType type, ULJSValue array_buffer, size_t byte_offset, size_t length) |
| Create a typed array over part of an existing ArrayBuffer without copying. | |
| bool | ulJSTypedArrayGetInfo (ULJSValue typed_array, ULJSTypedArrayInfo *info) |
| Describe a typed array: its element type, a pointer to its first element, and its lengths. | |
| ULJSValue | ulJSTypedArrayGetBuffer (ULJSValue typed_array) |
| Get the ArrayBuffer a typed array views. | |
Enumerations | |
| enum | ULJSTypedArrayType { kULJSTypedArrayType_None = 0 , kULJSTypedArrayType_Int8 , kULJSTypedArrayType_Uint8 , kULJSTypedArrayType_Uint8Clamped , kULJSTypedArrayType_Int16 , kULJSTypedArrayType_Uint16 , kULJSTypedArrayType_Int32 , kULJSTypedArrayType_Uint32 , kULJSTypedArrayType_Float32 , kULJSTypedArrayType_Float64 , kULJSTypedArrayType_BigInt64 , kULJSTypedArrayType_BigUint64 } |
| Element types for JavaScript typed arrays. More... | |
| ULJSValue ulCreateJSArrayBuffer | ( | ULJSContext | ctx, |
| const void * | bytes, | ||
| size_t | length ) |
Create an ArrayBuffer holding a copy of the given bytes.
| ctx | The context to create the value in. |
| bytes | The bytes to copy (may be NULL only when length is 0). |
| length | The number of bytes to copy (0 creates an empty ArrayBuffer). |
| ULJSValue ulCreateJSArrayBufferFromBuffer | ( | ULJSContext | ctx, |
| ULBuffer | buffer ) |
Create an ArrayBuffer that shares a Buffer's bytes with JavaScript without copying.
The ArrayBuffer keeps a reference to buffer until it's garbage collected or detached (see "Byte Lifetimes" above). You still own your buffer handle and must destroy it as usual.
| ctx | The context to create the value in. |
| buffer | The Buffer to share. An empty Buffer gives an ordinary empty ArrayBuffer that keeps no reference. |
| ULJSValue ulCreateJSTypedArray | ( | ULJSContext | ctx, |
| ULJSTypedArrayType | type, | ||
| size_t | length ) |
Create a typed array of zero-filled elements.
| ctx | The context to create the value in. |
| type | The element type. |
| length | The number of elements (not bytes). |
| ULJSValue ulCreateJSTypedArrayFromArrayBuffer | ( | ULJSContext | ctx, |
| ULJSTypedArrayType | type, | ||
| ULJSValue | array_buffer, | ||
| size_t | byte_offset, | ||
| size_t | length ) |
Create a typed array over part of an existing ArrayBuffer without copying.
The ArrayBuffer can still be detached.
| ctx | The context to create the value in. |
| type | The element type. |
| array_buffer | The ArrayBuffer to view. |
| byte_offset | The byte offset of the first element (must be a multiple of the element size). |
| length | The number of elements (not bytes). |
| ULJSValue ulCreateJSTypedArrayFromBuffer | ( | ULJSContext | ctx, |
| ULJSTypedArrayType | type, | ||
| ULBuffer | buffer ) |
Create a typed array that shares a Buffer's bytes with JavaScript without copying.
Lifetime works as for ulCreateJSArrayBufferFromBuffer().
| ctx | The context to create the value in. |
| type | The element type. |
| buffer | The Buffer to share (its size must be a multiple of the element size). An empty Buffer gives an ordinary empty typed array that keeps no reference. |
| bool ulJSArrayBufferDetach | ( | ULJSValue | array_buffer | ) |
Detach an ArrayBuffer so JavaScript can no longer reach its bytes.
Afterwards its byteLength is 0, typed arrays over it have length 0 (element reads return undefined), and creating a new typed array over it throws a TypeError. If it was created from a Buffer, its reference to that Buffer is released during a later ulUpdate().
| array_buffer | The ArrayBuffer to detach. |
Get a Buffer that views an ArrayBuffer's bytes without copying.
The Buffer is always a new one, even for an ArrayBuffer created from your own Buffer. Holding it keeps the ArrayBuffer alive while the page lives.
| array_buffer | The ArrayBuffer. |
| void * ulJSArrayBufferGetBytes | ( | ULJSValue | array_buffer, |
| size_t * | length ) |
Get a pointer to an ArrayBuffer's bytes without copying.
| array_buffer | The ArrayBuffer. |
| length | Where to store the byte length (may be NULL; written only on success). |
Get the ArrayBuffer a typed array views.
The ArrayBuffer can still be detached.
| typed_array | The typed array. |
| bool ulJSTypedArrayGetInfo | ( | ULJSValue | typed_array, |
| ULJSTypedArrayInfo * | info ) |
Describe a typed array: its element type, a pointer to its first element, and its lengths.
| typed_array | The typed array. |
| info | The struct to fill (if NULL, the function returns false). |
| ULJSTypedArrayType ulJSValueGetTypedArrayType | ( | ULJSValue | value | ) |
Get the element type of a typed array.
The ArrayBuffer can still be detached, so you can use it to check a value before ulJSTypedArrayGetInfo().
| value | The value to check. |
| bool ulJSValueIsArrayBuffer | ( | ULJSValue | value | ) |
Whether or not a value is an ArrayBuffer (detached ones included).
The ArrayBuffer can still be detached.
| value | The value to check. |
| enum ULJSTypedArrayType |
Element types for JavaScript typed arrays.