Function mozjs_sys::jsapi::JS::NewArrayBufferWithUserOwnedContents

source ·
pub unsafe extern "C" fn NewArrayBufferWithUserOwnedContents(
    cx: *mut JSContext,
    nbytes: usize,
    contents: *mut c_void,
) -> *mut JSObject
Expand description

Create a new ArrayBuffer with the given non-null |contents|.

Ownership of |contents| remains with the caller: it isn’t transferred to the returned ArrayBuffer. Callers of this function must ensure that they perform these two steps, in this order, to properly relinquish ownership of |contents|:

  1. Call |JS::DetachArrayBuffer| on the buffer returned by this function. (|JS::DetachArrayBuffer| is generally fallible, but a call under these circumstances is guaranteed to succeed.)
  2. |contents| may be deallocated or discarded consistent with the manner in which it was allocated.

Do not simply allow the returned buffer to be garbage-collected before deallocating |contents|, because in general there is no way to know when an object is fully garbage-collected to the point where this would be safe.