feat(tvix/eval): Support builtins.length
Get the length of a list Change-Id: I41d91e96d833269541a1b3c23b7cc879f96d1e5a Reviewed-on: https://cl.tvl.fyi/c/depot/+/6407 Tested-by: BuildkiteCI Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
parent
15e2bc54f1
commit
197fe37dae
5 changed files with 24 additions and 0 deletions
|
@ -45,6 +45,9 @@ fn pure_builtins() -> Vec<Builtin> {
|
|||
let a = args.pop().unwrap();
|
||||
Ok(arithmetic_op!(a, b, /))
|
||||
}),
|
||||
Builtin::new("length", 1, |args| {
|
||||
Ok(Value::Integer(args[0].as_list()?.len() as i64))
|
||||
}),
|
||||
Builtin::new("isAttrs", 1, |args| {
|
||||
Ok(Value::Bool(matches!(args[0], Value::Attrs(_))))
|
||||
}),
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
[ 0 1 3 ]
|
|
@ -0,0 +1,5 @@
|
|||
[
|
||||
(builtins.length [])
|
||||
(builtins.length [ 1 ])
|
||||
(builtins.length [ "one" "two" "three" ])
|
||||
]
|
|
@ -28,6 +28,10 @@ impl NixList {
|
|||
lhs
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
pub fn construct(count: usize, stack_slice: Vec<Value>) -> Self {
|
||||
debug_assert!(
|
||||
count == stack_slice.len(),
|
||||
|
|
|
@ -103,6 +103,17 @@ impl Value {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn as_list(&self) -> EvalResult<&NixList> {
|
||||
match self {
|
||||
Value::List(xs) => Ok(xs),
|
||||
other => Err(ErrorKind::TypeError {
|
||||
expected: "list",
|
||||
actual: other.type_of(),
|
||||
}
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_string(self) -> EvalResult<NixString> {
|
||||
match self {
|
||||
Value::String(s) => Ok(s),
|
||||
|
|
Loading…
Reference in a new issue