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

+3
View File
@@ -0,0 +1,3 @@
The operating-system surface the compositor component needs, nothing more. The native host
implements it with real syscalls; the component holds only integer handles (fds) and bytes.
Mirrors compositor/sys.
+113
View File
@@ -0,0 +1,113 @@
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
///|
fn wasmImportUnixListen(p0 : Int, p1 : Int) -> Int = "moonplasma:host/sys@0.1.0" "unix-listen"
///|
fn wasmImportUnixAccept(p0 : Int) -> Int = "moonplasma:host/sys@0.1.0" "unix-accept"
///|
fn wasmImportUnixConnect(p0 : Int, p1 : Int) -> Int = "moonplasma:host/sys@0.1.0" "unix-connect"
///|
fn wasmImportCloseFd(p0 : Int) = "moonplasma:host/sys@0.1.0" "close-fd"
///|
fn wasmImportUnlink(p0 : Int, p1 : Int) = "moonplasma:host/sys@0.1.0" "unlink"
///|
fn wasmImportPollRead(p0 : Int, p1 : Int) -> Int = "moonplasma:host/sys@0.1.0" "poll-read"
///|
fn wasmImportRecvmsg(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/sys@0.1.0" "recvmsg"
///|
fn wasmImportSendmsg(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int = "moonplasma:host/sys@0.1.0" "sendmsg"
///|
fn wasmImportMemfdCreate(p0 : Int, p1 : Int, p2 : Int) -> Int = "moonplasma:host/sys@0.1.0" "memfd-create"
///|
fn wasmImportPwrite(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int = "moonplasma:host/sys@0.1.0" "pwrite"
///|
fn wasmImportMmapRead(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/sys@0.1.0" "mmap-read"
///|
fn wasmImportReadFile(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/sys@0.1.0" "read-file"
///|
fn wasmImportWriteFile(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int = "moonplasma:host/sys@0.1.0" "write-file"
///|
fn wasmImportReadProc(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/sys@0.1.0" "read-proc"
///|
fn wasmImportMkdirP(p0 : Int, p1 : Int) -> Int = "moonplasma:host/sys@0.1.0" "mkdir-p"
///|
fn wasmImportListDesktopFiles(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/sys@0.1.0" "list-desktop-files"
///|
fn wasmImportSleepMs(p0 : Int) = "moonplasma:host/sys@0.1.0" "sleep-ms"
///|
fn wasmImportMonotonicMs() -> Int64 = "moonplasma:host/sys@0.1.0" "monotonic-ms"
///|
fn wasmImportLog(p0 : Int, p1 : Int) = "moonplasma:host/sys@0.1.0" "log"
///|
fn wasmImportGetpid() -> Int = "moonplasma:host/sys@0.1.0" "getpid"
///|
fn wasmImportWallTimeField(p0 : Int) -> Int = "moonplasma:host/sys@0.1.0" "wall-time-field"
///|
fn wasmImportGetenv(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/sys@0.1.0" "getenv"
///|
fn wasmImportHasExe(p0 : Int, p1 : Int) -> Int = "moonplasma:host/sys@0.1.0" "has-exe"
///|
fn wasmImportSpawn(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int = "moonplasma:host/sys@0.1.0" "spawn"
///|
#owned(str)
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0)
///|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load)
///|
extern "wasm" fn mbt_ffi_free(position : Int) =
#|(func (param i32) local.get 0 call $moonbit.decref)
///|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
#|(func (param i32) (result i32) (local i32)
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
#| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1)
///|
extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
#|(func (param i32) (param i32) (result i32)
#| local.get 0
#| local.get 1 call $moonbit.init_array8
#| local.get 0)
///|
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
#|(func (param i32) (param i32) (result i32)
#| local.get 0
#| local.get 1 call $moonbit.init_array16
#| local.get 0)
///|
#owned(bytes)
extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int =
#|(func (param i32) (result i32) local.get 0)
+9
View File
@@ -0,0 +1,9 @@
warnings = "-44"
// ffi.mbt is generated and wasm-only; native builds use native_stub.mbt (scripts/gen-native-stubs.py).
options(
targets: {
"ffi.mbt": ["wasm", "wasm-gc"],
"native_stub.mbt": ["native"],
},
)
@@ -0,0 +1,156 @@
// Generated by scripts/gen-native-stubs.py. Native stand-ins for the wasm-only ffi.mbt.
///|
fn wasmImportUnixListen(p0 : Int, p1 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportUnixAccept(p0 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportUnixConnect(p0 : Int, p1 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportCloseFd(p0 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportUnlink(p0 : Int, p1 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportPollRead(p0 : Int, p1 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportRecvmsg(p0 : Int, p1 : Int, p2 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportSendmsg(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportMemfdCreate(p0 : Int, p1 : Int, p2 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportPwrite(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportMmapRead(p0 : Int, p1 : Int, p2 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportReadFile(p0 : Int, p1 : Int, p2 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportWriteFile(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportReadProc(p0 : Int, p1 : Int, p2 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportMkdirP(p0 : Int, p1 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportListDesktopFiles(p0 : Int, p1 : Int, p2 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportSleepMs(p0 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportMonotonicMs() -> Int64 {
abort("wasm-only host import")
}
///|
fn wasmImportLog(p0 : Int, p1 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportGetpid() -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportWallTimeField(p0 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportGetenv(p0 : Int, p1 : Int, p2 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportHasExe(p0 : Int, p1 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportSpawn(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn mbt_ffi_str2ptr(str : String) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_load32(offset : Int) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_free(position : Int) -> Unit {
abort("wasm-only")
}
///|
fn mbt_ffi_malloc(size : Int) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] {
abort("wasm-only")
}
///|
fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String {
abort("wasm-only")
}
///|
fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int {
abort("wasm-only")
}
@@ -0,0 +1,60 @@
// Generated using `moon info`, DON'T EDIT IT
package "moonplasma/desktop/interface/moonplasma/host/sys"
// Values
pub fn close_fd(Int32) -> Unit
pub fn getenv(String) -> String
pub fn getpid() -> Int32
pub fn has_exe(String) -> Bool
pub fn list_desktop_files(String) -> Array[String]
pub fn memfd_create(String, Int32) -> Int32
pub fn mkdir_p(String) -> Bool
pub fn mmap_read(Int32, Int32) -> FixedArray[Byte]
pub fn poll_read(Int32, Int32) -> Int32
pub fn pwrite(Int32, FixedArray[Byte], Int32) -> Int32
pub fn read_file(String) -> FixedArray[Byte]
pub fn read_proc(String) -> FixedArray[Byte]
pub fn recvmsg(Int32, UInt32) -> Received
pub fn sendmsg(Int32, FixedArray[Byte], Int32) -> Int32
pub fn sleep_ms(Int32) -> Unit
pub fn spawn(String, String) -> Bool
pub fn unix_accept(Int32) -> Int32
pub fn unix_connect(String) -> Int32
pub fn unix_listen(String) -> Int32
pub fn unlink(String) -> Unit
pub fn wall_time_field(Int32) -> Int32
pub fn write_file(String, FixedArray[Byte]) -> Int32
// Errors
// Types and methods
pub(all) struct Received {
count : Int32
fd : Int32
data : FixedArray[Byte]
}
// Type aliases
// Traits
+274
View File
@@ -0,0 +1,274 @@
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
///|
/// Result of `recvmsg`: byte count (-1 would-block, -2 error, 0 EOF), an fd received over
/// SCM_RIGHTS (-1 when none), and the bytes read.
pub(all) struct Received {
count : Int; fd : Int; data : FixedArray[Byte]
}
///|
/// Listen on a unix socket at `path`; -1 on failure.
pub fn unix_listen(path : String) -> Int {
let ptr = mbt_ffi_str2ptr(path)
let result : Int = wasmImportUnixListen(ptr, path.length());
let ret = result
mbt_ffi_free(ptr)
return ret
}
///|
/// Accept a pending client (non-blocking); -1 when none.
pub fn unix_accept(listen_fd : Int) -> Int {
let result : Int = wasmImportUnixAccept(listen_fd);
let ret = result
return ret
}
///|
pub fn unix_connect(path : String) -> Int {
let ptr = mbt_ffi_str2ptr(path)
let result : Int = wasmImportUnixConnect(ptr, path.length());
let ret = result
mbt_ffi_free(ptr)
return ret
}
///|
pub fn close_fd(fd : Int) -> Unit {
wasmImportCloseFd(fd);
}
///|
pub fn unlink(path : String) -> Unit {
let ptr = mbt_ffi_str2ptr(path)
wasmImportUnlink(ptr, path.length());
mbt_ffi_free(ptr)
}
///|
/// 1 when `fd` is readable within `timeout-ms`, 0 on timeout, <0 on error.
pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
let result : Int = wasmImportPollRead(fd, timeout_ms);
let ret = result
return ret
}
///|
pub fn recvmsg(fd : Int, max_len : UInt) -> Received {
let return_area = mbt_ffi_malloc(16)
wasmImportRecvmsg(fd, (max_len).reinterpret_as_int(), return_area);
let result = mbt_ffi_ptr2bytes(mbt_ffi_load32((return_area) + 8), mbt_ffi_load32((return_area) + 12))
let ret = Received::{count : mbt_ffi_load32((return_area) + 0), fd : mbt_ffi_load32((return_area) + 4), data : result}
mbt_ffi_free(return_area)
return ret
}
///|
/// Send `data`, with `send-fd` (or -1) as SCM_RIGHTS; bytes sent or <0.
pub fn sendmsg(fd : Int, data : FixedArray[Byte], send_fd : Int) -> Int {
let ptr = mbt_ffi_bytes2ptr(data)
let result : Int = wasmImportSendmsg(fd, ptr, data.length(), send_fd);
let ret = result
mbt_ffi_free(ptr)
return ret
}
///|
pub fn memfd_create(name : String, size : Int) -> Int {
let ptr = mbt_ffi_str2ptr(name)
let result : Int = wasmImportMemfdCreate(ptr, name.length(), size);
let ret = result
mbt_ffi_free(ptr)
return ret
}
///|
pub fn pwrite(fd : Int, data : FixedArray[Byte], offset : Int) -> Int {
let ptr = mbt_ffi_bytes2ptr(data)
let result : Int = wasmImportPwrite(fd, ptr, data.length(), offset);
let ret = result
mbt_ffi_free(ptr)
return ret
}
///|
/// Copy `size` bytes of the file behind `fd` (mapped read-only by the host).
pub fn mmap_read(fd : Int, size : Int) -> FixedArray[Byte] {
let return_area = mbt_ffi_malloc(8)
wasmImportMmapRead(fd, size, return_area);
let result = mbt_ffi_ptr2bytes(mbt_ffi_load32((return_area) + 0), mbt_ffi_load32((return_area) + 4))
let ret = result
mbt_ffi_free(return_area)
return ret
}
///|
pub fn read_file(path : String) -> FixedArray[Byte] {
let ptr = mbt_ffi_str2ptr(path)
let return_area = mbt_ffi_malloc(8)
wasmImportReadFile(ptr, path.length(), return_area);
let result = mbt_ffi_ptr2bytes(mbt_ffi_load32((return_area) + 0), mbt_ffi_load32((return_area) + 4))
let ret = result
mbt_ffi_free(ptr)
mbt_ffi_free(return_area)
return ret
}
///|
pub fn write_file(path : String, data : FixedArray[Byte]) -> Int {
let ptr = mbt_ffi_str2ptr(path)
let ptr0 = mbt_ffi_bytes2ptr(data)
let result : Int = wasmImportWriteFile(ptr, path.length(), ptr0, data.length());
let ret = result
mbt_ffi_free(ptr)
mbt_ffi_free(ptr0)
return ret
}
///|
/// Contents of a small /proc or /sys file ("" when unreadable).
pub fn read_proc(path : String) -> FixedArray[Byte] {
let ptr = mbt_ffi_str2ptr(path)
let return_area = mbt_ffi_malloc(8)
wasmImportReadProc(ptr, path.length(), return_area);
let result = mbt_ffi_ptr2bytes(mbt_ffi_load32((return_area) + 0), mbt_ffi_load32((return_area) + 4))
let ret = result
mbt_ffi_free(ptr)
mbt_ffi_free(return_area)
return ret
}
///|
pub fn mkdir_p(path : String) -> Bool {
let ptr = mbt_ffi_str2ptr(path)
let result : Int = wasmImportMkdirP(ptr, path.length());
let ret = (result != 0)
mbt_ffi_free(ptr)
return ret
}
///|
/// Names of the *.desktop files directly in `dir`.
pub fn list_desktop_files(dir : String) -> Array[String] {
let ptr = mbt_ffi_str2ptr(dir)
let return_area = mbt_ffi_malloc(8)
wasmImportListDesktopFiles(ptr, dir.length(), return_area);
let array : Array[String] = [];
for index = 0; index < (mbt_ffi_load32((return_area) + 4)); index = index + 1 {
let iter_base = (mbt_ffi_load32((return_area) + 0)) + (index * 8)
let result = mbt_ffi_ptr2str(mbt_ffi_load32((iter_base) + 0), mbt_ffi_load32((iter_base) + 4))
array.push(result)
}
mbt_ffi_free(mbt_ffi_load32((return_area) + 0))
let ret = array
mbt_ffi_free(ptr)
mbt_ffi_free(return_area)
return ret
}
///|
pub fn sleep_ms(ms : Int) -> Unit {
wasmImportSleepMs(ms);
}
///|
/// Milliseconds on a monotonic clock (event timestamps).
pub fn monotonic_ms() -> UInt64 {
let result : Int64 = wasmImportMonotonicMs();
let ret = (result).reinterpret_as_uint64()
return ret
}
///|
/// Print a line to the host's stderr.
pub fn log(message : String) -> Unit {
let ptr = mbt_ffi_str2ptr(message)
wasmImportLog(ptr, message.length());
mbt_ffi_free(ptr)
}
///|
pub fn getpid() -> Int {
let result : Int = wasmImportGetpid();
let ret = result
return ret
}
///|
/// 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 {
let result : Int = wasmImportWallTimeField(which);
let ret = result
return ret
}
///|
pub fn getenv(name : String) -> String {
let ptr = mbt_ffi_str2ptr(name)
let return_area = mbt_ffi_malloc(8)
wasmImportGetenv(ptr, name.length(), return_area);
let result = mbt_ffi_ptr2str(mbt_ffi_load32((return_area) + 0), mbt_ffi_load32((return_area) + 4))
let ret = result
mbt_ffi_free(ptr)
mbt_ffi_free(return_area)
return ret
}
///|
pub fn has_exe(name : String) -> Bool {
let ptr = mbt_ffi_str2ptr(name)
let result : Int = wasmImportHasExe(ptr, name.length());
let ret = (result != 0)
mbt_ffi_free(ptr)
return ret
}
///|
/// Launch `cmd` detached with WAYLAND_DISPLAY set to `display`.
pub fn spawn(cmd : String, display : String) -> Bool {
let ptr = mbt_ffi_str2ptr(cmd)
let ptr0 = mbt_ffi_str2ptr(display)
let result : Int = wasmImportSpawn(ptr, cmd.length(), ptr0, display.length());
let ret = (result != 0)
mbt_ffi_free(ptr)
mbt_ffi_free(ptr0)
return ret
}