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.
42 lines • 2.38 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 sharedArraySpecialization = new IdSpecialization(bufferCategory, "JSU_SHARED_ARRAY");
/**
* @public
* Typed array shared between wasm and javascript.
*/
export class SharedArray extends SharedBufferView {
static createOne(wrapper, containerType, bindToReference, length, clearMemory = false, allocationFailThrows = true) {
const numberId = getNumberIdentifier(containerType);
const pointerToContainer = createSharedObject(wrapper, numberId, length, clearMemory, allocationFailThrows);
return pointerToContainer === nullPtr ? null : new SharedArray(containerType, wrapper, bindToReference, length, pointerToContainer, numberId);
}
getWrapper() {
return this.wrapper;
}
// @internal
constructor(ctor, wrapper, owner, length, pointerToContainer, numberId) {
super(wrapper, owner, ctor, wrapper.instance._sharedArray_getDataAddress(numberId, pointerToContainer), length * ctor.BYTES_PER_ELEMENT);
this.length = length;
this.pointer = pointerToContainer;
this.cleanup = new SharedObjectCleanup(this, ESharedObjectOwnerKind.SharedMemoryOwner);
SharedObjectCleanup.registerCleanup(this, this.cleanup, new SharedObjectCleanup.Options("SharedArray", null, ESharedObjectOwnerKind.SharedMemoryOwner));
// annotations
wrapper.interopIds.setSpecializations(this, [sharedArraySpecialization, getNumberSpecialization(ctor)]);
}
}
function createSharedObject(wrapper, numberId, length, clearMemory, allocationFailThrows) {
const pointer = wrapper.instance._sharedArray_createOne(numberId, length, clearMemory);
if (pointer === nullPtr && allocationFailThrows) {
throw new NestedError("Failed to allocate memory for shared array.", WasmErrorCause.allocationFailure);
}
return pointer;
}
//# sourceMappingURL=shared-array.js.map