54 lines
1.3 KiB
HTML
54 lines
1.3 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Examples - Buffer</title>
|
|
</head>
|
|
<body>
|
|
<h1>Buffer</h1>
|
|
|
|
<a href="index.html">List</a> - <a href="buffer.py.html">Source</a> - <a href="buffer.wat.html">WebAssembly</a>
|
|
|
|
<div style="white-space: pre;" id="results"></div>
|
|
|
|
|
|
<script type="text/javascript">
|
|
let importObject = {};
|
|
|
|
let results = document.getElementById('results');
|
|
|
|
function log(txt)
|
|
{
|
|
let span = document.createElement('span');
|
|
span.innerHTML = txt;
|
|
results.appendChild(span);
|
|
let br = document.createElement('br');
|
|
results.appendChild(br);
|
|
}
|
|
|
|
WebAssembly.instantiateStreaming(fetch('buffer.wasm'), importObject)
|
|
.then(app => {
|
|
// Allocate room within the memory of the WebAssembly class
|
|
let size = 8;
|
|
|
|
stdlib_alloc___init__ = app.instance.exports['stdlib.alloc.__init__']
|
|
stdlib_alloc___init__()
|
|
|
|
stdlib_types___alloc_bytes__ = app.instance.exports['stdlib.types.__alloc_bytes__']
|
|
|
|
let offset = stdlib_types___alloc_bytes__(size)
|
|
var i8arr = new Uint8Array(app.instance.exports.memory.buffer, offset + 4, size);
|
|
|
|
log('len(i8arr) = ' + app.instance.exports.length(offset));
|
|
|
|
//Fill it
|
|
for (var i = 0; i < size; i++) {
|
|
i8arr[i] = i + 5;
|
|
let from_wasm = app.instance.exports.index(offset, i);
|
|
log('i8arr[' + i + '] = ' + from_wasm);
|
|
}
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|