feat: manage independently resumable call frames in the object graph
This commit is contained in:
@@ -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),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user