feat(tvix/eval): emit instructions for dynamic var resolution

If an unknown variable is encountered and the with stack is not empty,
emit instructions for resolving the variable at runtime.

Change-Id: I752f4bd0025335744e4747364abd1bd34130374e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6223
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
Reviewed-by: grfn <grfn@gws.fyi>
This commit is contained in:
Vincent Ambo 2022-08-15 01:31:02 +03:00 committed by tazjin
parent 59f50dc81a
commit 89f566ef57

View file

@ -352,7 +352,17 @@ impl Compiler {
// features are not yet implemented.
match self.resolve_local(name) {
Some(idx) => self.chunk.push_op(OpCode::OpGetLocal(idx)),
None => return Err(Error::UnknownStaticVariable(node)),
None => {
if self.scope.with_stack.is_empty() {
return Err(Error::UnknownStaticVariable(node));
}
// Variable needs to be dynamically resolved
// at runtime.
let idx = self.chunk.push_constant(Value::String(name.into()));
self.chunk.push_op(OpCode::OpConstant(idx));
self.chunk.push_op(OpCode::OpResolveWith)
}
}
}
};