{build,wasmserve}: use wasmserve, drop apple_pie
This commit is contained in:
parent
ebb4b9c2fe
commit
5be9f04d85
18 changed files with 747 additions and 133 deletions
29
tools/html-generator/main.zig
Normal file
29
tools/html-generator/main.zig
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
const std = @import("std");
|
||||
|
||||
const source = @embedFile("template.html");
|
||||
const app_name_needle = "{ app_name }";
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
defer _ = gpa.deinit();
|
||||
|
||||
const args = try std.process.argsAlloc(allocator);
|
||||
defer std.process.argsFree(allocator, args);
|
||||
|
||||
if (args.len < 3) {
|
||||
std.debug.print("Usage: html-generator <output-name> <app-name>\n", .{});
|
||||
return;
|
||||
}
|
||||
|
||||
const output_name = args[1];
|
||||
const app_name = args[2];
|
||||
|
||||
const file = try std.fs.cwd().createFile(output_name, .{});
|
||||
defer file.close();
|
||||
var buf = try allocator.alloc(u8, std.mem.replacementSize(u8, source, app_name_needle, app_name));
|
||||
defer allocator.free(buf);
|
||||
|
||||
_ = std.mem.replace(u8, source, app_name_needle, app_name, buf);
|
||||
_ = try file.write(buf);
|
||||
}
|
||||
53
tools/html-generator/template.html
Normal file
53
tools/html-generator/template.html
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>{ app_name }</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script type="module">
|
||||
import { mach } from "./mach.js";
|
||||
import { zig } from "./mach-sysjs.js";
|
||||
import setupWasmserve from "./wasmserve.js";
|
||||
|
||||
setupWasmserve();
|
||||
|
||||
let imports = {
|
||||
env: { ...mach, ...zig },
|
||||
};
|
||||
|
||||
fetch("{ app_name }.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>
|
||||
Loading…
Add table
Add a link
Reference in a new issue