From fb81955f51e302a6b59f55b93a02d88040196797 Mon Sep 17 00:00:00 2001 From: Kasey White Date: Mon, 15 Aug 2022 00:51:12 -0400 Subject: [PATCH] start changing machine to use rc instead of box (does compile) --- crates/uplc/src/machine.rs | 46 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/crates/uplc/src/machine.rs b/crates/uplc/src/machine.rs index 852efb76..e21afd99 100644 --- a/crates/uplc/src/machine.rs +++ b/crates/uplc/src/machine.rs @@ -1,3 +1,5 @@ +use std::rc::Rc; + use crate::{ ast::{Constant, NamedDeBruijn, Term, Type}, builtins::DefaultFunction, @@ -13,8 +15,8 @@ pub use error::Error; use self::{cost_model::CostModel, runtime::BuiltinRuntime}; enum MachineStep { - Return(Context, Value), - Compute(Context, Vec, Term), + Return(Rc, Value), + Compute(Rc, Vec, Term), Done(Term), } @@ -58,7 +60,7 @@ impl Machine { self.spend_budget(startup_budget)?; self.stack - .push(Compute(Context::NoFrame, vec![], term.clone())); + .push(Compute(Rc::new(Context::NoFrame), vec![], term.clone())); while let Some(step) = self.stack.pop() { match step { @@ -81,7 +83,7 @@ impl Machine { fn compute( &mut self, - context: Context, + context: Rc, env: Vec, term: Term, ) -> Result<(), Error> { @@ -118,7 +120,7 @@ impl Machine { self.step_and_maybe_spend(StepKind::Apply)?; self.stack.push(MachineStep::Compute( - Context::FrameApplyArg(env.clone(), *argument, Box::new(context)), + Rc::new(Context::FrameApplyArg(env.clone(), *argument, context)), env, *function, )); @@ -132,7 +134,7 @@ impl Machine { self.step_and_maybe_spend(StepKind::Force)?; self.stack.push(MachineStep::Compute( - Context::FrameForce(Box::new(context)), + Rc::new(Context::FrameForce(context)), env, *body, )); @@ -145,11 +147,7 @@ impl Machine { self.stack.push(MachineStep::Return( context, - Value::Builtin { - fun, - term: term.clone(), - runtime, - }, + Value::Builtin { fun, term, runtime }, )); } }; @@ -157,17 +155,19 @@ impl Machine { Ok(()) } - fn return_compute(&mut self, context: Context, value: Value) -> Result<(), Error> { - match context { - Context::FrameApplyFun(function, ctx) => self.apply_evaluate(*ctx, function, value)?, + fn return_compute(&mut self, context: Rc, value: Value) -> Result<(), Error> { + match context.as_ref() { + Context::FrameApplyFun(function, ctx) => { + self.apply_evaluate(ctx.to_owned(), function.to_owned(), value)? + } Context::FrameApplyArg(arg_var_env, arg, ctx) => { self.stack.push(MachineStep::Compute( - Context::FrameApplyFun(value, ctx), - arg_var_env, - arg, + Rc::new(Context::FrameApplyFun(value, ctx.to_owned())), + arg_var_env.to_owned(), + arg.to_owned(), )); } - Context::FrameForce(ctx) => self.force_evaluate(*ctx, value)?, + Context::FrameForce(ctx) => self.force_evaluate(ctx.to_owned(), value)?, Context::NoFrame => { if self.unbudgeted_steps[7] > 0 { self.spend_unbudgeted_steps()?; @@ -246,7 +246,7 @@ impl Machine { rec(0, term, self, &env) } - fn force_evaluate(&mut self, context: Context, value: Value) -> Result<(), Error> { + fn force_evaluate(&mut self, context: Rc, value: Value) -> Result<(), Error> { match value { Value::Delay(body, env) => { self.stack.push(MachineStep::Compute(context, env, body)); @@ -278,7 +278,7 @@ impl Machine { fn apply_evaluate( &mut self, - context: Context, + context: Rc, function: Value, argument: Value, ) -> Result<(), Error> { @@ -386,9 +386,9 @@ impl Machine { #[derive(Clone)] enum Context { - FrameApplyFun(Value, Box), - FrameApplyArg(Vec, Term, Box), - FrameForce(Box), + FrameApplyFun(Value, Rc), + FrameApplyArg(Vec, Term, Rc), + FrameForce(Rc), NoFrame, }