feat: support opt-in shared reference assignment for host objects

This commit is contained in:
2026-10-20 20:55:25 +08:00
parent df1b77fc1a
commit 7c842789c0
7 changed files with 169 additions and 7 deletions
+27 -5
View File
@@ -220,6 +220,20 @@ impl Vm {
Self::stack_value(frame, frame.stack.len() - 1)
}
fn assignment_value(&self, previous: Option<Value>, incoming: Value) -> Result<Value, VmError> {
if let (Some(Value::Object(old)), Value::Object(new)) = (&previous, &incoming) {
if old == new
|| self
.object(*old)?
.payload()
.assign_from(self.object(*new)?.payload())?
{
return Ok(Value::Object(*old));
}
}
Ok(incoming)
}
fn push_borrowed(&mut self, frame: &mut EndorFrame, value: Value) -> Result<(), VmError> {
self.retain_value(&value)?;
frame.stack.push(StackEntry::Value(value));
@@ -372,9 +386,12 @@ impl Vm {
self.push_borrowed(frame, value)?;
}
Opcode::StoreFast => {
let value = Self::top_value(frame)?;
// The stack root is moved into the local slot.
frame.stack.pop();
let value = self.assignment_value(
frame.localsplus[argument].clone(),
Self::top_value(frame)?,
)?;
self.retain_value(&value)?;
self.discard(frame, 1);
if let Some(old) = frame.localsplus[argument].replace(value) {
self.drop_root(old);
}
@@ -398,8 +415,11 @@ impl Vm {
self.push_borrowed(frame, value)?;
}
Opcode::StoreGlobal => {
let value = Self::top_value(frame)?;
let key = Value::Text(frame.executable.layout().names[argument].clone());
let value = self.assignment_value(
self.dict_get(frame.globals, &key)?,
Self::top_value(frame)?,
)?;
self.dict_set(frame.globals, key, value)?;
self.discard(frame, 1);
}
@@ -426,7 +446,9 @@ impl Vm {
}
Opcode::StoreDeref => {
let cell = self.cell_slot(frame, argument)?;
self.cell_set(cell, Self::top_value(frame)?)?;
let value =
self.assignment_value(self.cell_get(cell).ok(), Self::top_value(frame)?)?;
self.cell_set(cell, value)?;
self.discard(frame, 1);
}
Opcode::DeleteDeref => {