refactor(tvix/eval): add VM::call_value helper method

This makes it possible to call a callable value (builtin or
closure/lambda) directly, without unwrapping it first. This is needed
for pretty much all higher-order functions to work correctly.

This is mostly equivalent to the previous code in coerce_to_string for
calling `__toString`, except it expects the argument(s) to already be
placed on the stack.

Note that the span for the `NotCallable` error is not currently
guaranteed to make any sense, will experiment with this.

Change-Id: I821224368d438a28900858b343defc1817e46a0a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6717
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
Vincent Ambo 2022-09-21 00:32:49 +03:00 committed by tazjin
parent f600aa5322
commit 8f2004d360
5 changed files with 32 additions and 34 deletions

View file

@ -16,7 +16,6 @@ mod thunk;
use crate::errors::ErrorKind;
use crate::opcode::StackIdx;
use crate::upvalues::UpvalueCarrier;
use crate::vm::VM;
pub use attrs::NixAttrs;
pub use builtin::Builtin;
@ -155,22 +154,9 @@ impl Value {
(Some(f), _) => {
// use a closure here to deal with the thunk borrow we need to do below
let call_to_string = |value: &Value, vm: &mut VM| {
// TODO(sterni): calling logic should be extracted into a helper
let result = match value {
Value::Closure(c) => {
vm.push(self.clone());
vm.call(c.lambda(), c.upvalues().clone(), 1)
.map_err(|e| e.kind)
}
Value::Builtin(b) => {
vm.push(self.clone());
vm.call_builtin(b.clone()).map_err(|e| e.kind)?;
Ok(vm.pop())
}
_ => Err(ErrorKind::NotCallable),
}?;
// Leave self on the stack as an argument to the function call.
vm.push(self.clone());
let result = vm.call_value(value)?;
match result {
Value::String(s) => Ok(s),