adjustments on capabilities on cartridges
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::object::{ObjectHeader, ObjectKind};
|
||||
use crate::call_frame::CallFrame;
|
||||
use crate::object::{ObjectHeader, ObjectKind};
|
||||
use prometeu_bytecode::{HeapRef, Value};
|
||||
|
||||
/// Internal stored object: header plus opaque payload bytes.
|
||||
@@ -50,14 +50,22 @@ pub struct Heap {
|
||||
}
|
||||
|
||||
impl Heap {
|
||||
pub fn new() -> Self { Self { objects: Vec::new() } }
|
||||
pub fn new() -> Self {
|
||||
Self { objects: Vec::new() }
|
||||
}
|
||||
|
||||
/// 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 };
|
||||
let obj = StoredObject {
|
||||
header,
|
||||
payload: payload.to_vec(),
|
||||
array_elems: None,
|
||||
closure_env: None,
|
||||
coroutine: None,
|
||||
};
|
||||
let idx = self.objects.len();
|
||||
// No free-list reuse in this PR: append and keep indices stable.
|
||||
self.objects.push(Some(obj));
|
||||
@@ -69,7 +77,13 @@ impl Heap {
|
||||
#[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 };
|
||||
let obj = StoredObject {
|
||||
header,
|
||||
payload: Vec::new(),
|
||||
array_elems: Some(elements),
|
||||
closure_env: None,
|
||||
coroutine: None,
|
||||
};
|
||||
let idx = self.objects.len();
|
||||
// No free-list reuse in this PR: append and keep indices stable.
|
||||
self.objects.push(Some(obj));
|
||||
@@ -125,7 +139,9 @@ impl Heap {
|
||||
/// Returns true if this handle refers to an allocated object.
|
||||
pub fn is_valid(&self, r: HeapRef) -> bool {
|
||||
let idx = r.0 as usize;
|
||||
if idx >= self.objects.len() { return false; }
|
||||
if idx >= self.objects.len() {
|
||||
return false;
|
||||
}
|
||||
self.objects[idx].is_some()
|
||||
}
|
||||
|
||||
@@ -147,18 +163,12 @@ impl Heap {
|
||||
|
||||
/// Get immutable access to an object's header by handle.
|
||||
pub fn header(&self, r: HeapRef) -> Option<&ObjectHeader> {
|
||||
self.objects
|
||||
.get(r.0 as usize)
|
||||
.and_then(|slot| slot.as_ref())
|
||||
.map(|o| &o.header)
|
||||
self.objects.get(r.0 as usize).and_then(|slot| slot.as_ref()).map(|o| &o.header)
|
||||
}
|
||||
|
||||
/// Internal: get mutable access to an object's header by handle.
|
||||
fn header_mut(&mut self, r: HeapRef) -> Option<&mut ObjectHeader> {
|
||||
self.objects
|
||||
.get_mut(r.0 as usize)
|
||||
.and_then(|slot| slot.as_mut())
|
||||
.map(|o| &mut o.header)
|
||||
self.objects.get_mut(r.0 as usize).and_then(|slot| slot.as_mut()).map(|o| &mut o.header)
|
||||
}
|
||||
|
||||
// Internal: list inner `HeapRef` children of an object without allocating.
|
||||
@@ -215,8 +225,12 @@ impl Heap {
|
||||
pub fn closure_fn_id(&self, r: HeapRef) -> Option<u32> {
|
||||
let idx = r.0 as usize;
|
||||
let slot = self.objects.get(idx)?.as_ref()?;
|
||||
if slot.header.kind != ObjectKind::Closure { return None; }
|
||||
if slot.payload.len() < 8 { return None; }
|
||||
if slot.header.kind != ObjectKind::Closure {
|
||||
return None;
|
||||
}
|
||||
if slot.payload.len() < 8 {
|
||||
return None;
|
||||
}
|
||||
debug_assert_eq!(slot.header.payload_len, 8);
|
||||
let mut bytes = [0u8; 4];
|
||||
bytes.copy_from_slice(&slot.payload[0..4]);
|
||||
@@ -228,7 +242,9 @@ impl Heap {
|
||||
pub fn closure_env_slice(&self, r: HeapRef) -> Option<&[Value]> {
|
||||
let idx = r.0 as usize;
|
||||
let slot = self.objects.get(idx)?.as_ref()?;
|
||||
if slot.header.kind != ObjectKind::Closure { return None; }
|
||||
if slot.header.kind != ObjectKind::Closure {
|
||||
return None;
|
||||
}
|
||||
if slot.payload.len() >= 8 {
|
||||
let mut nbytes = [0u8; 4];
|
||||
nbytes.copy_from_slice(&slot.payload[4..8]);
|
||||
@@ -246,17 +262,21 @@ impl Heap {
|
||||
let mut stack: Vec<HeapRef> = roots.into_iter().collect();
|
||||
|
||||
while let Some(r) = stack.pop() {
|
||||
if !self.is_valid(r) { continue; }
|
||||
if !self.is_valid(r) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If already marked, skip.
|
||||
let already_marked = self
|
||||
.header(r)
|
||||
.map(|h: &ObjectHeader| h.is_marked())
|
||||
.unwrap_or(false);
|
||||
if already_marked { continue; }
|
||||
let already_marked =
|
||||
self.header(r).map(|h: &ObjectHeader| h.is_marked()).unwrap_or(false);
|
||||
if already_marked {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set mark bit.
|
||||
if let Some(h) = self.header_mut(r) { h.set_marked(true); }
|
||||
if let Some(h) = self.header_mut(r) {
|
||||
h.set_marked(true);
|
||||
}
|
||||
|
||||
// Push children by scanning payload directly (no intermediate Vec allocs).
|
||||
let idx = r.0 as usize;
|
||||
@@ -272,7 +292,9 @@ impl Heap {
|
||||
.header(*child)
|
||||
.map(|h: &ObjectHeader| h.is_marked())
|
||||
.unwrap_or(false);
|
||||
if !marked { stack.push(*child); }
|
||||
if !marked {
|
||||
stack.push(*child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -283,7 +305,11 @@ impl Heap {
|
||||
nbytes.copy_from_slice(&obj.payload[4..8]);
|
||||
let env_len = u32::from_le_bytes(nbytes) as usize;
|
||||
if let Some(env) = obj.closure_env.as_ref() {
|
||||
debug_assert_eq!(env.len(), env_len, "closure env len must match encoded env_len");
|
||||
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
|
||||
&& self.is_valid(*child)
|
||||
@@ -292,7 +318,9 @@ impl Heap {
|
||||
.header(*child)
|
||||
.map(|h: &ObjectHeader| h.is_marked())
|
||||
.unwrap_or(false);
|
||||
if !marked { stack.push(*child); }
|
||||
if !marked {
|
||||
stack.push(*child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -307,7 +335,9 @@ impl Heap {
|
||||
.header(*child)
|
||||
.map(|h: &ObjectHeader| h.is_marked())
|
||||
.unwrap_or(false);
|
||||
if !marked { stack.push(*child); }
|
||||
if !marked {
|
||||
stack.push(*child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -336,7 +366,9 @@ impl Heap {
|
||||
}
|
||||
|
||||
/// Current number of allocated (live) objects.
|
||||
pub fn len(&self) -> usize { self.objects.iter().filter(|s| s.is_some()).count() }
|
||||
pub fn len(&self) -> usize {
|
||||
self.objects.iter().filter(|s| s.is_some()).count()
|
||||
}
|
||||
|
||||
/// 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
|
||||
@@ -430,11 +462,8 @@ mod tests {
|
||||
// Target object B (unreferenced yet)
|
||||
let b = heap.allocate_object(ObjectKind::Bytes, &[9, 9, 9]);
|
||||
// Array A that contains a reference to B among other primitives
|
||||
let a = heap.allocate_array(vec![
|
||||
Value::Int32(1),
|
||||
Value::HeapRef(b),
|
||||
Value::Boolean(false),
|
||||
]);
|
||||
let a =
|
||||
heap.allocate_array(vec![Value::Int32(1), Value::HeapRef(b), Value::Boolean(false)]);
|
||||
|
||||
// Mark starting from root A
|
||||
heap.mark_from_roots([a]);
|
||||
|
||||
@@ -2,16 +2,16 @@ mod call_frame;
|
||||
mod local_addressing;
|
||||
// Keep the verifier internal in production builds, but expose it for integration tests
|
||||
// so the golden verifier suite can exercise it without widening the public API in releases.
|
||||
mod heap;
|
||||
mod object;
|
||||
mod roots;
|
||||
mod scheduler;
|
||||
#[cfg(not(test))]
|
||||
mod verifier;
|
||||
#[cfg(test)]
|
||||
mod verifier;
|
||||
mod virtual_machine;
|
||||
mod vm_init_error;
|
||||
mod object;
|
||||
mod heap;
|
||||
mod roots;
|
||||
mod scheduler;
|
||||
|
||||
pub use prometeu_hal::{HostContext, HostReturn, NativeInterface, SyscallId};
|
||||
pub use virtual_machine::{BudgetReport, LogicalFrameEndingReason, VirtualMachine};
|
||||
|
||||
@@ -51,7 +51,8 @@
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||
pub enum ObjectKind {
|
||||
/// Reserved/unknown kind. Should not appear in valid allocations.
|
||||
#[allow(dead_code)] // Kept for stable tag layout and persisted images, even if not constructed in this crate yet
|
||||
#[allow(dead_code)]
|
||||
// Kept for stable tag layout and persisted images, even if not constructed in this crate yet
|
||||
Unknown = 0,
|
||||
|
||||
/// UTF-8 string. `payload_len` is the number of bytes.
|
||||
@@ -87,7 +88,6 @@ pub enum ObjectKind {
|
||||
/// contained `HeapRef`s directly.
|
||||
/// - `payload_len` is 0 for this fixed-layout object.
|
||||
Coroutine = 6,
|
||||
|
||||
// Future kinds must be appended here to keep tag numbers stable.
|
||||
}
|
||||
|
||||
@@ -120,10 +120,16 @@ impl ObjectHeader {
|
||||
}
|
||||
|
||||
/// Returns true if the GC mark bit is set.
|
||||
pub fn is_marked(&self) -> bool { (self.flags & object_flags::MARKED) != 0 }
|
||||
pub fn is_marked(&self) -> bool {
|
||||
(self.flags & object_flags::MARKED) != 0
|
||||
}
|
||||
|
||||
/// Sets or clears the GC mark bit. Note: actual GC logic lives elsewhere.
|
||||
pub fn set_marked(&mut self, value: bool) {
|
||||
if value { self.flags |= object_flags::MARKED; } else { self.flags &= !object_flags::MARKED; }
|
||||
if value {
|
||||
self.flags |= object_flags::MARKED;
|
||||
} else {
|
||||
self.flags &= !object_flags::MARKED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,13 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::VirtualMachine;
|
||||
|
||||
struct CollectVisitor { pub seen: Vec<HeapRef> }
|
||||
struct CollectVisitor {
|
||||
pub seen: Vec<HeapRef>,
|
||||
}
|
||||
impl RootVisitor for CollectVisitor {
|
||||
fn visit_heap_ref(&mut self, r: HeapRef) { self.seen.push(r); }
|
||||
fn visit_heap_ref(&mut self, r: HeapRef) {
|
||||
self.seen.push(r);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -28,7 +28,9 @@ struct SleepEntry {
|
||||
}
|
||||
|
||||
impl Scheduler {
|
||||
pub fn new() -> Self { Self::default() }
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
// ---------- Ready queue operations ----------
|
||||
|
||||
@@ -43,15 +45,25 @@ impl Scheduler {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn is_ready_empty(&self) -> bool { self.ready_queue.is_empty() }
|
||||
pub fn is_ready_empty(&self) -> bool {
|
||||
self.ready_queue.is_empty()
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub fn ready_len(&self) -> usize { self.ready_queue.len() }
|
||||
pub fn ready_len(&self) -> usize {
|
||||
self.ready_queue.len()
|
||||
}
|
||||
|
||||
// ---------- Current tracking (no switching here) ----------
|
||||
pub fn set_current(&mut self, coro: Option<HeapRef>) { self.current = coro; }
|
||||
pub fn set_current(&mut self, coro: Option<HeapRef>) {
|
||||
self.current = coro;
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub fn current(&self) -> Option<HeapRef> { self.current }
|
||||
pub fn clear_current(&mut self) { self.current = None; }
|
||||
pub fn current(&self) -> Option<HeapRef> {
|
||||
self.current
|
||||
}
|
||||
pub fn clear_current(&mut self) {
|
||||
self.current = None;
|
||||
}
|
||||
|
||||
// ---------- Sleeping operations ----------
|
||||
|
||||
@@ -63,15 +75,13 @@ impl Scheduler {
|
||||
self.next_seq = self.next_seq.wrapping_add(1);
|
||||
|
||||
// Binary search insertion point by wake_tick, then by seq to keep total order deterministic
|
||||
let idx = match self
|
||||
.sleeping
|
||||
.binary_search_by(|e| {
|
||||
if e.wake_tick == entry.wake_tick {
|
||||
e.seq.cmp(&entry.seq)
|
||||
} else {
|
||||
e.wake_tick.cmp(&entry.wake_tick)
|
||||
}
|
||||
}) {
|
||||
let idx = match self.sleeping.binary_search_by(|e| {
|
||||
if e.wake_tick == entry.wake_tick {
|
||||
e.seq.cmp(&entry.seq)
|
||||
} else {
|
||||
e.wake_tick.cmp(&entry.wake_tick)
|
||||
}
|
||||
}) {
|
||||
Ok(i) => i, // equal element position; insert after to preserve FIFO among equals
|
||||
Err(i) => i,
|
||||
};
|
||||
@@ -81,10 +91,10 @@ impl Scheduler {
|
||||
/// Move all sleeping coroutines with `wake_tick <= current_tick` to ready queue (FIFO by wake order).
|
||||
pub fn wake_ready(&mut self, current_tick: u64) {
|
||||
// Find split point where wake_tick > current_tick
|
||||
let split = self
|
||||
.sleeping
|
||||
.partition_point(|e| e.wake_tick <= current_tick);
|
||||
if split == 0 { return; }
|
||||
let split = self.sleeping.partition_point(|e| e.wake_tick <= current_tick);
|
||||
if split == 0 {
|
||||
return;
|
||||
}
|
||||
let mut ready_slice: Vec<SleepEntry> = self.sleeping.drain(0..split).collect();
|
||||
// Already in order by (wake_tick, seq); push in that order to preserve determinism
|
||||
for e in ready_slice.drain(..) {
|
||||
@@ -93,14 +103,18 @@ impl Scheduler {
|
||||
}
|
||||
|
||||
/// Returns true if there are any sleeping coroutines.
|
||||
pub fn has_sleeping(&self) -> bool { !self.sleeping.is_empty() }
|
||||
pub fn has_sleeping(&self) -> bool {
|
||||
!self.sleeping.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn hr(id: u32) -> HeapRef { HeapRef(id) }
|
||||
fn hr(id: u32) -> HeapRef {
|
||||
HeapRef(id)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fifo_ready_queue_is_deterministic() {
|
||||
|
||||
Reference in New Issue
Block a user