-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.ts
44 lines (38 loc) · 1.26 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { readFile } from 'fs/promises';
import { join } from 'path';
export const getWasm = async <
entry = (x?: number, y?: number, z?: number) => number
>(
testDirectory: string,
testName: string
) => {
const wasmPath = join(__dirname, testDirectory, `${testName}.wasm`);
const wasmBuffer = await readFile(wasmPath);
const wasmModule = await WebAssembly.compile(wasmBuffer);
const instance = await WebAssembly.instantiate(wasmModule);
return instance.exports.entry as entry;
}
export const getWasmMemory = (
testDirectory: string,
testName: string,
pointerOverride?: number
) => async <
Input extends number[],
>(...input: Input) => {
const wasmPath = join(__dirname, testDirectory, `${testName}.wasm`);
const wasmBuffer = await readFile(wasmPath);
const wasmModule = await WebAssembly.compile(wasmBuffer);
const instance = await WebAssembly.instantiate(wasmModule);
const { exports } = instance;
const { memory, entry } = exports as {
entry: (...input: Input) => number;
memory: WebAssembly.Memory
};
const pointer = pointerOverride ?? entry(...input);
const buffer = new Uint8Array(memory.buffer);
let output = '';
for (let i = pointer; buffer[i]; i++) {
output += String.fromCharCode(buffer[i]);
}
return output
}