pr6.4
This commit is contained in:
@@ -407,4 +407,70 @@ mod tests {
|
||||
assert!(!heap.is_valid(b));
|
||||
assert_eq!(heap.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gc_scans_closure_env_and_keeps_captured_heap_object() {
|
||||
let mut heap = Heap::new();
|
||||
|
||||
// Captured heap object.
|
||||
let obj = heap.allocate_object(ObjectKind::Bytes, &[4, 5, 6]);
|
||||
|
||||
// Closure capturing the heap object among other primitive values.
|
||||
let env = [Value::Boolean(true), Value::HeapRef(obj), Value::Int32(123)];
|
||||
let clo = heap.alloc_closure(1, &env);
|
||||
|
||||
// Mark from closure root: both closure and captured object must be marked.
|
||||
heap.mark_from_roots([clo]);
|
||||
|
||||
assert!(heap.header(clo).unwrap().is_marked());
|
||||
assert!(heap.header(obj).unwrap().is_marked());
|
||||
|
||||
// Sweep should keep both and clear their marks.
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(clo));
|
||||
assert!(heap.is_valid(obj));
|
||||
assert!(!heap.header(clo).unwrap().is_marked());
|
||||
assert!(!heap.header(obj).unwrap().is_marked());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gc_scans_nested_closures_and_keeps_inner_when_outer_is_rooted() {
|
||||
let mut heap = Heap::new();
|
||||
|
||||
// Inner closure (no env).
|
||||
let inner = heap.alloc_closure(2, &[]);
|
||||
|
||||
// Outer closure captures the inner closure as a Value::HeapRef.
|
||||
let outer = heap.alloc_closure(3, &[Value::HeapRef(inner)]);
|
||||
|
||||
// Root only the outer closure.
|
||||
heap.mark_from_roots([outer]);
|
||||
|
||||
// Both must be marked reachable.
|
||||
assert!(heap.header(outer).unwrap().is_marked());
|
||||
assert!(heap.header(inner).unwrap().is_marked());
|
||||
|
||||
// After sweep, both survive and have marks cleared.
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(outer));
|
||||
assert!(heap.is_valid(inner));
|
||||
assert!(!heap.header(outer).unwrap().is_marked());
|
||||
assert!(!heap.header(inner).unwrap().is_marked());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gc_collects_unreferenced_closure_and_captures() {
|
||||
let mut heap = Heap::new();
|
||||
|
||||
// Captured heap object and a closure capturing it.
|
||||
let captured = heap.allocate_object(ObjectKind::String, b"dead");
|
||||
let clo = heap.alloc_closure(9, &[Value::HeapRef(captured)]);
|
||||
|
||||
// No roots are provided; sweeping should reclaim both.
|
||||
heap.sweep();
|
||||
|
||||
assert!(!heap.is_valid(clo));
|
||||
assert!(!heap.is_valid(captured));
|
||||
assert_eq!(heap.len(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user