This commit is contained in:
2026-03-24 13:40:42 +00:00
parent 49c9b018bc
commit f6d33cd8e5
3 changed files with 251 additions and 51 deletions
+19 -1
View File
@@ -35,6 +35,7 @@ pub enum CoroutineState {
/// Stored payload for coroutine objects.
#[derive(Debug, Clone)]
pub struct CoroutineData {
pub pc: usize,
pub state: CoroutineState,
pub wake_tick: u64,
pub stack: Vec<Value>,
@@ -100,6 +101,7 @@ impl Heap {
/// `payload_len` is 0; stack and frames are stored out-of-line for GC visibility.
pub fn allocate_coroutine(
&mut self,
pc: usize,
state: CoroutineState,
wake_tick: u64,
stack: Vec<Value>,
@@ -111,7 +113,7 @@ impl Heap {
payload: Vec::new(),
array_elems: None,
closure_env: None,
coroutine: Some(CoroutineData { state, wake_tick, stack, frames }),
coroutine: Some(CoroutineData { pc, state, wake_tick, stack, frames }),
};
let idx = self.objects.len();
self.objects.push(Some(obj));
@@ -125,6 +127,21 @@ impl Heap {
self.objects[idx].is_some()
}
/// Returns a shared reference to the coroutine data for the given handle, if it is a Coroutine.
pub fn coroutine_data(&self, r: HeapRef) -> Option<&CoroutineData> {
let idx = r.0 as usize;
self.objects.get(idx).and_then(|slot| slot.as_ref()).and_then(|obj| obj.coroutine.as_ref())
}
/// Returns a mutable reference to the coroutine data for the given handle, if it is a Coroutine.
pub fn coroutine_data_mut(&mut self, r: HeapRef) -> Option<&mut CoroutineData> {
let idx = r.0 as usize;
self.objects
.get_mut(idx)
.and_then(|slot| slot.as_mut())
.and_then(|obj| obj.coroutine.as_mut())
}
/// Get immutable access to an object's header by handle.
pub fn header(&self, r: HeapRef) -> Option<&ObjectHeader> {
self.objects
@@ -363,6 +380,7 @@ mod tests {
// Create a coroutine with a small stack containing a HeapRef to verify GC traversal later.
let obj_ref = heap.allocate_object(ObjectKind::Bytes, &[4, 5, 6]);
let coro = heap.allocate_coroutine(
0,
CoroutineState::Ready,
0,
vec![Value::Int32(1), Value::HeapRef(obj_ref)],