rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
39 lines • 2.2 kB
JavaScript
import { nullPtr } from "../emscripten/null-pointer.js";
import { bufferCategory, getNumberIdentifier, getNumberSpecialization, IdSpecialization } from "../../runtime/rtti-interop.js";
import { SharedBufferView } from "../shared-memory/shared-buffer-view.js";
import { NestedError } from "../../error-handling/nested-error.js";
import { WasmErrorCause } from "../wasm-error-cause.js";
import { ESharedObjectOwnerKind, SharedObjectCleanup } from "../shared-memory/shared-object-cleanup.js";
/**
* @public
*/
export const resizableArraySpecialization = new IdSpecialization(bufferCategory, "JSU_RESIZABLE_ARRAY");
/**
* @public
* Typed array shared between wasm and javascript, unlike {@link SharedArray}, resizing is possible.
*/
export class ResizableArray extends SharedBufferView {
static createOne(wrapper, containerType, bindToReference, length) {
const numberId = getNumberIdentifier(containerType);
const sharedObject = createSharedObject(wrapper, numberId, length, true);
return new ResizableArray(wrapper, containerType, bindToReference, length, sharedObject, numberId);
}
// @internal
constructor(wrapper, ctor, owner, length, pointer, numberId) {
super(wrapper, owner, ctor, wrapper.instance._resizableArray_getDataAddress(numberId, pointer), length * ctor.BYTES_PER_ELEMENT);
this.length = length;
this.pointer = pointer;
this.cleanup = new SharedObjectCleanup(this, ESharedObjectOwnerKind.SharedMemoryOwner);
SharedObjectCleanup.registerCleanup(this, this.cleanup, new SharedObjectCleanup.Options("ResizableArray", null, ESharedObjectOwnerKind.SharedMemoryOwner));
// annotations
wrapper.interopIds.setSpecializations(this, [resizableArraySpecialization, getNumberSpecialization(ctor)]);
}
}
function createSharedObject(wrapper, numberId, length, allocationFailThrows) {
const ptr = wrapper.instance._resizableArray_createOne(numberId, length);
if (ptr === nullPtr && allocationFailThrows) {
throw new NestedError("Failed to allocate memory for resizable array.", WasmErrorCause.allocationFailure);
}
return ptr;
}
//# sourceMappingURL=resizable-array.js.map