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.
39 lines • 2.08 kB
JavaScript
import { isLittleEndian } from "../../web-assembly/util/is-little-endian.js";
import { nullPtr } from "../../web-assembly/emscripten/null-pointer.js";
import { NestedError } from "../../error-handling/nested-error.js";
import { WasmErrorCause } from "../../web-assembly/wasm-error-cause.js";
import { TypedArrayTuple } from "../../web-assembly/shared-array/typed-array-tuple.js";
export class ATypedTupleFactory {
constructor(elementCount, bytesPerElement, dataView, numberId, vectorId) {
this.elementCount = elementCount;
this.bytesPerElement = bytesPerElement;
this.dataView = dataView;
this.numberId = numberId;
this.vectorId = vectorId;
this.byteSize = elementCount * bytesPerElement;
}
copyFromBuffer(memoryDataView, pointer, writeTo = this.createOneEmpty(), littleEndian = ATypedTupleFactory.littleEndian) {
const bytesPerElement = this.bytesPerElement;
for (let i = 0, iEnd = this.elementCount; i < iEnd; ++i) {
writeTo[i] = this.dataView.getValue(memoryDataView, pointer, littleEndian);
pointer += bytesPerElement;
}
return writeTo;
}
copyToBuffer(memoryDataView, writeFrom, pointer, littleEndian = ATypedTupleFactory.littleEndian) {
const bytesPerElement = this.bytesPerElement;
for (let i = 0, iEnd = this.elementCount; i < iEnd; ++i) {
this.dataView.setValue(memoryDataView, pointer, writeFrom[i], littleEndian);
pointer += bytesPerElement;
}
}
static createSharedVector(wrapper, owner, ctor, bufferFactory) {
const ptr = wrapper.instance._jsUtilCreateVec(bufferFactory.numberId, bufferFactory.vectorId);
if (ptr == nullPtr) {
throw new NestedError("Failed to allocate memory for shared tuple.", WasmErrorCause.allocationFailure);
}
return new TypedArrayTuple(wrapper, owner, ctor, ptr, bufferFactory.numberId, bufferFactory.byteSize);
}
}
ATypedTupleFactory.littleEndian = isLittleEndian;
//# sourceMappingURL=a-typed-tuple-factory.js.map