feat(tvix/eval): Implement builtins.genList
Change-Id: Iabe28656229f508226b244d81382e517961eb3cf Reviewed-on: https://cl.tvl.fyi/c/depot/+/6901 Autosubmit: grfn <grfn@gws.fyi> Tested-by: BuildkiteCI Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
parent
b6089fb1e5
commit
0e97555644
4 changed files with 19 additions and 0 deletions
|
@ -222,6 +222,17 @@ fn pure_builtins() -> Vec<Builtin> {
|
|||
.map(|list| Value::List(NixList::from(list)))
|
||||
.map_err(Into::into)
|
||||
}),
|
||||
Builtin::new("genList", &[true, true], |args: Vec<Value>, vm: &mut VM| {
|
||||
let len = args[1].as_int()?;
|
||||
(0..len)
|
||||
.map(|i| {
|
||||
vm.push(i.into());
|
||||
vm.call_value(&args[0])
|
||||
})
|
||||
.collect::<Result<Vec<Value>, _>>()
|
||||
.map(|list| Value::List(NixList::from(list)))
|
||||
.map_err(Into::into)
|
||||
}),
|
||||
Builtin::new("getAttr", &[true, true], |args: Vec<Value>, _: &mut VM| {
|
||||
let k = args[0].to_str()?;
|
||||
let xs = args[1].to_attrs()?;
|
||||
|
|
1
tvix/eval/src/tests/tvix_tests/eval-okay-genlist.exp
Normal file
1
tvix/eval/src/tests/tvix_tests/eval-okay-genlist.exp
Normal file
|
@ -0,0 +1 @@
|
|||
[ 0 1 4 9 16 ]
|
1
tvix/eval/src/tests/tvix_tests/eval-okay-genlist.nix
Normal file
1
tvix/eval/src/tests/tvix_tests/eval-okay-genlist.nix
Normal file
|
@ -0,0 +1 @@
|
|||
builtins.genList (x: x * x) 5
|
|
@ -359,6 +359,12 @@ impl Display for Value {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<i64> for Value {
|
||||
fn from(i: i64) -> Self {
|
||||
Self::Integer(i)
|
||||
}
|
||||
}
|
||||
|
||||
fn type_error(expected: &'static str, actual: &Value) -> ErrorKind {
|
||||
ErrorKind::TypeError {
|
||||
expected,
|
||||
|
|
Loading…
Reference in a new issue