|
4 | 4 | // It sketches the entrypoint for running a WASM component and wiring host |
5 | 5 | // implementations from the broker to the component-generated bindings. |
6 | 6 |
|
| 7 | +#[cfg(feature = "wasmtime-host")] |
| 8 | +mod bindings { |
| 9 | + wasmtime::component::bindgen!({ path: "../wit", world: "app", trappable_imports: true }); |
| 10 | +} |
| 11 | + |
7 | 12 | #[cfg(feature = "wasmtime-host")] |
8 | 13 | mod impls { |
9 | 14 | use super::*; |
| 15 | + use crate::wasmtime_host::bindings; // use generated module from sibling mod |
| 16 | + use anyhow::Result; |
10 | 17 | use std::fs; |
11 | 18 | use std::path::Path; |
12 | 19 | use wasmtime::component::{Component, Linker}; |
13 | 20 | use wasmtime::{Config, Engine, Store}; |
14 | 21 |
|
15 | | - // Minimal loader to validate component parsing. Host bindings come next. |
16 | | - pub fn run_component(component_path: &Path, _core: CoreCtx) -> Result<(), String> { |
| 22 | + // Host adapter implementing imported interfaces, delegating to core hosts. |
| 23 | + struct Host<'a> { |
| 24 | + core: CoreCtx<'a>, |
| 25 | + } |
| 26 | + |
| 27 | + // fs |
| 28 | + impl<'a> bindings::saf::app::fs::Host for Host<'a> { |
| 29 | + fn list_dir(&mut self, path: String) -> Result<Vec<String>> { |
| 30 | + self.core |
| 31 | + .ctx |
| 32 | + .fs |
| 33 | + .list_dir(&path) |
| 34 | + .map_err(|e| anyhow::anyhow!(e)) |
| 35 | + } |
| 36 | + fn read_text(&mut self, path: String) -> Result<String> { |
| 37 | + self.core |
| 38 | + .ctx |
| 39 | + .fs |
| 40 | + .read_text(&path) |
| 41 | + .map_err(|e| anyhow::anyhow!(e)) |
| 42 | + } |
| 43 | + fn write_text(&mut self, path: String, content: String) -> Result<()> { |
| 44 | + self.core |
| 45 | + .ctx |
| 46 | + .fs |
| 47 | + .write_text(&path, &content) |
| 48 | + .map_err(|e| anyhow::anyhow!(e)) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // net |
| 53 | + impl<'a> bindings::saf::app::net::Host for Host<'a> { |
| 54 | + fn get_text(&mut self, url: String) -> Result<String> { |
| 55 | + self.core |
| 56 | + .ctx |
| 57 | + .net |
| 58 | + .get_text(&url) |
| 59 | + .map_err(|e| anyhow::anyhow!(e)) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // log |
| 64 | + impl<'a> bindings::saf::app::log::Host for Host<'a> { |
| 65 | + fn event(&mut self, message: String) -> Result<()> { |
| 66 | + self.core.ctx.log.event(&message); |
| 67 | + Ok(()) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // time (stub: use system time seconds) |
| 72 | + impl<'a> bindings::saf::app::time::Host for Host<'a> { |
| 73 | + fn now_unix_seconds(&mut self) -> Result<u64> { |
| 74 | + Ok(std::time::SystemTime::now() |
| 75 | + .duration_since(std::time::UNIX_EPOCH) |
| 76 | + .unwrap_or_default() |
| 77 | + .as_secs()) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // rand (stub: not deterministic; broker should gate as needed) |
| 82 | + impl<'a> bindings::saf::app::rand::Host for Host<'a> { |
| 83 | + fn fill(&mut self, len: u32) -> Result<Vec<u8>> { |
| 84 | + use rand::{rngs::OsRng, RngCore}; |
| 85 | + let mut buf = vec![0u8; len as usize]; |
| 86 | + OsRng.fill_bytes(&mut buf); |
| 87 | + Ok(buf) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + pub fn run_component(component_path: &Path, core: CoreCtx) -> Result<(), String> { |
| 92 | + // Engine |
17 | 93 | let mut cfg = Config::new(); |
18 | 94 | cfg.wasm_component_model(true); |
19 | | - |
20 | 95 | let engine = Engine::new(&cfg).map_err(|e| e.to_string())?; |
| 96 | + |
21 | 97 | if !component_path.exists() { |
22 | 98 | return Err(format!("component not found: {}", component_path.display())); |
23 | 99 | } |
24 | 100 |
|
25 | | - // Basic read for clearer errors before Wasmtime parse |
| 101 | + // Load component |
26 | 102 | let bytes = fs::read(component_path).map_err(|e| e.to_string())?; |
27 | | - let _component = Component::from_binary(&engine, &bytes).map_err(|e| e.to_string())?; |
| 103 | + let component = unsafe { Component::deserialize(&engine, &bytes) } |
| 104 | + .map_err(|e| e.to_string())?; |
| 105 | + |
| 106 | + // Store + linker with host stored in state |
| 107 | + struct State<'a> { |
| 108 | + host: Host<'a>, |
| 109 | + } |
| 110 | + let mut store: Store<State> = Store::new(&engine, State { host: Host { core } }); |
| 111 | + let mut linker: Linker<State> = Linker::new(&engine); |
| 112 | + |
| 113 | + // Instantiate bindings and provide host implementations |
| 114 | + bindings::saf::app::fs::add_to_linker(&mut linker, |s: &mut State| &mut s.host) |
| 115 | + .map_err(|e| e.to_string())?; |
| 116 | + bindings::saf::app::net::add_to_linker(&mut linker, |s: &mut State| &mut s.host) |
| 117 | + .map_err(|e| e.to_string())?; |
| 118 | + bindings::saf::app::log::add_to_linker(&mut linker, |s: &mut State| &mut s.host) |
| 119 | + .map_err(|e| e.to_string())?; |
| 120 | + bindings::saf::app::time::add_to_linker(&mut linker, |s: &mut State| &mut s.host) |
| 121 | + .map_err(|e| e.to_string())?; |
| 122 | + bindings::saf::app::rand::add_to_linker(&mut linker, |s: &mut State| &mut s.host) |
| 123 | + .map_err(|e| e.to_string())?; |
28 | 124 |
|
29 | | - // Placeholder store and linker; no instantiation yet |
30 | | - struct HostState; |
31 | | - let mut store = Store::new(&engine, HostState); |
32 | | - let _linker: Linker<HostState> = Linker::new(&engine); |
| 125 | + // Instantiate component |
| 126 | + let (_instance, _exports) = bindings::App::instantiate(&mut store, &component, &linker) |
| 127 | + .map_err(|e| e.to_string())?; |
33 | 128 |
|
34 | | - Err("component loaded; host bindings not implemented yet".to_string()) |
| 129 | + // If the world exports entry functions, call them here (not defined yet). |
| 130 | + Ok(()) |
35 | 131 | } |
36 | 132 | } |
37 | 133 |
|
|
0 commit comments