//! 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, pub functions: Vec, pub code: Vec, pub debug_info: Option, pub exports: Vec, pub syscalls: Vec, } /// 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 { [ 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, signature: Signature, local_slots: u32, max_stack_slots: u32, ) -> Self { Self { functions: vec![FunctionDecl { lifecycle_role: Default::default(), 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 { self.serialize_with_types(scalar_catalog()) } pub fn serialize_with_types(&self, types: Vec) -> Vec { let mut budget = LoadBudget::new(Default::default()); let registry = Registry::resolve( RegistryDeclarations { types, 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") } }