use crate::builtins;
use crate::env::{CelTypeRegistrar, Env, EnvBuilder};
use crate::error::{EnvError, RuntimeError};
use crate::macros::MacroRegistry;
use crate::types::{
FunctionDecl, IdentDecl, NamedType, OverloadDecl, is_assignable,
};
use crate::value::{IntoValue, TryFromValue, Value, ValueError};
use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;
pub type NativeFunction = Arc<
dyn Fn(&CallContext<'_>) -> Result<Value, RuntimeError>
+ Send
+ Sync
+ 'static,
>;
#[derive(Clone)]
pub struct Runtime {
env: Env,
variables: Arc<BTreeMap<String, Value>>,
functions: Arc<BTreeMap<String, NativeFunction>>,
}
impl fmt::Debug for Runtime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Runtime")
.field("env", &self.env)
.field("variables", &self.variables)
.field("functions", &self.functions.keys().collect::<Vec<_>>())
.finish()
}
}
impl Runtime {
pub fn builder() -> RuntimeBuilder {
RuntimeBuilder::default()
}
pub fn env(&self) -> &Env {
&self.env
}
pub fn variables(&self) -> &BTreeMap<String, Value> {
self.variables.as_ref()
}
pub fn functions(&self) -> &BTreeMap<String, NativeFunction> {
self.functions.as_ref()
}
pub fn macros(&self) -> &MacroRegistry {
self.env.macros()
}
pub fn child_builder(&self) -> RuntimeBuilder {
RuntimeBuilder::from_runtime(self)
}
pub fn eval(&self, source: &str) -> Result<Value, RuntimeError> {
crate::interpreter::eval(self, source)
}
pub fn get_variable(&self, name: &str) -> Option<&Value> {
self.variables.get(name)
}
pub(crate) fn resolve_identifier(&self, name: &str) -> Option<Value> {
if let Some(value) = self.variables.get(name) {
return Some(value.clone());
}
self.env
.lookup_ident(name)
.and_then(|decl| decl.value.as_ref().map(Value::from))
}
pub(crate) fn lookup_function(&self, name: &str) -> Option<NativeFunction> {
self.functions.get(name).cloned()
}
}
pub struct CallContext<'a> {
runtime: &'a Runtime,
args: &'a [Value],
}
impl<'a> CallContext<'a> {
pub(crate) fn new(runtime: &'a Runtime, args: &'a [Value]) -> Self {
Self { runtime, args }
}
pub fn runtime(&self) -> &'a Runtime {
self.runtime
}
pub fn args(&self) -> &'a [Value] {
self.args
}
pub fn arg(&self, index: usize) -> Option<&'a Value> {
self.args.get(index)
}
}
#[derive(Clone)]
pub struct RuntimeBuilder {
env_builder: EnvBuilder,
variables: BTreeMap<String, Value>,
functions: BTreeMap<String, NativeFunction>,
}
impl Default for RuntimeBuilder {
fn default() -> Self {
let mut builder = RuntimeBuilder {
env_builder: EnvBuilder::default(),
variables: BTreeMap::new(),
functions: BTreeMap::new(),
};
builder
.install_standard_library()
.expect("core functions must register");
builder
}
}
impl RuntimeBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn empty() -> Self {
RuntimeBuilder {
env_builder: EnvBuilder::empty(),
variables: BTreeMap::new(),
functions: BTreeMap::new(),
}
}
pub fn from_runtime(runtime: &Runtime) -> Self {
let mut env_builder = Env::builder();
env_builder
.import_env(runtime.env())
.expect("parent environment is always valid");
RuntimeBuilder {
env_builder,
variables: runtime.variables.as_ref().clone(),
functions: runtime
.functions
.iter()
.map(|(name, fun)| (name.clone(), fun.clone()))
.collect(),
}
}
pub fn import_env(&mut self, env: &Env) -> Result<&mut Self, EnvError> {
self.env_builder
.import_env(env)
.map_err(|err| EnvError::new(err.to_string()))?;
Ok(self)
}
pub fn import_env_owned(
&mut self,
env: Env,
) -> Result<&mut Self, EnvError> {
self.env_builder
.import_env_owned(env)
.map_err(|err| EnvError::new(err.to_string()))?;
Ok(self)
}
pub fn add_type(&mut self, ty: NamedType) -> Result<&mut Self, EnvError> {
self.env_builder
.add_type(ty)
.map_err(|err| EnvError::new(err.to_string()))?;
Ok(self)
}
pub fn add_ident(
&mut self,
ident: IdentDecl,
) -> Result<&mut Self, EnvError> {
self.env_builder
.add_ident(ident)
.map_err(|err| EnvError::new(err.to_string()))?;
Ok(self)
}
pub fn add_function_decl(
&mut self,
decl: FunctionDecl,
) -> Result<&mut Self, EnvError> {
self.env_builder
.add_function(decl)
.map_err(|err| EnvError::new(err.to_string()))?;
Ok(self)
}
pub fn set_variable<V>(
&mut self,
name: impl Into<String>,
value: V,
) -> Result<&mut Self, RuntimeError>
where
V: IntoValue,
{
let name = name.into();
let value = value.into_value();
self.validate_variable_type(&name, &value)?;
self.variables.insert(name, value);
Ok(self)
}
pub fn set_variables<I, K, V>(
&mut self,
entries: I,
) -> Result<&mut Self, RuntimeError>
where
I: IntoIterator<Item = (K, V)>,
K: Into<String>,
V: IntoValue,
{
for (key, value) in entries {
self.set_variable(key, value)?;
}
Ok(self)
}
pub fn set_function<F, Args, Out>(
&mut self,
name: impl Into<String>,
function: F,
) -> Result<&mut Self, RuntimeError>
where
F: IntoNativeFunction<Args, Out>,
{
let name = name.into();
let (native, arity) = function.into_native(name.clone());
self.insert_function(name, native, arity)
}
fn insert_function(
&mut self,
name: String,
handler: NativeFunction,
arity: Option<usize>,
) -> Result<&mut Self, RuntimeError> {
let decl = self.env_builder.lookup_function(&name).ok_or_else(|| {
RuntimeError::new(format!(
"Function '{name}' is not declared; register a FunctionDecl first",
))
})?;
if let Some(expected) = arity
&& !decl
.overloads
.iter()
.any(|overload| overload_arity(overload) == expected)
{
let allowed = declared_arities(decl);
return Err(RuntimeError::new(format!(
"Function '{name}' does not have an overload accepting {expected} arguments (declared: {allowed})",
)));
}
if self.functions.contains_key(&name) {
return Err(RuntimeError::new(format!(
"Function '{name}' is already registered",
)));
}
self.functions.insert(name, handler);
Ok(self)
}
fn validate_variable_type(
&self,
name: &str,
value: &Value,
) -> Result<(), RuntimeError> {
if let Some(decl) = self.env_builder.lookup_ident(name) {
let actual = value.cel_type();
if !is_assignable(&decl.ty, &actual) {
return Err(RuntimeError::new(format!(
"Variable '{name}' expects type {expected} but received {actual}",
expected = decl.ty,
actual = actual,
)));
}
}
Ok(())
}
pub fn macros(&self) -> &MacroRegistry {
self.env_builder.macros()
}
pub fn macros_mut(&mut self) -> &mut MacroRegistry {
self.env_builder.macros_mut()
}
pub fn build(self) -> Runtime {
Runtime {
env: self.env_builder.build(),
variables: Arc::new(self.variables),
functions: Arc::new(self.functions),
}
}
fn install_standard_library(&mut self) -> Result<(), RuntimeError> {
builtins::register(self)
}
}
impl CelTypeRegistrar for RuntimeBuilder {
fn register_type(&mut self, ty: NamedType) -> Result<(), EnvError> {
self.add_type(ty).map(|_| ())
}
}
fn overload_arity(overload: &OverloadDecl) -> usize {
overload.args.len() + if overload.receiver.is_some() { 1 } else { 0 }
}
fn declared_arities(decl: &FunctionDecl) -> String {
let mut counts = decl
.overloads
.iter()
.map(overload_arity)
.collect::<Vec<_>>();
counts.sort_unstable();
counts.dedup();
counts
.into_iter()
.map(|value| value.to_string())
.collect::<Vec<_>>()
.join(", ")
}
pub trait IntoNativeFunction<Args, Out> {
fn into_native(self, name: String) -> (NativeFunction, Option<usize>);
}
impl IntoNativeFunction<(), Value> for NativeFunction {
fn into_native(self, _name: String) -> (NativeFunction, Option<usize>) {
(self, None)
}
}
impl<F, R> IntoNativeFunction<(), R> for F
where
F: Fn() -> R + Send + Sync + 'static,
R: FunctionOutput,
{
fn into_native(self, name: String) -> (NativeFunction, Option<usize>) {
let fname = name.clone();
let handler: NativeFunction = Arc::new(move |ctx: &CallContext<'_>| {
if !ctx.args.is_empty() {
return Err(RuntimeError::wrong_arity(
&fname,
0,
ctx.args.len(),
));
}
let result = self();
result.into_runtime_result()
});
(handler, Some(0))
}
}
macro_rules! impl_into_native_function {
($(($param:ident, $var:ident)),+ $(,)?) => {
impl<F, R, $($param),+> IntoNativeFunction<($($param,)+), R> for F
where
F: Fn($($param),+) -> R + Send + Sync + 'static,
R: FunctionOutput,
$($param: TryFromValue,)+
{
fn into_native(self, name: String) -> (NativeFunction, Option<usize>) {
let fname = name.clone();
const EXPECTED: usize = impl_into_native_function!(@count $(($param, $var)),+);
let handler: NativeFunction = Arc::new(move |ctx: &CallContext<'_>| {
if ctx.args.len() != EXPECTED {
return Err(RuntimeError::wrong_arity(&fname, EXPECTED, ctx.args.len()));
}
impl_into_native_function!(@convert ctx, fname, self, [$(($param, $var)),+])
});
(handler, Some(EXPECTED))
}
}
};
(@count $(($param:ident, $var:ident)),+) => {
impl_into_native_function!(@count_helper $($param),+)
};
(@count_helper $head:ident $(,$tail:ident)*) => {
1usize + impl_into_native_function!(@count_helper $($tail),*)
};
(@count_helper) => {0usize};
(@convert $ctx:ident, $fname:ident, $func:ident, [$(($param:ident, $var:ident)),+]) => {{
let mut iter = $ctx.args.iter().enumerate();
$(
let $var = {
let (idx, value) = iter.next().expect("arity validated");
$param::try_from_value(value)
.map_err(|err| RuntimeError::argument(&$fname, idx, err))?
};
)+
let result = $func($($var),+);
result.into_runtime_result()
}};
}
impl_into_native_function!((A0, arg0));
impl_into_native_function!((A0, arg0), (A1, arg1));
impl_into_native_function!((A0, arg0), (A1, arg1), (A2, arg2));
impl_into_native_function!((A0, arg0), (A1, arg1), (A2, arg2), (A3, arg3));
impl_into_native_function!(
(A0, arg0),
(A1, arg1),
(A2, arg2),
(A3, arg3),
(A4, arg4)
);
impl_into_native_function!(
(A0, arg0),
(A1, arg1),
(A2, arg2),
(A3, arg3),
(A4, arg4),
(A5, arg5)
);
pub trait FunctionOutput {
fn into_runtime_result(self) -> Result<Value, RuntimeError>;
}
impl<R> FunctionOutput for R
where
R: IntoValue,
{
fn into_runtime_result(self) -> Result<Value, RuntimeError> {
Ok(self.into_value())
}
}
impl<R> FunctionOutput for Result<R, RuntimeError>
where
R: FunctionOutput,
{
fn into_runtime_result(self) -> Result<Value, RuntimeError> {
match self {
Ok(value) => value.into_runtime_result(),
Err(err) => Err(err),
}
}
}
impl<R> FunctionOutput for Result<R, ValueError>
where
R: FunctionOutput,
{
fn into_runtime_result(self) -> Result<Value, RuntimeError> {
match self {
Ok(value) => value.into_runtime_result(),
Err(err) => Err(RuntimeError::from(err)),
}
}
}
#[cfg(test)]
mod tests {
use crate::ListValue;
use crate::types::{FunctionDecl, IdentDecl, OverloadDecl, Type};
use super::*;
#[test]
fn size_builtin_is_available() {
let runtime = Runtime::builder().build();
let value = runtime.eval("size([1, 2, 3])").expect("size to work");
assert_eq!(value, Value::Int(3));
}
#[test]
fn starts_with_builtin_supports_methods() {
let runtime = Runtime::builder().build();
let positive = runtime
.eval("'foobar'.startsWith('foo')")
.expect("startsWith to work");
assert_eq!(positive, Value::Bool(true));
let negative = runtime
.eval("'foobar'.startsWith('bar')")
.expect("startsWith to work");
assert_eq!(negative, Value::Bool(false));
}
#[test]
fn evaluates_standard_macros() {
let mut builder = Runtime::builder();
builder
.set_variable("roles", ListValue::from(vec!["admin", "viewer"]))
.expect("variable to set");
let runtime = builder.build();
let exists = runtime
.eval("roles.exists(role, role == 'admin')")
.expect("exists macro to evaluate");
assert_eq!(exists, Value::Bool(true));
let all = runtime
.eval("roles.all(role, role == 'admin')")
.expect("all macro to evaluate");
assert_eq!(all, Value::Bool(false));
}
#[test]
fn set_variable_respects_declared_types() {
let mut builder = Runtime::builder();
builder
.add_ident(IdentDecl::new("x", Type::Int))
.expect("ident to register");
builder
.set_variable("x", 42_i64)
.expect("value matches declared type");
let err = builder.set_variable("x", "oops");
assert!(err.is_err());
}
#[test]
fn register_function_requires_declaration() {
let mut builder = Runtime::builder();
let err = builder.set_function("custom", |v: i64| v);
assert!(err.is_err());
let mut decl = FunctionDecl::new("custom");
decl.add_overload(OverloadDecl::new(
"custom_int",
vec![Type::Int],
Type::Int,
))
.expect("overload");
builder
.add_function_decl(decl)
.expect("function decl to register");
builder
.set_function("custom", |v: i64| v)
.expect("function to register once declaration exists");
}
#[test]
fn register_function_validates_arity() {
let mut builder = Runtime::builder();
let mut decl = FunctionDecl::new("adder");
decl.add_overload(OverloadDecl::new(
"adder_int",
vec![Type::Int, Type::Int],
Type::Int,
))
.expect("overload");
builder.add_function_decl(decl).expect("decl to register");
let err = builder.set_function("adder", |value: i64| value);
assert!(err.is_err());
builder
.set_function("adder", |lhs: i64, rhs: i64| lhs + rhs)
.expect("matching arity should register");
}
}