Files
chandra/compositor/sys/sys_wasm.mbt
T
nandiandClaude Sonnet 5.5 6aba9b6a4f
CI / test (push) Failing after 22s
Compile the compositor to a wasm component, run by a small wasmtime host
The whole compositor (Wayland server, window manager, scene, rasteriser, shell) now builds as
build/components/compositor.wasm: a component that imports only moonplasma:host/sys
(wit/host/sys.wit: sockets + SCM_RIGHTS, memfd, poll, spawn, files, clock) and exports run().
host-wasm/ is the native side: a wasmtime program implementing that interface with libc.

- compositor/sys: per-target backends (sys_native.mbt over the C shim, sys_wasm.mbt over the WIT
  imports) behind one API; wayland/nested/host/shell/scene/renderer/gfx build for wasm unchanged.
- compositor/app: the entry point (arg parsing + main loop) as a library for both targets;
  --serve keeps refreshing a PNG so a live compositor can be inspected without a display.
- compositor/font, compositor/wasm: wasm-side placeholders (bitmap font; no panel modules yet,
  since a component can't embed a second wasm runtime: that becomes a host import).
- scripts/gen-native-stubs.py: native stand-ins for wasm-only generated imports.
- just compositor-wasm / host-wasm / run-wasm.

Verified: foot connects to the component's socket and renders into it; native build and
tests unchanged (16/23, the same 7 need unbuilt panel modules).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 01:41:31 -08:00

170 lines
4.2 KiB
MoonBit

///| The compositor as a wasm component: the same API as sys_native.mbt, with every operation
/// forwarded to the host through the `moonplasma:host/sys` imports. The component holds only
/// integer handles (fds) and bytes; the host owns the real descriptors.
///| Encode a String into UTF-8 Bytes.
pub fn str_to_utf8_bytes(s : String) -> Bytes {
let buf : Array[Byte] = []
for ch in s {
let code = ch.to_int()
if code < 0x80 {
buf.push(code.to_byte())
} else if code < 0x800 {
buf.push((0xC0 | (code >> 6)).to_byte())
buf.push((0x80 | (code & 0x3F)).to_byte())
} else if code < 0x10000 {
buf.push((0xE0 | (code >> 12)).to_byte())
buf.push((0x80 | ((code >> 6) & 0x3F)).to_byte())
buf.push((0x80 | (code & 0x3F)).to_byte())
} else {
buf.push((0xF0 | (code >> 18)).to_byte())
buf.push((0x80 | ((code >> 12) & 0x3F)).to_byte())
buf.push((0x80 | ((code >> 6) & 0x3F)).to_byte())
buf.push((0x80 | (code & 0x3F)).to_byte())
}
}
Bytes::from_array(buf)
}
///|
fn to_fixed(b : Bytes) -> FixedArray[Byte] {
let out : FixedArray[Byte] = FixedArray::make(b.length(), b'\x00')
for i = 0; i < b.length(); i = i + 1 {
out[i] = b[i]
}
out
}
///|
fn from_fixed(a : FixedArray[Byte]) -> Bytes {
Bytes::from_fixedarray(a)
}
///|
pub fn unix_listen(path : String) -> Int {
@hostsys.unix_listen(path)
}
///|
pub fn unix_accept(listen_fd : Int) -> Int {
@hostsys.unix_accept(listen_fd)
}
///|
pub fn unix_connect(path : String) -> Int {
@hostsys.unix_connect(path)
}
///|
pub fn close_fd(fd : Int) -> Unit {
@hostsys.close_fd(fd)
}
///|
pub fn unlink(path : String) -> Unit {
@hostsys.unlink(path)
}
///|
pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
@hostsys.poll_read(fd, timeout_ms)
}
///| Receive bytes and an optional SCM_RIGHTS fd into `buf`. Returns (bytes_read, received_fd);
/// bytes_read == -1 means EAGAIN, -2 an error, 0 EOF.
pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) {
let r = @hostsys.recvmsg(fd, max_len.reinterpret_as_uint())
if r.count > 0 {
let n = if r.count < r.data.length() { r.count } else { r.data.length() }
for i = 0; i < n && i < buf.length(); i = i + 1 {
buf[i] = r.data[i]
}
}
(r.count, r.fd)
}
///|
pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int {
@hostsys.sendmsg(fd, to_fixed(buf), send_fd)
}
///|
pub fn memfd_create(name : String, size : Int) -> Int {
@hostsys.memfd_create(name, size)
}
///|
pub fn pwrite_bytes(fd : Int, buf : Bytes, offset : Int) -> Int {
@hostsys.pwrite(fd, to_fixed(buf), offset)
}
///|
pub fn mmap_read(fd : Int, size : Int) -> Bytes {
from_fixed(@hostsys.mmap_read(fd, size))
}
///|
pub fn read_file_bytes(path : String) -> Bytes {
from_fixed(@hostsys.read_file(path))
}
///|
pub fn write_file_bytes(path : String, data : Bytes) -> Int {
@hostsys.write_file(path, to_fixed(data))
}
///|
pub fn sleep_ms(ms : Int) -> Unit {
@hostsys.sleep_ms(ms)
}
///|
pub fn getpid() -> Int {
@hostsys.getpid()
}
///| Local wall clock: 0 year, 1 month (1-12), 2 day, 3 hour, 4 minute, 5 second, 6 weekday (0 = Sunday).
pub fn wall_time_field(which : Int) -> Int {
@hostsys.wall_time_field(which)
}
///| Whether `name` is an executable on PATH.
pub fn has_exe(name : String) -> Bool {
@hostsys.has_exe(name)
}
///| Launch `cmd` detached with WAYLAND_DISPLAY set to `display`.
pub fn spawn(cmd : String, display : String) -> Bool {
@hostsys.spawn(cmd, display)
}
///| Names of the `*.desktop` files directly in `dir`.
pub fn list_desktop_files(dir : String) -> Array[String] {
@hostsys.list_desktop_files(dir)
}
///| Environment variable, or "" when unset.
pub fn getenv(name : String) -> String {
@hostsys.getenv(name)
}
///| Create `path` and any missing parents; true when it exists afterwards.
pub fn mkdir_p(path : String) -> Bool {
@hostsys.mkdir_p(path)
}
///| Contents of a small /proc or /sys file ("" when unreadable).
pub fn read_proc_bytes(path : String) -> Bytes {
from_fixed(@hostsys.read_proc(path))
}
///| Milliseconds on the host's monotonic clock.
pub fn monotonic_ms() -> UInt64 {
@hostsys.monotonic_ms()
}
///| Print a line to the host's stderr.
pub fn log(message : String) -> Unit {
@hostsys.log(message)
}