feat(loader): materialize bounded PBX v1 payloads for PLN-0176 step 09
This commit is contained in:
@@ -84,6 +84,97 @@ fn writer_matches_independent_minimal_envelope() {
|
||||
assert_eq!(registry.declarations().functions[0].max_stack_slots, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn owned_v1_decoder_accepts_independent_bytes_without_publishing_an_image() {
|
||||
let mut b = budget();
|
||||
let owned = OwnedPbx::parse(&minimal_bytes(), &mut b).unwrap();
|
||||
assert_eq!(owned.registry.declarations(), minimal_registry().declarations());
|
||||
assert_eq!(owned.code, [1, 0]);
|
||||
assert!(owned.constants.is_empty());
|
||||
assert!(owned.exports.is_empty());
|
||||
assert!(owned.syscalls.is_empty());
|
||||
assert!(owned.debug.is_none());
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), owned.code.capacity() as u64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn owned_payloads_preserve_utf8_float_bits_names_and_binding_declarations() {
|
||||
use prometeu_bytecode::model::{ConstantPoolEntry as K, DebugInfo, Export, SyscallDecl};
|
||||
use prometeu_bytecode::SourceSpan;
|
||||
let registry = minimal_registry();
|
||||
let constants = [
|
||||
K::Null,
|
||||
K::Int32(i32::MIN),
|
||||
K::Int64(i64::MAX),
|
||||
K::Float64(f64::from_bits(0x7ff8000000000042)),
|
||||
K::Boolean(true),
|
||||
K::String("é猫".into()),
|
||||
K::String(String::new()),
|
||||
];
|
||||
let debug = DebugInfo {
|
||||
pc_to_span: vec![(0, SourceSpan { file_id: 9, start: 2, end: 7 })],
|
||||
function_names: vec![(0, "débug".into())],
|
||||
};
|
||||
let exports = [Export { func_idx: 0, symbol: "入口".into() }];
|
||||
let syscalls = [SyscallDecl {
|
||||
module: "system".into(),
|
||||
name: "hasCart".into(),
|
||||
version: 1,
|
||||
arg_slots: 0,
|
||||
ret_slots: 1,
|
||||
}];
|
||||
let bytes = PbxModule {
|
||||
registry: ®istry,
|
||||
constants: &constants,
|
||||
code: &[1, 0],
|
||||
debug: Some(&debug),
|
||||
exports: &exports,
|
||||
syscalls: &syscalls,
|
||||
}
|
||||
.serialize(&mut budget())
|
||||
.unwrap();
|
||||
let mut b = budget();
|
||||
let module = OwnedPbx::parse(&bytes, &mut b).unwrap();
|
||||
assert_eq!(&module.constants[..3], &constants[..3]);
|
||||
let K::Float64(value) = module.constants[3] else { panic!() };
|
||||
assert_eq!(value.to_bits(), 0x7ff8000000000042);
|
||||
assert_eq!(&module.constants[4..], &constants[4..]);
|
||||
assert_eq!(module.debug.as_ref(), Some(&debug));
|
||||
assert_eq!(module.exports, exports);
|
||||
assert_eq!(module.syscalls, syscalls);
|
||||
assert_eq!(b.used(LoadResource::ConstantEntries), 7);
|
||||
assert_eq!(b.used(LoadResource::ConstantStringBytes), 5);
|
||||
drop(bytes);
|
||||
assert_eq!(module.constants[5], K::String("é猫".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn owned_payload_refusal_rolls_back_all_ownership_and_preserves_prior_budget() {
|
||||
let bytes = minimal_bytes();
|
||||
let mut full = budget();
|
||||
OwnedPbx::parse(&bytes, &mut full).unwrap();
|
||||
let limits = LoadLimits { work: full.used(LoadResource::Work) - 1, ..Default::default() };
|
||||
let mut b = LoadBudget::new(limits);
|
||||
b.charge(LoadResource::TemporaryBytes, 7).unwrap();
|
||||
b.charge(LoadResource::RegistryBytes, 3).unwrap();
|
||||
let failure = OwnedPbx::parse(&bytes, &mut b).unwrap_err();
|
||||
assert_eq!(failure.resource, Some(LoadResource::Work));
|
||||
assert_eq!(failure.section, Some(2));
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 7);
|
||||
assert_eq!(b.used(LoadResource::RegistryBytes), 3);
|
||||
assert_eq!(b.used(LoadResource::Types), 0);
|
||||
assert_eq!(b.used(LoadResource::ConstantEntries), 0);
|
||||
let mut limits = LoadLimits::default();
|
||||
limits.temporary_bytes = 1;
|
||||
let mut b = LoadBudget::new(limits);
|
||||
assert_eq!(
|
||||
OwnedPbx::parse(&bytes, &mut b).unwrap_err().resource,
|
||||
Some(LoadResource::TemporaryBytes)
|
||||
);
|
||||
assert_eq!(b.used(LoadResource::RegistryBytes), 0);
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_module_writer_matches_independent_minimal_bytes_and_refunds_scratch() {
|
||||
let registry = minimal_registry();
|
||||
|
||||
Reference in New Issue
Block a user