From e0c47f32af6735ab860f7fc8cfcb35d2f7001fd5 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 6 Jan 2023 21:26:06 +0300 Subject: refactor(tvix/eval): use builtins macro for placeholders Change-Id: I30bc475e3e36a163fa169083481cdd4b4d0ca456 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7785 Reviewed-by: flokli Tested-by: BuildkiteCI Autosubmit: tazjin --- tvix/eval/src/builtins/mod.rs | 112 +++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 67 deletions(-) (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 7fd66d5afc75..fbb9fd5075f6 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -11,7 +11,6 @@ use builtin_macros::builtins; use regex::Regex; use crate::arithmetic_op; -use crate::value::BuiltinArgument; use crate::warnings::WarningKind; use crate::{ errors::{ErrorKind, EvalResult}, @@ -972,71 +971,50 @@ pub fn pure_builtins() -> Vec<(&'static str, Value)> { result } -/// Placeholder builtins that technically have a function which we do -/// not yet implement, but which is also not easily observable from -/// within a pure evaluation context. -/// -/// These are used as a crutch to make progress on nixpkgs evaluation. +#[builtins] +mod placeholder_builtins { + use super::*; + + #[builtin("addErrorContext")] + fn builtin_add_error_context( + vm: &mut VM, + #[lazy] _context: Value, + #[lazy] val: Value, + ) -> Result { + vm.emit_warning(WarningKind::NotImplemented("builtins.addErrorContext")); + Ok(val) + } + + #[builtin("unsafeDiscardStringContext")] + fn builtin_unsafe_discard_string_context( + vm: &mut VM, + #[lazy] s: Value, + ) -> Result { + vm.emit_warning(WarningKind::NotImplemented( + "builtins.unsafeDiscardStringContext", + )); + Ok(s) + } + + #[builtin("unsafeGetAttrPos")] + fn builtin_unsafe_get_attr_pos( + vm: &mut VM, + _name: Value, + _attrset: Value, + ) -> Result { + vm.emit_warning(WarningKind::NotImplemented("builtins.unsafeGetAttrsPos")); + let res = [ + ("line", 42.into()), + ("col", 42.into()), + ("file", Value::Path("/deep/thought".into())), + ]; + Ok(Value::attrs(NixAttrs::from_iter(res.into_iter()))) + } +} + pub fn placeholders() -> Vec<(&'static str, Value)> { - let ph = vec![ - Builtin::new( - "addErrorContext", - &[ - BuiltinArgument { - strict: false, - name: "context", - }, - BuiltinArgument { - strict: false, - name: "value", - }, - ], - None, - |mut args: Vec, vm: &mut VM| { - vm.emit_warning(WarningKind::NotImplemented("builtins.addErrorContext")); - Ok(args.pop().unwrap()) - }, - ), - Builtin::new( - "unsafeDiscardStringContext", - &[BuiltinArgument { - strict: true, - name: "s", - }], - None, - |mut args: Vec, vm: &mut VM| { - vm.emit_warning(WarningKind::NotImplemented( - "builtins.unsafeDiscardStringContext", - )); - Ok(args.pop().unwrap()) - }, - ), - Builtin::new( - "unsafeGetAttrPos", - &[ - BuiltinArgument { - strict: true, - name: "name", - }, - BuiltinArgument { - strict: true, - name: "attrset", - }, - ], - None, - |mut args: Vec, vm: &mut VM| { - vm.emit_warning(WarningKind::NotImplemented("builtins.unsafeGetAttrsPos")); - let _attrset = args.pop().unwrap().to_attrs(); - let _name = args.pop().unwrap().to_str(); - let res = [ - ("line", 42.into()), - ("col", 42.into()), - ("file", Value::Path("/deep/thought".into())), - ]; - Ok(Value::attrs(NixAttrs::from_iter(res.into_iter()))) - }, - ), - ]; - - ph.into_iter().map(builtin_tuple).collect() + placeholder_builtins::builtins() + .into_iter() + .map(builtin_tuple) + .collect() } -- cgit 1.4.1