Skip to content

Instantly share code, notes, and snippets.

@spectras
Created August 13, 2023 01:17
Show Gist options
  • Save spectras/bba9be79155382888dad24a8685fcb73 to your computer and use it in GitHub Desktop.
Save spectras/bba9be79155382888dad24a8685fcb73 to your computer and use it in GitHub Desktop.
// clang --target=wasm32 -nostdlib -Os -Wl,--no-entry -Wl,--export-all add.cpp -o add.wasm
extern "C" int add(int x, int y)
{
return x + y;
}
<!doctype html>
<html lang="en-us">
<head><title>Tech Focus Loader</title></head>
<body>
<script type='text/javascript'>
async function load() {
const response = await fetch("add.wasm");
const data = await response.arrayBuffer();
const Module = {
env: {
memory: new WebAssembly.Memory({ initial: 1 }) // 64kb
}
};
const { instance } = await WebAssembly.instantiate(data, Module);
if (instance.__wasm_call_ctors) { instance.__wasm_call_ctors(); }
return instance.exports;
};
load().then(result => {
Module = result;
console.log("Loaded as variable <Module>");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment