<!doctype html>
<title>WASM Test</title>
<script>
function fetchAndInstantiateWasm (url, imports) {
return fetch(url)
.then(res => {
if (res.ok)
return res.arrayBuffer();
throw new Error(`Unable to fetch Web Assembly file ${url}.`);
})
.then(bytes => WebAssembly.compile(bytes))
.then(module => WebAssembly.instantiate(module, imports || {}))
.then(instance => instance.exports);
}
fetchAndInstantiateWasm('https://cdn.rawgit.com/guybedford/wasm-intro/f61eb0d0/3-calling-js-from-wasm/program.wasm', {
env: {
consoleLog: num => console.log(num)
}
})
.then(m => {
console.log(m.getSqrt(5));
});
</script>
#include <math.h>
void consoleLog (float num);
float getSqrt (float num) {
consoleLog(num);
return sqrt(num);
}