2019-02-21 11:49:08 -08:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
|
|
|
|
</head>
|
|
|
|
<body>
|
2019-02-25 11:11:30 -08:00
|
|
|
<!-- Note the usage of `type=module` here as this is an ES6 module -->
|
2019-03-05 12:30:47 -06:00
|
|
|
<script type="module">
|
2019-02-25 11:11:30 -08:00
|
|
|
// Use ES module import syntax to import functionality from the module
|
|
|
|
// that we have compiled.
|
|
|
|
//
|
|
|
|
// Note that the `default` import is an initialization function which
|
|
|
|
// will "boot" the module and make it ready to use. Currently browsers
|
|
|
|
// don't support natively imported WebAssembly as an ES module, but
|
|
|
|
// eventually the manual initialization won't be required!
|
|
|
|
import { add, default as init } from './pkg/without_a_bundler.js';
|
2019-02-21 11:49:08 -08:00
|
|
|
|
|
|
|
async function run() {
|
|
|
|
// First up we need to actually load the wasm file, so we use the
|
2019-02-25 11:11:30 -08:00
|
|
|
// default export to inform it where the wasm file is located on the
|
|
|
|
// server, and then we wait on the returned promise to wait for the
|
2019-02-21 11:49:08 -08:00
|
|
|
// wasm to be loaded.
|
|
|
|
//
|
|
|
|
// Note that instead of a string here you can also pass in an instance
|
|
|
|
// of `WebAssembly.Module` which allows you to compile your own module.
|
2019-02-25 11:11:30 -08:00
|
|
|
// Also note that the promise, when resolved, yields the wasm module's
|
|
|
|
// exports which is the same as importing the `*_bg` module in other
|
|
|
|
// modes
|
2019-04-01 11:09:57 -07:00
|
|
|
// await init('./pkg/without_a_bundler_bg.wasm');
|
|
|
|
|
|
|
|
const url = await fetch('http://localhost:8001/pkg/without_a_bundler_bg.wasm');
|
|
|
|
const body = await url.arrayBuffer();
|
|
|
|
const module = await WebAssembly.compile(body);
|
|
|
|
await init(module);
|
2019-02-21 11:49:08 -08:00
|
|
|
|
|
|
|
// And afterwards we can use all the functionality defined in wasm.
|
|
|
|
const result = add(1, 2);
|
|
|
|
console.log(`1 + 2 = ${result}`);
|
|
|
|
if (result !== 3)
|
|
|
|
throw new Error("wasm addition doesn't work!");
|
|
|
|
}
|
|
|
|
|
|
|
|
run();
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|