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.

76 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SharedMemoryBlock = void 0; const null_pointer_js_1 = require("../emscripten/null-pointer.js"); const debug_protected_view_js_1 = require("../../debug/debug-protected-view.js"); const _debug_js_1 = require("../../debug/_debug.js"); const number_get_hex_string_js_1 = require("../../number/impl/number-get-hex-string.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-object-cleanup.js"); /** * @public * {@inheritDoc ISharedMemoryBlock} */ class SharedMemoryBlock { static createOne(wrapper, bindToReference, byteSize, allocationFailThrows = true) { _BUILD.DEBUG && wrapper.debugUtils.onAllocate.emit(); const pointer = wrapper.instance._jsUtilMalloc(byteSize); if (pointer == null_pointer_js_1.nullPtr) { if (allocationFailThrows) { throw new nested_error_js_1.NestedError("Failed to allocate memory for shared memory block.", wasm_error_cause_js_1.WasmErrorCause.allocationFailure); } else { return null; } } return new SharedMemoryBlock(wrapper, bindToReference, pointer, byteSize); } getWrapper() { return this.wrapper; } getDataView() { if (_BUILD.DEBUG) { _debug_js_1._Debug.assert(!this.resourceHandle.getIsDestroyed(), "use after free"); return this.wrapper.debugUtils.protectedViews .getValue(this.resourceHandle) .createProtectedView(this.impl.dataView); } else { return this.impl.dataView; } } constructor(wrapper, owner, pointer, byteSize) { this.wrapper = wrapper; this.resourceHandle = wrapper.lifecycleStrategy.createNode(owner); this.pointer = pointer; this.byteSize = byteSize; this.impl = new SharedMemoryBlockImpl(this, pointer, byteSize); shared_object_cleanup_js_1.SharedObjectCleanup.registerCleanup(this, this.impl, new shared_object_cleanup_js_1.SharedObjectCleanup.Options("SharedMemoryBlock", _BUILD.DEBUG ? new debug_protected_view_js_1.DebugProtectedView(`SharedMemoryBlock - memory resize danger: don't hold reference to the DataView ${(0, number_get_hex_string_js_1.numberGetHexString)(pointer)}`) : null, shared_object_cleanup_js_1.ESharedObjectOwnerKind.Freeable)); } } exports.SharedMemoryBlock = SharedMemoryBlock; class SharedMemoryBlockImpl extends shared_object_cleanup_js_1.SharedObjectCleanup { constructor(sharedMemoryBlock, pointer, byteSize) { super(sharedMemoryBlock, shared_object_cleanup_js_1.ESharedObjectOwnerKind.Freeable); this.pointer = pointer; this.byteSize = byteSize; this.dataView = this.recreateDataView(); this.wrapper.memoryResize.addListener(this); } onFree() { super.onFree(); this.wrapper.memoryResize.removeListener(this); } onMemoryResize() { if (this.wrapper == null) { _BUILD.DEBUG && _debug_js_1._Debug.error("object has been destroyed"); return; } this.dataView = this.recreateDataView(); } recreateDataView() { return new DataView(this.wrapper.memory.buffer, this.pointer, this.byteSize); } } //# sourceMappingURL=shared-memory-block.js.map