feat: manage independently resumable call frames in the object graph

This commit is contained in:
2026-10-23 20:01:58 +08:00
parent 1c729b4128
commit e6b27ba7b6
3 changed files with 303 additions and 2 deletions
+71
View File
@@ -113,3 +113,74 @@ pub(crate) struct SuspendedCall {
pub limits: ExecutionLimits,
pub executed: usize,
}
/// Independently parked mutable execution. Unlike read-only slicing, it does
/// not keep a VM-wide heap scope active between quanta.
#[derive(Debug)]
pub enum CoroutineProgress {
Pending { instructions: usize },
Complete { value: Value, instructions: usize },
}
#[derive(Debug)]
pub(crate) enum CoroutineState {
Frames(Arc<Vec<EndorFrame>>),
Ready(Value),
Running,
Finished,
}
#[derive(Debug)]
pub(crate) struct Coroutine {
pub state: CoroutineState,
pub limits: ExecutionLimits,
pub executed: usize,
}
impl EndorFrame {
/// Exactly the references held as execution roots while this frame runs.
/// code/globals/builtins remain reachable through its owning function.
pub(crate) fn trace_roots(&self, visit: &mut dyn FnMut(ObjectRef)) {
visit(self.function);
if let Some(reference) = self.return_type.and_then(|kind| kind.reference()) {
visit(reference);
}
for check in &self.native_returns {
for reference in check.expected.references() {
visit(reference);
}
}
for value in self
.localsplus
.iter()
.flatten()
.chain(self.stack.iter().filter_map(|entry| match entry {
StackEntry::Value(value) => Some(value),
StackEntry::Null => None,
}))
{
if let Value::Object(reference) = value {
visit(*reference);
}
}
}
}
impl crate::ObjectPayload for Coroutine {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn kind_name(&self) -> &'static str {
"coroutine"
}
fn reference_assignment_only(&self) -> bool {
true
}
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
match &self.state {
CoroutineState::Frames(frames) => {
for frame in frames.iter() {
frame.trace_roots(visit);
}
}
CoroutineState::Ready(Value::Object(reference)) => visit(*reference),
_ => {}
}
}
}