feat(vm)!: implement typed callables and single-context execution
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Add structural Fn signatures, typed capture environments, indirect calls and Trait dispatch while preserving logical parameter and result groups, ownership and budgets. Remove guest coroutine execution and legacy loading. Activate bounded sealed PBX v1 with mandatory capture metadata, preserve frame continuations and root return results, and migrate producers and shipped cartridges. Publish coordinated specs, handoffs and the completed runtime lesson. Validation: 880 workspace tests passed, 9 existing tests ignored; both cartridges completed six production-host frames.
This commit is contained in:
+15
-2
@@ -3,7 +3,7 @@ use prometeu_hal::{HostContext, HostReturn, NativeInterface, SyscallId};
|
||||
use prometeu_vm::{BudgetReport, LogicalFrameEndingReason, VirtualMachine};
|
||||
|
||||
#[test]
|
||||
fn scheduler_wake_and_ready_order_is_deterministic() {
|
||||
fn single_context_frame_continuation_is_deterministic() {
|
||||
// Black-box determinism: run a tiny program twice and ensure it yields the same sequence
|
||||
// of LogicalFrameEndingReason for identical budgets.
|
||||
|
||||
@@ -39,7 +39,20 @@ fn scheduler_wake_and_ready_order_is_deterministic() {
|
||||
emit(CoreOpCode::Halt, None, &mut rom);
|
||||
|
||||
let run_once = || -> Vec<LogicalFrameEndingReason> {
|
||||
let mut vm = VirtualMachine::new(rom.clone(), vec![]);
|
||||
let bytes = prometeu_test_support::program::TestProgram::single(
|
||||
rom.clone(),
|
||||
Default::default(),
|
||||
0,
|
||||
4,
|
||||
)
|
||||
.serialize();
|
||||
let image = prometeu_vm::ValidatedProgramImage::load(
|
||||
&bytes,
|
||||
&mut prometeu_bytecode::load_limits::LoadBudget::new(Default::default()),
|
||||
0,
|
||||
)
|
||||
.unwrap();
|
||||
let mut vm = VirtualMachine::from_image(image, Default::default());
|
||||
vm.prepare_call("0").expect("prepare entry call");
|
||||
let mut native = NoopNative;
|
||||
let mut ctx = HostContext::new(None);
|
||||
@@ -23,11 +23,11 @@ fn serialized_guest_cycle_and_dynamic_box_cross_real_safepoints_then_reclaim() {
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let r = vm.run_budget(100_000, &mut support::Native, &mut HostContext::new(None)).unwrap();
|
||||
assert_eq!(r.reason, LogicalFrameEndingReason::FrameSync);
|
||||
assert_eq!(vm.heap().len(), 4); // main coroutine, Array, Node, Dynamic box
|
||||
assert_eq!(vm.heap().len(), 3); // Array, Node, Dynamic box
|
||||
vm.step(&mut support::Native, &mut HostContext::new(None)).unwrap();
|
||||
let Value::HeapRef(array) = vm.operand_stack_top(1)[0] else { panic!() };
|
||||
let r = vm.run_budget(100_000, &mut support::Native, &mut HostContext::new(None)).unwrap();
|
||||
assert_eq!(r.reason, LogicalFrameEndingReason::FrameSync);
|
||||
assert_eq!(vm.heap().len(), 1);
|
||||
assert_eq!(vm.heap().len(), 0);
|
||||
assert!(!vm.heap().is_valid(array));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#[allow(dead_code)]
|
||||
#[path = "../../../console/prometeu-vm/tests/support/callables.rs"]
|
||||
mod support;
|
||||
use prometeu_bytecode::{registry::TypeId, Value};
|
||||
use prometeu_vm::LogicalFrameEndingReason;
|
||||
#[test]
|
||||
fn tuple_returned_callbacks_capture_state_across_a_real_frame_boundary() {
|
||||
let mut vm=support::vm(&[("CALL 1\nSET_GLOBAL 1\nSET_GLOBAL 0\nFRAME_SYNC\nGET_GLOBAL 0\nCALL_CLOSURE 4\nGET_GLOBAL 1\nCALL_CLOSURE 4\nRET",support::sig(&[],&[0,0]),vec![]),("PUSH_I32 13\nMAKE_CLOSURE fn=2,type=4\nPUSH_I32 29\nMAKE_CLOSURE fn=2,type=4\nRET",support::sig(&[],&[4,4]),vec![]),("GET_CAPTURE 0\nRET",support::sig(&[],&[0]),vec![TypeId(0)])]);
|
||||
assert_eq!(support::run(&mut vm, 1000).reason, LogicalFrameEndingReason::FrameSync);
|
||||
assert_eq!(support::run(&mut vm, 1000).reason, LogicalFrameEndingReason::EndOfRom);
|
||||
assert_eq!(vm.operand_stack_top(2), vec![Value::Int32(29), Value::Int32(13)]);
|
||||
}
|
||||
@@ -2,9 +2,11 @@ use prometeu_vm::{VirtualMachine, VmInitError};
|
||||
|
||||
#[test]
|
||||
fn invalid_image_format_is_rejected_before_execution() {
|
||||
// Provide bytes that are not a valid PBX image. The VM must reject it with InvalidFormat.
|
||||
// Provide bytes that are not a valid PBX image. The VM must reject it with a structured load error.
|
||||
let program_bytes = b"NOT_PBX_IMAGE".to_vec();
|
||||
let mut vm = VirtualMachine::default();
|
||||
let result = vm.initialize(program_bytes);
|
||||
assert!(matches!(result, Err(VmInitError::InvalidFormat)));
|
||||
assert!(
|
||||
matches!(result,Err(VmInitError::RegistryLoadFailed(e)) if e.code==prometeu_bytecode::load_limits::LoadCode::InvalidFormat)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -36,11 +36,20 @@ fn vm_executes_valid_program_in_slices() {
|
||||
emit(CoreOpCode::PushI32, Some(&2i32.to_le_bytes()), &mut rom);
|
||||
emit(CoreOpCode::PushI32, Some(&3i32.to_le_bytes()), &mut rom);
|
||||
emit(CoreOpCode::Add, None, &mut rom);
|
||||
emit(CoreOpCode::SetGlobal, Some(&0u32.to_le_bytes()), &mut rom);
|
||||
emit(CoreOpCode::FrameSync, None, &mut rom);
|
||||
emit(CoreOpCode::GetGlobal, Some(&0u32.to_le_bytes()), &mut rom);
|
||||
emit(CoreOpCode::Halt, None, &mut rom);
|
||||
|
||||
// Construct VM directly; assume input is already verified.
|
||||
let mut vm = VirtualMachine::new(rom, vec![]);
|
||||
let bytes = prometeu_test_support::program::TestProgram::single(rom, Default::default(), 0, 8)
|
||||
.serialize();
|
||||
let image = prometeu_vm::ValidatedProgramImage::load(
|
||||
&bytes,
|
||||
&mut prometeu_bytecode::load_limits::LoadBudget::new(Default::default()),
|
||||
0,
|
||||
)
|
||||
.unwrap();
|
||||
let mut vm = VirtualMachine::from_image(image, Default::default());
|
||||
vm.prepare_call("0").expect("prepare entry call");
|
||||
|
||||
let mut native = NoopNative;
|
||||
|
||||
@@ -6,3 +6,4 @@ license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
rand = { version = "0.8", features = ["std", "std_rng"] }
|
||||
prometeu-bytecode = { path = "../../console/prometeu-bytecode" }
|
||||
|
||||
@@ -106,3 +106,5 @@ mod tests {
|
||||
assert_eq!(c.now_millis(), 1032);
|
||||
}
|
||||
}
|
||||
|
||||
pub mod program;
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
//! Explicit typed test producers. All bytes use the official bounded v1 writer;
|
||||
//! executable tests still go through ValidatedProgramImage::load.
|
||||
use prometeu_bytecode::{
|
||||
load_limits::LoadBudget,
|
||||
model::{ConstantPoolEntry, DebugInfo, Export, SyscallDecl},
|
||||
registry::*,
|
||||
registry_codec::PbxModule,
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TestProgram {
|
||||
pub const_pool: Vec<ConstantPoolEntry>,
|
||||
pub functions: Vec<FunctionDecl>,
|
||||
pub code: Vec<u8>,
|
||||
pub debug_info: Option<DebugInfo>,
|
||||
pub exports: Vec<Export>,
|
||||
pub syscalls: Vec<SyscallDecl>,
|
||||
}
|
||||
/// Scalar Int64 signatures for tests that explicitly choose this carrier.
|
||||
/// Other carriers and multi-slot logical values must declare their own TypeIds.
|
||||
pub fn int64_signature(parameters: usize, results: usize) -> Signature {
|
||||
Signature {
|
||||
parameters: (0..parameters).map(|_| NamedType { type_id: TypeId(0), name: None }).collect(),
|
||||
results: vec![TypeId(0); results],
|
||||
}
|
||||
}
|
||||
pub fn scalar_catalog() -> Vec<TypeDecl> {
|
||||
[
|
||||
BuiltinId::INT64,
|
||||
BuiltinId::INT32,
|
||||
BuiltinId::FLOAT,
|
||||
BuiltinId::BOOL,
|
||||
BuiltinId::STRING,
|
||||
BuiltinId::DYNAMIC,
|
||||
BuiltinId::VEC2,
|
||||
BuiltinId::PIXEL,
|
||||
BuiltinId::COLOR,
|
||||
]
|
||||
.into_iter()
|
||||
.map(|id| TypeDecl { name: None, kind: TypeKind::Builtin(id) })
|
||||
.collect()
|
||||
}
|
||||
impl TestProgram {
|
||||
pub fn single(
|
||||
code: Vec<u8>,
|
||||
signature: Signature,
|
||||
local_slots: u32,
|
||||
max_stack_slots: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
functions: vec![FunctionDecl {
|
||||
name: None,
|
||||
signature,
|
||||
captures: vec![],
|
||||
code_offset: 0,
|
||||
code_len: code.len() as u32,
|
||||
local_slots,
|
||||
max_stack_slots,
|
||||
}],
|
||||
code,
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
pub fn serialize(&self) -> Vec<u8> {
|
||||
let mut budget = LoadBudget::new(Default::default());
|
||||
let registry = Registry::resolve(
|
||||
RegistryDeclarations {
|
||||
types: scalar_catalog(),
|
||||
functions: self.functions.clone(),
|
||||
implementations: vec![],
|
||||
},
|
||||
self.code.len(),
|
||||
&mut budget,
|
||||
)
|
||||
.expect("typed test declarations");
|
||||
PbxModule {
|
||||
registry: ®istry,
|
||||
constants: &self.const_pool,
|
||||
code: &self.code,
|
||||
debug: self.debug_info.as_ref(),
|
||||
exports: &self.exports,
|
||||
syscalls: &self.syscalls,
|
||||
}
|
||||
.serialize(&mut budget)
|
||||
.expect("official bounded v1 writer")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user