feat(tvix/eval): implement 'throw' and 'abort' builtins

These do essentially the same, but return different error variants as
upstream Nix considers `throw` to be (sometimes) catchable.

Change-Id: I1a9ea84567d46fb37287dbf3f3f67052f9382cca
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6259
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
Vincent Ambo 2022-08-24 17:13:00 +03:00 committed by tazjin
parent 3d0280ec03
commit bd0fc69f07
2 changed files with 18 additions and 1 deletions

View file

@ -8,13 +8,26 @@ use std::{
rc::Rc,
};
use crate::value::{Builtin, NixAttrs, NixString, Value};
use crate::{
errors::ErrorKind,
value::{Builtin, NixAttrs, NixString, Value},
};
fn pure_builtins() -> Vec<Builtin> {
vec![
Builtin::new("abort", 1, |mut args| {
return Err(
ErrorKind::Abort(args.pop().unwrap().to_string()?.as_str().to_owned()).into(),
);
}),
Builtin::new("isNull", 1, |args| {
Ok(Value::Bool(matches!(args[0], Value::Null)))
}),
Builtin::new("throw", 1, |mut args| {
return Err(
ErrorKind::Throw(args.pop().unwrap().to_string()?.as_str().to_owned()).into(),
);
}),
Builtin::new("toString", 1, |args| {
// TODO: toString is actually not the same as Display
Ok(Value::String(format!("{}", args[0]).into()))

View file

@ -38,6 +38,10 @@ pub enum ErrorKind {
ParseErrors(Vec<rnix::parser::ParseError>),
AssertionFailed,
// These are user-generated errors through builtins.
Throw(String),
Abort(String),
}
#[derive(Clone, Debug)]