1use std::sync::Arc;
4
5use sim_codec_lisp::LispCodecLib;
6use sim_kernel::{
7 AbiVersion, Args, CORE_FUNCTION_CLASS_ID, Callable, ClassRef, CodecId, Cx, Error, Export, Expr,
8 Lib, LibManifest, LibTarget, Linker, LoadCx, Object, ObjectCompat, Result, Symbol, Value,
9 Version,
10};
11use sim_run_core::{Bootloader, cli_main_entrypoint_symbol};
12
13use crate::serve::{ServeConfig, serve_with_cx};
14
15pub struct AtelierCliLib;
17
18pub struct BrowseCliLib;
20
21impl Lib for AtelierCliLib {
22 fn manifest(&self) -> LibManifest {
23 cli_manifest("atelier", "cli/main/atelier")
24 }
25
26 fn load(&self, cx: &mut LoadCx, linker: &mut Linker<'_>) -> Result<()> {
27 register_cli_entrypoint(cx, linker, "atelier")
28 }
29}
30
31impl Lib for BrowseCliLib {
32 fn manifest(&self) -> LibManifest {
33 cli_manifest("browse", "cli/main/browse")
34 }
35
36 fn load(&self, cx: &mut LoadCx, linker: &mut Linker<'_>) -> Result<()> {
37 register_cli_entrypoint(cx, linker, "browse")
38 }
39}
40
41fn cli_manifest(id: &str, entrypoint: &str) -> LibManifest {
42 LibManifest {
43 id: Symbol::new(id),
44 version: Version(env!("CARGO_PKG_VERSION").to_owned()),
45 abi: AbiVersion { major: 0, minor: 1 },
46 target: LibTarget::HostRegistered,
47 requires: Vec::new(),
48 capabilities: Vec::new(),
49 exports: vec![Export::Function {
50 symbol: symbol_from_slash(entrypoint),
51 function_id: None,
52 }],
53 }
54}
55
56fn register_cli_entrypoint(
57 cx: &mut LoadCx,
58 linker: &mut Linker<'_>,
59 verb: &'static str,
60) -> Result<()> {
61 linker.function_value(
62 Symbol::qualified("cli", format!("main/{verb}")),
63 cx.factory()
64 .opaque(Arc::new(WebShellCliEntrypoint { verb }))?,
65 )?;
66 Ok(())
67}
68
69#[derive(Clone)]
70struct WebShellCliEntrypoint {
71 verb: &'static str,
72}
73
74impl Object for WebShellCliEntrypoint {
75 fn display(&self, _cx: &mut Cx) -> Result<String> {
76 Ok(format!("#<function cli/main/{}>", self.verb))
77 }
78
79 fn as_any(&self) -> &dyn std::any::Any {
80 self
81 }
82}
83
84impl ObjectCompat for WebShellCliEntrypoint {
85 fn class(&self, cx: &mut Cx) -> Result<ClassRef> {
86 if let Some(value) = cx
87 .registry()
88 .class_by_symbol(&Symbol::qualified("core", "Function"))
89 {
90 return Ok(value.clone());
91 }
92 cx.factory().class_stub(
93 CORE_FUNCTION_CLASS_ID,
94 Symbol::qualified("core", "Function"),
95 )
96 }
97
98 fn as_callable(&self) -> Option<&dyn Callable> {
99 Some(self)
100 }
101}
102
103impl Callable for WebShellCliEntrypoint {
104 fn call(&self, cx: &mut Cx, args: Args) -> Result<Value> {
105 verify_cli_envelope(cx, &args, self.verb)?;
106 cx.factory().bool(true)
107 }
108}
109
110fn verify_cli_envelope(cx: &mut Cx, args: &Args, verb: &str) -> Result<()> {
111 let envelope = args
112 .values()
113 .first()
114 .ok_or_else(|| Error::Eval(format!("cli/main/{verb} expects a CLI envelope")))?;
115 let envelope_verb = envelope_string_field(cx, envelope, "verb")?;
116 if envelope_verb != verb {
117 return Err(Error::Eval(format!(
118 "cli/main/{verb} received verb {envelope_verb}"
119 )));
120 }
121 let payload_args = envelope_args(cx, envelope)?;
122 if payload_args.first().map(String::as_str) != Some(verb) {
123 return Err(Error::Eval(format!(
124 "cli/main/{verb} expects the first payload argument to be {verb}"
125 )));
126 }
127 Ok(())
128}
129
130fn envelope_string_field(cx: &mut Cx, envelope: &Value, field: &str) -> Result<String> {
131 let Some(table) = envelope.object().as_table_impl() else {
132 return Err(Error::Eval("CLI envelope is not a table".to_owned()));
133 };
134 match table.get(cx, Symbol::new(field))?.object().as_expr(cx)? {
135 Expr::String(text) => Ok(text),
136 Expr::Nil => Err(Error::Eval(format!("CLI envelope field {field} is nil"))),
137 other => Err(Error::Eval(format!(
138 "CLI envelope field {field} is not a string: {other:?}"
139 ))),
140 }
141}
142
143fn envelope_args(cx: &mut Cx, envelope: &Value) -> Result<Vec<String>> {
144 let Some(table) = envelope.object().as_table_impl() else {
145 return Err(Error::Eval("CLI envelope is not a table".to_owned()));
146 };
147 let value = table.get(cx, Symbol::new("args"))?;
148 let Some(list) = value.object().as_list() else {
149 return Err(Error::Eval(
150 "CLI envelope field args is not a list".to_owned(),
151 ));
152 };
153 list.to_vec(cx, Some(64))?
154 .into_iter()
155 .map(|value| match value.object().as_expr(cx)? {
156 Expr::String(text) => Ok(text),
157 other => Err(Error::Eval(format!(
158 "CLI payload argument is not a string: {other:?}"
159 ))),
160 })
161 .collect()
162}
163
164fn symbol_from_slash(text: &str) -> Symbol {
165 match text.split_once('/') {
166 Some((head, tail)) => Symbol::qualified(head, tail),
167 None => Symbol::new(text),
168 }
169}
170
171pub const WEB_SERVE_VERB: &str = "serve";
177
178pub fn web_serve_entrypoint_symbol() -> Symbol {
180 cli_main_entrypoint_symbol(WEB_SERVE_VERB)
181}
182
183pub fn configure_web_bootloader(loader: Bootloader) -> Bootloader {
187 loader
188 .host_lib("codec/lisp", || {
189 Box::new(LispCodecLib::new(CodecId(1)).expect("lisp boot codec"))
190 })
191 .host_verb(WEB_SERVE_VERB, "lib/web-serve", || Box::new(WebServeLib))
192}
193
194pub fn web_bootloader() -> Bootloader {
198 configure_web_bootloader(Bootloader::standard())
199}
200
201pub struct WebServeLib;
203
204impl Lib for WebServeLib {
205 fn manifest(&self) -> LibManifest {
206 LibManifest {
207 id: Symbol::qualified("lib", "web-serve"),
208 version: Version(env!("CARGO_PKG_VERSION").to_owned()),
209 abi: AbiVersion { major: 0, minor: 1 },
210 target: LibTarget::HostRegistered,
211 requires: Vec::new(),
212 capabilities: Vec::new(),
213 exports: vec![Export::Function {
214 symbol: web_serve_entrypoint_symbol(),
215 function_id: None,
216 }],
217 }
218 }
219
220 fn load(&self, cx: &mut LoadCx, linker: &mut Linker<'_>) -> Result<()> {
221 linker.function_value(
222 web_serve_entrypoint_symbol(),
223 cx.factory().opaque(Arc::new(WebServeEntrypoint))?,
224 )?;
225 Ok(())
226 }
227}
228
229#[derive(Clone)]
230struct WebServeEntrypoint;
231
232impl Object for WebServeEntrypoint {
233 fn display(&self, _cx: &mut Cx) -> Result<String> {
234 Ok("cli/main/serve".to_owned())
235 }
236
237 fn as_any(&self) -> &dyn std::any::Any {
238 self
239 }
240}
241
242impl ObjectCompat for WebServeEntrypoint {
243 fn as_callable(&self) -> Option<&dyn Callable> {
244 Some(self)
245 }
246}
247
248impl Callable for WebServeEntrypoint {
249 fn call(&self, cx: &mut Cx, args: Args) -> Result<Value> {
250 let config = match args.values().first() {
253 Some(envelope) => {
254 let payload = envelope_args(cx, envelope)?;
255 parse_serve_config(payload.into_iter().skip(1))
256 }
257 None => ServeConfig::default(),
258 };
259 serve_with_cx(cx, &config)
260 .map_err(|err| Error::Eval(format!("web serve failed: {err}")))?;
261 cx.factory().bool(true)
262 }
263}
264
265fn parse_serve_config(args: impl Iterator<Item = String>) -> ServeConfig {
266 let mut config = ServeConfig::default();
267 let mut iter = args;
268 while let Some(arg) = iter.next() {
269 match arg.as_str() {
270 "--addr" => {
271 if let Some(addr) = iter.next() {
272 config.addr = addr;
273 }
274 }
275 other if other.starts_with("--addr=") => {
276 config.addr = other["--addr=".len()..].to_owned();
277 }
278 "--atelier-root" => {
279 if let Some(root) = iter.next() {
280 config.atelier_root = root.into();
281 }
282 }
283 other if other.starts_with("--atelier-root=") => {
284 config.atelier_root = other["--atelier-root=".len()..].into();
285 }
286 "--dry-run" => {
287 config.dry_run = true;
288 }
289 _ => {}
290 }
291 }
292 config
293}