euv_core/dynamic/wasm/struct.rs
1use super::*;
2
3/// Sandbox safety, memory, fuel, timeout, retry, and caching configuration for dynamic WASM execution.
4#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
5pub struct WasmSandboxConfig {
6 /// Maximum allowed `.wasm` payload size in bytes.
7 #[get(type(copy))]
8 pub(crate) max_binary_bytes: usize,
9 /// Maximum allowed linear memory allocation in bytes.
10 #[get(type(copy))]
11 pub(crate) max_memory_bytes: usize,
12 /// Maximum instructions executed per WASM call before terminating with `OutOfFuel`.
13 #[get(type(copy))]
14 pub(crate) max_fuel: u64,
15 /// Download timeout ceiling in milliseconds.
16 #[get(type(copy))]
17 pub(crate) timeout_ms: u64,
18 /// Automatic retry count for transient transport failures (`Timeout` / `HttpStatus >= 500`).
19 #[get(type(copy))]
20 pub(crate) max_retries: u32,
21 /// Whether verified cached `.wasm` entries may be served when the network is offline.
22 #[get(type(copy))]
23 pub(crate) allow_offline_cache: bool,
24 /// Whether non-empty `expected_signature` verification is mandatory.
25 #[get(type(copy))]
26 pub(crate) require_signature: bool,
27}
28
29/// Declarative request descriptor for downloading and executing a dynamic `.wasm` module.
30#[derive(Clone, Data, Debug, Eq, Hash, New, PartialEq)]
31pub struct WasmDownloadRequest {
32 /// Source URL (`https://`, `http://`, `euv-cdn://`, or `file://`).
33 pub(crate) url: String,
34 /// Execution scope (`Partial { slot_id }` or `Global`).
35 pub(crate) scope: WasmDynamicScope,
36 /// Monotonic release version (`>= 1`).
37 #[get(type(copy))]
38 pub(crate) version: u64,
39 /// Optional expected FNV-1a checksum (`0` disables checksum comparison).
40 #[new(skip)]
41 #[get(type(copy))]
42 pub(crate) expected_checksum: u64,
43 /// Optional expected signature tag (verified with `compute_wasm_signature`).
44 #[new(skip)]
45 pub(crate) expected_signature: String,
46 /// Whether this request may install a version `<=` the currently mounted version.
47 #[new(skip)]
48 #[get(type(copy))]
49 pub(crate) allow_downgrade: bool,
50 /// Optional cache key override (defaults to `url`).
51 #[new(skip)]
52 pub(crate) cache_key: String,
53}
54
55/// Raw downloaded `.wasm` payload returned by a `WasmFetcher` implementation.
56#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
57pub struct WasmFetchedPayload {
58 /// Raw WebAssembly binary bytes.
59 pub(crate) bytes: Vec<u8>,
60 /// Optional ETag / content revision identifier.
61 #[new(skip)]
62 pub(crate) etag: String,
63 /// Whether this payload originated from cache.
64 #[new(skip)]
65 #[get(type(copy))]
66 pub(crate) from_cache: bool,
67}
68
69/// Parsed WebAssembly function signature `(param_count, result_count)`.
70#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
71pub struct WasmFuncSignature {
72 /// Number of `i32` parameters.
73 #[get(type(copy))]
74 pub(crate) param_count: usize,
75 /// Number of `i32` results (`0` or `1`).
76 #[get(type(copy))]
77 pub(crate) result_count: usize,
78}
79
80/// Parsed WebAssembly host import declaration.
81#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
82pub struct WasmImportEntry {
83 /// Imported module namespace (e.g. `"env"`).
84 pub(crate) module: String,
85 /// Imported function name (e.g. `"euv_emit_ui"`).
86 pub(crate) name: String,
87 /// Type index inside the module's type table.
88 #[get(type(copy))]
89 pub(crate) type_index: u32,
90}
91
92/// Parsed WebAssembly active linear memory data segment.
93#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
94pub struct WasmDataSegment {
95 /// Linear memory byte offset where `bytes` are initialized.
96 #[get(type(copy))]
97 pub(crate) offset: u32,
98 /// Raw data segment payload bytes.
99 pub(crate) bytes: Vec<u8>,
100}
101
102/// Parsed WebAssembly function body ready for sandbox interpretation.
103#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
104pub struct WasmParsedFunction {
105 /// Type index inside the module's type table.
106 #[get(type(copy))]
107 pub(crate) type_index: u32,
108 /// Count of additional `i32` local variables declared in the body header.
109 #[get(type(copy))]
110 pub(crate) extra_locals: usize,
111 /// Raw WebAssembly bytecode instructions ending with `0x0B`.
112 pub(crate) body: Vec<u8>,
113}
114
115/// Validated in-memory representation of a decoded `.wasm` binary module.
116#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
117pub struct WasmParsedModule {
118 /// Declared function signatures from the Type section.
119 pub(crate) types: Vec<WasmFuncSignature>,
120 /// Imported host functions from the Import section.
121 pub(crate) imports: Vec<WasmImportEntry>,
122 /// Defined function bodies paired with type indices.
123 pub(crate) functions: Vec<WasmParsedFunction>,
124 /// Exported function name -> absolute function index map.
125 pub(crate) exports: HashMap<String, u32>,
126 /// Minimum linear memory pages declared (`1 page = 64 KiB`).
127 #[get(type(copy))]
128 pub(crate) memory_min_pages: u32,
129 /// Initialized linear memory data segments.
130 pub(crate) data_segments: Vec<WasmDataSegment>,
131 /// Custom sections keyed by section name.
132 pub(crate) custom_sections: HashMap<String, Vec<u8>>,
133}
134
135/// Output produced by executing an exported function inside the WASM sandbox.
136#[derive(Clone, Data, Debug, Default, PartialEq)]
137pub struct WasmExecutionOutput {
138 /// Optional `i32` return value from the invoked WASM function.
139 #[get(type(copy))]
140 pub(crate) return_value: i32,
141 /// Total instructions metered during execution.
142 #[get(type(copy))]
143 pub(crate) fuel_used: u64,
144 /// Native UI subtree emitted via `env::euv_emit_ui` (if any).
145 #[get(skip)]
146 pub(crate) emitted_spec: Option<DynamicNodeSpec>,
147 /// Native global app bundle emitted via `env::euv_emit_bundle` (if any).
148 #[get(skip)]
149 pub(crate) emitted_bundle: Option<DynamicUpdateBundle>,
150 /// Diagnostic messages emitted via `env::euv_log`.
151 pub(crate) logs: Vec<String>,
152}
153
154/// Verified `.wasm` binary cached locally for instant reload and offline fallback.
155#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
156pub struct WasmCachedEntry {
157 /// Source URL.
158 pub(crate) url: String,
159 /// Verified release version.
160 #[get(type(copy))]
161 pub(crate) version: u64,
162 /// Verified FNV-1a checksum.
163 #[get(type(copy))]
164 pub(crate) checksum: u64,
165 /// Verified raw `.wasm` binary bytes.
166 pub(crate) bytes: Vec<u8>,
167 /// Server ETag / content identifier.
168 pub(crate) etag: String,
169}
170
171/// Verified mounted WASM revision snapshot stored on a partial slot or global rollback stack.
172#[derive(Clone, Data, Debug, PartialEq)]
173pub struct WasmMountedSnapshot {
174 /// Active release version.
175 #[get(type(copy))]
176 pub(crate) version: u64,
177 /// Source URL.
178 pub(crate) url: String,
179 /// Verified checksum.
180 #[get(type(copy))]
181 pub(crate) checksum: u64,
182 /// Parsed validated `.wasm` module kept ready for live `euv_on_event` callbacks.
183 pub(crate) module: WasmParsedModule,
184 /// Current integer state value passed into `euv_render(state)` / updated by `euv_on_event`.
185 #[get(type(copy))]
186 pub(crate) state_value: i32,
187 /// Optional emitted partial spec snapshot.
188 #[get(skip)]
189 pub(crate) spec: Option<DynamicNodeSpec>,
190 /// Optional emitted global bundle snapshot.
191 #[get(skip)]
192 pub(crate) bundle: Option<DynamicUpdateBundle>,
193}
194
195/// Cumulative reactive telemetry counters for dynamic WASM loading, caching, sandboxing, and rollbacks.
196#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, PartialEq)]
197pub struct WasmDynamicTelemetry {
198 /// Total download attempts initiated.
199 #[get(type(copy))]
200 pub(crate) downloads_started: u64,
201 /// Total successful downloads + mounts.
202 #[get(type(copy))]
203 pub(crate) downloads_succeeded: u64,
204 /// Total verified offline/local cache hits.
205 #[get(type(copy))]
206 pub(crate) cache_hits: u64,
207 /// Total automatic transport retries executed.
208 #[get(type(copy))]
209 pub(crate) retries_performed: u64,
210 /// Total automatic or manual rollbacks executed.
211 #[get(type(copy))]
212 pub(crate) rollbacks_executed: u64,
213 /// Total WASM runtime traps isolated without harming host UI.
214 #[get(type(copy))]
215 pub(crate) sandbox_traps_caught: u64,
216 /// Total infinite-loop fuel exhaustions terminated cleanly.
217 #[get(type(copy))]
218 pub(crate) fuel_exhaustions_caught: u64,
219 /// Total checksum, signature, binary, or ABI rejections.
220 #[get(type(copy))]
221 pub(crate) integrity_rejections: u64,
222 /// Total interactive events dispatched into mounted WASM modules.
223 #[get(type(copy))]
224 pub(crate) events_handled: u64,
225}
226
227/// Registered entry inside `RegistryWasmFetcher` for deterministic cross-platform CDN and fault simulation.
228#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
229pub struct RegistryWasmRecord {
230 /// Raw `.wasm` bytes served for this URL.
231 pub(crate) bytes: Vec<u8>,
232 /// ETag label returned with the payload.
233 pub(crate) etag: String,
234 /// Optional injected fault scenario.
235 #[get(type(copy))]
236 pub(crate) fault: WasmFaultKind,
237}
238
239/// Cross-platform in-memory WASM CDN registry and deterministic fault-injection fetcher.
240#[derive(Clone, Debug, Default)]
241pub struct RegistryWasmFetcher {
242 /// URL -> `.wasm` record registry.
243 pub(crate) records: Rc<RefCell<HashMap<String, RegistryWasmRecord>>>,
244 /// Global offline toggle simulating airplane mode across all URLs.
245 pub(crate) offline: Rc<RefCell<bool>>,
246}
247
248/// Native pure-std HTTP (`http://`) and local filesystem (`file://`) WASM fetcher with timeout and progress streaming.
249#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, PartialEq)]
250pub struct HttpWasmFetcher {
251 /// Socket connect/read timeout in milliseconds.
252 #[get(type(copy))]
253 pub(crate) timeout_ms: u64,
254}
255
256/// Internal interior-mutable state owned by `WasmDynamicManager`.
257#[derive(CustomDebug)]
258pub(crate) struct WasmDynamicManagerState {
259 /// Active `DynamicUpdateHub` receiving compiled native scene updates.
260 pub(crate) hub: DynamicUpdateHub,
261 /// Pluggable transport fetcher (`HttpWasmFetcher`, `RegistryWasmFetcher`, or custom CDN).
262 #[debug(skip)]
263 pub(crate) fetcher: Rc<dyn WasmFetcher>,
264 /// Sandbox fuel, memory, binary size, timeout, retry, and caching limits.
265 pub(crate) config: WasmSandboxConfig,
266 /// Monotonic request generation counter for cancellation and stale-response protection.
267 pub(crate) next_request_id: u64,
268 /// Active latest request generation per scope key.
269 pub(crate) active_request_ids: HashMap<String, u64>,
270 /// Verified `.wasm` binary cache keyed by cache key / URL.
271 pub(crate) cache: HashMap<String, WasmCachedEntry>,
272 /// Mounted revision stacks per partial slot ID.
273 pub(crate) partial_stacks: HashMap<String, Vec<WasmMountedSnapshot>>,
274 /// Mounted revision stack for global full-app WASM takeovers.
275 pub(crate) global_stack: Vec<WasmMountedSnapshot>,
276 /// Per-scope reactive `WasmLoadState` signals.
277 pub(crate) state_signals: HashMap<String, Signal<WasmLoadState>>,
278 /// Reactive global active version signal (`0` = built-in native shell).
279 pub(crate) global_version_signal: Signal<u64>,
280 /// Reactive telemetry snapshot signal.
281 pub(crate) telemetry_signal: Signal<WasmDynamicTelemetry>,
282 /// Last diagnostic log entries emitted by executed WASM modules.
283 pub(crate) last_logs: Signal<Vec<String>>,
284}
285
286/// Shared cross-platform manager orchestrating partial and global dynamic `.wasm` download, verification, execution, and native rendering.
287#[derive(Clone, CustomDebug)]
288pub struct WasmDynamicManager {
289 /// Interior-mutable manager state shared across slots, pages, and host shell.
290 pub(crate) inner: Rc<RefCell<WasmDynamicManagerState>>,
291}
292
293/// Spec-compliant `.wasm` binary encoder producing genuine executable WebAssembly modules for euv native rendering.
294#[derive(Clone, Data, Debug, Default)]
295pub struct WasmModuleBuilder {
296 /// ABI version returned by `euv_abi_version`.
297 #[get(type(copy))]
298 pub(crate) abi_version: u32,
299 /// Whether to export `euv_render`.
300 #[get(type(copy))]
301 pub(crate) export_render: bool,
302 /// Whether `euv_render` emits a global `DynamicUpdateBundle` instead of a partial `DynamicNodeSpec`.
303 #[get(type(copy))]
304 pub(crate) is_global_bundle: bool,
305 /// UTF-8 JSON UI payload placed in linear memory and emitted via host import.
306 pub(crate) ui_payload_json: String,
307 /// Optional second variant JSON payload emitted when interactive state is odd (`state & 1 != 0`).
308 pub(crate) alt_ui_payload_json: String,
309 /// Optional diagnostic log line emitted during `euv_render`.
310 pub(crate) log_message: String,
311 /// Optional fault behavior compiled directly into `euv_render` bytecode (`InfiniteLoopOutOfFuel` / `ExecutionTrap`).
312 #[get(type(copy))]
313 pub(crate) fault: WasmFaultKind,
314 /// Optional custom metadata section bytes.
315 pub(crate) custom_meta: Vec<u8>,
316}
317
318/// Mutable execution context threaded through sandboxed WASM interpreter invocations.
319pub(crate) struct WasmExecContext<'a> {
320 /// Active WASM linear memory byte slice.
321 pub(crate) memory: &'a mut [u8],
322 /// Accumulated host import outputs and logs.
323 pub(crate) out: &'a mut WasmExecutionOutput,
324 /// Running instruction fuel counter.
325 pub(crate) fuel_used: u64,
326 /// Hard instruction fuel limit.
327 pub(crate) max_fuel: u64,
328 /// Current recursion call depth.
329 pub(crate) depth: usize,
330}