feat(bytecode): complete PLN-0176 step 08 typed PBX v1 writer
This commit is contained in:
@@ -83,6 +83,170 @@ fn writer_matches_independent_minimal_envelope() {
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
assert_eq!(registry.declarations().functions[0].max_stack_slots, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_module_writer_matches_independent_minimal_bytes_and_refunds_scratch() {
|
||||
let registry = minimal_registry();
|
||||
let module = PbxModule {
|
||||
registry: ®istry,
|
||||
constants: &[],
|
||||
code: &[1, 0],
|
||||
debug: None,
|
||||
exports: &[],
|
||||
syscalls: &[],
|
||||
};
|
||||
let mut b = budget();
|
||||
b.charge(LoadResource::TemporaryBytes, 7).unwrap();
|
||||
let bytes = module.serialize(&mut b).unwrap();
|
||||
assert_eq!(bytes, minimal_bytes());
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 7 + bytes.capacity() as u64);
|
||||
assert_eq!(b.used(LoadResource::RegistryBytes), 0);
|
||||
assert_eq!(b.used(LoadResource::ConstantEntries), 0);
|
||||
ParsedPbx::parse(&bytes, &mut budget()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_module_writer_preserves_non_registry_encodings() {
|
||||
use prometeu_bytecode::model::{ConstantPoolEntry as K, DebugInfo, Export, SyscallDecl};
|
||||
use prometeu_bytecode::SourceSpan;
|
||||
let registry = minimal_registry();
|
||||
let constants = [
|
||||
K::Null,
|
||||
K::Int64(-1),
|
||||
K::Float64(f64::from_bits(0x7ff8000000001234)),
|
||||
K::Boolean(true),
|
||||
K::String("é".into()),
|
||||
K::Int32(-2),
|
||||
K::String(String::new()),
|
||||
];
|
||||
let debug = DebugInfo {
|
||||
pc_to_span: vec![(0, SourceSpan { file_id: 7, start: 2, end: 9 })],
|
||||
function_names: vec![(0, String::new())],
|
||||
};
|
||||
let exports = [Export { symbol: "boot".into(), func_idx: 0 }];
|
||||
let syscalls = [SyscallDecl {
|
||||
module: "m".into(),
|
||||
name: "n".into(),
|
||||
version: 3,
|
||||
arg_slots: 2,
|
||||
ret_slots: 1,
|
||||
}];
|
||||
let module = PbxModule {
|
||||
registry: ®istry,
|
||||
constants: &constants,
|
||||
code: &[1, 0],
|
||||
debug: Some(&debug),
|
||||
exports: &exports,
|
||||
syscalls: &syscalls,
|
||||
};
|
||||
let bytes = module.serialize(&mut budget()).unwrap();
|
||||
let parsed = ParsedPbx::parse(&bytes, &mut budget()).unwrap();
|
||||
assert_eq!(
|
||||
parsed.section(0).unwrap().bytes,
|
||||
hex("07 00 00 00 00
|
||||
01 ff ff ff ff ff ff ff ff
|
||||
02 34 12 00 00 00 00 f8 7f
|
||||
03 01 04 02 00 00 00 c3 a9 05 fe ff ff ff 04 00 00 00 00")
|
||||
);
|
||||
assert_eq!(
|
||||
parsed.section(3).unwrap().bytes,
|
||||
hex("01 00 00 00 00 00 00 00
|
||||
07 00 00 00 02 00 00 00 09 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00")
|
||||
);
|
||||
assert_eq!(
|
||||
parsed.section(4).unwrap().bytes,
|
||||
hex("01 00 00 00 00 00 00 00 04 00 00 00 62 6f 6f 74")
|
||||
);
|
||||
assert_eq!(
|
||||
parsed.section(5).unwrap().bytes,
|
||||
hex("01 00 00 00 01 00 6d 01 00 6e 03 00 02 00 01 00")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_module_writer_rejects_invalid_payload_records_without_retaining_scratch() {
|
||||
use prometeu_bytecode::model::{DebugInfo, Export, SyscallDecl};
|
||||
use prometeu_bytecode::SourceSpan;
|
||||
let registry = minimal_registry();
|
||||
let bad_export = [Export { symbol: "bad".into(), func_idx: u32::MAX }];
|
||||
let syscall = SyscallDecl { module: "m".into(), name: "n".into(), ..Default::default() };
|
||||
let duplicates = [syscall.clone(), syscall];
|
||||
let bad_debug = DebugInfo {
|
||||
pc_to_span: vec![(2, SourceSpan { file_id: 0, start: 0, end: 1 })],
|
||||
..Default::default()
|
||||
};
|
||||
for (exports, syscalls, debug, reason) in [
|
||||
(&bad_export[..], &[][..], None, LoadReason::FunctionId),
|
||||
(&[][..], &duplicates[..], None, LoadReason::DuplicateSyscall),
|
||||
(&[][..], &[][..], Some(&bad_debug), LoadReason::DebugRange),
|
||||
] {
|
||||
let mut b = budget();
|
||||
b.charge(LoadResource::TemporaryBytes, 7).unwrap();
|
||||
let e = PbxModule {
|
||||
registry: ®istry,
|
||||
constants: &[],
|
||||
code: &[1, 0],
|
||||
debug,
|
||||
exports,
|
||||
syscalls,
|
||||
}
|
||||
.serialize(&mut b)
|
||||
.unwrap_err();
|
||||
assert_eq!(e.reason, reason);
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 7);
|
||||
assert!(b.used(LoadResource::Work) > 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_module_writer_checks_constant_quotas_before_payload_allocation() {
|
||||
use prometeu_bytecode::model::ConstantPoolEntry as K;
|
||||
let registry = minimal_registry();
|
||||
let constants = [K::String("abc".into()), K::String("abc".into())];
|
||||
let module = PbxModule {
|
||||
registry: ®istry,
|
||||
constants: &constants,
|
||||
code: &[1, 0],
|
||||
debug: None,
|
||||
exports: &[],
|
||||
syscalls: &[],
|
||||
};
|
||||
for (resource, limit) in [
|
||||
(LoadResource::ConstantEntries, 1),
|
||||
(LoadResource::ConstantStringBytes, 5),
|
||||
(LoadResource::TemporaryBytes, 1),
|
||||
] {
|
||||
let mut limits = LoadLimits::default();
|
||||
match resource {
|
||||
LoadResource::ConstantEntries => limits.constant_entries = limit,
|
||||
LoadResource::ConstantStringBytes => limits.constant_string_bytes = limit,
|
||||
LoadResource::TemporaryBytes => limits.temporary_bytes = limit,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
let mut b = LoadBudget::new(limits);
|
||||
let error = module.serialize(&mut b).unwrap_err();
|
||||
assert_eq!(error.code, LoadCode::ResourceLimit);
|
||||
assert_eq!(error.resource, Some(resource));
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_module_writer_rejects_code_range_mismatch() {
|
||||
let registry = minimal_registry();
|
||||
let e = PbxModule {
|
||||
registry: ®istry,
|
||||
constants: &[],
|
||||
code: &[],
|
||||
debug: None,
|
||||
exports: &[],
|
||||
syscalls: &[],
|
||||
}
|
||||
.serialize(&mut budget())
|
||||
.unwrap_err();
|
||||
assert_eq!((e.code, e.reason), (LoadCode::InvalidCode, LoadReason::CodeRange));
|
||||
assert_eq!((e.function, e.pc), (Some(0), Some(0)));
|
||||
}
|
||||
#[test]
|
||||
fn representative_records_match_fixed_bytes_and_preserve_names_without_debug() {
|
||||
let types = hex("
|
||||
|
||||
Reference in New Issue
Block a user