From 5bd0e723c13b8a41bfba442bd8ef5351e31099e6 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 23 Oct 2022 13:24:05 -0400 Subject: refactor(tvix/eval): Implement value comparison with a method Rather than implementing all of the interesting semantics of value comparison with a macro bound to the VM, implement the bulk of the logic with a method on Value itself that returns an Ordering, and then use the macro to implement the comparison against that Ordering. This has no functional change, but paves the way to implementing lexicographic comparison of list values, which is supported in the latest version of upstream nix. Change-Id: I8af1a020b41577021af5939f5edc160c407d4a9e Reviewed-on: https://cl.tvl.fyi/c/depot/+/7069 Autosubmit: grfn Tested-by: BuildkiteCI Reviewed-by: tazjin --- tvix/eval/src/builtins/mod.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'tvix/eval/src/builtins/mod.rs') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index e7eb4e8bf953..336c320e2f00 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -3,7 +3,7 @@ //! See //tvix/eval/docs/builtins.md for a some context on the //! available builtins in Nix. -use std::cmp; +use std::cmp::{self, Ordering}; use std::collections::{BTreeMap, HashMap, HashSet}; use std::path::PathBuf; @@ -16,7 +16,7 @@ use crate::{ vm::VM, }; -use crate::{arithmetic_op, cmp_op}; +use crate::arithmetic_op; use self::versions::{VersionPart, VersionPartsIter}; @@ -407,7 +407,12 @@ fn pure_builtins() -> Vec { Builtin::new( "lessThan", &[false, false], - |args: Vec, vm: &mut VM| cmp_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, <), + |args: Vec, vm: &mut VM| { + Ok(Value::Bool(matches!( + args[0].force(vm)?.nix_cmp(&*args[1].force(vm)?)?, + Some(Ordering::Less) + ))) + }, ), Builtin::new("listToAttrs", &[true], |args: Vec, vm: &mut VM| { let list = args[0].to_list()?; -- cgit 1.4.1