refactor(tvix/eval): order builtins alphabetically

This makes it easier to compare currently implemented ones with the full
list.

Change-Id: Ibaffd99d05afa15fc9ab644fd101afa24fc7a1b2
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7008
Tested-by: BuildkiteCI
Autosubmit: j4m3s <james.landrein@gmail.com>
Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
James Landrein 2022-10-13 23:29:12 +02:00 committed by clbot
parent d4bc58b38d
commit cbbe283b6f

View file

@ -50,14 +50,14 @@ pub fn coerce_value_to_path(v: &Value, vm: &mut VM) -> Result<PathBuf, ErrorKind
/// WASM). /// WASM).
fn pure_builtins() -> Vec<Builtin> { fn pure_builtins() -> Vec<Builtin> {
vec![ vec![
Builtin::new("abort", &[true], |args: Vec<Value>, _: &mut VM| {
Err(ErrorKind::Abort(args[0].to_str()?.to_string()))
}),
Builtin::new( Builtin::new(
"add", "add",
&[false, false], &[false, false],
|args: Vec<Value>, vm: &mut VM| arithmetic_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, +), |args: Vec<Value>, vm: &mut VM| arithmetic_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, +),
), ),
Builtin::new("abort", &[true], |args: Vec<Value>, _: &mut VM| {
Err(ErrorKind::Abort(args[0].to_str()?.to_string()))
}),
Builtin::new("all", &[true, true], |args: Vec<Value>, vm: &mut VM| { Builtin::new("all", &[true, true], |args: Vec<Value>, vm: &mut VM| {
for value in args[1].to_list()?.into_iter() { for value in args[1].to_list()?.into_iter() {
let pred_result = vm.call_with(&args[0], [value])?; let pred_result = vm.call_with(&args[0], [value])?;
@ -276,23 +276,6 @@ fn pure_builtins() -> Vec<Builtin> {
}), }),
} }
}), }),
Builtin::new("length", &[true], |args: Vec<Value>, _: &mut VM| {
Ok(Value::Integer(args[0].to_list()?.len() as i64))
}),
Builtin::new("map", &[true, true], |args: Vec<Value>, vm: &mut VM| {
let list: NixList = args[1].to_list()?;
list.into_iter()
.map(|val| vm.call_with(&args[0], [val]))
.collect::<Result<Vec<Value>, _>>()
.map(|list| Value::List(NixList::from(list)))
.map_err(Into::into)
}),
Builtin::new(
"lessThan",
&[false, false],
|args: Vec<Value>, vm: &mut VM| cmp_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, <),
),
Builtin::new("hasAttr", &[true, true], |args: Vec<Value>, _: &mut VM| { Builtin::new("hasAttr", &[true, true], |args: Vec<Value>, _: &mut VM| {
let k = args[0].to_str()?; let k = args[0].to_str()?;
let xs = args[1].to_attrs()?; let xs = args[1].to_attrs()?;
@ -345,6 +328,14 @@ fn pure_builtins() -> Vec<Builtin> {
let value = args[0].force(vm)?; let value = args[0].force(vm)?;
Ok(Value::Bool(matches!(*value, Value::String(_)))) Ok(Value::Bool(matches!(*value, Value::String(_))))
}), }),
Builtin::new("length", &[true], |args: Vec<Value>, _: &mut VM| {
Ok(Value::Integer(args[0].to_list()?.len() as i64))
}),
Builtin::new(
"lessThan",
&[false, false],
|args: Vec<Value>, vm: &mut VM| cmp_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, <),
),
Builtin::new("listToAttrs", &[true], |args: Vec<Value>, vm: &mut VM| { Builtin::new("listToAttrs", &[true], |args: Vec<Value>, vm: &mut VM| {
let list = args[0].to_list()?; let list = args[0].to_list()?;
let mut map = BTreeMap::new(); let mut map = BTreeMap::new();
@ -362,30 +353,20 @@ fn pure_builtins() -> Vec<Builtin> {
} }
Ok(Value::attrs(NixAttrs::from_map(map))) Ok(Value::attrs(NixAttrs::from_map(map)))
}), }),
Builtin::new("map", &[true, true], |args: Vec<Value>, vm: &mut VM| {
let list: NixList = args[1].to_list()?;
list.into_iter()
.map(|val| vm.call_with(&args[0], [val]))
.collect::<Result<Vec<Value>, _>>()
.map(|list| Value::List(NixList::from(list)))
.map_err(Into::into)
}),
Builtin::new( Builtin::new(
"mul", "mul",
&[false, false], &[false, false],
|args: Vec<Value>, vm: &mut VM| arithmetic_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, *), |args: Vec<Value>, vm: &mut VM| arithmetic_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, *),
), ),
Builtin::new(
"removeAttrs",
&[true, true],
|args: Vec<Value>, _: &mut VM| {
let attrs = args[0].to_attrs()?;
let keys = args[1]
.to_list()?
.into_iter()
.map(|v| v.to_str())
.collect::<Result<HashSet<_>, _>>()?;
let mut res = BTreeMap::new();
for (k, v) in attrs.iter() {
if !keys.contains(k) {
res.insert(k.clone(), v.clone());
}
}
Ok(Value::attrs(NixAttrs::from_map(res)))
},
),
Builtin::new("parseDrvName", &[true], |args: Vec<Value>, _vm: &mut VM| { Builtin::new("parseDrvName", &[true], |args: Vec<Value>, _vm: &mut VM| {
// This replicates cppnix's (mis?)handling of codepoints // This replicates cppnix's (mis?)handling of codepoints
// above U+007f following 0x2d ('-') // above U+007f following 0x2d ('-')
@ -411,6 +392,25 @@ fn pure_builtins() -> Vec<Builtin> {
("version".into(), version), ("version".into(), version),
])))) ]))))
}), }),
Builtin::new(
"removeAttrs",
&[true, true],
|args: Vec<Value>, _: &mut VM| {
let attrs = args[0].to_attrs()?;
let keys = args[1]
.to_list()?
.into_iter()
.map(|v| v.to_str())
.collect::<Result<HashSet<_>, _>>()?;
let mut res = BTreeMap::new();
for (k, v) in attrs.iter() {
if !keys.contains(k) {
res.insert(k.clone(), v.clone());
}
}
Ok(Value::attrs(NixAttrs::from_map(res)))
},
),
Builtin::new("splitVersion", &[true], |args: Vec<Value>, _: &mut VM| { Builtin::new("splitVersion", &[true], |args: Vec<Value>, _: &mut VM| {
let s = args[0].to_str()?; let s = args[0].to_str()?;
let s = VersionPartsIter::new(s.as_str()); let s = VersionPartsIter::new(s.as_str());
@ -480,6 +480,12 @@ fn pure_builtins() -> Vec<Builtin> {
Builtin::new("throw", &[true], |args: Vec<Value>, _: &mut VM| { Builtin::new("throw", &[true], |args: Vec<Value>, _: &mut VM| {
Err(ErrorKind::Throw(args[0].to_str()?.to_string())) Err(ErrorKind::Throw(args[0].to_str()?.to_string()))
}), }),
// coerce_to_string forces for us
Builtin::new("toString", &[false], |args: Vec<Value>, vm: &mut VM| {
args[0]
.coerce_to_string(CoercionKind::Strong, vm)
.map(Value::String)
}),
Builtin::new( Builtin::new(
"trace", "trace",
&[true, true], &[true, true],
@ -507,12 +513,6 @@ fn pure_builtins() -> Vec<Builtin> {
} }
Ok(Value::attrs(NixAttrs::from_map(res))) Ok(Value::attrs(NixAttrs::from_map(res)))
}), }),
// coerce_to_string forces for us
Builtin::new("toString", &[false], |args: Vec<Value>, vm: &mut VM| {
args[0]
.coerce_to_string(CoercionKind::Strong, vm)
.map(Value::String)
}),
Builtin::new("typeOf", &[false], |args: Vec<Value>, vm: &mut VM| { Builtin::new("typeOf", &[false], |args: Vec<Value>, vm: &mut VM| {
// We force manually here because it also unwraps the Thunk // We force manually here because it also unwraps the Thunk
// representation, if any. // representation, if any.