about summary refs log tree commit diff
path: root/tvix/eval/src/builtins
diff options
context:
space:
mode:
authorGriffin Smith <grfn@gws.fyi>2022-10-13T04·12-0400
committertazjin <tazjin@tvl.su>2022-10-17T11·29+0000
commit0063e7e913c199538fe67d55e714dd34c09cece3 (patch)
treea3e0a15c8f19491afb98442fef0102fec77df163 /tvix/eval/src/builtins
parent2a3d49810482b36de9f2d3087e5064545183dbb3 (diff)
feat(nix/eval): Implement builtins.functionArgs r/5155
Now that we're tracking formals on Lambda this ends up being quite easy;
we just pull them off of the Lambda for the argument closure and use
them to construct the result attribute set.

Change-Id: I811cb61ec34c6bef123a4043000b18c0e4ea0125
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7003
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/builtins')
-rw-r--r--tvix/eval/src/builtins/mod.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs
index a5676a63f895..0b5911de85b6 100644
--- a/tvix/eval/src/builtins/mod.rs
+++ b/tvix/eval/src/builtins/mod.rs
@@ -273,6 +273,21 @@ fn pure_builtins() -> Vec<Builtin> {
                 Ok(res)
             },
         ),
+        Builtin::new("functionArgs", &[true], |args: Vec<Value>, _: &mut VM| {
+            let lambda = args[0].to_closure()?.lambda();
+            let formals = if let Some(formals) = &lambda.formals {
+                formals
+            } else {
+                return Ok(Value::attrs(NixAttrs::empty()));
+            };
+            Ok(Value::attrs(NixAttrs::from_map(
+                formals
+                    .arguments
+                    .iter()
+                    .map(|(k, v)| (k.clone(), (*v).into()))
+                    .collect(),
+            )))
+        }),
         Builtin::new("fromJSON", &[true], |args: Vec<Value>, _: &mut VM| {
             let json_str = args[0].to_str()?;
             let json: serde_json::Value = serde_json::from_str(&json_str)?;