From e834a2cbc47cf18d5f35bb258ccc754c54c4f4e1 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Mon, 5 Sep 2022 11:43:17 -0700 Subject: feat(tvix/eval): Support builtins.attrNames Define `.len()` method on `NixAttrs` to preallocate the capacity of the result vector. Also anchor an errant comment to its context (I think). Change-Id: I268f15025d453d7b3ae1146558c80e51433dd2a8 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6546 Reviewed-by: wpcarro Reviewed-by: sterni Autosubmit: wpcarro Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tvix/eval/src/builtins') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 3598ac71f7..33abfe492d 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -51,6 +51,18 @@ fn pure_builtins() -> Vec { args.pop().unwrap().to_str()?.as_str().to_owned(), )); }), + Builtin::new("attrNames", 1, |args, vm| { + force!(vm, &args[0], value, { + let xs = value.to_attrs()?; + let mut output = Vec::with_capacity(xs.len()); + + for (key, _val) in xs.iter() { + output.push(Value::String(key.clone())); + } + + Ok(Value::List(NixList::construct(output.len(), output))) + }) + }), Builtin::new("catAttrs", 2, |mut args, _| { let list = args.pop().unwrap().to_list()?; let key = args.pop().unwrap().to_str()?; -- cgit 1.4.1