forked from cloudflare/worker-emscripten-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (25 loc) · 908 Bytes
/
index.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
import Module from "./pi.js";
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
// Initialize WebAssembly module
let output = "";
const m = await Module({
// By default, stdout/stderr is output to console.log/warn
print: text => output += `${text}\n`,
printErr: text => output += `${text}\n`,
// Instead of downloading the .wasm file, fetch it from a global var
instantiateWasm: (imports, callback) => {
// Note that "PI_WASM" is defined in wrangler.toml
const instance = new WebAssembly.Instance(PI_WASM, imports);
callback(instance);
return instance.exports;
}
});
// Parse user parameter
const position = new URL(request.url).searchParams.get("n") || 0;
// Call main function where first argument = nth digit of PI to calculate
m.callMain([ position ]);
return new Response(output);
}