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 web_bootloader() -> Bootloader {
187 Bootloader::standard()
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 struct WebServeLib;
196
197impl Lib for WebServeLib {
198 fn manifest(&self) -> LibManifest {
199 LibManifest {
200 id: Symbol::qualified("lib", "web-serve"),
201 version: Version(env!("CARGO_PKG_VERSION").to_owned()),
202 abi: AbiVersion { major: 0, minor: 1 },
203 target: LibTarget::HostRegistered,
204 requires: Vec::new(),
205 capabilities: Vec::new(),
206 exports: vec![Export::Function {
207 symbol: web_serve_entrypoint_symbol(),
208 function_id: None,
209 }],
210 }
211 }
212
213 fn load(&self, cx: &mut LoadCx, linker: &mut Linker<'_>) -> Result<()> {
214 linker.function_value(
215 web_serve_entrypoint_symbol(),
216 cx.factory().opaque(Arc::new(WebServeEntrypoint))?,
217 )?;
218 Ok(())
219 }
220}
221
222#[derive(Clone)]
223struct WebServeEntrypoint;
224
225impl Object for WebServeEntrypoint {
226 fn display(&self, _cx: &mut Cx) -> Result<String> {
227 Ok("cli/main/serve".to_owned())
228 }
229
230 fn as_any(&self) -> &dyn std::any::Any {
231 self
232 }
233}
234
235impl ObjectCompat for WebServeEntrypoint {
236 fn as_callable(&self) -> Option<&dyn Callable> {
237 Some(self)
238 }
239}
240
241impl Callable for WebServeEntrypoint {
242 fn call(&self, cx: &mut Cx, args: Args) -> Result<Value> {
243 let config = match args.values().first() {
246 Some(envelope) => {
247 let payload = envelope_args(cx, envelope)?;
248 parse_serve_config(payload.into_iter().skip(1))
249 }
250 None => ServeConfig::default(),
251 };
252 serve_with_cx(cx, &config)
253 .map_err(|err| Error::Eval(format!("web serve failed: {err}")))?;
254 cx.factory().bool(true)
255 }
256}
257
258fn parse_serve_config(args: impl Iterator<Item = String>) -> ServeConfig {
259 let mut config = ServeConfig::default();
260 let mut iter = args;
261 while let Some(arg) = iter.next() {
262 match arg.as_str() {
263 "--addr" => {
264 if let Some(addr) = iter.next() {
265 config.addr = addr;
266 }
267 }
268 other if other.starts_with("--addr=") => {
269 config.addr = other["--addr=".len()..].to_owned();
270 }
271 "--atelier-root" => {
272 if let Some(root) = iter.next() {
273 config.atelier_root = root.into();
274 }
275 }
276 other if other.starts_with("--atelier-root=") => {
277 config.atelier_root = other["--atelier-root=".len()..].into();
278 }
279 _ => {}
280 }
281 }
282 config
283}