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.
46 lines • 2.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SharedArray = exports.sharedArraySpecialization = void 0;
const null_pointer_js_1 = require("../emscripten/null-pointer.js");
const rtti_interop_js_1 = require("../../runtime/rtti-interop.js");
const shared_buffer_view_js_1 = require("../shared-memory/shared-buffer-view.js");
const nested_error_js_1 = require("../../error-handling/nested-error.js");
const wasm_error_cause_js_1 = require("../wasm-error-cause.js");
const shared_object_cleanup_js_1 = require("../shared-memory/shared-object-cleanup.js");
/**
* @public
*/
exports.sharedArraySpecialization = new rtti_interop_js_1.IdSpecialization(rtti_interop_js_1.bufferCategory, "JSU_SHARED_ARRAY");
/**
* @public
* Typed array shared between wasm and javascript.
*/
class SharedArray extends shared_buffer_view_js_1.SharedBufferView {
static createOne(wrapper, containerType, bindToReference, length, clearMemory = false, allocationFailThrows = true) {
const numberId = (0, rtti_interop_js_1.getNumberIdentifier)(containerType);
const pointerToContainer = createSharedObject(wrapper, numberId, length, clearMemory, allocationFailThrows);
return pointerToContainer === null_pointer_js_1.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 shared_object_cleanup_js_1.SharedObjectCleanup(this, shared_object_cleanup_js_1.ESharedObjectOwnerKind.SharedMemoryOwner);
shared_object_cleanup_js_1.SharedObjectCleanup.registerCleanup(this, this.cleanup, new shared_object_cleanup_js_1.SharedObjectCleanup.Options("SharedArray", null, shared_object_cleanup_js_1.ESharedObjectOwnerKind.SharedMemoryOwner));
// annotations
wrapper.interopIds.setSpecializations(this, [exports.sharedArraySpecialization, (0, rtti_interop_js_1.getNumberSpecialization)(ctor)]);
}
}
exports.SharedArray = SharedArray;
function createSharedObject(wrapper, numberId, length, clearMemory, allocationFailThrows) {
const pointer = wrapper.instance._sharedArray_createOne(numberId, length, clearMemory);
if (pointer === null_pointer_js_1.nullPtr && allocationFailThrows) {
throw new nested_error_js_1.NestedError("Failed to allocate memory for shared array.", wasm_error_cause_js_1.WasmErrorCause.allocationFailure);
}
return pointer;
}
//# sourceMappingURL=shared-array.js.map