about summary refs log tree commit diff
path: root/tvix
diff options
context:
space:
mode:
Diffstat (limited to 'tvix')
-rw-r--r--tvix/eval/src/opcode.rs5
-rw-r--r--tvix/eval/src/value/function.rs11
-rw-r--r--tvix/eval/src/vm.rs13
3 files changed, 28 insertions, 1 deletions
diff --git a/tvix/eval/src/opcode.rs b/tvix/eval/src/opcode.rs
index 12a76d368e4f..45a6337229d2 100644
--- a/tvix/eval/src/opcode.rs
+++ b/tvix/eval/src/opcode.rs
@@ -32,7 +32,6 @@ pub struct JumpOffset(pub usize);
 #[derive(Clone, Copy, Debug)]
 pub struct Count(pub usize);
 
-#[allow(clippy::enum_variant_names)]
 #[warn(variant_size_differences)]
 #[derive(Clone, Copy, Debug)]
 pub enum OpCode {
@@ -108,6 +107,10 @@ pub enum OpCode {
     OpGetUpvalue(UpvalueIdx),
     OpClosure(ConstantIdx),
 
+    /// Finalise initialisation of the upvalues of the value in the
+    /// given stack index after the scope is fully bound.
+    OpFinalise(StackIdx),
+
     // The closure and thunk creation instructions have a variable
     // number of arguments to the instruction, which is represented
     // here by making their data part of the opcodes.
diff --git a/tvix/eval/src/value/function.rs b/tvix/eval/src/value/function.rs
index e5db43d58ace..e8301d82da11 100644
--- a/tvix/eval/src/value/function.rs
+++ b/tvix/eval/src/value/function.rs
@@ -60,4 +60,15 @@ impl Closure {
     pub fn push_upvalue(&self, value: Value) {
         self.0.borrow_mut().upvalues.push(value)
     }
+
+    /// Resolve the deferred upvalues in the closure from a slice of
+    /// the current stack, using the indices stored in the deferred
+    /// values.
+    pub fn resolve_deferred_upvalues(&self, stack: &[Value]) {
+        for upvalue in self.0.borrow_mut().upvalues.iter_mut() {
+            if let Value::DeferredUpvalue(idx) = upvalue {
+                *upvalue = stack[idx.0].clone();
+            }
+        }
+    }
 }
diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs
index d4eb0657251a..1aaba9b0573e 100644
--- a/tvix/eval/src/vm.rs
+++ b/tvix/eval/src/vm.rs
@@ -435,6 +435,19 @@ impl VM {
                     }
                 }
 
+                OpCode::OpFinalise(StackIdx(idx)) => {
+                    match &self.stack[self.frame().stack_offset + idx] {
+                        Value::Closure(closure) => closure
+                            .resolve_deferred_upvalues(&self.stack[self.frame().stack_offset..]),
+
+                        v => {
+                            #[cfg(feature = "disassembler")]
+                            drop(tracer);
+                            panic!("compiler error: invalid finaliser value: {}", v);
+                        }
+                    }
+                }
+
                 // Data-carrying operands should never be executed,
                 // that is a critical error in the VM.
                 OpCode::DataLocalIdx(_)