feat: support opt-in shared reference assignment for host objects
This commit is contained in:
@@ -35,6 +35,130 @@ fn return_constant(vm: &mut Vm, value: Value) -> ObjectRef {
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct SharedReference(AtomicUsize);
|
||||
impl ObjectPayload for SharedReference {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"shared-reference"
|
||||
}
|
||||
fn assign_from(&self, source: &dyn ObjectPayload) -> Result<bool, VmError> {
|
||||
let Some(source) = source.as_any().downcast_ref::<Self>() else {
|
||||
return Ok(false);
|
||||
};
|
||||
let value = source.0.load(Ordering::SeqCst);
|
||||
if value == 99 {
|
||||
return Err(VmError::InvalidArgument("rejected replacement"));
|
||||
}
|
||||
self.0.store(value, Ordering::SeqCst);
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opt_in_assignment_keeps_alias_identity_ordinary_values_and_root_accounting() {
|
||||
for local in [false, true] {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let old = vm.allocate(SharedReference(AtomicUsize::new(1))).unwrap();
|
||||
let new = vm.allocate(SharedReference(AtomicUsize::new(2))).unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
vm.dict_set(globals, text("alias"), Value::Object(old))
|
||||
.unwrap();
|
||||
let (store, load) = if local {
|
||||
(Opcode::StoreFast, Opcode::LoadFast)
|
||||
} else {
|
||||
(Opcode::StoreGlobal, Opcode::LoadGlobal)
|
||||
};
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(store, 0),
|
||||
op(Opcode::LoadConst, 1),
|
||||
op(store, 0),
|
||||
op(load, 0),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Object(old), Value::Object(new)],
|
||||
if local {
|
||||
CodeLayout {
|
||||
locals: vec!["value".into()],
|
||||
..CodeLayout::default()
|
||||
}
|
||||
} else {
|
||||
CodeLayout {
|
||||
names: vec!["value".into()],
|
||||
..CodeLayout::default()
|
||||
}
|
||||
},
|
||||
);
|
||||
let value = vm.execute(code, globals).unwrap();
|
||||
assert_eq!(value, Value::Object(old));
|
||||
assert_eq!(
|
||||
vm.dict_get(globals, &text("alias")).unwrap(),
|
||||
Some(Value::Object(old))
|
||||
);
|
||||
assert_eq!(
|
||||
vm.payload::<SharedReference>(old, "shared-reference")
|
||||
.unwrap()
|
||||
.0
|
||||
.load(Ordering::SeqCst),
|
||||
2
|
||||
);
|
||||
vm.release_value(value).unwrap();
|
||||
vm.release(code).unwrap();
|
||||
vm.dict_clear(globals).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
vm.release(old).unwrap();
|
||||
vm.release(new).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_opt_in_assignment_preserves_target_and_cleans_execution_roots() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let old = vm.allocate(SharedReference(AtomicUsize::new(1))).unwrap();
|
||||
let rejected = vm.allocate(SharedReference(AtomicUsize::new(99))).unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
vm.dict_set(globals, text("value"), Value::Object(old))
|
||||
.unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::StoreGlobal, 0),
|
||||
op(Opcode::LoadConst, 1),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Object(rejected), Value::Nil],
|
||||
CodeLayout {
|
||||
names: vec!["value".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
assert!(vm.execute(code, globals).is_err());
|
||||
assert_eq!(
|
||||
vm.payload::<SharedReference>(old, "shared-reference")
|
||||
.unwrap()
|
||||
.0
|
||||
.load(Ordering::SeqCst),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
vm.dict_get(globals, &text("value")).unwrap(),
|
||||
Some(Value::Object(old))
|
||||
);
|
||||
vm.release(code).unwrap();
|
||||
vm.dict_clear(globals).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
vm.release(old).unwrap();
|
||||
vm.release(rejected).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wordcode_extended_operands_decode_and_execute_without_truncation() {
|
||||
let encoded = Instruction::encode_all(&[op(Opcode::LoadConst, 0x01020304)]).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user