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:
@@ -14,7 +14,7 @@ pub struct Section<'a> {
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub struct PbxEnvelope<'a> {
|
||||
sections: [Option<Section<'a>>; 8],
|
||||
sections: [Option<Section<'a>>; 9],
|
||||
}
|
||||
impl<'a> PbxEnvelope<'a> {
|
||||
pub fn section(&self, kind: u32) -> Option<Section<'a>> {
|
||||
@@ -47,12 +47,12 @@ impl<'a> PbxEnvelope<'a> {
|
||||
if directory_end > bytes.len() {
|
||||
return Err(E::new(C::InvalidFormat, R::Truncated));
|
||||
}
|
||||
let mut result = Self { sections: [None; 8] };
|
||||
let mut result = Self { sections: [None; 9] };
|
||||
for i in 0..count {
|
||||
b.work(13)?;
|
||||
let pos = 32 + i * 12;
|
||||
let kind = u32::from_le_bytes(bytes[pos..pos + 4].try_into().unwrap());
|
||||
if kind > 7 {
|
||||
if kind > 8 {
|
||||
return Err(E::new(C::UnsupportedFormat, R::SectionKind).at(kind, pos));
|
||||
}
|
||||
if result.sections[kind as usize].is_some() {
|
||||
@@ -83,7 +83,7 @@ impl<'a> PbxEnvelope<'a> {
|
||||
result.sections[kind as usize] =
|
||||
Some(Section { kind, offset: start, bytes: &bytes[start..end] });
|
||||
}
|
||||
for kind in [0, 1, 2, 4, 5, 6, 7] {
|
||||
for kind in [0, 1, 2, 4, 5, 6, 7, 8] {
|
||||
if result.sections[kind].is_none() {
|
||||
return Err(E::new(C::InvalidFormat, R::MissingSection).at(kind as u32, 8));
|
||||
}
|
||||
@@ -103,7 +103,8 @@ impl<'a> PbxEnvelope<'a> {
|
||||
];
|
||||
let before = resources.map(|r| b.used(r));
|
||||
let types = Reader::new(self.section(6).unwrap(), b).types()?;
|
||||
let functions = Reader::new(self.section(1).unwrap(), b).functions()?;
|
||||
let mut functions = Reader::new(self.section(1).unwrap(), b).functions()?;
|
||||
Reader::new(self.section(8).unwrap(), b).captures(&mut functions)?;
|
||||
let implementations = Reader::new(self.section(7).unwrap(), b).implementations()?;
|
||||
// Ownership moves from parse scratch to retained Registry. No clone or
|
||||
// simultaneous second declaration allocation occurs at this boundary.
|
||||
@@ -226,6 +227,7 @@ impl<'a, 'b> Reader<'a, 'b> {
|
||||
0 => TypeKind::Builtin(BuiltinId(self.u32()?)),
|
||||
1 => TypeKind::Struct(self.named()?),
|
||||
2 => TypeKind::Array(TypeId(self.u32()?)),
|
||||
4 => TypeKind::Fn(self.signature()?),
|
||||
3 => {
|
||||
let (mut methods, n) = self.list(Q::NestedRecords, 9)?;
|
||||
for j in 0..n {
|
||||
@@ -258,6 +260,7 @@ impl<'a, 'b> Reader<'a, 'b> {
|
||||
code_len: self.u32()?,
|
||||
local_slots: self.u32()?,
|
||||
max_stack_slots: self.u32()?,
|
||||
captures: Vec::new(),
|
||||
})
|
||||
})()
|
||||
.map_err(|e: E| e.record(i as u32))?;
|
||||
@@ -266,6 +269,26 @@ impl<'a, 'b> Reader<'a, 'b> {
|
||||
self.finish()?;
|
||||
Ok(out)
|
||||
}
|
||||
fn captures(mut self, functions: &mut [FunctionDecl]) -> Result<(), E> {
|
||||
let n = self.u32()? as usize;
|
||||
if n != functions.len() {
|
||||
let mut error = self.error(C::InvalidFormat, R::CaptureCount);
|
||||
error.requested = Some(n as u64);
|
||||
error.limit = Some(functions.len() as u64);
|
||||
return Err(error);
|
||||
}
|
||||
for (i, function) in functions.iter_mut().enumerate() {
|
||||
self.budget.work(1)?;
|
||||
let (mut captures, n) =
|
||||
self.list(Q::NestedRecords, 4).map_err(|e| e.record(i as u32))?;
|
||||
for _ in 0..n {
|
||||
self.budget.work(1)?;
|
||||
captures.push(TypeId(self.u32()?));
|
||||
}
|
||||
function.captures = captures;
|
||||
}
|
||||
self.finish()
|
||||
}
|
||||
fn implementations(mut self) -> Result<Vec<ImplementationDecl>, E> {
|
||||
let (mut out, n) = self.list(Q::Implementations, 12)?;
|
||||
for i in 0..n {
|
||||
@@ -298,17 +321,17 @@ pub struct SectionData<'a> {
|
||||
/// Assemble strict v1 sections. The output buffer is charged to temporary load storage.
|
||||
pub fn encode_envelope(sections: &[SectionData<'_>], b: &mut LoadBudget) -> Result<Vec<u8>, E> {
|
||||
b.transaction(|b| {
|
||||
let mut by_kind = [None; 8];
|
||||
let mut by_kind = [None; 9];
|
||||
for section in sections {
|
||||
b.work(1)?;
|
||||
if section.kind > 7 {
|
||||
if section.kind > 8 {
|
||||
return Err(E::new(C::UnsupportedFormat, R::SectionKind));
|
||||
}
|
||||
if by_kind[section.kind as usize].replace(section.bytes).is_some() {
|
||||
return Err(E::new(C::InvalidFormat, R::DuplicateSection));
|
||||
}
|
||||
}
|
||||
for k in [0, 1, 2, 4, 5, 6, 7] {
|
||||
for k in [0, 1, 2, 4, 5, 6, 7, 8] {
|
||||
if by_kind[k].is_none() {
|
||||
return Err(E::new(C::InvalidFormat, R::MissingSection).at(k as u32, 8));
|
||||
}
|
||||
@@ -347,6 +370,7 @@ pub struct RegistrySections {
|
||||
pub types: Vec<u8>,
|
||||
pub functions: Vec<u8>,
|
||||
pub implementations: Vec<u8>,
|
||||
pub captures: Vec<u8>,
|
||||
}
|
||||
/// Field-by-field serialization of already resolved Registry declarations.
|
||||
/// There is no source-language inference or unsupported-value-to-null fallback.
|
||||
@@ -361,12 +385,14 @@ pub fn encode_registry(registry: &Registry, b: &mut LoadBudget) -> Result<Regist
|
||||
TypeKind::Struct(_) => 1,
|
||||
TypeKind::Array(_) => 2,
|
||||
TypeKind::Trait(_) => 3,
|
||||
TypeKind::Fn(_) => 4,
|
||||
})?;
|
||||
w.name(&t.name)?;
|
||||
match &t.kind {
|
||||
TypeKind::Builtin(id) => w.u32(id.0)?,
|
||||
TypeKind::Struct(fields) => w.named(fields)?,
|
||||
TypeKind::Array(id) => w.u32(id.0)?,
|
||||
TypeKind::Fn(s) => w.signature(s)?,
|
||||
TypeKind::Trait(methods) => {
|
||||
w.count(methods.len())?;
|
||||
for m in methods {
|
||||
@@ -402,7 +428,17 @@ pub fn encode_registry(registry: &Registry, b: &mut LoadBudget) -> Result<Regist
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(RegistrySections { types, functions, implementations })
|
||||
let captures = write_section(b, |w| {
|
||||
w.count(d.functions.len())?;
|
||||
for f in &d.functions {
|
||||
w.count(f.captures.len())?;
|
||||
for id in &f.captures {
|
||||
w.u32(id.0)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(RegistrySections { types, functions, implementations, captures })
|
||||
})
|
||||
}
|
||||
// Count first so a malicious producer cannot trigger unbounded geometric growth.
|
||||
|
||||
Reference in New Issue
Block a user