pr9.4
This commit is contained in:
@@ -54,6 +54,7 @@ impl Heap {
|
||||
|
||||
/// Allocate a new object with the given kind and raw payload bytes.
|
||||
/// Returns an opaque `HeapRef` handle.
|
||||
#[cfg(test)]
|
||||
pub fn allocate_object(&mut self, kind: ObjectKind, payload: &[u8]) -> HeapRef {
|
||||
let header = ObjectHeader::new(kind, payload.len() as u32);
|
||||
let obj = StoredObject { header, payload: payload.to_vec(), array_elems: None, closure_env: None, coroutine: None };
|
||||
@@ -65,6 +66,7 @@ impl Heap {
|
||||
|
||||
/// Allocate a new `Array` object with the given `Value` elements.
|
||||
/// `payload_len` stores the element count; raw `payload` bytes are empty.
|
||||
#[cfg(test)]
|
||||
pub fn allocate_array(&mut self, elements: Vec<Value>) -> HeapRef {
|
||||
let header = ObjectHeader::new(ObjectKind::Array, elements.len() as u32);
|
||||
let obj = StoredObject { header, payload: Vec::new(), array_elems: Some(elements), closure_env: None, coroutine: None };
|
||||
@@ -128,6 +130,7 @@ impl Heap {
|
||||
}
|
||||
|
||||
/// Returns a shared reference to the coroutine data for the given handle, if it is a Coroutine.
|
||||
#[cfg(test)]
|
||||
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())
|
||||
@@ -158,8 +161,8 @@ impl Heap {
|
||||
.map(|o| &mut o.header)
|
||||
}
|
||||
|
||||
/// Internal: list inner `HeapRef` children of an object without allocating.
|
||||
/// Note: GC mark no longer uses this helper; kept for potential diagnostics.
|
||||
// Internal: list inner `HeapRef` children of an object without allocating.
|
||||
// Note: GC mark no longer uses this helper; kept for potential diagnostics.
|
||||
// fn children_of(&self, r: HeapRef) -> Box<dyn Iterator<Item = HeapRef> + '_> {
|
||||
// let idx = r.0 as usize;
|
||||
// if let Some(Some(o)) = self.objects.get(idx) {
|
||||
@@ -221,6 +224,7 @@ impl Heap {
|
||||
}
|
||||
|
||||
/// Get the captured environment slice of a closure. Returns None if kind mismatch or invalid ref.
|
||||
#[cfg(test)]
|
||||
pub fn closure_env_slice(&self, r: HeapRef) -> Option<&[Value]> {
|
||||
let idx = r.0 as usize;
|
||||
let slot = self.objects.get(idx)?.as_ref()?;
|
||||
@@ -261,14 +265,14 @@ impl Heap {
|
||||
ObjectKind::Array => {
|
||||
if let Some(elems) = obj.array_elems.as_ref() {
|
||||
for val in elems.iter() {
|
||||
if let Value::HeapRef(child) = val {
|
||||
if self.is_valid(*child) {
|
||||
let marked = self
|
||||
.header(*child)
|
||||
.map(|h: &ObjectHeader| h.is_marked())
|
||||
.unwrap_or(false);
|
||||
if !marked { stack.push(*child); }
|
||||
}
|
||||
if let Value::HeapRef(child) = val
|
||||
&& self.is_valid(*child)
|
||||
{
|
||||
let marked = self
|
||||
.header(*child)
|
||||
.map(|h: &ObjectHeader| h.is_marked())
|
||||
.unwrap_or(false);
|
||||
if !marked { stack.push(*child); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -281,14 +285,14 @@ impl Heap {
|
||||
if let Some(env) = obj.closure_env.as_ref() {
|
||||
debug_assert_eq!(env.len(), env_len, "closure env len must match encoded env_len");
|
||||
for val in env[..env_len].iter() {
|
||||
if let Value::HeapRef(child) = val {
|
||||
if self.is_valid(*child) {
|
||||
let marked = self
|
||||
.header(*child)
|
||||
.map(|h: &ObjectHeader| h.is_marked())
|
||||
.unwrap_or(false);
|
||||
if !marked { stack.push(*child); }
|
||||
}
|
||||
if let Value::HeapRef(child) = val
|
||||
&& self.is_valid(*child)
|
||||
{
|
||||
let marked = self
|
||||
.header(*child)
|
||||
.map(|h: &ObjectHeader| h.is_marked())
|
||||
.unwrap_or(false);
|
||||
if !marked { stack.push(*child); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -296,14 +300,14 @@ impl Heap {
|
||||
ObjectKind::Coroutine => {
|
||||
if let Some(co) = obj.coroutine.as_ref() {
|
||||
for val in co.stack.iter() {
|
||||
if let Value::HeapRef(child) = val {
|
||||
if self.is_valid(*child) {
|
||||
let marked = self
|
||||
.header(*child)
|
||||
.map(|h: &ObjectHeader| h.is_marked())
|
||||
.unwrap_or(false);
|
||||
if !marked { stack.push(*child); }
|
||||
}
|
||||
if let Value::HeapRef(child) = val
|
||||
&& self.is_valid(*child)
|
||||
{
|
||||
let marked = self
|
||||
.header(*child)
|
||||
.map(|h: &ObjectHeader| h.is_marked())
|
||||
.unwrap_or(false);
|
||||
if !marked { stack.push(*child); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -333,7 +337,6 @@ impl Heap {
|
||||
|
||||
/// Current number of allocated (live) objects.
|
||||
pub fn len(&self) -> usize { self.objects.iter().filter(|s| s.is_some()).count() }
|
||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
|
||||
/// Enumerate handles of coroutines that are currently suspended (i.e., not running):
|
||||
/// Ready or Sleeping. These must be treated as GC roots by the runtime so their
|
||||
@@ -341,14 +344,12 @@ impl Heap {
|
||||
pub fn suspended_coroutine_handles(&self) -> Vec<HeapRef> {
|
||||
let mut out = Vec::new();
|
||||
for (idx, slot) in self.objects.iter().enumerate() {
|
||||
if let Some(obj) = slot {
|
||||
if obj.header.kind == ObjectKind::Coroutine {
|
||||
if let Some(co) = &obj.coroutine {
|
||||
if matches!(co.state, CoroutineState::Ready | CoroutineState::Sleeping) {
|
||||
out.push(HeapRef(idx as u32));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(obj) = slot
|
||||
&& obj.header.kind == ObjectKind::Coroutine
|
||||
&& let Some(co) = &obj.coroutine
|
||||
&& matches!(co.state, CoroutineState::Ready | CoroutineState::Sleeping)
|
||||
{
|
||||
out.push(HeapRef(idx as u32));
|
||||
}
|
||||
}
|
||||
out
|
||||
|
||||
Reference in New Issue
Block a user