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.

79 lines 3.1 kB
import { DebugProtectedView } from "../../debug/debug-protected-view.js"; import { _Debug } from "../../debug/_debug.js"; import { numberGetHexString } from "../../number/impl/number-get-hex-string.js"; import { getNumberIdentifier } from "../../runtime/rtti-interop.js"; import { ESharedObjectOwnerKind, SharedObjectCleanup } from "./shared-object-cleanup.js"; /** * @public * {@inheritDoc ISharedBufferView} */ export 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 = getNumberIdentifier(ctor); this.impl = new SharedBufferViewImpl(this, ctor, pointerToData, byteSize); SharedObjectCleanup.registerCleanup(this, this.impl, new SharedObjectCleanup.Options("SharedBufferView", _BUILD.DEBUG ? new DebugProtectedView(`SharedBufferView - memory resize danger: don't hold reference to the DataView ${numberGetHexString(pointerToData)}`) : null, ESharedObjectOwnerKind.NotOwning)); } getWrapper() { return this.wrapper; } getSharedObjectHandle() { return this; } getDataView() { if (_BUILD.DEBUG) { _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.assert(!this.resourceHandle.getIsDestroyed(), "use after free"); return this.wrapper.debugUtils.protectedViews .getValue(this.resourceHandle) .createProtectedView(this.impl.array); } else { return this.impl.array; } } } class SharedBufferViewImpl extends SharedObjectCleanup { constructor(sbv, ctor, pointer, byteSize) { super(sbv, 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.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