I now have the complete picture. Here is the detailed architecture report.
Pathland WIT World & Host/Demo Crates — Architecture Report
1. WIT World — /Users/michael/Development/michaelkrog/Pathland/wit/pathland.wit
Package: package pathland:view; (line 13)
Interface types (lines 16–66)
| Item | Lines | Definition |
|---|
variant component | 18–24 | hstack, vstack, text, button, spacer (only 5 of the 12 protocol component IDs) |
variant property | 27–33 | spacing, padding, font-size, color, background-color |
record point | 36–40 | x: f32, y: f32, secondary: bool |
record hover | 43–48 | x: f32, y: f32, hovering: bool, leaving: bool |
record key | 51–55 | key-code: u16, modifiers: u8, repeat: bool |
variant event | 59–65 | pointer-down(tuple<u32, point>), pointer-move(tuple<u32, hover>), pointer-up(tuple<u32, point>), key-down(tuple<u32, key>), key-up(tuple<u32, u16, u8>) |
Interface builder (lines 69–84) — flat, id-based tree mutation
use types.{component, property}; (line 70). Exact signatures:
create-node (line 75): func(id: u32, component: component) — create/reset a node; host owns stable ids.
insert-child (line 77): func(parent: u32, child: u32, index: u32) — u32::MAX appends.
remove-child (line 79): func(parent: u32, child: u32)
set-text (line 81): func(id: u32, text: string)
set-property (line 83): func(id: u32, property: property, value: f32) — f32 payload.
Interface engine (lines 87–97) — frame lifecycle & opcode output
begin-frame (line 89): func()
emit (line 92): func() — reconcile retained tree, write deltas into ring/arena in linear memory.
end-frame (line 94): func() — publishes frameCount.
take-batch (line 96): func() -> list<u8> — returns pending frame as network batch (opcodes + arena delta).
Interface input (lines 99–105) — raw-input ingress (host → guest)
dispatch (line 104): func(event: event)
Interface app (lines 107–113) — host-provided callback
on-event (line 112): func(event: event)
World pathland (lines 115–121)
world pathland { // 116
import app; // 117 (host → guest import)
export builder; // 118 (guest exports)
export engine; // 119 (guest exports)
export input; // 120 (guest exports)
}
2. pathland-guest — /Users/michael/Development/michaelkrog/Pathland/lib/rust/crates/pathland-guest/src/lib.rs
gating
- Line 21:
#![cfg(target_arch = "wasm32")] — the entire crate is empty for native builds; only built as a wasm32 component (cdylib; Cargo.toml:16–17).
FlatNode (lines 41–48)
struct FlatNode {
id: u32,
view_component: pathland_view::Component,
properties: BTreeMap<u16, u32>,
parent: Option<u32>,
children: Vec<u32>,
}
State (lines 55–61) — the guest's retained state
struct State {
nodes: HashMap<u32, FlatNode>, // retained flat tree
engine: Engine, // diff-based pathland_engine
mem: Vec<u8>, // the WASM linear memory buffer (header+ring+arena)
layout: MemoryLayout, // fixed layout descriptor
encoder: BatchEncoder, // streamed network-batch encoder
}
- Stored in a global
static STATE: Mutex<Option<State>> (line 63).
ensure_state() (lines 70–84) lazily initializes: MemoryLayout::default() → vec![0u8; layout.total_bytes()] (line 74) → init_memory(&mut mem, &layout) (line 75).
The WASM memory + ring location (critical)
The ring buffer and arena live inside the guest's own WASM linear memory, in State.mem: Vec<u8>. Its layout is defined in pathland-opcode/src/memory.rs:
- Header 64 bytes (offsets
memory.rs:42), ring of 4096 slots × 16 bytes = 65536 bytes (memory.rs:14, 64–67), arena 1 MiB (memory.rs:16) → total_bytes() ≈ 1,114,176 (memory.rs:70–72).
Guest (opcode/src/lib.rs:99–120) splits mem into disjoint header / slots / arena slices and writes opcodes at writeCursor; end_frame bumps frameCount (lib.rs:139–142).
BuilderGuest impl (lines 130–203)
create_node(id: u32, component: Component) (131–144): entry(id).or_insert(...), then resets view_component and clears properties.
insert_child(parent, child, index) (146–163): u32::MAX → append; clamps index; sets child.parent = Some(parent).
remove_child(parent, child) (165–175): retains children, clears parent.
set_text(id, text: String) (177–188): writes into Text { text } or Button { label }.
set_property(id, property, value: f32) (190–202): maps to protocol id via to_property_id (98–106); colors stored as value as u32, everything else as value.to_bits().
EngineGuest impl (lines 205–240)
begin_frame (206–208): just ensure_state().
emit (210–221) — the core write-to-ring path:
let root = root_id(&s.nodes).and_then(|r| materialize(&s.nodes, r));
if let Some(root) = root {
let mut ring = RingGuest::new(&mut s.mem, &s.layout); // 216
ring.begin_frame(); // 217
s.engine.emit(&root, &mut ring).ok(); // 218 diff → TREE/STYLE opcodes
ring.end_frame(); // 219 bumps frameCount
}
(RingGuest is an alias for pathland_opcode::Guest, imported at line 28.)
end_frame (line 223): empty — the ring end_frame already ran inside emit.
take_batch (225–239) — drains the ring inside the guest memory with a Host view and encodes:
let mut host = Host::new(&mut s.mem, &s.layout); // 230
for frame in host.frames() { // 232
let opcodes: Vec<Opcode> = frame.opcodes().collect(); // 233
let arena = frame.arena().to_vec(); // 234
let frame_count = host.frame_count(); // 235
bytes.extend(s.encoder.encode(frame_count, &opcodes, &arena)); // 236
}
Bytes cross the WIT boundary as list<u8>. The host-side wasmtime store copies them out of WASM linear memory into native Vec<u8>.
InputGuest impl (lines 242–246) + route_event (249–251)
dispatch(event) → route_event(event) → bindings::pathland::view::app::on_event(event) (line 250) — calls back into the host's app import. Exported via bindings::export!(State ...) at line 253.
materialize / root_id
materialize (109–120): pre-order walk of HashMap<u32, FlatNode> → pathland_view::Node tree rooted at root_id, cloning id/properties/children.
root_id (123–128): the single node with parent.is_none().
bindings
Generated by wit-bindgen 0.41.0 into src/bindings.rs (806 lines; checked in). Configured via Cargo.toml:19–24 ([package.metadata.component], package = "pathland:view", world = "pathland", derives PartialEq).
3. pathland-wit-host — /Users/michael/Development/michaelkrog/Pathland/lib/rust/crates/pathland-wit-host/src/lib.rs
bindgen! (lines 22–25)
bindgen!({
world: "pathland",
path: "../../../../wit",
});
Generates (under wit/wit-bindgen outputs) the pathland module with:
Pathland instance type (GuestHost holds it, line 89) and accessors pathland_view_builder(), pathland_view_engine(), pathland_view_input() (used at lines 135, 145, 181).
exports::pathland::view::builder::Guest, .engine::Guest, .input::Guest callable handles (used at line 187).
pathland::view::types::Host, pathland::view::app::Host traits and pathland::view::app::add_to_linker (lines 62–67, 113).
pub use pathland::view::types::{self as types, Component, Event, Property}; re-exported at line 27.
AppHandler trait + NullApp (lines 46–56)
pub trait AppHandler: Send {
fn on_event(&mut self, event: &Event); // line 48
}
pub struct NullApp; // line 52
AppHost (lines 58–84) — store data type
- Implements
pathland::view::types::Host (line 62) and pathland::view::app::Host (63–67); the latter forwards to AppHandler::on_event(self.app.as_mut(), &event).
- Struct (71–75):
app: Box<dyn AppHandler>, wasi: WasiCtx, table: ResourceTable.
WasiView impl (77–84): satisfies the wasip1 guest's WASI reactor imports even though Pathland doesn't use them.
GuestHost (lines 87–149)
- Struct (87–90):
store: Store<AppHost>, pathland: Pathland.
new(component_bytes, app) (94–117): wasmtime Config::wasm_component_model(true) (96), builds Store<AppHost> with WASI ctx (98–105), compiles Component (107), add_wasi_to_linker::<AppHost> (111), pathland::view::app::add_to_linker::<_, HasSelf<_>> (113), then Pathland::instantiate(...) (114).
app() (120–122): &mut dyn AppHandler from store data.
import_tree(root: &Node) (128–131): delegates to free function import_tree(&mut self.pathland, &mut self.store, root).
emit() -> Result<Vec<u8>, HostError> (134–141): calls engine builder in sequence — call_begin_frame (136), call_emit (137), call_end_frame (138), then call_take_batch (139) → returns the network-batch bytes.
dispatch(event: &Event) (144–148): input.call_dispatch(&mut self.store, *event).
Bridge: import_tree / import_node (lines 176–216)
import_tree (176–184): grabs the builder handle and recurses.
import_node (186–216), pre-order:
call_create_node(node.id, to_wit_component(&node.component)) (line 191).
- For
Text/Button: call_set_text(node.id, text) (193–200).
- For each property with a WIT mapping (
to_wit_property, 163–172): colors passed as *value as f32, others as f32::from_bits(*value) (201–209).
- Children first (so they exist before insertion), then
call_insert_child(node.id, child.id, u32::MAX) (211–214).
to_wit_component (152–160) maps view component → WIT variant.
HostError (30–40) wraps wasmtime::Error.
Tests (218–308) prove the full bridge: bridge_to_batch_to_render_tree (235–251) walks DSL → guest → batch → BatchDecoder → RenderTree; reemit_produces_only_deltas (254–278) shows zero opcodes for unchanged tree; dispatch_routes_event_back_to_host (281–308) round-trips Event::PointerDown via a capturing AppHandler.
4. pathland-gtk-demo — /Users/michael/Development/michaelkrog/Pathland/lib/rust/crates/pathland-gtk-demo/src/main.rs
Setup
GUEST_WASM (25–26): loads target/wasm32-wasip1/debug/pathland_guest.wasm (built by cargo component build).
build_tree(count) (32–43): the developer DSL surface — vstack![hstack![text(...), button("Increment")].padding(16.0), text(...).color(...)].padding(24.0).build(), producing a pathland_view::Node.
Demo (46–51): { host: GuestHost, count: u32, on_click: Option<Rc<dyn Fn()>> }.
Interaction loop (lines 86–105)
Button click (via render_node's btn.connect_clicked, line 167):
- Builds
types::Event::PointerDown((0, types::Point { x:0.0, y:0.0, secondary:false })) (92–99).
d.host.dispatch(&event) (100) → guest input.dispatch → app.on_event back into the host (a trace point; DemoApp's handler is a no-op, 212–215).
d.count = d.count.wrapping_add(1) (101), then render(...) (103).
render() — the full pipeline (lines 112–135)
let tree = build_tree(d.count); // 115 DSL authoring
assign_ids(&mut tree, &mut 1); // 117 stable ids
d.host.import_tree(&tree).unwrap(); // 119 bridge: Node → WIT builder calls → guest
let batch = d.host.emit().unwrap(); // 120 guest begin-frame/emit/end-frame/take-batch
let mut decoder = BatchDecoder::new(); // 122
let batch = decoder.decode(&batch).unwrap(); // 124 decode network batch
render.apply_frame(batch.as_frame()); // 125 → RenderTree (pathland-host)
Then it clears the GTK root box (127–129) and rebuilds the widget subtree via render_node (130–132).
render_node (138–173) — GTK widget mapping
- VSTACK (141–149):
GtkBox::new(Vertical, spacing(...)), halign/valign Fill.
- HSTACK (150–158):
GtkBox::new(Horizontal, spacing(...)).
- TEXT (159–163):
Label::new(...) + apply_text_style.
- BUTTON (164–170):
gtk::Button::with_label(...) + connect_clicked.
spacing() (176–182): reads property_id::SPACING from RenderTree (default 4).
apply_text_style (185–204): FONT_SIZE → Pango points; COLOR → 0xAARRGGBB → 16-bit Pango RGB.
CONFIRMATION: the demo uses the network batch path, not a shared-memory ring
main.rs:119–125 shows the only data path: import_tree → emit() (returns Vec<u8>) → BatchDecoder::decode → render.apply_frame(batch.as_frame()).
- There is no
Guest/Host ring view, no MemoryLayout, no init_memory, and no shared Vec<u8> memory buffer in the demo or in pathland-wit-host. The host never touches the guest's ring.
- The ring + arena exist only inside the guest's WASM linear memory (
State.mem, guest lib.rs:58,74), where take_batch drains them (guest lib.rs:230–236) and serializes them with BatchEncoder into network bytes. BatchDecoder (transport batch.rs:218–260) then rebuilds a mirrored arena so Frame::arena_str resolves absolute offsets exactly as in shared memory. pathland_host::RenderTree::apply_frame (host lib.rs:56–121) consumes the Frame transparently, since Frame is the transport seam (opcode/src/lib.rs:227–229).
Data-flow summary (current)
[DSL] build_tree() → Node ── assign_ids ──►
[bridge] import_node(): WIT builder calls (create-node/set-text/set-property/insert-child)
[guest] State.nodes (HashMap) + pathland_engine (diff snapshot)
emit(): RingGuest over State.mem (guest WASM linear memory, header|ring|arena)
engine.emit(root) → 16-byte TREE/STYLE opcodes + arena strings
end_frame bumps frameCount
take_batch(): Host over State.mem drains ring → BatchEncoder.encode(frame_count, opcodes, arena_delta)
→ list<u8> across the WIT boundary (wasmtime copies out)
[wit-host] GuestHost::emit() returns Vec<u8>
[transport] BatchDecoder.decode(bytes) → Batch (Frame view + mirrored arena)
[host] RenderTree.apply_frame(frame) → HashMap<u32, HostNode>
[renderer] render_node() → GTK4 widgets (GtkBox/Label/Button)
[events] button click → host.dispatch(PointerDown) → guest input.dispatch → app.on_event → host
Where the WASM memory + ring live: entirely inside the pathland-guest WASM instance — State.mem: Vec<u8> sized by MemoryLayout::default() (64 B header + 4096×16 B ring + 1 MiB arena ≈ 1.06 MiB), initialized via init_memory at guest startup. The native host communicates exclusively through take_batch's network-batch bytes; the SPSC ring cursor machinery (writeCursor/readCursor/frameCount) runs inside the guest memory and is consumed there by the guest's own Host view during take_batch.