+/* @internal */
+export function encodeUint8Array (inputArray: Uint8Array): number {
+ const cArrayPointer = wasm.TS_malloc(inputArray.length + 4);
+ const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1);
+ arrayLengthView[0] = inputArray.length;
+ const arrayMemoryView = new Uint8Array(wasm.memory.buffer, cArrayPointer + 4, inputArray.length);
+ arrayMemoryView.set(inputArray);
+ return cArrayPointer;
+}
+/* @internal */
+export function encodeUint32Array (inputArray: Uint32Array|Array<number>): number {
+ const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 4);
+ const arrayMemoryView = new Uint32Array(wasm.memory.buffer, cArrayPointer, inputArray.length);
+ arrayMemoryView.set(inputArray, 1);
+ arrayMemoryView[0] = inputArray.length;
+ return cArrayPointer;
+}
+/* @internal */
+export function encodeUint64Array (inputArray: BigUint64Array|Array<bigint>): number {
+ const cArrayPointer = wasm.TS_malloc(inputArray.length * 8 + 1);
+ const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1);
+ arrayLengthView[0] = inputArray.length;
+ const arrayMemoryView = new BigUint64Array(wasm.memory.buffer, cArrayPointer + 4, inputArray.length);
+ arrayMemoryView.set(inputArray);
+ return cArrayPointer;
+}
+
+/* @internal */
+export function check_arr_len(arr: Uint8Array, len: number): Uint8Array {
+ if (arr.length != len) { throw new Error("Expected array of length " + len + "got " + arr.length); }
+ return arr;
+}
+
+/* @internal */
+export function getArrayLength(arrayPointer: number): number {
+ const arraySizeViewer = new Uint32Array(wasm.memory.buffer, arrayPointer, 1);
+ return arraySizeViewer[0];
+}
+/* @internal */
+export function decodeUint8Array (arrayPointer: number, free = true): Uint8Array {
+ const arraySize = getArrayLength(arrayPointer);
+ const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 4, arraySize);
+ // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
+ // will free the underlying memory when it becomes unreachable instead of copying here.
+ // Note that doing so may have edge-case interactions with memory resizing (invalidating the buffer).
+ const actualArray = actualArrayViewer.slice(0, arraySize);
+ if (free) {
+ wasm.TS_free(arrayPointer);
+ }
+ return actualArray;
+}
+const decodeUint32Array = (arrayPointer: number, free = true) => {
+ const arraySize = getArrayLength(arrayPointer);
+ const actualArrayViewer = new Uint32Array(
+ wasm.memory.buffer, // value
+ arrayPointer + 4, // offset (ignoring length bytes)
+ arraySize // uint32 count
+ );
+ // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
+ // will free the underlying memory when it becomes unreachable instead of copying here.
+ const actualArray = actualArrayViewer.slice(0, arraySize);
+ if (free) {
+ wasm.TS_free(arrayPointer);
+ }
+ return actualArray;
+}
+
+
+export function freeWasmMemory(pointer: number) { wasm.TS_free(pointer); }
+
+/* @internal */
+export function getU32ArrayElem(arrayPointer: number, idx: number): number {
+ const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 4, idx + 1);
+ return actualArrayViewer[idx];
+}
+
+/* @internal */
+export function getU8ArrayElem(arrayPointer: number, idx: number): number {
+ const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 4, idx + 1);
+ return actualArrayViewer[idx];
+}
+
+
+/* @internal */
+export function encodeString(str: string): number {
+ const charArray = new TextEncoder().encode(str);
+ return encodeUint8Array(charArray);
+}
+
+/* @internal */
+export function decodeString(stringPointer: number, free = true): string {
+ const arraySize = getArrayLength(stringPointer);
+ const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer + 4, arraySize);
+ const result = new TextDecoder("utf-8").decode(memoryView);
+
+ if (free) {
+ wasm.TS_free(stringPointer);
+ }
+
+ return result;
+}