Add host stdlib modules and unify view/run capabilities.

Ship fs/time/sys/json/net L1 libs (plus existing text/table), default-on host caps with soft exit under view, bilingual public/stdlib docs, and design notes for deferred math/foreign.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-05 10:03:54 +08:00
1 parent a5d30c00b3
commit a53ebb65ee
66 files changed
+2703 -45

No files matched your search

+39
View File
@@ -427,6 +427,45 @@ impl<'a> FnCompiler<'a> {
}
return Ok(());
}
other if crate::host::HostFn::from_name(other).is_some() => {
let hf = crate::host::HostFn::from_name(other).unwrap();
let params = hf.all_params();
let mut named: HashMap<String, &Expr> = HashMap::new();
let mut positionals = Vec::new();
for a in &call.args {
match a {
Arg::Positional(e) => positionals.push(e),
Arg::Named { name, value } => {
named.insert(name.clone(), value);
}
}
}
let mut pos_i = 0usize;
let required_n = hf.required_params().len();
for (i, p) in params.iter().enumerate() {
if let Some(e) = named.get(*p) {
self.compile_expr(e)?;
} else if pos_i < positionals.len() {
self.compile_expr(positionals[pos_i])?;
pos_i += 1;
} else if i < required_n {
return Err(self.err(format!("{} requires `{p}`", hf.name())));
} else {
self.emit(Op::None_);
}
}
if pos_i < positionals.len() {
return Err(self.err(format!(
"too many positional arguments ({} extra)",
positionals.len() - pos_i
)));
}
self.emit(Op::HostCall(hf.as_u16(), params.len() as u8));
if as_stmt {
self.emit(Op::Pop);
}
return Ok(());
}
_ => {}
}
+2
View File
@@ -54,6 +54,8 @@ pub enum Op {
GetIndex,
/// Call function by index; argc values are on the stack (param order).
Call(u16, u8),
/// Host primitive: pop `argc` args (param order), push result.
HostCall(u16, u8),
Return,
}
+39
View File
@@ -8,11 +8,13 @@ use crate::builtin::{
builtin_at, builtin_int, builtin_join, builtin_len, builtin_split, builtin_str, builtin_trim,
builtin_type,
};
use crate::host::{call_host, HostContext, HostFn};
use crate::bytecode::{Op, Program};
use crate::debug::emit_trace;
use crate::diagnostics::{bail_at, Span};
use crate::input_feed::InputFeed;
use crate::value::Value;
use std::collections::HashMap;
pub struct Vm {
path: Option<PathBuf>,
@@ -20,6 +22,7 @@ pub struct Vm {
pub captured_stdout: String,
input: InputFeed,
trace: bool,
host: HostContext,
}
struct Frame {
@@ -36,6 +39,7 @@ impl Vm {
captured_stdout: String::new(),
input: InputFeed::new(false, Vec::new()),
trace: false,
host: HostContext::for_run(path, Default::default(), Vec::new()),
}
}
@@ -46,6 +50,7 @@ impl Vm {
captured_stdout: String::new(),
input: InputFeed::new(true, Vec::new()),
trace: false,
host: HostContext::for_capture(path, Default::default()),
}
}
@@ -59,6 +64,11 @@ impl Vm {
self
}
pub fn with_host(mut self, host: HostContext) -> Self {
self.host = host;
self
}
fn emit_line(&mut self, text: &str) {
if self.capture {
self.captured_stdout.push_str(text);
@@ -276,6 +286,35 @@ impl Vm {
let list = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
stack.push(builtin_at(&list, &idx).map_err(|m| self.err_at(span, m))?);
}
Op::HostCall(id, argc) => {
let hf = HostFn::from_u16(id)
.ok_or_else(|| self.err_at(span, format!("unknown host id {id}")))?;
let argc = argc as usize;
let params = hf.all_params();
if params.len() != argc {
return Err(self.err_at(span, "host call argc mismatch"));
}
let mut vals = Vec::with_capacity(argc);
for _ in 0..argc {
vals.push(pop(&mut stack).map_err(|m| self.err_at(span, m))?);
}
vals.reverse();
let mut bound = HashMap::new();
for (p, v) in params.iter().zip(vals.into_iter()) {
bound.insert((*p).to_string(), v);
}
for req in hf.required_params() {
if !bound.contains_key(*req) {
return Err(self.err_at(
span,
format!("{} requires `{req}`", hf.name()),
));
}
}
let result =
call_host(&self.host, hf, &bound).map_err(|m| self.err_at(span, m))?;
stack.push(result);
}
Op::Call(fid, argc) => {
let fid = fid as usize;
let argc = argc as usize;