mach/www/template.html
iddev5 0ff8edebd9 mach: Add js-runtime as a dependency for wasm applications
This is a workaround for a limitation right now. The html-generator has
no way to dynamically add JS sources (and that can't be done without
using a preprocessor library) so we hardcode js-runtime in it.

In the future, I think the correct behavior would be to move tools/
inside js-runtime along with a wasm application building SDK and get rid
of any direct JS access we have today (which is just
src/platform/wasm.zig and src/platform/mach.js).
2022-07-07 04:35:19 -07:00

46 lines
1.3 KiB
HTML

<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script type="module">
import {{ mach }} from "./mach.js";
import {{ zig }} from "./js-runtime.js";
let imports = {{
env: {{ ...mach, ...zig }},
}};
fetch("{s}.wasm")
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer, imports))
.then(results => results.instance)
.then(instance => {{
zig.init(instance);
mach.init(instance);
instance.exports.wasmInit();
let frame = true;
let last_update_time = performance.now();
let update = function() {{
if (!frame) return;
if (mach.machHasEvent() ||
(last_update_time + (mach.wait_event_timeout * 1000)) <= performance.now()) {{
instance.exports.wasmUpdate();
last_update_time = performance.now();
}}
window.requestAnimationFrame(update);
}};
window.requestAnimationFrame(update);
window.addEventListener("mach-close", () => {{
instance.exports.wasmDeinit();
frame = false;
}});
}})
.catch(err => console.error(err));
</script>
</body>
</html>