xur-std 0.1.8

The standard library of the Xur language
Documentation
//! `std::reflect::func`: inspecting function signatures at runtime.

use xur_runtime::registry::{RawSig, XurType};
use xur_runtime::{RunResult, RuntimeError, TypeDesc, Value};

pub(super) fn register(module: &mut xur_runtime::registry::ModuleBuilder<'_>) {
    let mut func = module.submodule("func");
    func.raw_function_sig(
        "name",
        RawSig::new()
            .param("f", "fn")
            .returns(TypeDesc::of::<String>()),
        |cx, args| {
            let [value] = args else {
                return arity("name", "f");
            };
            let info = cx
                .fn_info(value)
                .ok_or_else(|| RuntimeError::new("`reflect::func::name` expects a function"))?;
            Ok(Value::str(&info.name))
        },
    )
    .raw_function_sig(
        "params",
        RawSig::new()
            .param("f", "fn")
            .returns(TypeDesc::of::<i64>()),
        |cx, args| {
            let [value] = args else {
                return arity("params", "f");
            };
            let info = cx
                .fn_info(value)
                .ok_or_else(|| RuntimeError::new("`reflect::func::params` expects a function"))?;
            Ok(Value::int(info.param_count as i64))
        },
    )
    .raw_function_sig(
        "param_names",
        RawSig::new()
            .param("f", "fn")
            .returns("list<str>")
            .lsp(XurType::list::<String>()),
        |cx, args| {
            let [value] = args else {
                return arity("param_names", "f");
            };
            let info = cx.fn_info(value).ok_or_else(|| {
                RuntimeError::new("`reflect::func::param_names` expects a function")
            })?;
            let names: Vec<Value> = info
                .param_names
                .iter()
                .map(|n| match n {
                    Some(name) => Value::str(name),
                    None => Value::str("_"),
                })
                .collect();
            Ok(Value::list(names))
        },
    )
    .raw_function_sig(
        "param_types",
        RawSig::new()
            .param("f", "fn")
            .returns("list<Option<type>>")
            .lsp(XurType::list_named("type")),
        |cx, args| {
            let [value] = args else {
                return arity("param_types", "f");
            };
            let info = cx.fn_info(value).ok_or_else(|| {
                RuntimeError::new("`reflect::func::param_types` expects a function")
            })?;
            let lang = cx.lang();
            let types: Vec<Value> = info
                .param_types
                .iter()
                .map(|t| match t {
                    Some(ty) => Value::adt(lang.some, vec![Value::Type(Box::new(ty.clone()))]),
                    None => Value::adt(lang.none, Vec::new()),
                })
                .collect();
            Ok(Value::list(types))
        },
    )
    .raw_function_sig(
        "return_type",
        RawSig::new().param("f", "fn").returns("Option<type>"),
        |cx, args| {
            let [value] = args else {
                return arity("return_type", "f");
            };
            let info = cx.fn_info(value).ok_or_else(|| {
                RuntimeError::new("`reflect::func::return_type` expects a function")
            })?;
            let lang = cx.lang();
            match info.return_type {
                Some(ty) => Ok(Value::adt(lang.some, vec![Value::Type(Box::new(ty))])),
                None => Ok(Value::adt(lang.none, Vec::new())),
            }
        },
    );
}

fn arity(name: &str, params: &str) -> RunResult<Value> {
    Err(RuntimeError::new(format!(
        "`reflect::func::{name}` takes ({params})"
    )))
}