//! In-memory browser / WASM session: boot `# main`, then call handlers with shared entry env. use anyhow::{bail, Result}; use crate::ast::Module; use crate::host::json::{json_to_value, value_to_json}; use crate::host::{HostCaps, HostContext}; use crate::interp::Interpreter; use crate::load::load_module_from_source; use crate::value::Value; use crate::Backend; /// Persistent Marqdo program for the browser host bridge. /// /// Session calls (`call`) use the **tree** interpreter so entry bindings persist /// across handlers (C3). One-shot [`crate::run_source`] may still use bytecode. pub struct BrowserSession { module: Module, interp: Interpreter, } impl BrowserSession { /// Parse source, run `# main`, keep entry bindings for later [`Self::call`]. pub fn boot(source: &str) -> Result<(Self, Value)> { Self::boot_with(source, Backend::Tree) } /// Like [`Self::boot`]. `Backend::Bytecode` is rejected for sessions (no shared entry env yet). pub fn boot_with(source: &str, backend: Backend) -> Result<(Self, Value)> { if source.trim().is_empty() { bail!("source is empty"); } if backend != Backend::Tree { bail!("browser session requires tree backend (bytecode has no entry-env invoke yet)"); } let module = load_module_from_source(source)?; let caps = HostCaps { fs_write: false, exec: false, net: false, plugin: false, }; let mut host = HostContext::for_capture(None, caps); host.set_entry_source(None, source); host.sleep_limit_ms = Some(0); let mut interp = Interpreter::with_capture(None, false).with_host(host); let value = interp.run_module(&module)?; Ok((Self { module, interp }, value)) } /// Invoke `name` with optional JSON object of named args; updates entry bindings. pub fn call(&mut self, name: &str, args_json: &str) -> Result { let args = parse_named_args(args_json)?; self.interp.captured_stdout.clear(); self.interp.invoke_in_entry(&self.module, name, &args) } pub fn take_stdout(&mut self) -> String { std::mem::take(&mut self.interp.captured_stdout) } } fn parse_named_args(args_json: &str) -> Result> { let t = args_json.trim(); if t.is_empty() || t == "{}" || t == "null" { return Ok(Vec::new()); } let v: serde_json::Value = serde_json::from_str(t).map_err(|e| anyhow::anyhow!("call args json: {e}"))?; match v { serde_json::Value::Object(map) => { let mut out = Vec::new(); for (k, j) in map { out.push((k, json_to_value(&j).map_err(|e| anyhow::anyhow!(e))?)); } Ok(out) } serde_json::Value::Null => Ok(Vec::new()), _ => bail!("call args must be a JSON object"), } } /// Serialize a runtime [`Value`] to JSON (for WASM bridge responses). pub fn value_as_json(v: &Value) -> Result { value_to_json(v) } #[cfg(test)] mod tests { use super::*; #[test] fn boot_and_call_share_count() { let src = r##"--- import: json: lib/json.mq.md --- # main **count = 0** `wire` = | @ | 选择器 | 事件 | 调用 | |---|--------|------|------| | 1 | "#btn" | click | bump | *wire* ## bump **count = count + 1** **label = > str count** **patch = > json.set map=None key="#out" value=label** *> json.set map=None key="set_text" value=patch* "##; let (mut sess, wire) = BrowserSession::boot(src).expect("boot"); assert!(matches!(wire, Value::List(_)), "wire={wire:?}"); let _v1 = sess.call("bump", "{}").expect("call1"); let v2 = sess.call("bump", "{}").expect("call2"); let j2 = value_as_json(&v2).unwrap(); assert_eq!(j2["set_text"]["#out"], "2"); } #[test] fn fetch_mq_load_returns_fetch_effect() { // A protocol regression must not depend on a retired public example. let src = r###"--- markup: occlusion title: browser fetch (C4) description: fetch/after effects via lib/browser — no json glue import: browser: lib/browser.mq.md --- # main `wire` = | @ | 选择器 | 事件 | 调用 | |---|--------|------|------| | 1 | "#load" | click | load | | 2 | "#ping" | click | ping | *`wire`* ## load **`msg` = > browser.set_text sel="#status" text="loading…"** **`req` = > browser.fetch url="https://httpbin.org/uuid" then="on_uuid"** *> browser.merge a=msg b=req* ## on_uuid + `ok` + `status` + `body`="" + `error`="" 1. ok *> browser.set_text sel="#status" text=body* 2. * *> browser.set_text sel="#status" text="fetch failed"* ## ping **`msg` = > browser.set_text sel="#status" text="wait 600ms…"** **`t` = > browser.after ms=600 then="on_ping"** *> browser.merge a=msg b=t* ## on_ping + `ok`=True *> browser.set_text sel="#status" text="pong"* "###; let (mut sess, wire) = BrowserSession::boot(src).expect("boot"); assert!(matches!(wire, Value::List(_))); let v = sess.call("load", "{}").expect("load"); let j = value_as_json(&v).unwrap(); assert_eq!(j["set_text"]["#status"], "loading…"); assert_eq!(j["fetch"]["then"], "on_uuid"); assert!(j["fetch"]["url"].as_str().unwrap().contains("httpbin")); let done = sess .call( "on_uuid", r#"{"ok":true,"status":200,"body":"{\"uuid\":\"x\"}"}"#, ) .expect("on_uuid"); let jd = value_as_json(&done).unwrap(); assert!(jd["set_text"]["#status"].as_str().unwrap().contains("uuid")); } #[test] fn session_rejects_bytecode_backend() { let err = BrowserSession::boot_with("# main\n\n> print text=x\n", Backend::Bytecode) .err() .expect("err"); assert!(err.to_string().contains("tree backend")); } }