Created
August 13, 2023 01:17
-
-
Save spectras/bba9be79155382888dad24a8685fcb73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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