From bd9cda2af7d9fd8064b719e9135b3a48cf13c36a Mon Sep 17 00:00:00 2001 From: sterni Date: Wed, 21 Sep 2022 22:30:39 +0200 Subject: feat(tvix/eval): add builtins.stringLength Fairly straightforward, only thing of note is that we coerce (weakly) to string here as well. Change-Id: I03b427e657e402f1f9eb0f795b689bbf5092aba1 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6745 Tested-by: BuildkiteCI Reviewed-by: wpcarro Reviewed-by: tazjin --- tvix/eval/src/builtins/mod.rs | 5 +++++ .../src/tests/tvix_tests/eval-okay-builtins-string-length.exp | 1 + .../src/tests/tvix_tests/eval-okay-builtins-string-length.nix | 10 ++++++++++ 3 files changed, 16 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-string-length.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-string-length.nix (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 1a6155a82352..872033648aff 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -234,6 +234,11 @@ fn pure_builtins() -> Vec { .collect::>(); Ok(Value::List(NixList::construct(parts.len(), parts))) }), + Builtin::new("stringLength", &[false], |args, vm| { + // also forces the value + let s = args[0].coerce_to_string(CoercionKind::Weak, vm)?; + Ok(Value::Integer(s.as_str().len() as i64)) + }), Builtin::new( "sub", &[false, false], diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-string-length.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-string-length.exp new file mode 100644 index 000000000000..b019be4bfd7a --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-string-length.exp @@ -0,0 +1 @@ +[ 3 "hello" 9 4 ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-string-length.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-string-length.nix new file mode 100644 index 000000000000..b7d51db3c578 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-string-length.nix @@ -0,0 +1,10 @@ +[ + (builtins.stringLength "foo") + (let s = "hello"; in (builtins.substring 0 (builtins.stringLength s) s)) + (builtins.stringLength ("foo" + "${"bar" + "baz"}")) + + # feel free to delete this test case at any time, it's just to show: This is a + # thing at the moment. We may want to break compatibility with this aspect of + # the C++ Nix implementation at any time. + (builtins.stringLength "😀") +] -- cgit 1.4.1