about summary refs log tree commit diff
path: root/tvix/eval/src/builtins
diff options
context:
space:
mode:
authorAdam Joseph <adam@westernsemico.com>2023-12-11T11·57-0800
committerclbot <clbot@tvl.fyi>2023-12-12T14·54+0000
commit1ac57b0d1cdade99cd6cdf34211aa23e63db6f97 (patch)
treed4e50c627f6d06ec0889f42da2402a33ae89ccd2 /tvix/eval/src/builtins
parent370137974526ef9af1f0d3496d17e4232e3babfe (diff)
feat(tvix/eval): nonrecursive coerce_to_string() r/7176
After this commit, the only non-builtins uses of generators are:

  - coerce_to_string() uses generators::request_enter_lambda()
  - Thunk::force() uses generators::request_enter_lambda()

That's it!  Once those two are taken care of, GenCo can become an
implementation detail of `builtins::BuiltinGen`.  No more crazy
nonlocal flow control within the interpreter: if you've got a GenCo
floating around in your code it's because you're writing a builtin,
which isn't part of the core interpreter.  The interpreter won't
need GenCos to talk to itself anymore.

Technically generators::request_path_import() is also used by
coerce_to_string(), but that's just because the io_handle happens to
be part of the VM.  There's no recursion-depth issue there, so the
call doesn't need to go through the generator mechanism
(request_path_import() doesn't call back to the interpreter!)

Change-Id: I83ce5774d49b88fdafdd61160975b4937a435bb0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10256
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Autosubmit: Adam Joseph <adam@westernsemico.com>
Diffstat (limited to 'tvix/eval/src/builtins')
-rw-r--r--tvix/eval/src/builtins/mod.rs27
1 files changed, 21 insertions, 6 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs
index 5f99704e63c3..62684b82453e 100644
--- a/tvix/eval/src/builtins/mod.rs
+++ b/tvix/eval/src/builtins/mod.rs
@@ -133,7 +133,11 @@ mod pure_builtins {
 
     #[builtin("baseNameOf")]
     async fn builtin_base_name_of(co: GenCo, s: Value) -> Result<Value, ErrorKind> {
-        let s = s.coerce_to_string(co, CoercionKind::Weak).await?.to_str()?;
+        let span = generators::request_span(&co).await;
+        let s = s
+            .coerce_to_string(co, CoercionKind::Weak, span)
+            .await?
+            .to_str()?;
         let result: String = s.rsplit_once('/').map(|(_, x)| x).unwrap_or(&s).into();
         Ok(result.into())
     }
@@ -248,7 +252,11 @@ mod pure_builtins {
     #[builtin("dirOf")]
     async fn builtin_dir_of(co: GenCo, s: Value) -> Result<Value, ErrorKind> {
         let is_path = s.is_path();
-        let str = s.coerce_to_string(co, CoercionKind::Weak).await?.to_str()?;
+        let span = generators::request_span(&co).await;
+        let str = s
+            .coerce_to_string(co, CoercionKind::Weak, span)
+            .await?
+            .to_str()?;
         let result = str
             .rsplit_once('/')
             .map(|(x, _)| match x {
@@ -915,7 +923,8 @@ mod pure_builtins {
     #[builtin("stringLength")]
     async fn builtin_string_length(co: GenCo, #[lazy] s: Value) -> Result<Value, ErrorKind> {
         // also forces the value
-        let s = s.coerce_to_string(co, CoercionKind::Weak).await?;
+        let span = generators::request_span(&co).await;
+        let s = s.coerce_to_string(co, CoercionKind::Weak, span).await?;
         Ok(Value::Integer(s.to_str()?.as_str().len() as i64))
     }
 
@@ -933,7 +942,11 @@ mod pure_builtins {
     ) -> Result<Value, ErrorKind> {
         let beg = start.as_int()?;
         let len = len.as_int()?;
-        let x = s.coerce_to_string(co, CoercionKind::Weak).await?.to_str()?;
+        let span = generators::request_span(&co).await;
+        let x = s
+            .coerce_to_string(co, CoercionKind::Weak, span)
+            .await?
+            .to_str()?;
 
         if beg < 0 {
             return Err(ErrorKind::IndexOutOfBounds { index: beg });
@@ -978,7 +991,8 @@ mod pure_builtins {
     #[builtin("toString")]
     async fn builtin_to_string(co: GenCo, #[lazy] x: Value) -> Result<Value, ErrorKind> {
         // coerce_to_string forces for us
-        x.coerce_to_string(co, CoercionKind::Strong).await
+        let span = generators::request_span(&co).await;
+        x.coerce_to_string(co, CoercionKind::Strong, span).await
     }
 
     #[builtin("toXML")]
@@ -1010,7 +1024,8 @@ mod pure_builtins {
             Err(cek) => Ok(Value::Catchable(cek)),
             Ok(path) => {
                 let path: Value = crate::value::canon_path(path).into();
-                Ok(path.coerce_to_string(co, CoercionKind::Weak).await?)
+                let span = generators::request_span(&co).await;
+                Ok(path.coerce_to_string(co, CoercionKind::Weak, span).await?)
             }
         }
     }