mod any;
mod fmt;
mod fs;
mod io;
mod list;
mod math;
mod net;
mod num;
mod str;
mod sync;
mod thread;
use xur_runtime::{Context, Registry, RunResult, RuntimeError, Value};
pub fn registry() -> Registry {
let registry = Registry::new();
register(®istry);
registry
}
pub fn register(registry: &Registry) {
registry
.module("std")
.raw_function("import", import)
.raw_function("panic", panic);
io::register(registry);
fs::register(registry);
thread::register(registry);
sync::register(registry);
net::register(registry);
any::register(registry);
math::register(registry);
list::register(registry);
str::register(registry);
num::register(registry);
}
fn import(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
match args {
[Value::Str(path)] => cx.import(path),
_ => Err(RuntimeError::new(
"`std::import` takes a single path string",
)),
}
}
fn option(cx: &dyn Context, value: Option<Value>) -> Value {
let lang = cx.lang();
match value {
Some(value) => Value::adt(lang.some, vec![value]),
None => Value::adt(lang.none, Vec::new()),
}
}
fn error_text(error: impl std::fmt::Display) -> String {
error.to_string()
}
fn panic(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let Some((Value::Str(template), args)) = args.split_first() else {
return Err(RuntimeError::new("`panic` takes a format string"));
};
Err(RuntimeError::new(fmt::format(cx, template, args)?))
}