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.

83 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SharedBufferView = void 0; 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 rtti_interop_js_1 = require("../../runtime/rtti-interop.js"); const shared_object_cleanup_js_1 = require("./shared-object-cleanup.js"); /** * @public * {@inheritDoc ISharedBufferView} */ class SharedBufferView { constructor(wrapper, owner, ctor, pointerToData, byteSize) { this.wrapper = wrapper; this.resourceHandle = wrapper.lifecycleStrategy.createNode(owner); this.ctor = ctor; this.pointer = pointerToData; this.byteSize = byteSize; this.numberId = (0, rtti_interop_js_1.getNumberIdentifier)(ctor); this.impl = new SharedBufferViewImpl(this, ctor, pointerToData, byteSize); shared_object_cleanup_js_1.SharedObjectCleanup.registerCleanup(this, this.impl, new shared_object_cleanup_js_1.SharedObjectCleanup.Options("SharedBufferView", _BUILD.DEBUG ? new debug_protected_view_js_1.DebugProtectedView(`SharedBufferView - memory resize danger: don't hold reference to the DataView ${(0, number_get_hex_string_js_1.numberGetHexString)(pointerToData)}`) : null, shared_object_cleanup_js_1.ESharedObjectOwnerKind.NotOwning)); } getWrapper() { return this.wrapper; } getSharedObjectHandle() { return this; } 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; } } getArray() { 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.array); } else { return this.impl.array; } } } exports.SharedBufferView = SharedBufferView; class SharedBufferViewImpl extends shared_object_cleanup_js_1.SharedObjectCleanup { constructor(sbv, ctor, pointer, byteSize) { super(sbv, shared_object_cleanup_js_1.ESharedObjectOwnerKind.NotOwning); this.ctor = ctor; this.pointer = pointer; this.byteSize = byteSize; this.dataView = this.recreateDataView(); this.array = this.recreateArray(); 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(); this.array = this.recreateArray(); } recreateDataView() { return new DataView(this.wrapper.memory.buffer, this.pointer, this.byteSize); } recreateArray() { return new this.ctor(this.wrapper.memory.buffer, this.pointer, this.byteSize / this.ctor.BYTES_PER_ELEMENT); } } //# sourceMappingURL=shared-buffer-view.js.map