Compile the compositor to a wasm component, run by a small wasmtime host
CI / test (push) Failing after 22s

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>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 01:41:31 -08:00
1 parent 92d2b29889
commit 6aba9b6a4f
42 files changed
+3163 -10

No files matched your search

+258
View File
@@ -0,0 +1,258 @@
///| Minimal POSIX OS primitives for the pure-MoonBit Wayland compositor.
///|
#borrow(path)
extern "C" fn c_unix_listen(path : Bytes) -> Int = "mp_unix_listen"
///|
extern "C" fn c_unix_accept(listen_fd : Int) -> Int = "mp_unix_accept"
///|
#borrow(path)
extern "C" fn c_unix_connect(path : Bytes) -> Int = "mp_unix_connect"
///|
extern "C" fn c_close_fd(fd : Int) -> Int = "mp_close_fd"
///|
#borrow(path)
extern "C" fn c_unlink(path : Bytes) -> Int = "mp_unlink"
///|
extern "C" fn c_poll_read(fd : Int, timeout_ms : Int) -> Int = "mp_poll_read"
///|
#borrow(buf, out_fd)
extern "C" fn c_recvmsg_fd(
fd : Int,
buf : FixedArray[Byte],
max_len : Int,
out_fd : FixedArray[Int],
) -> Int = "mp_recvmsg_fd"
///|
#borrow(buf)
extern "C" fn c_sendmsg_fd(
fd : Int,
buf : Bytes,
len : Int,
send_fd : Int,
) -> Int = "mp_sendmsg_fd"
///|
#borrow(name)
extern "C" fn c_memfd_create(name : Bytes, size : Int) -> Int = "mp_memfd_create"
///|
#borrow(buf)
extern "C" fn c_pwrite_bytes(
fd : Int,
buf : Bytes,
len : Int,
offset : Int,
) -> Int = "mp_pwrite_bytes"
///|
extern "C" fn c_mmap_read(fd : Int, size : Int) -> Bytes = "mp_mmap_read"
///|
#borrow(path)
extern "C" fn c_read_file(path : Bytes) -> Bytes = "mp_read_file"
///|
#borrow(path, buf)
extern "C" fn c_write_file(path : Bytes, buf : Bytes, len : Int) -> Int = "mp_write_file"
///|
extern "C" fn c_sleep_ms(ms : Int) -> Unit = "mp_sleep_ms"
///|
extern "C" fn c_getpid() -> Int = "mp_getpid"
///| Encode ASCII/UTF-8 String into Bytes for C paths/names.
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)
}
///|
pub fn unix_listen(path : String) -> Int {
c_unix_listen(str_to_utf8_bytes(path))
}
///|
pub fn unix_accept(listen_fd : Int) -> Int {
c_unix_accept(listen_fd)
}
///|
pub fn unix_connect(path : String) -> Int {
c_unix_connect(str_to_utf8_bytes(path))
}
///|
pub fn close_fd(fd : Int) -> Unit {
ignore(c_close_fd(fd))
}
///|
pub fn unlink(path : String) -> Unit {
ignore(c_unlink(str_to_utf8_bytes(path)))
}
///|
pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
c_poll_read(fd, timeout_ms)
}
///| Receive bytes and optional SCM_RIGHTS fd. Returns (bytes_read, received_fd).
/// bytes_read == -1 means EAGAIN; -2 means error; 0 means EOF.
pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) {
let out_fd : FixedArray[Int] = FixedArray::make(1, -1)
let n = c_recvmsg_fd(fd, buf, max_len, out_fd)
(n, out_fd[0])
}
///|
pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int {
c_sendmsg_fd(fd, buf, buf.length(), send_fd)
}
///|
pub fn memfd_create(name : String, size : Int) -> Int {
c_memfd_create(str_to_utf8_bytes(name), size)
}
///|
pub fn pwrite_bytes(fd : Int, buf : Bytes, offset : Int) -> Int {
c_pwrite_bytes(fd, buf, buf.length(), offset)
}
///|
pub fn mmap_read(fd : Int, size : Int) -> Bytes {
c_mmap_read(fd, size)
}
///|
pub fn read_file_bytes(path : String) -> Bytes {
c_read_file(str_to_utf8_bytes(path))
}
///|
pub fn write_file_bytes(path : String, data : Bytes) -> Int {
c_write_file(str_to_utf8_bytes(path), data, data.length())
}
///|
pub fn sleep_ms(ms : Int) -> Unit {
c_sleep_ms(ms)
}
///|
pub fn getpid() -> Int {
c_getpid()
}
///|
extern "C" fn c_wall_time_field(which : Int) -> Int = "mp_wall_time_field"
///| 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 {
c_wall_time_field(which)
}
///|
extern "C" fn c_has_exe(name : Bytes) -> Int = "mp_has_exe"
///|
extern "C" fn c_spawn(cmd : Bytes, display : Bytes) -> Int = "mp_spawn"
///| Whether `name` is an executable on PATH.
pub fn has_exe(name : String) -> Bool {
c_has_exe(str_to_utf8_bytes(name)) != 0
}
///| Launch `cmd` detached with WAYLAND_DISPLAY set to `display`.
pub fn spawn(cmd : String, display : String) -> Bool {
c_spawn(str_to_utf8_bytes(cmd), str_to_utf8_bytes(display)) == 0
}
///|
extern "C" fn c_list_desktop(dir : Bytes) -> Bytes = "mp_list_desktop"
///|
extern "C" fn c_getenv(name : Bytes) -> Bytes = "mp_getenv"
///| Newline-separated names of the `*.desktop` files directly in `dir`.
pub fn list_desktop_files(dir : String) -> Array[String] {
let raw = c_list_desktop(str_to_utf8_bytes(dir))
let out : Array[String] = []
let sb = StringBuilder::new()
let mut i = 0
while i < raw.length() {
if raw[i] == b'\n' {
out.push(sb.to_string())
sb.reset()
} else {
sb.write_char(Int::unsafe_to_char(raw[i].to_int()))
}
i = i + 1
}
out
}
///| Environment variable, or "" when unset.
pub fn getenv(name : String) -> String {
let raw = c_getenv(str_to_utf8_bytes(name))
let sb = StringBuilder::new()
for i = 0; i < raw.length(); i = i + 1 {
sb.write_char(Int::unsafe_to_char(raw[i].to_int()))
}
sb.to_string()
}
///|
extern "C" fn c_mkdir_p(path : Bytes) -> Int = "mp_mkdir_p"
///| Create `path` and any missing parents; true when it exists afterwards.
pub fn mkdir_p(path : String) -> Bool {
c_mkdir_p(str_to_utf8_bytes(path)) == 0
}
///|
#borrow(path)
extern "C" fn c_read_proc(path : Bytes) -> Bytes = "mp_read_proc"
///| Contents of a small /proc or /sys file ("" when unreadable). Unlike `read_file_bytes`
/// this works on files that report size 0.
pub fn read_proc_bytes(path : String) -> Bytes {
c_read_proc(str_to_utf8_bytes(path))
}
///| Milliseconds on a monotonic clock.
pub fn monotonic_ms() -> UInt64 {
@env.now()
}
///| Print a line to stderr (stdout on native).
pub fn log(message : String) -> Unit {
println(message)
}