From da1d71a4e8e3afee09d8f72b79b6042d4ccbfc2d Mon Sep 17 00:00:00 2001 From: sterni Date: Tue, 13 Sep 2022 15:37:19 +0200 Subject: feat(tvix/eval): implement correct toString behavior Implement C++ Nix's `EvalState::coerceToString` minus some of the Path / store handling. This is currently only used for `toString` which does all possible coercions, but we've already prepared the weaker coercion variant which is e.g. used for builtins that expect string arguments. `EvalState::coerceToPath` is still missing for builtins that need a path, but it'll be easy to build on top of this. Change-Id: I78d15576b18921791d04b6b1e964b951fdef22c6 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6571 Autosubmit: sterni Reviewed-by: tazjin Tested-by: BuildkiteCI --- .../tvix_tests/eval-okay-builtins-toString.exp | 2 +- .../tvix_tests/eval-okay-builtins-toString.nix | 29 +++++++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) (limited to 'tvix/eval/src/tests/tvix_tests') diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-toString.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-toString.exp index 0dda9a682704..a148ebc3b53f 100644 --- a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-toString.exp +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-toString.exp @@ -1 +1 @@ -[ "1" ] +[ "1" "4.200000" "" "" "1" "foo" "/etc" "Hello World" "Hello World" "1" "out" "2" "1 4.200000 1 foo /etc Hello World Hello World 1 out 2" ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-toString.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-toString.nix index 05b2917a0fb3..e4dc18ac96a7 100644 --- a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-toString.nix +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-toString.nix @@ -1,6 +1,23 @@ -# TODO: add some examples for the "weird" types -[ - (toString 1) - # TODO: floats must be padded to 6 digits - # (toString 4.2) -] +let + toStringableSet = { + __toString = self: self.content; + content = "Hello World"; + }; + + toStringExamples = [ + (toString 1) + (toString 4.2) + (toString null) + (toString false) + (toString true) + (toString "foo") + (toString /etc) + (toString toStringableSet) + (toString { __toString = _: toStringableSet; }) + (toString { __toString = _: true; }) + (toString { outPath = "out"; }) + (toString { outPath = { outPath = { __toString = _: 2; }; }; }) + ]; +in + +toStringExamples ++ [ (toString toStringExamples) ] -- cgit 1.4.1