-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonion.js
40 lines (34 loc) · 1.42 KB
/
onion.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
40
function loadWebAssembly(filename, imports = {}) {
return fetch(filename)
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.compile(buffer))
.then(module => {
imports.env = imports.env || {}
Object.assign(imports.env, {
memoryBase: 0,
tableBase: 0,
memory: new WebAssembly.Memory({ initial: 256, maximum: 256 }),
table: new WebAssembly.Table({ initial: 0, maximum: 0, element: 'anyfunc' })
});
return new WebAssembly.Instance(module, imports);
});
}
document.write('<div id="input_div"></div>');
document.write('<div id="time"></div>');
document.write('<div id="output"></div>');
input_div.innerHTML += '<input name="layers" type="number" maxlenghth="7" id="layers" class="number" value="0" autofocus></input>'
input_div.innerHTML += '<button onclick="run()">Run</button>';
function run() {
loadWebAssembly('../onion.wasm').then(instance => {
const { onion } = instance.exports;
const layers = document.getElementById("layers").value;
const beg = performance.now();
const result = onion(layers);
const end = performance.now();
time.innerHTML = 'Processed in ';
time.innerHTML += '<strong>' + `${end - beg} ms` + '</strong>';
time.innerHTML += '<br/><br/>';
output.innerHTML = `onion(${layers}) \t = ${result}`;
output.innerHTML += `<br/>`;
});
}