Files
chandra/wasmhost/bin/main.mbt
Claude 2f4ce827c3
CI / test (push) Failing after 17s
MoonBit host for the compositor component on prebuilt libwasmtime (sys, font, raster)
wasmhost/ runs build/compositor.wasm with wasmtime linked as the upstream C API
library (fetched by setup-toolchain.sh) instead of compiling it: a cold build is
about 4 s against about 2 min for host-wasm. sys and font reuse the native
compositor's packages; the rasteriser is a port of host-wasm/src/raster.rs with the
pixel work in C, SVG through nanosvg. Panel modules, --drm and --window come next;
host-wasm stays the working host until then.

wit: sys.append-file. The C API moves list<u8> as one dynamic value per byte and
wasmtime caps what one call may copy, so the component writes files over 1 MiB as
write-file plus appends (host-wasm implements it too).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiNfjrw2edja7LiRLB
2026-11-25 19:42:20 +00:00

110 lines
2.9 KiB
MoonBit

///| The MoonBit host for the compositor component (build/compositor.wasm).
///
/// It runs the component with wasmtime, linked as the prebuilt C API library, and implements the
/// interfaces the component imports (wit/host): sys, font, raster, display and plasmoids. Most of
/// it is the native compositor's own packages (compositor/sys, font, kms, wasm, renderer), so the
/// native and wasm builds share one implementation of each.
///
/// wasmhost build/compositor.wasm --socket PATH [--window | --drm | --gfx-out PNG [--serve]]
///|
#borrow(s)
extern "C" fn c_eprint(s : Bytes) = "wh_eprint"
///|
extern "C" fn c_ignore_sigchld() = "wh_ignore_sigchld"
///|
extern "C" fn c_exit(code : Int) = "wh_exit"
///|
fn exit(code : Int) -> Unit {
c_exit(code)
}
///|
fn eprint(s : String) -> Unit {
c_eprint(@utf8.encode(s))
}
///| Every host function, by interface, in registration order (its id is its index).
let handlers : Array[(Array[@wt.Val]) -> Array[@wt.Val]] = []
///| "interface.function" of each handler, for WASMHOST_TRACE.
let handler_names : Array[String] = []
///|
fn register(
iface : String,
funcs : Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])],
) -> Unit {
let first = handlers.length()
for f in funcs {
handlers.push(f.1)
handler_names.push(iface + "." + f.0)
}
match @wt.define_funcs(iface, funcs.map(f => f.0), first) {
Ok(_) => ()
Err(e) => {
eprint("error: " + e)
exit(1)
}
}
}
///|
fn main {
let argv = @env.args()
let args : Array[String] = if argv.length() > 1 {
argv[1:].to_array()
} else {
[]
}
// First argument may be the component path; otherwise use the default build location.
let path = if args.length() > 0 && args[0].has_suffix(".wasm") {
args.remove(0)
} else {
"build/compositor.wasm"
}
if args.length() > 0 && args[0] == "--" {
args.remove(0) |> ignore
}
c_ignore_sigchld()
match @wt.load(path) {
Ok(_) => ()
Err(e) => {
eprint(
"error: cannot load \{path}: \{e}\n build it with `just compositor-wasm`",
)
exit(1)
}
}
if @sys.getenv("WASMHOST_TRACE") != "" {
@wt.set_handler(fn(id, a) {
eprint("> " + handler_names[id] + " " + a.length().to_string())
handlers[id](a)
})
} else {
@wt.set_handler(fn(id, a) { handlers[id](a) })
}
register("chandra:host/sys@0.1.0", sys_funcs())
register("chandra:host/font@0.1.0", font_funcs())
register("chandra:host/raster@0.1.0", raster_funcs())
register("chandra:host/display@0.1.0", display_funcs())
register("chandra:host/plasmoids@0.1.0", plasmoid_funcs())
match @wt.instantiate() {
Ok(_) => ()
Err(e) => {
eprint("error: " + e)
exit(1)
}
}
match @wt.call("", "run", [@wt.Val::strings(args)]) {
Ok(r) => exit(if r.length() > 0 { r[0].to_int() } else { 0 })
Err(e) => {
eprint("error: " + e)
exit(1)
}
}
}