about summary refs log tree commit diff
path: root/tvix/eval/src/vm.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-09-20T21·32+0300
committertazjin <tazjin@tvl.su>2022-09-20T23·48+0000
commit8f2004d360dde108f90d2b49b0609bd43b7b6d7d (patch)
tree6ee59500b7d5a6f54de607627357d45e8cf285af /tvix/eval/src/vm.rs
parentf600aa5322f6628e1af63e9dd4c6ad073020e152 (diff)
refactor(tvix/eval): add VM::call_value helper method r/4943
This makes it possible to call a callable value (builtin or
closure/lambda) directly, without unwrapping it first. This is needed
for pretty much all higher-order functions to work correctly.

This is mostly equivalent to the previous code in coerce_to_string for
calling `__toString`, except it expects the argument(s) to already be
placed on the stack.

Note that the span for the `NotCallable` error is not currently
guaranteed to make any sense, will experiment with this.

Change-Id: I821224368d438a28900858b343defc1817e46a0a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6717
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'tvix/eval/src/vm.rs')
-rw-r--r--tvix/eval/src/vm.rs35
1 files changed, 23 insertions, 12 deletions
diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs
index 909e219bcd78..69ffc7d5c293 100644
--- a/tvix/eval/src/vm.rs
+++ b/tvix/eval/src/vm.rs
@@ -175,7 +175,28 @@ impl<'o> VM<'o> {
         }
     }
 
-    #[allow(clippy::let_and_return)] // due to disassembler
+    /// Execute the given value in this VM's context, if it is a
+    /// callable.
+    ///
+    /// The stack of the VM must be prepared with all required
+    /// arguments before calling this and the value must have already
+    /// been forced.
+    pub fn call_value(&mut self, callable: &Value) -> EvalResult<Value> {
+        match callable {
+            Value::Closure(c) => self.call(c.lambda(), c.upvalues().clone(), 1),
+
+            Value::Builtin(b) => {
+                self.call_builtin(b.clone())?;
+                Ok(self.pop())
+            }
+
+            Value::Thunk(t) => self.call_value(&t.value()),
+
+            // TODO: this isn't guaranteed to be a useful span, actually
+            _ => Err(self.error(ErrorKind::NotCallable)),
+        }
+    }
+
     /// Execute the given lambda in this VM's context, returning its
     /// value after its stack frame completes.
     pub fn call(
@@ -456,17 +477,7 @@ impl<'o> VM<'o> {
 
                 OpCode::OpCall => {
                     let callable = self.pop();
-                    match callable {
-                        Value::Closure(closure) => {
-                            let result =
-                                self.call(closure.lambda(), closure.upvalues().clone(), 1)?;
-                            self.push(result)
-                        }
-
-                        Value::Builtin(builtin) => self.call_builtin(builtin)?,
-
-                        _ => return Err(self.error(ErrorKind::NotCallable)),
-                    };
+                    self.call_value(&callable)?;
                 }
 
                 OpCode::OpTailCall => {