This commit is contained in:
2026-03-24 13:40:43 +00:00
parent 1a782cbb5c
commit a199857280
32 changed files with 118 additions and 463 deletions
@@ -98,6 +98,7 @@ impl Value {
}
}
#[allow(clippy::inherent_to_string)]
pub fn to_string(&self) -> String {
match self {
Value::Int32(i) => i.to_string(),
+6 -5
View File
@@ -1,3 +1,4 @@
#![allow(clippy::collapsible_if)]
use crate::memory_banks::{SoundBankPoolInstaller, TileBankPoolInstaller};
use prometeu_hal::AssetBridge;
use prometeu_hal::asset::{
@@ -486,12 +487,12 @@ impl AssetManager {
let palette_data = &buffer[pixel_data_size..pixel_data_size + 2048];
let mut palettes = [[Color::BLACK; 16]; 64];
for p in 0..64 {
for c in 0..16 {
for (p, pal) in palettes.iter_mut().enumerate() {
for (c, slot) in pal.iter_mut().enumerate() {
let offset = (p * 16 + c) * 2;
let color_raw =
u16::from_le_bytes([palette_data[offset], palette_data[offset + 1]]);
palettes[p][c] = Color(color_raw);
*slot = Color(color_raw);
}
}
@@ -687,7 +688,7 @@ impl AssetManager {
match slot.asset_type {
BankType::TILES => {
let slots = self.gfx_slots.read().unwrap();
let asset_id = slots.get(slot.index).and_then(|s| s.clone());
let asset_id = slots.get(slot.index).and_then(|s| *s);
let (bytes, asset_name) = if let Some(id) = &asset_id {
let bytes = self
@@ -708,7 +709,7 @@ impl AssetManager {
}
BankType::SOUNDS => {
let slots = self.sound_slots.read().unwrap();
let asset_id = slots.get(slot.index).and_then(|s| s.clone());
let asset_id = slots.get(slot.index).and_then(|s| *s);
let (bytes, asset_name) = if let Some(id) = &asset_id {
let bytes = self
@@ -8,6 +8,7 @@ pub enum LoopMode {
}
pub trait AudioBridge {
#[allow(clippy::too_many_arguments)]
fn play(
&mut self,
bank_id: u8,
@@ -19,6 +20,7 @@ pub trait AudioBridge {
priority: u8,
loop_mode: LoopMode,
);
#[allow(clippy::too_many_arguments)]
fn play_sample(
&mut self,
sample: Arc<Sample>,
+1 -1
View File
@@ -27,7 +27,7 @@ pub struct Button {
impl Button {
pub fn begin_frame(&mut self, is_down_now: bool) {
let was_down = self.down;
self.down = is_down_now.clone();
self.down = is_down_now;
self.pressed = !was_down && self.down;
self.released = was_down && !self.down;
+45 -45
View File
@@ -50,55 +50,55 @@ impl Certifier {
let mut violations = 0;
if let Some(budget) = self.config.cycles_budget_per_frame {
if telemetry.cycles_used > budget {
log_service.log(
ts_ms,
telemetry.frame_index,
LogLevel::Warn,
LogSource::Pos,
0xCA01,
format!(
"Cert: cycles_used exceeded budget ({} > {})",
telemetry.cycles_used, budget
),
);
violations += 1;
}
if let Some(budget) = self.config.cycles_budget_per_frame
&& telemetry.cycles_used > budget
{
log_service.log(
ts_ms,
telemetry.frame_index,
LogLevel::Warn,
LogSource::Pos,
0xCA01,
format!(
"Cert: cycles_used exceeded budget ({} > {})",
telemetry.cycles_used, budget
),
);
violations += 1;
}
if let Some(limit) = self.config.max_syscalls_per_frame {
if telemetry.syscalls > limit {
log_service.log(
ts_ms,
telemetry.frame_index,
LogLevel::Warn,
LogSource::Pos,
0xCA02,
format!(
"Cert: syscalls per frame exceeded limit ({} > {})",
telemetry.syscalls, limit
),
);
violations += 1;
}
if let Some(limit) = self.config.max_syscalls_per_frame
&& telemetry.syscalls > limit
{
log_service.log(
ts_ms,
telemetry.frame_index,
LogLevel::Warn,
LogSource::Pos,
0xCA02,
format!(
"Cert: syscalls per frame exceeded limit ({} > {})",
telemetry.syscalls, limit
),
);
violations += 1;
}
if let Some(limit) = self.config.max_host_cpu_us_per_frame {
if telemetry.host_cpu_time_us > limit {
log_service.log(
ts_ms,
telemetry.frame_index,
LogLevel::Warn,
LogSource::Pos,
0xCA03,
format!(
"Cert: host_cpu_time_us exceeded limit ({} > {})",
telemetry.host_cpu_time_us, limit
),
);
violations += 1;
}
if let Some(limit) = self.config.max_host_cpu_us_per_frame
&& telemetry.host_cpu_time_us > limit
{
log_service.log(
ts_ms,
telemetry.frame_index,
LogLevel::Warn,
LogSource::Pos,
0xCA03,
format!(
"Cert: host_cpu_time_us exceeded limit ({} > {})",
telemetry.host_cpu_time_us, limit
),
);
violations += 1;
}
violations
+36 -35
View File
@@ -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
+5
View File
@@ -51,12 +51,15 @@
#[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
Unknown = 0,
/// UTF-8 string. `payload_len` is the number of bytes.
#[allow(dead_code)] // Public/stable tag retained; construction may live in higher layers
String = 1,
/// Homogeneous array of VM values/handles. `payload_len` is element count.
#[allow(dead_code)] // Public/stable tag retained; constructed via Heap helpers in tests
Array = 2,
/// Compiled closure/function value.
@@ -69,9 +72,11 @@ pub enum ObjectKind {
Closure = 3,
/// Byte buffer / blob. `payload_len` is the number of bytes.
#[allow(dead_code)] // Public/stable tag retained for future/host APIs
Bytes = 4,
/// User-defined/native host object. Payload shape is host-defined.
#[allow(dead_code)] // Reserved for host/native integrations
UserData = 5,
/// Coroutine object: suspended execution context with its own stack/frames.
@@ -42,11 +42,14 @@ impl Scheduler {
self.ready_queue.pop_front()
}
#[cfg(test)]
pub fn is_ready_empty(&self) -> bool { self.ready_queue.is_empty() }
#[cfg(test)]
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; }
#[cfg(test)]
pub fn current(&self) -> Option<HeapRef> { self.current }
pub fn clear_current(&mut self) { self.current = None; }
+9 -13
View File
@@ -208,10 +208,8 @@ impl Verifier {
}
// Coroutine safety: forbid YIELD when operand stack is not empty (minimal rule)
if let OpCode::Yield = instr.opcode {
if in_height != 0 {
return Err(VerifierError::InvalidYieldContext { pc: func_start + pc, height: in_height });
}
if let OpCode::Yield = instr.opcode && in_height != 0 {
return Err(VerifierError::InvalidYieldContext { pc: func_start + pc, height: in_height });
}
// Compute out types vector with closure-aware rules
@@ -317,7 +315,7 @@ impl Verifier {
}
}
let _pushes_final = dynamic_pushes.unwrap_or_else(|| match instr.opcode {
let _pushes_final = dynamic_pushes.unwrap_or(match instr.opcode {
OpCode::MakeClosure => 1,
OpCode::CallClosure => {
// If we reached here, we handled it above and set dynamic_pushes
@@ -340,14 +338,12 @@ impl Verifier {
});
}
if instr.opcode == OpCode::Ret {
if in_height != func.return_slots {
return Err(VerifierError::BadRetStackHeight {
pc: func_start + pc,
height: in_height,
expected: func.return_slots,
});
}
if instr.opcode == OpCode::Ret && in_height != func.return_slots {
return Err(VerifierError::BadRetStackHeight {
pc: func_start + pc,
height: in_height,
expected: func.return_slots,
});
}
// Propagate to successors
@@ -228,7 +228,7 @@ impl VirtualMachine {
// Resolve the entrypoint: empty (defaults to func 0), numeric func_idx, or symbol name.
let pc = if entrypoint.is_empty() {
program.functions.get(0).map(|f| f.code_offset as usize).unwrap_or(0)
program.functions.first().map(|f| f.code_offset as usize).unwrap_or(0)
} else if let Ok(func_idx) = entrypoint.parse::<usize>() {
program
.functions
@@ -525,7 +525,7 @@ impl VirtualMachine {
.imm_u32()
.map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?
as usize;
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
let val = self.pop().map_err(LogicalFrameEndingReason::Panic)?;
match val {
Value::Boolean(false) => {
let func_start = self
@@ -551,7 +551,7 @@ impl VirtualMachine {
.imm_u32()
.map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?
as usize;
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
let val = self.pop().map_err(LogicalFrameEndingReason::Panic)?;
match val {
Value::Boolean(true) => {
let func_start = self
@@ -622,7 +622,7 @@ impl VirtualMachine {
// Pop args top-first, then reverse to logical order arg1..argN
let mut args: Vec<Value> = Vec::with_capacity(arg_count);
for _ in 0..arg_count {
args.push(self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?);
args.push(self.pop().map_err(LogicalFrameEndingReason::Panic)?);
}
args.reverse();
@@ -1040,7 +1040,7 @@ impl VirtualMachine {
_ => Err(OpError::Panic("Invalid types for MOD".into())),
})?,
OpCode::BoundToInt => {
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
let val = self.pop().map_err(LogicalFrameEndingReason::Panic)?;
if let Value::Bounded(b) = val {
self.push(Value::Int64(b as i64));
} else {
@@ -1050,13 +1050,13 @@ impl VirtualMachine {
}
}
OpCode::IntToBoundChecked => {
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
let val = self.pop().map_err(LogicalFrameEndingReason::Panic)?;
let int_val = val.as_integer().ok_or_else(|| {
LogicalFrameEndingReason::Panic(
"Expected integer for INT_TO_BOUND_CHECKED".into(),
)
})?;
if int_val < 0 || int_val > 0xFFFF {
if !(0..=0xFFFF).contains(&int_val) {
return Err(self.trap(
TRAP_OOB,
OpCode::IntToBoundChecked as u16,
@@ -1101,7 +1101,7 @@ impl VirtualMachine {
_ => Err(OpError::Panic("Invalid types for OR".into())),
})?,
OpCode::Not => {
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
let val = self.pop().map_err(LogicalFrameEndingReason::Panic)?;
if let Value::Boolean(b) = val {
self.push(Value::Boolean(!b));
} else {
@@ -1148,7 +1148,7 @@ impl VirtualMachine {
_ => Err(OpError::Panic("Invalid types for Shr".into())),
})?,
OpCode::Neg => {
let val = self.pop().map_err(|e| LogicalFrameEndingReason::Panic(e))?;
let val = self.pop().map_err(LogicalFrameEndingReason::Panic)?;
match val {
Value::Int32(a) => self.push(Value::Int32(a.wrapping_neg())),
Value::Int64(a) => self.push(Value::Int64(a.wrapping_neg())),