From 9144257524c2be7e21fa7d99103fd429d643fcd9 Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 12 Sep 2024 14:22:07 -0400 Subject: [PATCH] Replace VM's "clone" method with "take" to avoid cloning values --- dust-lang/src/vm.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/dust-lang/src/vm.rs b/dust-lang/src/vm.rs index 327301c..18e04cc 100644 --- a/dust-lang/src/vm.rs +++ b/dust-lang/src/vm.rs @@ -50,7 +50,7 @@ impl Vm { Operation::DeclareLocal => { let register_index = instruction.destination as usize; let local_index = u16::from_le_bytes(instruction.arguments) as usize; - let value = self.clone(register_index, position)?; + let value = self.take(register_index, position)?; self.chunk.define_local(local_index, value, position)?; } @@ -148,14 +148,10 @@ impl Vm { } } - fn clone(&mut self, index: usize, position: Span) -> Result { + fn take(&mut self, index: usize, position: Span) -> Result { if let Some(register) = self.register_stack.get_mut(index) { - if let Some(mut value) = register.take() { - if value.is_raw() { - value = value.into_reference(); - } - - Ok(value.clone()) + if let Some(value) = register.take() { + Ok(value) } else { Err(VmError::EmptyRegister { index, position }) } @@ -165,7 +161,7 @@ impl Vm { } fn take_or_use_constant(&mut self, index: usize, position: Span) -> Result { - if let Ok(value) = self.clone(index, position) { + if let Ok(value) = self.take(index, position) { Ok(value) } else { let value = self.chunk.use_constant(index, position)?;