-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
111 lines (91 loc) · 3.34 KB
/
index.html
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<html>
<head>
<script>
// Check for wasm support.
if (!('WebAssembly' in window)) {
alert('you need a browser with wasm support enabled :(');
}
const memory = new WebAssembly.Memory({
initial: 256,
maximum: 256
});
// Load a WebAssembly library
function loadWebAssembly(filename) {
const importObject = {
global: {},
env: {
'abortStackOverflow': _ => { throw new Error('overflow'); },
'__memory_base': 1024,
'__table_base': 0,
'memory': memory,
'table': new WebAssembly.Table({initial: 4, maximum: 4, element: 'anyfunc'}),
'STACKTOP': 0,
'STACK_MAX': memory.buffer.byteLength,
abort: alert,
___syscall146: () => {},
___setErrNo: () => {},
_abort: () => {},
__emval_take_value: () => {},
__emval_incref: () => {},
__emval_decref: () => {},
___syscall6: () => {},
___syscall140: () => {},
abortOnCannotGrowMemory: _ => { throw new Error('abortOnCannotGrowMemory'); },
_emscripten_resize_heap: () => {},
_emscripten_memcpy_big: () => {},
_emscripten_get_heap_size: () => {},
DYNAMICTOP_PTR: 0,
}
}
async function createWebAssembly(path, imports) {
const fetchPromise = fetch(path);
return WebAssembly.instantiateStreaming(fetchPromise, imports);
}
return createWebAssembly(filename, importObject);
}
// Main part of this example, loads the module and uses it.
loadWebAssembly('build/bin/library.wasm')
.then(response => {
console.log(response);
const { instance } = response;
console.log(instance);
const { exports } = instance;
console.log(exports);
// JavaScript sends data to WebAssembly, WebAssembly accesses the data via pointer
const jsInput = new Uint8Array(3);
const wasmInputPtr = exports._create_buffer(jsInput.length);
const wasmInput = new Uint8Array(memory.buffer, wasmInputPtr, jsInput.length);
// Copy data in to be used by WebAssembly
wasmInput.set(jsInput);
// Process
exports._inc_array(wasmInputPtr, jsInput.length);
// Copy data out to JavaScript
jsInput.set(wasmInput);
exports._destroy_buffer(wasmInputPtr);
console.log(jsInput);
// WebAssembly owns the data, JavaScript uses the data
const staticDataPtr = exports._get_data();
const jsData = new Uint8Array(memory.buffer, staticDataPtr, 3);
console.log(jsData);
// Copy WebAssembly owned data to JavaScript array
const jsLocalData = new Uint8Array(3);
jsLocalData.set(jsData);
console.log(jsLocalData);
let input = 21;
// Now we are ready, set up the button so the user can run the code
var button = document.getElementById('run');
button.value = 'Call a method in the WebAssembly module';
button.addEventListener('click', function() {
const result = exports._doubler(input);
alert(input + ' doubled is ' + result);
input = result;
}, false);
}
)
.catch(err => { throw err; });
</script>
</head>
<body>
<input type="button" id="run" value="(waiting for WebAssembly)"/>
</body>
</html>