about summary refs log tree commit diff
path: root/tvix/eval/src/value/builtin.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-09-02T01·38+0300
committertazjin <tazjin@tvl.su>2022-09-08T07·59+0000
commit0d7ad5e6d1992d4f80f0ea08fee636b7e34eec59 (patch)
tree28474ff042e31b55d3c8698bbfa1d43e117c0282 /tvix/eval/src/value/builtin.rs
parent5ee89bcf5ca3f0b0d3b809b01ac04bf38f51d7e4 (diff)
refactor(tvix/eval): pass a VM reference to builtins r/4744
This makes it possible for builtins to force values on their own,
without the VM having to apply a strictness mask to the arguments
first.

Change-Id: Ib49a94e56ca2a8d515c39647381ab55a727766e3
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6411
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/value/builtin.rs')
-rw-r--r--tvix/eval/src/value/builtin.rs18
1 files changed, 14 insertions, 4 deletions
diff --git a/tvix/eval/src/value/builtin.rs b/tvix/eval/src/value/builtin.rs
index d1248b1ec219..7572103ec96e 100644
--- a/tvix/eval/src/value/builtin.rs
+++ b/tvix/eval/src/value/builtin.rs
@@ -3,13 +3,23 @@
 //!
 //! Builtins are directly backed by Rust code operating on Nix values.
 
-use crate::errors::ErrorKind;
+use crate::{errors::ErrorKind, vm::VM};
 
 use super::Value;
 
 use std::fmt::{Debug, Display};
 
-pub type BuiltinFn = fn(arg: Vec<Value>) -> Result<Value, ErrorKind>;
+/// Function pointer type for builtins implemented directly by backing
+/// Rust code.
+///
+/// Builtins declare their arity and are passed a vector with the
+/// right number of arguments. Additionally, as they might have to
+/// force the evaluation of thunks, they are passed a reference to the
+/// current VM which they can use for forcing a value.
+///
+/// Errors returned from a builtin will be annotated with the location
+/// of the call to the builtin.
+pub type BuiltinFn = fn(arg: Vec<Value>, vm: &mut VM) -> Result<Value, ErrorKind>;
 
 /// Represents a single built-in function which directly executes Rust
 /// code that operates on a Nix value.
@@ -50,11 +60,11 @@ impl Builtin {
     /// Apply an additional argument to the builtin, which will either
     /// lead to execution of the function or to returning a partial
     /// builtin.
-    pub fn apply(mut self, arg: Value) -> Result<Value, ErrorKind> {
+    pub fn apply(mut self, vm: &mut VM, arg: Value) -> Result<Value, ErrorKind> {
         self.partials.push(arg);
 
         if self.partials.len() == self.arity {
-            return (self.func)(self.partials);
+            return (self.func)(self.partials, vm);
         }
 
         // Function is not yet ready to be called.