- return Math.ceil(value / 4) * 4;
-}
-
-const encodeArray = (inputArray) => {
- // TODO: (matt) is this correct, or should it go back to length * 4?
- // const cArrayPointer = wasm.wasm_malloc(inputArray.length * 4);
- const cArrayPointer = wasm.wasm_malloc(nextMultipleOfFour(inputArray.length));
-
- const arrayMemoryView = new Uint32Array(memory.buffer, cArrayPointer, inputArray.length);
- arrayMemoryView.set(inputArray);
- return cArrayPointer;
-}
-
-const decodeArray = (arrayPointer, free = true) => {
- const arraySizeViewer = new Uint32Array(
- memory.buffer, // value
- arrayPointer, // offset
- 1 // one int
- );
- const arraySize = arraySizeViewer[0];
- const actualArrayViewer = new Uint32Array(
- memory.buffer, // value
- arrayPointer, // offset
- arraySize + 1
- );
- const actualArray = actualArrayViewer.slice(1, arraySize + 1);
- if (free) {
- // wasm.free_array(arrayPointer);
- wasm.wasm_free(arrayPointer); // TODO: check if passing *void still captures remaining values
- }
- return actualArray;
-}
-
-const encodeString = (string) => {
- // make malloc count divisible by 4
- const memoryNeed = nextMultipleOfFour(string.length + 1);
- const stringPointer = wasm.wasm_malloc(memoryNeed);
- const stringMemoryView = new Uint8Array(
- memory.buffer, // value
- stringPointer, // offset
- string.length + 1 // length
- );
- for (let i = 0; i < string.length; i++) {
- stringMemoryView[i] = string.charCodeAt(i);
- }
- stringMemoryView[string.length] = 0;
- return stringPointer;
-}
-
-const decodeString = (stringPointer, free = true) => {
- const memoryView = new Uint8Array(memory.buffer, stringPointer);
- let cursor = 0;
- let result = '';
-
- while (memoryView[cursor] !== 0) {
- result += String.fromCharCode(memoryView[cursor]);
- cursor++;
- }
-
- if (free) {
- wasm.wasm_free(stringPointer);
- }
-
- return result;
-};
+ return Math.ceil(value / 4) * 4;
+}
+
+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;
+}
+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;
+}
+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;
+}
+
+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;
+}
+
+export function getArrayLength(arrayPointer: number): number {
+ const arraySizeViewer = new Uint32Array(wasm.memory.buffer, arrayPointer, 1);
+ return arraySizeViewer[0];
+}
+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 getU32ArrayElem(arrayPointer: number, idx: number): number {
+ const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 4, idx + 1);
+ return actualArrayViewer[idx];
+}
+
+export function encodeString(str: string): number {
+ const charArray = new TextEncoder().encode(str);
+ return encodeUint8Array(charArray);
+}
+
+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;
+}