diff --git a/crates/uplc/src/machine.rs b/crates/uplc/src/machine.rs index e9ed033a..aa54f915 100644 --- a/crates/uplc/src/machine.rs +++ b/crates/uplc/src/machine.rs @@ -1,9 +1,6 @@ use std::rc::Rc; -use crate::{ - ast::{Constant, NamedDeBruijn, Term, Type}, - builtins::DefaultFunction, -}; +use crate::ast::{Constant, NamedDeBruijn, Term, Type}; pub mod cost_model; mod discharge; @@ -147,11 +144,7 @@ impl Machine { Ok(MachineState::Return( context, - Value::Builtin { - fun, - term: term.into(), - runtime, - }, + Value::Builtin { fun, runtime }, )) } } @@ -183,21 +176,21 @@ impl Machine { Value::Delay(body, env) => { Ok(MachineState::Compute(context, env, body.as_ref().clone())) } - Value::Builtin { - fun, - term, - mut runtime, - } => { - let force_term = Term::Force(term); - + Value::Builtin { fun, mut runtime } => { if runtime.needs_force() { runtime.consume_force(); - let res = self.eval_builtin_app(fun, force_term.into(), runtime)?; + let res = if runtime.is_ready() { + self.eval_builtin_app(runtime)? + } else { + Value::Builtin { fun, runtime } + }; Ok(MachineState::Return(context, res)) } else { - Err(Error::BuiltinTermArgumentExpected(force_term)) + Err(Error::BuiltinTermArgumentExpected(Term::Constant( + Constant::Unit.into(), + ))) } } rest => Err(Error::NonPolymorphicInstantiation(rest)), @@ -222,47 +215,40 @@ impl Machine { body.as_ref().clone(), )) } - Value::Builtin { fun, term, runtime } => { - let arg_term = discharge::value_as_term(argument.clone()); - - let t = Rc::new(Term::::Apply { - function: term, - argument: arg_term, - }); + Value::Builtin { fun, runtime } => { + // let arg_term = discharge::value_as_term(argument.clone()); if runtime.is_arrow() && !runtime.needs_force() { let mut runtime = runtime; runtime.push(argument)?; - let res = self.eval_builtin_app(fun, t, runtime.to_owned())?; + let res = if runtime.is_ready() { + self.eval_builtin_app(runtime)? + } else { + Value::Builtin { fun, runtime } + }; Ok(MachineState::Return(context, res)) } else { - Err(Error::UnexpectedBuiltinTermArgument(t.as_ref().clone())) + Err(Error::UnexpectedBuiltinTermArgument(Term::Constant( + Constant::Unit.into(), + ))) } } rest => Err(Error::NonFunctionalApplication(rest, argument)), } } - fn eval_builtin_app( - &mut self, - fun: DefaultFunction, - term: Rc>, - runtime: BuiltinRuntime, - ) -> Result { - if runtime.is_ready() { - let cost = match self.version { - Language::PlutusV1 => runtime.to_ex_budget_v1(&self.costs.builtin_costs), - Language::PlutusV2 => runtime.to_ex_budget_v2(&self.costs.builtin_costs), - }; - self.spend_budget(cost)?; + fn eval_builtin_app(&mut self, runtime: BuiltinRuntime) -> Result { + let cost = match self.version { + Language::PlutusV1 => runtime.to_ex_budget_v1(&self.costs.builtin_costs), + Language::PlutusV2 => runtime.to_ex_budget_v2(&self.costs.builtin_costs), + }; - runtime.call(&mut self.logs) - } else { - Ok(Value::Builtin { fun, term, runtime }) - } + self.spend_budget(cost)?; + + runtime.call(&mut self.logs) } fn lookup_var(&mut self, name: &NamedDeBruijn, env: &[Value]) -> Result { diff --git a/crates/uplc/src/machine/discharge.rs b/crates/uplc/src/machine/discharge.rs index 080bbe49..335de7a6 100644 --- a/crates/uplc/src/machine/discharge.rs +++ b/crates/uplc/src/machine/discharge.rs @@ -1,6 +1,6 @@ use std::rc::Rc; -use crate::ast::{NamedDeBruijn, Term}; +use crate::ast::{Constant, NamedDeBruijn, Term}; use super::value::{Env, Value}; @@ -28,7 +28,9 @@ pub(super) fn value_as_term(value: Value) -> Rc> { match stack_frame { DischargeStep::DischargeValue(value) => match value { Value::Con(x) => arg_stack.push(Term::Constant(x.clone()).into()), - Value::Builtin { term, .. } => arg_stack.push(term.clone()), + Value::Builtin { .. } => { + arg_stack.push(Term::Constant(Constant::Unit.into()).into()) + } Value::Delay(body, env) => { stack.push(DischargeStep::DischargeValueEnv( 0, diff --git a/crates/uplc/src/machine/value.rs b/crates/uplc/src/machine/value.rs index 741914c3..45a7d363 100644 --- a/crates/uplc/src/machine/value.rs +++ b/crates/uplc/src/machine/value.rs @@ -24,7 +24,6 @@ pub enum Value { }, Builtin { fun: DefaultFunction, - term: Rc>, runtime: BuiltinRuntime, }, }