UNPKG

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.

43 lines 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ResizableArray = exports.resizableArraySpecialization = 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.resizableArraySpecialization = new rtti_interop_js_1.IdSpecialization(rtti_interop_js_1.bufferCategory, "JSU_RESIZABLE_ARRAY"); /** * @public * Typed array shared between wasm and javascript, unlike {@link SharedArray}, resizing is possible. */ class ResizableArray extends shared_buffer_view_js_1.SharedBufferView { static createOne(wrapper, containerType, bindToReference, length) { const numberId = (0, rtti_interop_js_1.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 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("ResizableArray", null, shared_object_cleanup_js_1.ESharedObjectOwnerKind.SharedMemoryOwner)); // annotations wrapper.interopIds.setSpecializations(this, [exports.resizableArraySpecialization, (0, rtti_interop_js_1.getNumberSpecialization)(ctor)]); } } exports.ResizableArray = ResizableArray; function createSharedObject(wrapper, numberId, length, allocationFailThrows) { const ptr = wrapper.instance._resizableArray_createOne(numberId, length); if (ptr === null_pointer_js_1.nullPtr && allocationFailThrows) { throw new nested_error_js_1.NestedError("Failed to allocate memory for resizable array.", wasm_error_cause_js_1.WasmErrorCause.allocationFailure); } return ptr; } //# sourceMappingURL=resizable-array.js.map