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-08-11T19·01+0300
committertazjin <tazjin@tvl.su>2022-08-26T15·06+0000
commitb8cec6d61e8cf30e12924dbbc6b24fecea310a17 (patch)
treee4ae7312632f51b852390070a29b838cb3ef6c03 /tvix/eval/src/vm.rs
parent999b9c7a138a5dd5277085570042fc67a893e5af (diff)
feat(tvix/vm): add opcodes for new Value::NotFound sentinel r/4499
This sentinel value is going to be used for certain nested accesses
into attribute sets.

There is a new instruction similar to `OpAttrsSelect` which leaves the
sentinel on the stack if a key is not found, instead of raising an
error.

Additionally, a new jump instruction makes its jump operation
conditional on finding such a sentinel value.

Change-Id: I2642f0a0bcc85bbe0ead68ea09a7dd794dbedeac
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6166
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to '')
-rw-r--r--tvix/eval/src/vm.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs
index 332398d7b9..232e27aabb 100644
--- a/tvix/eval/src/vm.rs
+++ b/tvix/eval/src/vm.rs
@@ -173,6 +173,16 @@ impl VM {
                     }
                 }
 
+                OpCode::OpAttrOrNotFound => {
+                    let key = self.pop().as_string()?;
+                    let attrs = self.pop().as_attrs()?;
+
+                    match attrs.select(key.as_str()) {
+                        Some(value) => self.push(value.clone()),
+                        None => self.push(Value::NotFound),
+                    }
+                }
+
                 OpCode::OpAttrsIsSet => {
                     let key = self.pop().as_string()?;
                     let attrs = self.pop().as_attrs()?;
@@ -210,6 +220,13 @@ impl VM {
                     }
                 }
 
+                OpCode::OpJumpIfNotFound(offset) => {
+                    if matches!(self.peek(0), Value::NotFound) {
+                        self.pop();
+                        self.ip += offset;
+                    }
+                }
+
                 // These assertion operations error out if the stack
                 // top is not of the expected type. This is necessary
                 // to implement some specific behaviours of Nix