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.

77 lines 2.96 kB
import { ESharedObjectOwnerKind, SharedObjectCleanup } from "../shared-memory/shared-object-cleanup.js"; import { DebugProtectedView } from "../../debug/debug-protected-view.js"; import { numberGetHexString } from "../../number/impl/number-get-hex-string.js"; import { _Debug } from "../../debug/_debug.js"; /** * @public */ export class TypedArrayTuple { constructor(wrapper, owner, ctor, pointerToData, numberId, byteSize) { this.wrapper = wrapper; this.resourceHandle = wrapper.lifecycleStrategy.createNode(owner); this.ctor = ctor; this.pointer = pointerToData; this.byteSize = byteSize; this.numberId = numberId; this.impl = new TypedArrayTupleImpl(this, ctor, pointerToData, byteSize); SharedObjectCleanup.registerCleanup(this, this.impl, new SharedObjectCleanup.Options("TypedArrayTuple", _BUILD.DEBUG ? new DebugProtectedView(`TypedArrayTuple - 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 TypedArrayTupleImpl 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); } } //# sourceMappingURL=typed-array-tuple.js.map