docs: specify await ownership and add parked operand regression

This commit is contained in:
2026-10-23 20:14:15 +08:00
parent 8a2135897e
commit ae75c54707
10 changed files with 116 additions and 0 deletions
+90
View File
@@ -1622,3 +1622,93 @@ fn managed_coroutine_hard_budget_accumulates_and_error_releases_frames() {
}
assert_eq!(vm.object_count(), 0);
}
#[derive(Debug)]
struct AwaitGate {
ready: Arc<std::sync::atomic::AtomicBool>,
value: Value,
}
impl ObjectPayload for AwaitGate {
fn as_any(&self) -> &dyn Any {
self
}
fn kind_name(&self) -> &'static str {
"await-gate"
}
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
if let Value::Object(reference) = self.value {
visit(reference);
}
}
fn poll_await(&self) -> Result<endor_core::AwaitPoll, VmError> {
Ok(if self.ready.load(Ordering::Acquire) {
endor_core::AwaitPoll::Ready(self.value.clone())
} else {
endor_core::AwaitPoll::Pending
})
}
}
#[test]
fn await_keeps_operand_and_owned_object_result_without_replaying_prefix() {
use endor_core::CoroutineProgress;
let mut vm = Vm::new().unwrap();
let ready = Arc::new(std::sync::atomic::AtomicBool::new(false));
let value = vm.new_object().unwrap();
vm.set_attr(value, "marker", text("kept")).unwrap();
let gate = vm
.allocate(AwaitGate {
ready: ready.clone(),
value: Value::Object(value),
})
.unwrap();
let globals = vm.new_dict().unwrap();
let code = make_code(
&mut vm,
&[
op(Opcode::LoadConst, 0),
op(Opcode::StoreGlobal, 0),
op(Opcode::LoadConst, 1),
simple(Opcode::Await),
simple(Opcode::ReturnValue),
],
vec![text("prefix"), Value::Object(gate)],
CodeLayout {
names: vec!["step".into()],
..CodeLayout::default()
},
);
let function = vm
.new_function("awaited", code, globals, vec![], vec![])
.unwrap();
let coroutine = vm
.new_coroutine(function, &[], ExecutionLimits::default())
.unwrap();
assert!(
matches!(vm.resume_coroutine(coroutine, 64).unwrap(), CoroutineProgress::Waiting { awaitable, .. } if awaitable == gate)
);
vm.release(value).unwrap();
vm.release(gate).unwrap();
vm.dict_set(globals, text("step"), text("interleaved"))
.unwrap();
vm.collect_cycles();
ready.store(true, Ordering::Release);
let CoroutineProgress::Complete { value: result, .. } =
vm.resume_coroutine(coroutine, 64).unwrap()
else {
panic!("ready await must complete")
};
assert_eq!(
vm.dict_get(globals, &text("step")).unwrap(),
Some(text("interleaved"))
);
for object in [coroutine, function, code, globals] {
vm.release(object).unwrap();
}
let Value::Object(reference) = result else {
panic!("owned object result")
};
assert_eq!(vm.get_attr(reference, "marker").unwrap(), text("kept"));
vm.release_value(result).unwrap();
assert_eq!(vm.object_count(), 0);
}