From cbbe283b6fda93e86478c16ea295e13374eccd58 Mon Sep 17 00:00:00 2001 From: James Landrein Date: Thu, 13 Oct 2022 23:29:12 +0200 Subject: [PATCH] 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 Reviewed-by: tazjin --- tvix/eval/src/builtins/mod.rs | 90 +++++++++++++++++------------------ 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index cabdaf007..a0bc9540d 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -50,14 +50,14 @@ pub fn coerce_value_to_path(v: &Value, vm: &mut VM) -> Result Vec { vec![ + Builtin::new("abort", &[true], |args: Vec, _: &mut VM| { + Err(ErrorKind::Abort(args[0].to_str()?.to_string())) + }), Builtin::new( "add", &[false, false], |args: Vec, vm: &mut VM| arithmetic_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, +), ), - Builtin::new("abort", &[true], |args: Vec, _: &mut VM| { - Err(ErrorKind::Abort(args[0].to_str()?.to_string())) - }), Builtin::new("all", &[true, true], |args: Vec, vm: &mut VM| { for value in args[1].to_list()?.into_iter() { let pred_result = vm.call_with(&args[0], [value])?; @@ -276,23 +276,6 @@ fn pure_builtins() -> Vec { }), } }), - Builtin::new("length", &[true], |args: Vec, _: &mut VM| { - Ok(Value::Integer(args[0].to_list()?.len() as i64)) - }), - Builtin::new("map", &[true, true], |args: Vec, vm: &mut VM| { - let list: NixList = args[1].to_list()?; - - list.into_iter() - .map(|val| vm.call_with(&args[0], [val])) - .collect::, _>>() - .map(|list| Value::List(NixList::from(list))) - .map_err(Into::into) - }), - Builtin::new( - "lessThan", - &[false, false], - |args: Vec, vm: &mut VM| cmp_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, <), - ), Builtin::new("hasAttr", &[true, true], |args: Vec, _: &mut VM| { let k = args[0].to_str()?; let xs = args[1].to_attrs()?; @@ -345,6 +328,14 @@ fn pure_builtins() -> Vec { let value = args[0].force(vm)?; Ok(Value::Bool(matches!(*value, Value::String(_)))) }), + Builtin::new("length", &[true], |args: Vec, _: &mut VM| { + Ok(Value::Integer(args[0].to_list()?.len() as i64)) + }), + Builtin::new( + "lessThan", + &[false, false], + |args: Vec, vm: &mut VM| cmp_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, <), + ), Builtin::new("listToAttrs", &[true], |args: Vec, vm: &mut VM| { let list = args[0].to_list()?; let mut map = BTreeMap::new(); @@ -362,30 +353,20 @@ fn pure_builtins() -> Vec { } Ok(Value::attrs(NixAttrs::from_map(map))) }), + Builtin::new("map", &[true, true], |args: Vec, vm: &mut VM| { + let list: NixList = args[1].to_list()?; + + list.into_iter() + .map(|val| vm.call_with(&args[0], [val])) + .collect::, _>>() + .map(|list| Value::List(NixList::from(list))) + .map_err(Into::into) + }), Builtin::new( "mul", &[false, false], |args: Vec, vm: &mut VM| arithmetic_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, *), ), - Builtin::new( - "removeAttrs", - &[true, true], - |args: Vec, _: &mut VM| { - let attrs = args[0].to_attrs()?; - let keys = args[1] - .to_list()? - .into_iter() - .map(|v| v.to_str()) - .collect::, _>>()?; - 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, _vm: &mut VM| { // This replicates cppnix's (mis?)handling of codepoints // above U+007f following 0x2d ('-') @@ -411,6 +392,25 @@ fn pure_builtins() -> Vec { ("version".into(), version), ])))) }), + Builtin::new( + "removeAttrs", + &[true, true], + |args: Vec, _: &mut VM| { + let attrs = args[0].to_attrs()?; + let keys = args[1] + .to_list()? + .into_iter() + .map(|v| v.to_str()) + .collect::, _>>()?; + 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, _: &mut VM| { let s = args[0].to_str()?; let s = VersionPartsIter::new(s.as_str()); @@ -480,6 +480,12 @@ fn pure_builtins() -> Vec { Builtin::new("throw", &[true], |args: Vec, _: &mut VM| { Err(ErrorKind::Throw(args[0].to_str()?.to_string())) }), + // coerce_to_string forces for us + Builtin::new("toString", &[false], |args: Vec, vm: &mut VM| { + args[0] + .coerce_to_string(CoercionKind::Strong, vm) + .map(Value::String) + }), Builtin::new( "trace", &[true, true], @@ -507,12 +513,6 @@ fn pure_builtins() -> Vec { } Ok(Value::attrs(NixAttrs::from_map(res))) }), - // coerce_to_string forces for us - Builtin::new("toString", &[false], |args: Vec, vm: &mut VM| { - args[0] - .coerce_to_string(CoercionKind::Strong, vm) - .map(Value::String) - }), Builtin::new("typeOf", &[false], |args: Vec, vm: &mut VM| { // We force manually here because it also unwraps the Thunk // representation, if any.