feat(tvix/vm): add opcodes for new Value::NotFound sentinel

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>
This commit is contained in:
Vincent Ambo 2022-08-11 22:01:45 +03:00 committed by tazjin
parent 999b9c7a13
commit b8cec6d61e
3 changed files with 24 additions and 3 deletions

View file

@ -41,12 +41,14 @@ pub enum OpCode {
OpJump(usize),
OpJumpIfTrue(usize),
OpJumpIfFalse(usize),
OpJumpIfNotFound(usize),
// Attribute sets
OpAttrs(usize),
OpAttrPath(usize),
OpAttrsUpdate,
OpAttrsSelect,
OpAttrOrNotFound,
OpAttrsIsSet,
// Lists

View file

@ -26,6 +26,7 @@ pub enum Value {
// are never returned to or created directly by users.
AttrPath(Vec<NixString>),
Blackhole,
NotFound,
}
impl Value {
@ -48,7 +49,7 @@ impl Value {
Value::List(_) => "list",
// Internal types
Value::AttrPath(_) | Value::Blackhole => "internal",
Value::AttrPath(_) | Value::Blackhole | Value::NotFound => "internal",
}
}
@ -116,8 +117,9 @@ impl Display for Value {
)),
// internal types
Value::AttrPath(_) => f.write_str("internal<attrpath>"),
Value::Blackhole => f.write_str("internal<blackhole>"),
Value::AttrPath(_) => f.write_str("internal[attrpath]"),
Value::Blackhole => f.write_str("internal[blackhole]"),
Value::NotFound => f.write_str("internal[not found]"),
}
}
}

View file

@ -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