1
0

Replace VM's "clone" method with "take" to avoid cloning values

This commit is contained in:
Jeff 2024-09-12 14:22:07 -04:00
parent caf59894b6
commit 9144257524

View File

@ -50,7 +50,7 @@ impl Vm {
Operation::DeclareLocal => { Operation::DeclareLocal => {
let register_index = instruction.destination as usize; let register_index = instruction.destination as usize;
let local_index = u16::from_le_bytes(instruction.arguments) 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)?; self.chunk.define_local(local_index, value, position)?;
} }
@ -148,14 +148,10 @@ impl Vm {
} }
} }
fn clone(&mut self, index: usize, position: Span) -> Result<Value, VmError> { fn take(&mut self, index: usize, position: Span) -> Result<Value, VmError> {
if let Some(register) = self.register_stack.get_mut(index) { if let Some(register) = self.register_stack.get_mut(index) {
if let Some(mut value) = register.take() { if let Some(value) = register.take() {
if value.is_raw() { Ok(value)
value = value.into_reference();
}
Ok(value.clone())
} else { } else {
Err(VmError::EmptyRegister { index, position }) Err(VmError::EmptyRegister { index, position })
} }
@ -165,7 +161,7 @@ impl Vm {
} }
fn take_or_use_constant(&mut self, index: usize, position: Span) -> Result<Value, VmError> { fn take_or_use_constant(&mut self, index: usize, position: Span) -> Result<Value, VmError> {
if let Ok(value) = self.clone(index, position) { if let Ok(value) = self.take(index, position) {
Ok(value) Ok(value)
} else { } else {
let value = self.chunk.use_constant(index, position)?; let value = self.chunk.use_constant(index, position)?;