-
Notifications
You must be signed in to change notification settings - Fork 1
/
swiftScript.js
39 lines (32 loc) · 1.03 KB
/
swiftScript.js
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
import {WASI} from "https://cdn.skypack.dev/@wasmer/wasi";
import { WasmFs } from "https://cdn.skypack.dev/@wasmer/wasmfs";
export async function executeSwiftWasm(value){
const wasmFS = new WasmFs();
let wasi = new WASI({
args: [],
env: {},
bindings: {
...WASI.defaultBindings,
fs:wasmFS.fs,
},
});
let returnObject = {"result": -1, "time": -1};
try {
const response = await fetch("./swiftO2.wasm");
const bytes = await response.arrayBuffer();
// Instantiate the WebAssembly file
const {instance} = await WebAssembly.instantiate(bytes, {
wasi_snapshot_preview1:wasi.wasiImport
})
wasi.start(instance);
const start = window.performance.now();
const result = instance.exports.find_primes(value);
const end = window.performance.now();
const time = end - start;
returnObject.result = result
returnObject.time = time;
} catch (error) {
console.log(error);
}
return returnObject;
};