refactor(tvix/eval): add macros for generating Value casters

The casting methods of `Value` are pretty verbose, and actually
incorrect before this commit as they did not account for inner thunk
values.

To address this, we first attempt to make them correct by introducing
a standard macro to generate them and traverse the inner thunk(s) if
necessary.

This is likely to be a performance hit as it will now involve more
cloning of values. We can do multiple things to alleviate this, but
should do some measurements first.

Change-Id: If315d6e2afe7b69db727df535bc6cbfb89a691aa
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6412
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2022-09-02 05:40:50 +03:00 committed by tazjin
parent 0d7ad5e6d1
commit 6b3c3c9826
3 changed files with 56 additions and 76 deletions

View file

@ -24,12 +24,12 @@ fn pure_builtins() -> Vec<Builtin> {
}),
Builtin::new("abort", 1, |mut args, _| {
return Err(ErrorKind::Abort(
args.pop().unwrap().to_string()?.as_str().to_owned(),
args.pop().unwrap().to_str()?.as_str().to_owned(),
));
}),
Builtin::new("catAttrs", 2, |mut args, _| {
let list = args.pop().unwrap().to_list()?;
let key = args.pop().unwrap().to_string()?;
let key = args.pop().unwrap().to_str()?;
let mut output = vec![];
for set in list.into_iter() {
@ -46,7 +46,7 @@ fn pure_builtins() -> Vec<Builtin> {
arithmetic_op!(a, b, /)
}),
Builtin::new("length", 1, |args, _| {
Ok(Value::Integer(args[0].as_list()?.len() as i64))
Ok(Value::Integer(args[0].to_list()?.len() as i64))
}),
Builtin::new("isAttrs", 1, |args, _| {
Ok(Value::Bool(matches!(args[0], Value::Attrs(_))))
@ -90,7 +90,7 @@ fn pure_builtins() -> Vec<Builtin> {
}),
Builtin::new("throw", 1, |mut args, _| {
return Err(ErrorKind::Throw(
args.pop().unwrap().to_string()?.as_str().to_owned(),
args.pop().unwrap().to_str()?.as_str().to_owned(),
));
}),
Builtin::new("toString", 1, |args, _| {