///| 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) } } }