Panel modules in the wasm compositor: the host runs them (moonplasma:host/plasmoids)
CI / test (push) Failing after 46s

A component can't embed a second wasm runtime, so the compositor drives panel modules through a
host import and the wasmtime host instantiates them: load, render, popup, on-event, plus the data
they read (apps, windows, layouts, modules, system) and the requests they make (launch, focus
window, set layout, load/unload, browse), with a zenity file chooser for 'Add widget'.

- wit/worlds.wit: world plasmoid-host, every capability a module can import + the plasmoid export.
  The host instantiates any module against it; a module imports the subset it uses.
- wit/host/plasmoids.wit + generated MoonBit bindings; compositor/wasm gets a wasm backend
  (wasm_host.mbt) with the same public API as the native wasmer one.
- host-wasm/src/plasmoid.rs: typed component bindings, one Store per module, shared host data.
  Tests load the built launcher/layout modules and drive them (typed filter, Enter, launch request).
- scripts/gen-native-stubs.py keeps a package's imports when it rewrites moon.pkg.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 02:09:57 -08:00
1 parent 5c3bebc7fd
commit e769566d8f
23 files changed
+2001 -302

No files matched your search

+138
View File
@@ -15,9 +15,17 @@ use std::process::{Command, Stdio};
use wasmtime::component::{Component, HasSelf, Linker};
use wasmtime::{Config, Engine, Result, Store};
mod plasmoid;
wasmtime::component::bindgen!({
path: "../wit/host",
world: "compositor",
// The desktop value types are the same WIT the modules use: share the Rust types, so draw ops
// flow from a module straight to the compositor without conversion.
with: {
"moonplasma:desktop/types@0.1.0": plasmoid::ph::moonplasma::desktop::types,
"moonplasma:desktop/theme@0.1.0": plasmoid::ph::moonplasma::desktop::theme,
},
});
use ab_glyph::{Font, FontVec, OutlineCurve};
@@ -33,6 +41,8 @@ struct State {
started: Option<std::time::Instant>,
/// TTF faces loaded by the component (`font.load`), by slot.
fonts: [Option<FontVec>; 4],
/// Runs the panel modules; set once the engine exists.
pl: Option<plasmoid::Runtime>,
}
fn sockaddr_un(path: &str) -> (libc::sockaddr_un, libc::socklen_t) {
@@ -91,6 +101,132 @@ impl State {
}
}
impl State {
fn pl(&mut self) -> &mut plasmoid::Runtime {
self.pl.as_mut().expect("plasmoid runtime is set before the component runs")
}
}
impl moonplasma::host::plasmoids::Host for State {
fn load(&mut self, path: String) -> Result<moonplasma::host::plasmoids::Loaded, String> {
let l = self.pl().load(&path)?;
Ok(moonplasma::host::plasmoids::Loaded {
handle: l.handle,
id: l.info.id,
name: l.info.name,
preferred: l.info.preferred,
min_width: l.info.min_width,
stretch: l.info.stretch,
imports: l.imports,
})
}
fn unload(&mut self, handle: u32) {
self.pl().unload(handle);
}
fn set_tokens(&mut self, t: plasmoid::ph::moonplasma::desktop::theme::Tokens) {
self.pl().shared.borrow_mut().tokens = Some(t);
}
fn render(
&mut self,
handle: u32,
box_: plasmoid::ph::moonplasma::desktop::types::Size,
) -> Vec<plasmoid::ph::moonplasma::desktop::types::DrawOp> {
self.pl().render(handle, box_)
}
fn popup(&mut self, handle: u32) -> Option<moonplasma::host::plasmoids::PopupSurface> {
self.pl().popup(handle).map(|p| moonplasma::host::plasmoids::PopupSurface { box_: p.size, ops: p.ops })
}
fn on_event(
&mut self,
handle: u32,
on_popup: bool,
event: plasmoid::ph::moonplasma::desktop::types::InputEvent,
) -> bool {
self.pl().on_event(handle, on_popup, event)
}
fn publish_table(&mut self, which: u32, rows: Vec<u8>) {
if let Some(t) = self.pl().shared.borrow_mut().tables.get_mut(which as usize) {
*t = rows;
}
}
fn publish_state(&mut self, layout_mode: i32, inspector_on: bool) {
let sh = &self.pl().shared;
let mut sh = sh.borrow_mut();
sh.layout_mode = layout_mode;
sh.inspector_on = inspector_on;
}
fn publish_system(&mut self, s: moonplasma::host::plasmoids::SystemSnapshot) {
use plasmoid::ph::moonplasma::desktop::system::Snapshot;
self.pl().shared.borrow_mut().system = Some(Snapshot {
cpu_permille: s.cpu_permille,
mem_used_kib: s.mem_used_kib,
mem_total_kib: s.mem_total_kib,
battery_percent: s.battery_percent,
charging: s.charging,
uptime_secs: s.uptime_secs,
net_rx_bytes_per_sec: s.net_rx_bytes_per_sec,
net_tx_bytes_per_sec: s.net_tx_bytes_per_sec,
load_centi: s.load_centi,
});
}
fn publish_windows(&mut self, wins: Vec<moonplasma::host::plasmoids::WindowEntry>) {
self.pl().shared.borrow_mut().windows =
wins.into_iter().map(|w| (w.id, w.title, w.app_id, w.focused)).collect();
}
fn next_request(&mut self) -> Option<moonplasma::host::plasmoids::Request> {
use moonplasma::host::plasmoids::Request as R;
use plasmoid::Req;
let r = self.pl().shared.borrow_mut().requests.pop_front()?;
Some(match r {
Req::Launch(s) => R::Launch(s),
Req::FocusWindow(n) => R::FocusWindow(n),
Req::SetLayout(n) => R::SetLayout(n),
Req::SetInspector(b) => R::SetInspector(b),
Req::LoadModule(s) => R::LoadModule(s),
Req::UnloadInstance(s) => R::UnloadInstance(s),
Req::Browse => R::Browse,
})
}
fn take_focus_request(&mut self) -> Option<u64> {
self.pl().shared.borrow_mut().focus_req.take()
}
fn list_wasm_files(&mut self, dir: String) -> Vec<String> {
let Ok(rd) = std::fs::read_dir(dir) else { return Vec::new() };
let mut v: Vec<String> = rd
.filter_map(|e| e.ok())
.map(|e| e.file_name().to_string_lossy().into_owned())
.filter(|n| n.ends_with(".wasm"))
.collect();
v.sort();
v
}
fn picker_start(&mut self) -> bool {
self.pl().picker_start()
}
fn picker_poll(&mut self) -> moonplasma::host::plasmoids::PickerState {
use moonplasma::host::plasmoids::PickerState as P;
match self.pl().picker_poll() {
None => P::Open,
Some(None) => P::Cancelled,
Some(Some(path)) => P::Chosen(path),
}
}
}
impl moonplasma::host::font::Host for State {
fn load(&mut self, slot: u32, path: String) -> bool {
if slot as usize >= self.fonts.len() {
@@ -478,7 +614,9 @@ fn main() -> Result<()> {
let mut linker: Linker<State> = Linker::new(&engine);
moonplasma::host::sys::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
moonplasma::host::font::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
moonplasma::host::plasmoids::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
let mut store = Store::new(&engine, State::default());
store.data_mut().pl = Some(plasmoid::Runtime::new(&engine)?);
let compositor = Compositor::instantiate(&mut store, &component, &linker)?;
let code = compositor.call_run(&mut store, &args)?;
std::process::exit(code);