Ship oracle journal values, bytecode/frontend hardening, and replay-policy Lean.
Host exposes oracle journals as language values with refuse/copy gates; bytecode and foreign session paths get modularized; applications and verification replay-policy sources land with Lake builds ignored. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e3d9021bb6
commit
8f1010f967
101 files changed
+17962
-1727
No files matched your search
@@ -0,0 +1,327 @@
|
||||
//! Check actual staging, scratch separation and post-evaluation call selection.
|
||||
//! Resolution and dispatch implementations remain semantic interfaces.
|
||||
use super::{expression::Cursor, ExpressionRule, FnChunk, Op, Program};
|
||||
use crate::{
|
||||
ast::{Arg, CallExpr},
|
||||
value::Value,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CallStage {
|
||||
pub expression: usize,
|
||||
pub slot: usize,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum PathFallback {
|
||||
User { target: usize, tag_instance: bool },
|
||||
Error { message: String },
|
||||
}
|
||||
impl PathFallback {
|
||||
fn evidence(&self) -> serde_json::Value {
|
||||
match self {
|
||||
Self::User { target, tag_instance } => serde_json::json!({"kind":"user","target":target,"tag_instance":tag_instance}),
|
||||
Self::Error { message } => serde_json::json!({"kind":"error","message":message}),
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum CallDispatch {
|
||||
User { target: usize, tag_instance: bool },
|
||||
Method { receiver: String },
|
||||
Builtin,
|
||||
Host { function: u16 },
|
||||
Plugin,
|
||||
RejectDuplicate { name: String },
|
||||
Path { receiver: String, check_receiver: bool, fallback: PathFallback },
|
||||
ResolutionFault { message: String },
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CallEmission {
|
||||
pub expression: usize,
|
||||
/// Expanded and normalized source, captured before argument replacement.
|
||||
pub source: CallExpr,
|
||||
pub stages: Vec<CallStage>,
|
||||
pub dispatch: Option<CallDispatch>,
|
||||
pub as_statement: bool,
|
||||
}
|
||||
impl CallEmission {
|
||||
pub fn binding_evidence(&self,program:&Program)->serde_json::Value {
|
||||
let signature=match self.dispatch.as_ref() {
|
||||
Some(CallDispatch::User{target,..})|Some(CallDispatch::Path{fallback:PathFallback::User{target,..},..})=>{
|
||||
program.functions.get(*target).map(|callee|(callee.params.clone(),callee.defaults.clone()))
|
||||
},
|
||||
Some(CallDispatch::Builtin)=>crate::arguments::builtin_signature(&self.source.callee).map(|(params,optional)|
|
||||
(params.iter().map(|p|p.to_string()).collect::<Vec<_>>(),vec![optional;params.len()])),
|
||||
Some(CallDispatch::Host{function})=>crate::host::HostFn::from_u16(*function).map(|h|{
|
||||
let params=h.all_params();let len=params.len();(params.into_iter().map(str::to_string).collect(),vec![true;len])
|
||||
}),
|
||||
_=>None,
|
||||
};
|
||||
match signature {
|
||||
Some((params,omissions))=>crate::arguments::plan_evidence(¶ms,&names(self),&omissions),
|
||||
None=>serde_json::Value::Null,
|
||||
}
|
||||
}
|
||||
pub fn evidence(&self) -> serde_json::Value {
|
||||
use serde_json::json;
|
||||
let dispatch = match &self.dispatch {
|
||||
Some(CallDispatch::User {
|
||||
target,
|
||||
tag_instance,
|
||||
}) => json!({"kind":"user","target":target,"tag_instance":tag_instance}),
|
||||
Some(CallDispatch::Method { receiver }) => json!({"kind":"method","receiver":receiver}),
|
||||
Some(CallDispatch::Builtin) => json!({"kind":"builtin"}),
|
||||
Some(CallDispatch::Host { function }) => {
|
||||
let host = crate::host::HostFn::from_u16(*function);
|
||||
json!({"kind":"host","function":function,"params":host.map(|h|h.all_params()),"required":host.map(|h|h.required_params().len())})
|
||||
}
|
||||
Some(CallDispatch::Plugin) => json!({"kind":"plugin"}),
|
||||
Some(CallDispatch::RejectDuplicate { name }) => json!({"kind":"duplicate","name":name}),
|
||||
Some(CallDispatch::Path { receiver, check_receiver, fallback }) => json!({"kind":"path","receiver":receiver,"check_receiver":check_receiver,"fallback":fallback.evidence()}),
|
||||
Some(CallDispatch::ResolutionFault { message }) => json!({"kind":"resolution_fault","message":message}),
|
||||
None => serde_json::Value::Null,
|
||||
};
|
||||
json!({"expression":self.expression,"callee":self.source.callee,"receiver":self.source.receiver,"path":self.source.path,
|
||||
"arguments":self.source.args.iter().map(|a|match a {Arg::Positional(_)=>None,Arg::Named{name,..}=>Some(name)}).collect::<Vec<_>>(),
|
||||
"source_rules":self.source.args.iter().map(|a|match a {Arg::Positional(e)|Arg::Named{value:e,..}=>ExpressionRule::from_source(e).evidence()}).collect::<Vec<_>>(),
|
||||
"stages":self.stages.iter().map(|s|json!({"expression":s.expression,"slot":s.slot})).collect::<Vec<_>>(),
|
||||
"dispatch":dispatch,"as_statement":self.as_statement})
|
||||
}
|
||||
}
|
||||
fn names(call: &CallEmission) -> Vec<Option<String>> {
|
||||
call.source
|
||||
.args
|
||||
.iter()
|
||||
.map(|a| match a {
|
||||
Arg::Positional(_) => None,
|
||||
Arg::Named { name, .. } => Some(name.clone()),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
fn read(c: &mut Cursor<'_>, slot: usize) -> Result<(), String> {
|
||||
c.op(Op::GetLocal(slot))
|
||||
}
|
||||
fn read_child(
|
||||
c: &mut Cursor<'_>,
|
||||
ids: &mut std::slice::Iter<'_, usize>,
|
||||
slot: usize,
|
||||
) -> Result<(), String> {
|
||||
let id = *ids.next().ok_or("missing staged argument reload witness")?;
|
||||
let expr = c
|
||||
.fun
|
||||
.expressions
|
||||
.get(id)
|
||||
.ok_or("missing reload expression")?;
|
||||
if expr.start != c.position
|
||||
|| expr.end != c.position + 1
|
||||
|| !matches!(&expr.rule,ExpressionRule::Read(name) if c.fun.locals.get(slot)==Some(name))
|
||||
|| !expr.children.is_empty()
|
||||
{
|
||||
return Err("staged argument reload source differs".into());
|
||||
}
|
||||
read(c, slot)
|
||||
}
|
||||
fn layout(c: &mut Cursor<'_>, arguments: &[Option<String>]) -> Result<usize, String> {
|
||||
let id = match c.fun.code.get(c.position) {
|
||||
Some(Op::CallArgs(_, id) | Op::MethodCallArgs(_, id)) => *id,
|
||||
_ => return Err("missing argument-layout call".into()),
|
||||
};
|
||||
if c.fun.call_layouts.get(id).map(Vec::as_slice) != Some(arguments) {
|
||||
return Err("call argument layout differs from source".into());
|
||||
}
|
||||
Ok(id)
|
||||
}
|
||||
pub fn verify(program: &Program, fun: &FnChunk) -> Result<(), String> {
|
||||
let caller=program.functions.iter().position(|f|std::ptr::eq(f,fun)).ok_or("missing call owner")?;
|
||||
if fun.source_local_count < fun.params.len() || fun.source_local_count > fun.locals.len() {
|
||||
return Err("invalid source/scratch local boundary".into());
|
||||
}
|
||||
let mut owners = HashSet::new();
|
||||
let loops = fun
|
||||
.statements
|
||||
.iter()
|
||||
.flat_map(|s| s.auxiliary.iter().copied())
|
||||
.collect::<HashSet<_>>();
|
||||
let mut active: Vec<(&super::ExpressionEmission, HashSet<usize>)> = Vec::new();
|
||||
for call in &fun.calls {
|
||||
let node = fun
|
||||
.expressions
|
||||
.get(call.expression)
|
||||
.ok_or("missing call source expression")?;
|
||||
if !matches!(node.rule, ExpressionRule::CallBoundary(_))
|
||||
|| !owners.insert(call.expression)
|
||||
|| call.as_statement
|
||||
{
|
||||
return Err("call source owner differs".into());
|
||||
}
|
||||
let arguments = names(call);
|
||||
if call.stages.len() != arguments.len() || node.children.len() < arguments.len() {
|
||||
return Err("call authored-actual inventory".into());
|
||||
}
|
||||
let slots = call.stages.iter().map(|s| s.slot).collect::<HashSet<_>>();
|
||||
if slots.len() != arguments.len()
|
||||
|| slots
|
||||
.iter()
|
||||
.any(|s| *s < fun.source_local_count || *s >= fun.locals.len() || loops.contains(s))
|
||||
{
|
||||
return Err("call scratch aliases source/loop/another actual".into());
|
||||
}
|
||||
while active
|
||||
.last()
|
||||
.is_some_and(|(parent, _)| parent.end <= node.start)
|
||||
{
|
||||
active.pop();
|
||||
}
|
||||
if active.iter().any(|(parent, other)| {
|
||||
node.start < parent.start || node.end > parent.end || !slots.is_disjoint(other)
|
||||
}) {
|
||||
return Err("nested call scratch overlap or interval order".into());
|
||||
}
|
||||
let mut c = Cursor {
|
||||
fun,
|
||||
position: node.start,
|
||||
end: node.end,
|
||||
};
|
||||
for (i, stage) in call.stages.iter().enumerate() {
|
||||
if node.children[i] != stage.expression {
|
||||
return Err("authored actual order differs".into());
|
||||
}
|
||||
let source = match &call.source.args[i] {
|
||||
Arg::Positional(e) | Arg::Named { value: e, .. } => e,
|
||||
};
|
||||
if ExpressionRule::from_source(source).evidence()
|
||||
!= fun.expressions[stage.expression].rule.evidence()
|
||||
{
|
||||
return Err("authored argument source rule differs".into());
|
||||
}
|
||||
c.child(stage.expression)?;
|
||||
c.op(Op::SetLocal(stage.slot))?;
|
||||
c.op(Op::Pop)?;
|
||||
}
|
||||
let mut reloads = node.children[arguments.len()..].iter();
|
||||
let dispatch=call.dispatch.as_ref().ok_or("missing call dispatch obligation")?;
|
||||
use crate::sema::{DynamicReason, resolution::Target};
|
||||
let target=super::names::resolve(program,caller,&call.source);
|
||||
let user=match &target { Target::Local(id)|Target::Import{fun:id,..}=>Some(*id),_=>None };
|
||||
let user_dispatch=|id:usize|CallDispatch::User{target:id,tag_instance:program.parents[id].is_none()&&program.functions[id].source_ast.level==1};
|
||||
let expected=if let Some(receiver)=&call.source.receiver {
|
||||
CallDispatch::Method{receiver:receiver.clone()}
|
||||
} else if let Some(path)=call.source.path.as_ref().filter(|p|p.len()==2) {
|
||||
let fallback=match user.map(user_dispatch) {
|
||||
Some(CallDispatch::User{target,tag_instance})=>PathFallback::User{target,tag_instance},
|
||||
_=>PathFallback::Error{message:match target {Target::Unresolved{message,..}=>message,_=>format!("unresolved dynamic call `{}`",crate::sema::call_display(&call.source))}},
|
||||
};
|
||||
CallDispatch::Path{receiver:path[0].clone(),check_receiver:!fun.source_ast.imports.contains(&path[0]),fallback}
|
||||
} else if let Some(id)=user {user_dispatch(id)} else {match target {
|
||||
Target::Builtin(_)|Target::Dynamic{reason:DynamicReason::CallFn,..}=>CallDispatch::Builtin,
|
||||
Target::Host(h)=>CallDispatch::Host{function:h.as_u16()},
|
||||
Target::Dynamic{reason:DynamicReason::Plugin,..}=>CallDispatch::Plugin,
|
||||
Target::Unresolved{message,..}=>CallDispatch::ResolutionFault{message},
|
||||
_=>return Err("unmodeled source call resolution".into()),
|
||||
}};
|
||||
if *dispatch!=expected{return Err("call resolution differs from source declaration graph".into());}
|
||||
match dispatch {
|
||||
CallDispatch::Path{..}=>{
|
||||
for stage in &call.stages{read_child(&mut c,&mut reloads,stage.slot)?;}
|
||||
let id=match fun.code.get(c.position){Some(Op::PathCallArgs(owner,id)) if fun.calls.get(*owner).is_some_and(|other|std::ptr::eq(other,call))=>*id,_=>return Err("path call owner differs".into())};
|
||||
if fun.call_layouts.get(id)!=Some(&arguments){return Err("path call layout differs".into());}
|
||||
let owner=fun.calls.iter().position(|other|std::ptr::eq(other,call)).unwrap();
|
||||
c.op(Op::PathCallArgs(owner,id))?;
|
||||
},
|
||||
CallDispatch::ResolutionFault{message}=>{
|
||||
let index=match fun.code.get(c.position){Some(Op::RejectCallResolution(index))=>*index,_=>return Err("missing call resolution fault".into())};
|
||||
if fun.constants.get(index)!=Some(&Value::Text(message.clone())){return Err("call resolution fault payload differs".into());}
|
||||
c.op(Op::RejectCallResolution(index))?;
|
||||
},
|
||||
CallDispatch::RejectDuplicate { name } => {
|
||||
let index = match fun.code.get(c.position) {
|
||||
Some(Op::RejectDuplicateArgument(i)) => *i,
|
||||
_ => return Err("duplicate argument refusal missing".into()),
|
||||
};
|
||||
if fun.constants.get(index) != Some(&Value::Text(name.clone())) {
|
||||
return Err("duplicate argument name differs".into());
|
||||
}
|
||||
c.op(Op::RejectDuplicateArgument(index))?;
|
||||
}
|
||||
CallDispatch::User {
|
||||
target,
|
||||
tag_instance,
|
||||
} => {
|
||||
if *target >= program.functions.len()
|
||||
|| *tag_instance != program.objects.iter().any(|(_, id)| id == target)
|
||||
{
|
||||
return Err("resolved call target/tag differs".into());
|
||||
}
|
||||
for stage in &call.stages {
|
||||
read_child(&mut c, &mut reloads, stage.slot)?;
|
||||
}
|
||||
let id = layout(&mut c, &arguments)?;
|
||||
c.op(Op::CallArgs(*target, id))?;
|
||||
if *tag_instance {
|
||||
c.op(Op::TagInstance(*target))?;
|
||||
}
|
||||
}
|
||||
CallDispatch::Method { receiver } => {
|
||||
if call.source.receiver.as_ref() != Some(receiver)
|
||||
&& call.source.path.as_ref().and_then(|p| p.first()) != Some(receiver)
|
||||
{
|
||||
return Err("method receiver differs from source".into());
|
||||
}
|
||||
for stage in &call.stages {
|
||||
read_child(&mut c, &mut reloads, stage.slot)?;
|
||||
}
|
||||
let slot = fun
|
||||
.locals
|
||||
.iter()
|
||||
.position(|n| n == receiver)
|
||||
.ok_or("method receiver slot")?;
|
||||
read(&mut c, slot)?;
|
||||
let index = match fun.code.get(c.position) {
|
||||
Some(Op::MethodCallArgs(i, _)) => *i,
|
||||
_ => return Err("method dispatch missing".into()),
|
||||
};
|
||||
if fun.constants.get(index) != Some(&Value::Text(call.source.callee.clone())) {
|
||||
return Err("method name differs from source".into());
|
||||
}
|
||||
let id = layout(&mut c, &arguments)?;
|
||||
c.op(Op::MethodCallArgs(index, id))?;
|
||||
}
|
||||
CallDispatch::Builtin|CallDispatch::Plugin|CallDispatch::Host{..}=>{
|
||||
for stage in &call.stages{read_child(&mut c,&mut reloads,stage.slot)?;}
|
||||
let (first,id)=match fun.code.get(c.position){Some(Op::BuiltinCallArgs(n,l)|Op::PluginCallArgs(n,l))=>(*n,*l),Some(Op::HostCallArgs(n,l))=>(*n as usize,*l),_=>return Err("missing native argument dispatch".into())};
|
||||
if fun.call_layouts.get(id)!=Some(&arguments){return Err("native argument layout differs".into());}
|
||||
match dispatch {
|
||||
CallDispatch::Builtin=>{
|
||||
if crate::arguments::builtin_signature(&call.source.callee).is_none() || fun.constants.get(first)!=Some(&Value::Text(call.source.callee.clone())){return Err("builtin identity differs".into());}
|
||||
c.op(Op::BuiltinCallArgs(first,id))?;
|
||||
},
|
||||
CallDispatch::Plugin=>{
|
||||
if fun.constants.get(first)!=Some(&Value::Text(call.source.callee.clone())){return Err("plugin identity differs".into());}
|
||||
c.op(Op::PluginCallArgs(first,id))?;
|
||||
},
|
||||
CallDispatch::Host{function}=>{
|
||||
let hf=crate::host::HostFn::from_name(&call.source.callee).ok_or("source host name")?;
|
||||
if hf.as_u16()!=*function || first!=*function as usize{return Err("host primitive identity differs".into());}
|
||||
c.op(Op::HostCallArgs(*function,id))?;
|
||||
},_=>unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if reloads.next().is_some() || c.position != node.end {
|
||||
return Err("call instruction/expression coverage differs".into());
|
||||
}
|
||||
active.push((node, slots));
|
||||
}
|
||||
if fun
|
||||
.expressions
|
||||
.iter()
|
||||
.filter(|e| matches!(e.rule, ExpressionRule::CallBoundary(_)))
|
||||
.count()
|
||||
!= owners.len()
|
||||
{
|
||||
return Err("uncovered call source expression".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
+382
-479
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,439 @@
|
||||
//! Source-directed expression emission obligations. Call staging and selection
|
||||
//! are checked separately in call.rs; dispatch semantics remain a proof boundary.
|
||||
use super::{FnChunk, Op};
|
||||
use crate::{
|
||||
ast::{BinaryOp, Expr, IndexKey, InterpPart, Literal, UnaryOp},
|
||||
value::Value,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ExpressionRule {
|
||||
Literal(Value),
|
||||
Read(String),
|
||||
Unary(UnaryOp),
|
||||
Binary(BinaryOp),
|
||||
Interpolation(Vec<InterpPart>),
|
||||
List(usize),
|
||||
Map(Vec<String>),
|
||||
Index {
|
||||
label: IndexKey,
|
||||
base_variable: Option<String>,
|
||||
},
|
||||
Occluded(bool),
|
||||
CallBoundary(Vec<String>),
|
||||
CallFault(String),
|
||||
}
|
||||
impl ExpressionRule {
|
||||
pub fn from_source(expr: &Expr) -> Self {
|
||||
match expr {
|
||||
Expr::Literal(value) => Self::Literal(match value {
|
||||
Literal::None => Value::None,
|
||||
Literal::Bool(v) => Value::Bool(*v),
|
||||
Literal::Int(v) => Value::Int(*v),
|
||||
Literal::Num(v) => Value::Num(*v),
|
||||
Literal::Text(v) => Value::Text(v.clone()),
|
||||
}),
|
||||
Expr::Var(name) => Self::Read(name.clone()),
|
||||
Expr::Unary { op, .. } => Self::Unary(*op),
|
||||
Expr::Binary { op, .. } => Self::Binary(*op),
|
||||
Expr::Interp(parts) => Self::Interpolation(parts.clone()),
|
||||
Expr::List(items) => Self::List(items.len()),
|
||||
Expr::Map(items) => Self::Map(items.iter().map(|(k, _)| k.clone()).collect()),
|
||||
Expr::Index { base, label } => Self::Index {
|
||||
label: label.clone(),
|
||||
base_variable: if let Expr::Var(name) = base.as_ref() {
|
||||
Some(name.clone())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
},
|
||||
Expr::Formula(value) => Self::Literal(Value::Formula(value.clone())),
|
||||
Expr::Code(value) => Self::Literal(Value::Code(value.clone())),
|
||||
Expr::Occluded { payload, .. } => Self::Occluded(payload.is_some()),
|
||||
Expr::Call(call) if call.for_evaluation().is_err() => {
|
||||
Self::CallFault(call.for_evaluation().unwrap_err())
|
||||
}
|
||||
Expr::Call(call) => Self::CallBoundary(
|
||||
call.receiver
|
||||
.iter()
|
||||
.cloned()
|
||||
.chain(call.path.iter().filter_map(|path| path.first().cloned()))
|
||||
.collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExpressionEmission {
|
||||
pub rule: ExpressionRule,
|
||||
pub start: usize,
|
||||
pub end: usize,
|
||||
pub children: Vec<usize>,
|
||||
}
|
||||
|
||||
pub(super) fn formula_equal(a: &crate::formula::Expr, b: &crate::formula::Expr) -> bool {
|
||||
use crate::formula::Expr::*;
|
||||
match (a, b) {
|
||||
(Num(x), Num(y)) => x.to_bits() == y.to_bits(),
|
||||
(Var(x), Var(y)) => x == y,
|
||||
(UnaryNeg(x), UnaryNeg(y)) => formula_equal(x, y),
|
||||
(
|
||||
Bin {
|
||||
op: a,
|
||||
left: x,
|
||||
right: y,
|
||||
},
|
||||
Bin {
|
||||
op: b,
|
||||
left: u,
|
||||
right: v,
|
||||
},
|
||||
) => a == b && formula_equal(x, u) && formula_equal(y, v),
|
||||
(Call { name: a, args: x }, Call { name: b, args: y }) => {
|
||||
a == b && x.len() == y.len() && x.iter().zip(y).all(|(a, b)| formula_equal(a, b))
|
||||
}
|
||||
(Matrix { source: a, rows: x }, Matrix { source: b, rows: y }) => {
|
||||
a == b
|
||||
&& x.len() == y.len()
|
||||
&& x.iter().zip(y).all(|(a, b)| {
|
||||
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x.to_bits() == y.to_bits())
|
||||
})
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
fn value_equal(a: &Value, b: &Value) -> bool {
|
||||
match (a, b) {
|
||||
(Value::Num(x), Value::Num(y)) => x.to_bits() == y.to_bits(),
|
||||
(Value::Formula(x), Value::Formula(y)) => formula_equal(x, y),
|
||||
_ => a == b,
|
||||
}
|
||||
}
|
||||
pub(super) struct Cursor<'a> {
|
||||
pub(super) fun: &'a FnChunk,
|
||||
pub(super) position: usize,
|
||||
pub(super) end: usize,
|
||||
}
|
||||
impl Cursor<'_> {
|
||||
pub(super) fn op(&mut self, expected: Op) -> Result<(), String> {
|
||||
if self.position >= self.end || self.fun.code.get(self.position) != Some(&expected) {
|
||||
return Err(format!(
|
||||
"source expression opcode differs at {}: expected {expected:?}",
|
||||
self.position
|
||||
));
|
||||
}
|
||||
self.position += 1;
|
||||
Ok(())
|
||||
}
|
||||
pub(super) fn literal(&mut self, value: &Value) -> Result<(), String> {
|
||||
match value {
|
||||
Value::None => self.op(Op::None_),
|
||||
Value::Bool(true) => self.op(Op::True),
|
||||
Value::Bool(false) => self.op(Op::False),
|
||||
_ => {
|
||||
let Some(Op::Constant(index)) = self.fun.code.get(self.position) else {
|
||||
return Err("source literal is not a constant instruction".into());
|
||||
};
|
||||
if !self
|
||||
.fun
|
||||
.constants
|
||||
.get(*index)
|
||||
.is_some_and(|actual| value_equal(actual, value))
|
||||
{
|
||||
return Err("source literal value differs".into());
|
||||
}
|
||||
self.op(Op::Constant(*index))
|
||||
}
|
||||
}
|
||||
}
|
||||
fn read(&mut self, name: &str) -> Result<(), String> {
|
||||
let slot = self
|
||||
.fun
|
||||
.locals
|
||||
.iter()
|
||||
.position(|local| local == name)
|
||||
.ok_or("source variable has no slot")?;
|
||||
self.op(Op::GetLocal(slot))
|
||||
}
|
||||
pub(super) fn child(&mut self, id: usize) -> Result<(), String> {
|
||||
let child = &self.fun.expressions[id];
|
||||
if child.start != self.position {
|
||||
return Err("source operand order/range differs".into());
|
||||
}
|
||||
self.position = child.end;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub fn verify(fun: &FnChunk) -> Result<usize, String> {
|
||||
let mut boundaries = 0;
|
||||
for (id, node) in fun.expressions.iter().enumerate() {
|
||||
if node.start >= node.end || node.end > fun.code.len() {
|
||||
return Err("invalid source expression interval".into());
|
||||
}
|
||||
for child in &node.children {
|
||||
if *child <= id || *child >= fun.expressions.len() {
|
||||
return Err("invalid source expression child".into());
|
||||
}
|
||||
let c = &fun.expressions[*child];
|
||||
if c.start < node.start || c.end > node.end {
|
||||
return Err("expression child outside parent".into());
|
||||
}
|
||||
}
|
||||
let mut c = Cursor {
|
||||
fun,
|
||||
position: node.start,
|
||||
end: node.end,
|
||||
};
|
||||
let expected_children = match &node.rule {
|
||||
ExpressionRule::Literal(v) => {
|
||||
c.literal(v)?;
|
||||
0
|
||||
}
|
||||
ExpressionRule::Read(name) => {
|
||||
c.read(name)?;
|
||||
0
|
||||
}
|
||||
ExpressionRule::Unary(op) => {
|
||||
if node.children.len() != 1 {
|
||||
return Err("unary source arity".into());
|
||||
}
|
||||
c.child(node.children[0])?;
|
||||
c.op(match op {
|
||||
UnaryOp::Not => Op::Not,
|
||||
UnaryOp::Neg => Op::Negate,
|
||||
})?;
|
||||
1
|
||||
}
|
||||
ExpressionRule::Binary(op) => {
|
||||
if node.children.len() != 2 {
|
||||
return Err("binary source arity".into());
|
||||
}
|
||||
let left = &fun.expressions[node.children[0]];
|
||||
let right = &fun.expressions[node.children[1]];
|
||||
c.child(node.children[0])?;
|
||||
match op {
|
||||
BinaryOp::And => {
|
||||
c.op(Op::JumpIfFalse(right.end + 5))?;
|
||||
c.child(node.children[1])?;
|
||||
for op in [
|
||||
Op::JumpIfFalse(right.end + 3),
|
||||
Op::True,
|
||||
Op::Jump(node.end),
|
||||
Op::False,
|
||||
Op::Jump(node.end),
|
||||
Op::False,
|
||||
] {
|
||||
c.op(op)?;
|
||||
}
|
||||
}
|
||||
BinaryOp::Or => {
|
||||
c.op(Op::JumpIfFalse(right.start))?;
|
||||
c.op(Op::True)?;
|
||||
c.op(Op::Jump(node.end))?;
|
||||
c.child(node.children[1])?;
|
||||
for op in [
|
||||
Op::JumpIfFalse(right.end + 3),
|
||||
Op::True,
|
||||
Op::Jump(node.end),
|
||||
Op::False,
|
||||
] {
|
||||
c.op(op)?;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let _ = left;
|
||||
c.child(node.children[1])?;
|
||||
c.op(match op {
|
||||
BinaryOp::Add => Op::Add,
|
||||
BinaryOp::Sub => Op::Sub,
|
||||
BinaryOp::Mul => Op::Mul,
|
||||
BinaryOp::Div => Op::Div,
|
||||
BinaryOp::Eq => Op::Equal,
|
||||
BinaryOp::Ne => Op::NotEqual,
|
||||
BinaryOp::Lt => Op::Less,
|
||||
BinaryOp::Le => Op::Lessequal,
|
||||
BinaryOp::Gt => Op::Greater,
|
||||
BinaryOp::Ge => Op::GreaterEqual,
|
||||
_ => unreachable!(),
|
||||
})?;
|
||||
}
|
||||
}
|
||||
2
|
||||
}
|
||||
ExpressionRule::List(count) => {
|
||||
for child in &node.children {
|
||||
c.child(*child)?;
|
||||
}
|
||||
c.op(Op::BuildList(*count))?;
|
||||
*count
|
||||
}
|
||||
ExpressionRule::Map(keys) => {
|
||||
if node.children.len() != keys.len() {
|
||||
return Err("map source arity".into());
|
||||
}
|
||||
for (key, child) in keys.iter().zip(&node.children) {
|
||||
c.literal(&Value::Text(key.clone()))?;
|
||||
c.child(*child)?;
|
||||
}
|
||||
c.op(Op::BuildMap(keys.len()))?;
|
||||
keys.len()
|
||||
}
|
||||
ExpressionRule::Occluded(payload) => {
|
||||
if *payload {
|
||||
if node.children.len() != 1 {
|
||||
return Err("occluded source arity".into());
|
||||
}
|
||||
c.child(node.children[0])?;
|
||||
1
|
||||
} else {
|
||||
c.literal(&Value::None)?;
|
||||
0
|
||||
}
|
||||
}
|
||||
ExpressionRule::Index {
|
||||
label,
|
||||
base_variable,
|
||||
} => {
|
||||
if node.children.len() != 1 {
|
||||
return Err("index source arity".into());
|
||||
}
|
||||
c.child(node.children[0])?;
|
||||
match label {
|
||||
IndexKey::Lit(key) => c.literal(&Value::Text(key.clone()))?,
|
||||
IndexKey::Var(name) => {
|
||||
c.read(name)?;
|
||||
c.op(Op::Str)?;
|
||||
}
|
||||
}
|
||||
if let Some(name) = base_variable
|
||||
.as_ref()
|
||||
.filter(|name| fun.var_contracts.contains_key(*name))
|
||||
{
|
||||
let slot = fun
|
||||
.locals
|
||||
.iter()
|
||||
.position(|local| local == name)
|
||||
.ok_or("index source slot")?;
|
||||
c.op(Op::ContractFootnoteGet(slot))?;
|
||||
} else {
|
||||
c.op(Op::FootnoteGet)?;
|
||||
}
|
||||
1
|
||||
}
|
||||
ExpressionRule::Interpolation(parts) => {
|
||||
if parts.is_empty() {
|
||||
c.literal(&Value::Text(String::new()))?;
|
||||
}
|
||||
for (i, part) in parts.iter().enumerate() {
|
||||
match part {
|
||||
InterpPart::Lit(value) => c.literal(&Value::Text(value.clone()))?,
|
||||
InterpPart::Var(name) => c.read(name)?,
|
||||
InterpPart::Index { base, labels } => {
|
||||
c.read(base)?;
|
||||
for label in labels {
|
||||
c.literal(&Value::Text(label.clone()))?;
|
||||
c.op(Op::FootnoteGet)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
c.op(Op::Str)?;
|
||||
if i > 0 {
|
||||
c.op(Op::Add)?;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
ExpressionRule::CallFault(message) => {
|
||||
let Some(Op::RejectArgumentExpansion(index)) = fun.code.get(c.position) else {
|
||||
return Err("missing argument preparation fault".into());
|
||||
};
|
||||
if fun.constants.get(*index) != Some(&Value::Text(message.clone())) {
|
||||
return Err("argument preparation message differs".into());
|
||||
}
|
||||
c.op(Op::RejectArgumentExpansion(*index))?;
|
||||
0
|
||||
}
|
||||
ExpressionRule::CallBoundary(_) => {
|
||||
boundaries += 1;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if node.children.len() != expected_children || c.position != node.end {
|
||||
return Err("source expression coverage differs".into());
|
||||
}
|
||||
}
|
||||
Ok(boundaries)
|
||||
}
|
||||
|
||||
/// Local evidence preserves machine values, not the narrower foreign copy domain.
|
||||
pub(super) fn interpolation_evidence(part: &InterpPart) -> serde_json::Value {
|
||||
use serde_json::json;
|
||||
match part {
|
||||
InterpPart::Lit(value) => json!({"kind":"text","value":value}),
|
||||
InterpPart::Var(name) => json!({"kind":"read","name":name}),
|
||||
InterpPart::Index { base, labels } => json!({"kind":"index","base":base,"labels":labels}),
|
||||
}
|
||||
}
|
||||
pub(super) fn index_evidence(key: &IndexKey) -> serde_json::Value {
|
||||
use serde_json::json;
|
||||
match key {
|
||||
IndexKey::Lit(value) => json!({"kind":"literal","value":value}),
|
||||
IndexKey::Var(name) => json!({"kind":"read","name":name}),
|
||||
}
|
||||
}
|
||||
pub(super) fn formula_evidence(value: &crate::formula::Expr) -> serde_json::Value {
|
||||
use crate::formula::Expr::*;
|
||||
use serde_json::json;
|
||||
match value {
|
||||
Num(n) => json!({"kind":"number","bits":n.to_bits()}),
|
||||
Var(name) => json!({"kind":"variable","name":name}),
|
||||
UnaryNeg(value) => json!({"kind":"negative","value":formula_evidence(value)}),
|
||||
Bin { op, left, right } => {
|
||||
json!({"kind":"binary","op":format!("{op:?}"),"left":formula_evidence(left),"right":formula_evidence(right)})
|
||||
}
|
||||
Call { name, args } => {
|
||||
json!({"kind":"call","name":name,"args":args.iter().map(formula_evidence).collect::<Vec<_>>()})
|
||||
}
|
||||
Matrix { source, rows } => {
|
||||
json!({"kind":"matrix","source":source,"rows":rows.iter().map(|row|row.iter().map(|n|n.to_bits()).collect::<Vec<_>>()).collect::<Vec<_>>()})
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn value_evidence(value: &Value) -> serde_json::Value {
|
||||
use serde_json::json;
|
||||
match value {
|
||||
Value::None => json!({"tag":"none"}),
|
||||
Value::Bool(v) => json!({"tag":"bool","value":v}),
|
||||
Value::Int(v) => json!({"tag":"int","value":v}),
|
||||
Value::Num(v) => json!({"tag":"num","bits":v.to_bits()}),
|
||||
Value::Text(v) => json!({"tag":"text","value":v}),
|
||||
Value::Code(v) => json!({"tag":"code","lang":v.lang,"source":v.source}),
|
||||
Value::Formula(v) => json!({"tag":"formula","value":formula_evidence(v)}),
|
||||
_ => json!({"tag":"nonliteral","value":format!("{value:?}")}),
|
||||
}
|
||||
}
|
||||
impl ExpressionRule {
|
||||
pub fn evidence(&self) -> serde_json::Value {
|
||||
use serde_json::json;
|
||||
match self {
|
||||
Self::Literal(v) => json!({"kind":"literal","value":value_evidence(v)}),
|
||||
Self::Read(name) => json!({"kind":"read","name":name}),
|
||||
Self::Unary(op) => json!({"kind":"unary","op":format!("{op:?}")}),
|
||||
Self::Binary(op) => json!({"kind":"binary","op":format!("{op:?}")}),
|
||||
Self::CallBoundary(names) => json!({"kind":"call_boundary","names":names}),
|
||||
Self::CallFault(message) => json!({"kind":"call_expansion_fault","message":message}),
|
||||
Self::Interpolation(parts) => {
|
||||
json!({"kind":"interpolation_boundary","parts":parts.iter().map(interpolation_evidence).collect::<Vec<_>>()})
|
||||
}
|
||||
Self::List(n) => json!({"kind":"list","arity":n}),
|
||||
Self::Map(keys) => json!({"kind":"map","keys":keys}),
|
||||
Self::Index {
|
||||
label,
|
||||
base_variable,
|
||||
} => {
|
||||
json!({"kind":"index_boundary","label":index_evidence(label),"base_variable":base_variable})
|
||||
}
|
||||
Self::Occluded(payload) => json!({"kind":"occluded","payload":payload}),
|
||||
}
|
||||
}
|
||||
}
|
||||
+74
-13
@@ -2,6 +2,17 @@
|
||||
|
||||
mod compile;
|
||||
mod vm;
|
||||
mod verify;
|
||||
mod expression;
|
||||
mod statement;
|
||||
mod call;
|
||||
mod source;
|
||||
mod names;
|
||||
pub use source::SourceFunction;
|
||||
pub use call::{CallEmission, CallStage, CallDispatch, PathFallback};
|
||||
pub use statement::{StatementRule,StatementEmission};
|
||||
pub use expression::{ExpressionRule,ExpressionEmission,value_evidence as expression_value_evidence};
|
||||
pub use verify::StackCertificate;
|
||||
|
||||
pub use compile::{compile_for_call, compile_module};
|
||||
pub use vm::Vm;
|
||||
@@ -13,13 +24,13 @@ use crate::value::Value;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Op {
|
||||
Constant(u16),
|
||||
Constant(usize),
|
||||
True,
|
||||
False,
|
||||
None_,
|
||||
Pop,
|
||||
GetLocal(u8),
|
||||
SetLocal(u8),
|
||||
GetLocal(usize),
|
||||
SetLocal(usize),
|
||||
Add,
|
||||
Sub,
|
||||
Mul,
|
||||
@@ -33,14 +44,14 @@ pub enum Op {
|
||||
Less,
|
||||
Lessequal,
|
||||
/// Absolute jump to instruction index.
|
||||
Jump(u16),
|
||||
JumpIfFalse(u16),
|
||||
Jump(usize),
|
||||
JumpIfFalse(usize),
|
||||
Print,
|
||||
/// Pop prompt text, print it (no newline), read one stdin line → push Text.
|
||||
Input,
|
||||
BuildList(u16),
|
||||
BuildList(usize),
|
||||
/// Stack (bottom→top): k0, v0, k1, v1, … (n pairs); push Map.
|
||||
BuildMap(u16),
|
||||
BuildMap(usize),
|
||||
/// Pop value → push `len` (text/list).
|
||||
Len,
|
||||
/// Pop value → push display text.
|
||||
@@ -61,25 +72,73 @@ pub enum Op {
|
||||
/// Pop footnote label text, then base → push indexed value (1-based list / map key).
|
||||
FootnoteGet,
|
||||
/// Call function by index; argc values are on the stack (param order).
|
||||
Call(u16, u8),
|
||||
Call(usize, usize),
|
||||
/// Evaluated actuals in authored order; layout preserves optional names.
|
||||
CallArgs(usize, usize),
|
||||
/// Runtime binding fault after all explicit actual effects.
|
||||
RejectDuplicateArgument(usize),
|
||||
/// Modifier/argument preparation failed before any actual is evaluated.
|
||||
RejectArgumentExpansion(usize),
|
||||
/// Skip a callee default when the parameter was explicitly supplied.
|
||||
JumpIfProvided(usize, usize),
|
||||
/// Entry/raw-call parameters without a value retain language None after defaults.
|
||||
FillMissingParameters,
|
||||
ResetLoop(usize),
|
||||
CheckLoop(usize),
|
||||
CheckArguments,
|
||||
RestoreReceiver,
|
||||
CheckFrameArguments,
|
||||
CheckReturn,
|
||||
/// Check the stack top against the named local's field contract.
|
||||
CheckBinding(usize),
|
||||
/// Checked footnote access; operand identifies the contracted base local.
|
||||
ContractFootnoteGet(usize),
|
||||
/// Pop function name text → call zero-arg top-level or lexically visible function.
|
||||
CallFn,
|
||||
/// After a constructor Call: tag top-of-stack map with `_type` = constants[idx].
|
||||
TagInstance(u16),
|
||||
/// After a constructor call: stamp name and module of functions[idx].
|
||||
TagInstance(usize),
|
||||
/// Method call: stack has argc args (param order) then receiver on top.
|
||||
/// constants[name_idx] is method name text; resolve via receiver `_type`.
|
||||
MethodCall(u16, u8),
|
||||
MethodCall(usize, usize),
|
||||
/// Resolve the receiver first, then bind against that method's signature.
|
||||
MethodCallArgs(usize, usize),
|
||||
/// Plugin call: stack has argc args in registered param order; name in constants.
|
||||
PluginCall(u16, u8),
|
||||
PluginCall(usize, usize),
|
||||
/// Host primitive: pop `argc` args (param order), push result.
|
||||
HostCall(u16, u8),
|
||||
HostCall(u16, usize),
|
||||
/// Native dispatch with authored argument labels; binding happens at runtime.
|
||||
BuiltinCallArgs(usize, usize),
|
||||
HostCallArgs(u16, usize),
|
||||
PluginCallArgs(usize, usize),
|
||||
/// Two-component paths inspect actual receiver presence before static fallback.
|
||||
PathCallArgs(usize, usize),
|
||||
RejectCallResolution(usize),
|
||||
Return,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FnChunk {
|
||||
/// Captured directly from the loaded declaration before emission.
|
||||
pub source_ast: SourceFunction,
|
||||
pub module_scope: u32,
|
||||
pub module_path: Vec<String>,
|
||||
pub source: crate::diagnostics::SourceIdentity,
|
||||
pub declaration_span: Span,
|
||||
pub name: String,
|
||||
pub params: Vec<String>,
|
||||
pub contract: Option<crate::contract::Contract>,
|
||||
pub var_contracts: HashMap<String, crate::contract::Contract>,
|
||||
pub receiver_aliases: bool,
|
||||
pub defaults: Vec<bool>,
|
||||
pub default_expressions: Vec<Option<usize>>,
|
||||
pub call_layouts: Vec<Vec<Option<String>>>,
|
||||
pub calls: Vec<CallEmission>,
|
||||
/// Source names are reserved before synthetic slots are allocated.
|
||||
pub source_local_count: usize,
|
||||
pub expressions: Vec<ExpressionEmission>,
|
||||
pub statements: Vec<StatementEmission>,
|
||||
pub statement_roots: Vec<usize>,
|
||||
pub body_start: usize,
|
||||
pub code: Vec<Op>,
|
||||
/// Parallel to `code`: source location for diagnostics / debug.
|
||||
pub spans: Vec<Span>,
|
||||
@@ -92,6 +151,8 @@ pub struct FnChunk {
|
||||
pub struct Program {
|
||||
pub functions: Vec<FnChunk>,
|
||||
pub entry: usize,
|
||||
/// Applied by run(), not by named or nested calls to the entry function.
|
||||
pub entry_metadata: Vec<(String, Value)>,
|
||||
/// Top-level object name → function index (level-1).
|
||||
pub objects: Vec<(String, usize)>,
|
||||
/// Parallel to `objects`: base object flat index (`# Child = > Parent`).
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
//! Source-grounded declaration identities and the flat view of name resolution.
|
||||
use super::Program;
|
||||
use crate::sema::resolution::{NameView, Target};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
pub(crate) struct FlatView<'a>(pub &'a Program);
|
||||
impl NameView for FlatView<'_> {
|
||||
type Function = usize;
|
||||
type Module = Vec<String>;
|
||||
fn roots(&self, m: &Self::Module) -> Vec<usize> {
|
||||
self.0
|
||||
.functions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(i, f)| &f.module_path == m && self.0.parents[*i].is_none())
|
||||
.map(|(i, _)| i)
|
||||
.collect()
|
||||
}
|
||||
fn children(&self, f: usize) -> Vec<usize> {
|
||||
self.0.children[f].clone()
|
||||
}
|
||||
fn parent(&self, _: &Self::Module, f: usize) -> Option<usize> {
|
||||
self.0.parents[f]
|
||||
}
|
||||
fn same(&self, a: usize, b: usize) -> bool {
|
||||
a == b
|
||||
}
|
||||
fn name(&self, f: usize) -> String {
|
||||
self.0.functions[f].source_ast.name.clone()
|
||||
}
|
||||
fn level(&self, f: usize) -> u8 {
|
||||
self.0.functions[f].source_ast.level
|
||||
}
|
||||
fn is_object(&self, f: usize) -> bool {
|
||||
self.level(f) == 1
|
||||
}
|
||||
fn uses_receiver(&self, f: usize) -> bool {
|
||||
self.0.functions[f].source_ast.receiver_aliases
|
||||
}
|
||||
fn import(&self, m: &Self::Module, name: &str) -> Option<Self::Module> {
|
||||
let owner = self.0.functions.iter().find(|f| &f.module_path == m)?;
|
||||
if !owner.source_ast.imports.iter().any(|n| n == name) {
|
||||
return None;
|
||||
}
|
||||
let mut path = m.clone();
|
||||
path.push(name.into());
|
||||
Some(path)
|
||||
}
|
||||
fn used_path(&self, m: &Self::Module, name: &str) -> Option<Vec<String>> {
|
||||
self.0
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| &f.module_path == m)?
|
||||
.source_ast
|
||||
.uses
|
||||
.iter()
|
||||
.find(|(n, _)| n == name)
|
||||
.map(|(_, p)| p.clone())
|
||||
}
|
||||
}
|
||||
pub(crate) fn resolve(
|
||||
program: &Program,
|
||||
caller: usize,
|
||||
call: &crate::ast::CallExpr,
|
||||
) -> Target<usize, Vec<String>> {
|
||||
crate::sema::resolution::resolve(
|
||||
&FlatView(program),
|
||||
&program.functions[caller].module_path,
|
||||
caller,
|
||||
call,
|
||||
)
|
||||
}
|
||||
pub(crate) fn lookup(program: &Program, caller: usize, name: &str) -> Option<usize> {
|
||||
crate::sema::resolution::lookup(
|
||||
&FlatView(program),
|
||||
&program.functions[caller].module_path,
|
||||
caller,
|
||||
name,
|
||||
)
|
||||
}
|
||||
pub(crate) fn verify(program: &Program) -> Result<(), String> {
|
||||
let objects = program
|
||||
.functions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(id, f)| program.parents[*id].is_none() && f.source_ast.level == 1)
|
||||
.map(|(id, f)| (f.source_ast.name.clone(), id))
|
||||
.collect::<Vec<_>>();
|
||||
if program.objects != objects {
|
||||
return Err("object names differ from source declarations".into());
|
||||
}
|
||||
let methods = objects
|
||||
.iter()
|
||||
.flat_map(|(_, owner)| {
|
||||
program.children[*owner].iter().map(|child| {
|
||||
(
|
||||
*owner,
|
||||
program.functions[*child].source_ast.name.clone(),
|
||||
*child,
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if program.methods != methods {
|
||||
return Err("method names differ from source declarations".into());
|
||||
}
|
||||
let mut identities = HashSet::new();
|
||||
let mut scopes = HashMap::new();
|
||||
let mut namespaces = HashMap::new();
|
||||
for f in &program.functions {
|
||||
let source = &f.source_ast;
|
||||
if source.declaration_path.is_empty()
|
||||
|| !identities.insert((f.module_path.clone(), source.declaration_path.clone()))
|
||||
{
|
||||
return Err("duplicate or missing source declaration identity".into());
|
||||
}
|
||||
if scopes
|
||||
.insert(f.module_scope, f.module_path.clone())
|
||||
.is_some_and(|p| p != f.module_path)
|
||||
|| namespaces
|
||||
.insert(f.module_path.clone(), f.module_scope)
|
||||
.is_some_and(|s| s != f.module_scope)
|
||||
|| (f.module_scope == 0) != f.module_path.is_empty()
|
||||
{
|
||||
return Err("source module scope mapping differs".into());
|
||||
}
|
||||
let owner = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|g| g.module_path == f.module_path)
|
||||
.unwrap();
|
||||
if source.module_root_count != owner.source_ast.module_root_count
|
||||
|| source.imports != owner.source_ast.imports
|
||||
|| source.uses != owner.source_ast.uses
|
||||
{
|
||||
return Err("inconsistent source module header".into());
|
||||
}
|
||||
}
|
||||
for (id, f) in program.functions.iter().enumerate() {
|
||||
let source = &f.source_ast;
|
||||
let parent_path = &source.declaration_path[..source.declaration_path.len() - 1];
|
||||
match program.parents[id] {
|
||||
Some(parent)
|
||||
if program.functions[parent].source_ast.declaration_path == parent_path =>
|
||||
{
|
||||
()
|
||||
}
|
||||
None if parent_path.is_empty() => (),
|
||||
_ => return Err("flat parent differs from source declaration".into()),
|
||||
}
|
||||
if program.children[id].len() != source.child_count {
|
||||
return Err("source child inventory differs".into());
|
||||
}
|
||||
for (ordinal, child) in program.children[id].iter().enumerate() {
|
||||
let mut expected = source.declaration_path.clone();
|
||||
expected.push(ordinal);
|
||||
if program.functions[*child].source_ast.declaration_path != expected {
|
||||
return Err("source child order differs".into());
|
||||
}
|
||||
}
|
||||
let roots = FlatView(program).roots(&f.module_path);
|
||||
if roots.len() != source.module_root_count
|
||||
|| roots
|
||||
.iter()
|
||||
.enumerate()
|
||||
.any(|(i, r)| program.functions[*r].source_ast.declaration_path != [i])
|
||||
{
|
||||
return Err("source root inventory/order differs".into());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,366 @@
|
||||
//! Structural correspondence from a separately captured loaded AST to emission
|
||||
//! witnesses. Parsing and capture of the source snapshot remain trusted inputs.
|
||||
use super::{ExpressionRule, FnChunk};
|
||||
use crate::ast::{Arg, CallExpr, Expr, Function, Literal, Param, Stmt};
|
||||
use serde_json::{json, Value as Json};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SourceFunction {
|
||||
pub declaration_path: Vec<usize>,
|
||||
pub level: u8,
|
||||
pub child_count: usize,
|
||||
pub module_root_count: usize,
|
||||
pub imports: Vec<String>,
|
||||
pub uses: Vec<(String, Vec<String>)>,
|
||||
pub name: String,
|
||||
pub line: u32,
|
||||
pub params: Vec<Param>,
|
||||
pub body: Vec<Stmt>,
|
||||
pub receiver_aliases: bool,
|
||||
pub has_contract: bool,
|
||||
pub binding_contracts: Vec<String>,
|
||||
pub contract: Option<crate::contract::Contract>,
|
||||
pub var_contracts: std::collections::HashMap<String, crate::contract::Contract>,
|
||||
}
|
||||
impl SourceFunction {
|
||||
pub fn from_function(fun: &Function) -> Self {
|
||||
let mut bindings = fun.var_contracts.keys().cloned().collect::<Vec<_>>();
|
||||
bindings.sort();
|
||||
Self {
|
||||
declaration_path: Vec::new(),
|
||||
level: fun.level,
|
||||
child_count: fun.children.len(),
|
||||
module_root_count: 0,
|
||||
imports: Vec::new(),
|
||||
uses: Vec::new(),
|
||||
name: fun.name.clone(),
|
||||
line: fun.span.line,
|
||||
params: fun.params.clone(),
|
||||
body: fun.body.clone(),
|
||||
receiver_aliases: crate::ast::function_uses_receiver(fun),
|
||||
has_contract: fun.contract.is_some(),
|
||||
binding_contracts: bindings,
|
||||
contract: fun.contract.clone(),
|
||||
var_contracts: fun.var_contracts.clone(),
|
||||
}
|
||||
}
|
||||
pub fn from_declaration(module: &crate::ast::Module, fun: &Function) -> Self {
|
||||
fn locate(nodes: &[Function], target: &Function, path: &mut Vec<usize>) -> bool {
|
||||
for (index, node) in nodes.iter().enumerate() {
|
||||
path.push(index);
|
||||
if std::ptr::eq(node, target) || locate(&node.children, target, path) { return true; }
|
||||
path.pop();
|
||||
}
|
||||
false
|
||||
}
|
||||
let mut result = Self::from_function(fun);
|
||||
assert!(locate(&module.functions, fun, &mut result.declaration_path), "source declaration belongs to its module");
|
||||
result.module_root_count = module.functions.len();
|
||||
result.imports = module.import_modules.keys().cloned().collect();
|
||||
result.imports.sort();
|
||||
result.uses = module.uses.iter().map(|u| (u.bind.clone(), u.path.clone())).collect();
|
||||
result
|
||||
}
|
||||
pub fn evidence(&self) -> Result<Json, String> {
|
||||
Ok(
|
||||
json!({"declaration_path":self.declaration_path,"level":self.level,"child_count":self.child_count,"module_root_count":self.module_root_count,"imports":self.imports,"uses":self.uses,
|
||||
"name":self.name,"line":self.line,"params":self.params.iter().map(|p|Ok(json!({"name":p.name,"default":p.default.as_ref().map(expression_tree).transpose()?}))).collect::<Result<Vec<_>,String>>()?,
|
||||
"body":self.body.iter().map(statement_tree).collect::<Result<Vec<_>,_>>()?,"receiver_aliases":self.receiver_aliases,"has_contract":self.has_contract,"binding_contracts":self.binding_contracts,"contracts":Self::contract_metadata(self.contract.as_ref(),&self.var_contracts)}),
|
||||
)
|
||||
}
|
||||
pub fn contract_metadata(
|
||||
contract: Option<&crate::contract::Contract>,
|
||||
bindings: &std::collections::HashMap<String, crate::contract::Contract>,
|
||||
) -> Json {
|
||||
fn ty(ty: &crate::contract::Ty) -> Json {
|
||||
match ty {
|
||||
crate::contract::Ty::Object(name) => json!({"kind":"object","name":name}),
|
||||
other => json!({"kind":other.name()}),
|
||||
}
|
||||
}
|
||||
fn encode(c: &crate::contract::Contract) -> Json {
|
||||
json!({"params":c.params.iter().map(|p|json!({"name":p.name,"type":ty(&p.ty),"description":p.desc})).collect::<Vec<_>>(),
|
||||
"returns":c.returns.as_ref().map(|r|json!({"type":ty(&r.ty),"description":r.desc})),
|
||||
"fields":c.fields.iter().map(|f|json!({"name":f.name,"type":ty(&f.ty),"nullable":f.nullable,"description":f.desc})).collect::<Vec<_>>(),
|
||||
"start_line":c.start_line,"end_line":c.end_line,"quote":c.quote})
|
||||
}
|
||||
let bindings = bindings
|
||||
.iter()
|
||||
.map(|(name, c)| (name, encode(c)))
|
||||
.collect::<std::collections::BTreeMap<_, _>>();
|
||||
json!({"function":contract.map(encode),"bindings":bindings})
|
||||
}
|
||||
}
|
||||
fn normalized(call: &CallExpr) -> Result<CallExpr, String> {
|
||||
call.for_evaluation()
|
||||
}
|
||||
fn call_shape(call: &CallExpr) -> Json {
|
||||
json!({"callee":call.callee,"receiver":call.receiver,"path":call.path,"arguments":call.args.iter().map(|a|match a {Arg::Positional(_)=>None,Arg::Named{name,..}=>Some(name)}).collect::<Vec<_>>()})
|
||||
}
|
||||
fn arg_expr(arg: &Arg) -> &Expr {
|
||||
match arg {
|
||||
Arg::Positional(e) | Arg::Named { value: e, .. } => e,
|
||||
}
|
||||
}
|
||||
|
||||
// Deliberately match the loaded AST directly, rather than invoking the emitter's
|
||||
// ExpressionRule::from_source or StatementRule::from_source recorder.
|
||||
fn rule(expr: &Expr) -> Json {
|
||||
use crate::value::Value;
|
||||
match expr {
|
||||
Expr::Literal(lit) => {
|
||||
let value = match lit {
|
||||
Literal::None => json!({"tag":"none"}),
|
||||
Literal::Bool(v) => json!({"tag":"bool","value":v}),
|
||||
Literal::Int(v) => json!({"tag":"int","value":v}),
|
||||
Literal::Num(v) => json!({"tag":"num","bits":v.to_bits()}),
|
||||
Literal::Text(v) => json!({"tag":"text","value":v}),
|
||||
};
|
||||
json!({"kind":"literal","value":value})
|
||||
}
|
||||
Expr::Var(name) => json!({"kind":"read","name":name}),
|
||||
Expr::Unary { op, .. } => json!({"kind":"unary","op":format!("{op:?}")}),
|
||||
Expr::Binary { op, .. } => json!({"kind":"binary","op":format!("{op:?}")}),
|
||||
Expr::Call(call) => {
|
||||
if let Err(message) = normalized(call) {
|
||||
return json!({"kind":"call_expansion_fault","message":message});
|
||||
}
|
||||
json!({"kind":"call_boundary","names":call.receiver.iter().cloned().chain(call.path.iter().filter_map(|p|p.first().cloned())).collect::<Vec<_>>()})
|
||||
}
|
||||
Expr::List(items) => json!({"kind":"list","arity":items.len()}),
|
||||
Expr::Map(items) => {
|
||||
json!({"kind":"map","keys":items.iter().map(|(name,_)|name).collect::<Vec<_>>()})
|
||||
}
|
||||
Expr::Interp(parts) => {
|
||||
json!({"kind":"interpolation_boundary","parts":parts.iter().map(super::expression::interpolation_evidence).collect::<Vec<_>>()})
|
||||
}
|
||||
Expr::Index { base, label } => {
|
||||
json!({"kind":"index_boundary","label":super::expression::index_evidence(label),"base_variable":if let Expr::Var(name)=base.as_ref(){Some(name)}else{None}})
|
||||
}
|
||||
Expr::Occluded { payload, .. } => json!({"kind":"occluded","payload":payload.is_some()}),
|
||||
Expr::Formula(value) => {
|
||||
json!({"kind":"literal","value":super::expression::value_evidence(&Value::Formula(value.clone()))})
|
||||
}
|
||||
Expr::Code(value) => {
|
||||
json!({"kind":"literal","value":{"tag":"code","lang":value.lang,"source":value.source}})
|
||||
}
|
||||
}
|
||||
}
|
||||
fn children(expr: &Expr) -> Vec<&Expr> {
|
||||
match expr {
|
||||
Expr::Unary { expr, .. } => vec![expr],
|
||||
Expr::Binary { left, right, .. } => vec![left, right],
|
||||
Expr::List(items) => items.iter().collect(),
|
||||
Expr::Map(items) => items.iter().map(|(_, e)| e).collect(),
|
||||
Expr::Index { base, .. } => vec![base],
|
||||
Expr::Occluded { payload, .. } => payload.iter().map(|e| e.as_ref()).collect(),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
fn expression_tree(expr: &Expr) -> Result<Json, String> {
|
||||
if matches!(expr,Expr::Call(call) if normalized(call).is_err()) {
|
||||
return Ok(json!({"rule":rule(expr),"children":[],"call":null}));
|
||||
}
|
||||
let (children, call) = if let Expr::Call(call) = expr {
|
||||
let expanded = normalized(call)?;
|
||||
(
|
||||
expanded
|
||||
.args
|
||||
.iter()
|
||||
.map(|a| expression_tree(arg_expr(a)))
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
Some(call_shape(&expanded)),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
children(expr)
|
||||
.iter()
|
||||
.map(|e| expression_tree(e))
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
None,
|
||||
)
|
||||
};
|
||||
Ok(json!({"rule":rule(expr),"children":children,"call":call}))
|
||||
}
|
||||
fn statement_rule(stmt: &Stmt) -> Json {
|
||||
match stmt {
|
||||
Stmt::Assign { name, .. } => json!({"kind":"assign","name":name}),
|
||||
Stmt::Return { .. } => json!({"kind":"return"}),
|
||||
Stmt::Call { .. } => json!({"kind":"call"}),
|
||||
Stmt::Expr { .. } => json!({"kind":"expression"}),
|
||||
Stmt::Branch { arms, .. } => {
|
||||
json!({"kind":"branch","arms":arms.iter().map(|a|(a.condition.is_some(),a.body.len())).collect::<Vec<_>>()})
|
||||
}
|
||||
Stmt::While { body, .. } => json!({"kind":"while","body":body.len()}),
|
||||
Stmt::ForEach {
|
||||
item,
|
||||
collection,
|
||||
body,
|
||||
..
|
||||
} => json!({"kind":"foreach","item":item,"collection":collection,"body":body.len()}),
|
||||
}
|
||||
}
|
||||
fn statement_children(stmt: &Stmt) -> Vec<&Stmt> {
|
||||
match stmt {
|
||||
Stmt::Branch { arms, .. } => arms.iter().flat_map(|a| a.body.iter()).collect(),
|
||||
Stmt::While { body, .. } | Stmt::ForEach { body, .. } => body.iter().collect(),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
fn statement_expressions(stmt: &Stmt) -> Vec<Expr> {
|
||||
match stmt {
|
||||
Stmt::Assign { value, .. } | Stmt::Return { value, .. } | Stmt::Expr { value, .. } => {
|
||||
vec![value.clone()]
|
||||
}
|
||||
Stmt::Call { call, .. } => vec![Expr::Call(call.clone())],
|
||||
Stmt::Branch { arms, .. } => arms.iter().filter_map(|a| a.condition.clone()).collect(),
|
||||
Stmt::While { condition, .. } => vec![condition.clone()],
|
||||
Stmt::ForEach { collection, .. } => vec![Expr::Var(collection.clone())],
|
||||
}
|
||||
}
|
||||
fn statement_tree(stmt: &Stmt) -> Result<Json, String> {
|
||||
Ok(
|
||||
json!({"rule":statement_rule(stmt),"expressions":statement_expressions(stmt).iter().map(expression_tree).collect::<Result<Vec<_>,_>>()?,"statements":statement_children(stmt).iter().map(|s|statement_tree(s)).collect::<Result<Vec<_>,_>>()?}),
|
||||
)
|
||||
}
|
||||
|
||||
struct Walk<'a> {
|
||||
fun: &'a FnChunk,
|
||||
expressions: Vec<bool>,
|
||||
statements: Vec<bool>,
|
||||
}
|
||||
impl Walk<'_> {
|
||||
fn expression(&mut self, source: &Expr, id: usize) -> Result<(), String> {
|
||||
let node = self
|
||||
.fun
|
||||
.expressions
|
||||
.get(id)
|
||||
.ok_or("AST expression reference")?;
|
||||
if self.expressions[id] {
|
||||
return Err("AST expression has multiple owners".into());
|
||||
}
|
||||
self.expressions[id] = true;
|
||||
if node.rule.evidence() != rule(source) {
|
||||
return Err("loaded AST expression rule differs from witness".into());
|
||||
}
|
||||
if let Expr::Call(call) = source {
|
||||
let call = match normalized(call) {
|
||||
Ok(call) => call,
|
||||
Err(_) => {
|
||||
if !node.children.is_empty()
|
||||
|| self.fun.calls.iter().any(|c| c.expression == id)
|
||||
{
|
||||
return Err("AST preparation fault evaluated actuals".into());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let record = self
|
||||
.fun
|
||||
.calls
|
||||
.iter()
|
||||
.find(|c| c.expression == id)
|
||||
.ok_or("AST call obligation missing")?;
|
||||
if call_shape(&call) != call_shape(&record.source)
|
||||
|| node.children.len() < call.args.len()
|
||||
{
|
||||
return Err("loaded AST call differs from witness".into());
|
||||
}
|
||||
for (arg, child) in call.args.iter().zip(&node.children) {
|
||||
self.expression(arg_expr(arg), *child)?;
|
||||
}
|
||||
for &generated in &node.children[call.args.len()..] {
|
||||
let expr = self
|
||||
.fun
|
||||
.expressions
|
||||
.get(generated)
|
||||
.ok_or("generated reload reference")?;
|
||||
if self.expressions[generated]
|
||||
|| !expr.children.is_empty()
|
||||
|| !matches!(expr.rule, ExpressionRule::Read(_))
|
||||
{
|
||||
return Err("generated reload is shared or not a read".into());
|
||||
}
|
||||
self.expressions[generated] = true;
|
||||
}
|
||||
} else {
|
||||
let expected = children(source);
|
||||
if expected.len() != node.children.len() {
|
||||
return Err("loaded AST expression child inventory differs".into());
|
||||
}
|
||||
for (source, child) in expected.iter().zip(&node.children) {
|
||||
self.expression(source, *child)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn statement(&mut self, source: &Stmt, id: usize) -> Result<(), String> {
|
||||
let node = self
|
||||
.fun
|
||||
.statements
|
||||
.get(id)
|
||||
.ok_or("AST statement reference")?;
|
||||
if self.statements[id] {
|
||||
return Err("AST statement has multiple owners".into());
|
||||
}
|
||||
self.statements[id] = true;
|
||||
if node.rule.evidence() != statement_rule(source) {
|
||||
return Err("loaded AST statement rule differs from witness".into());
|
||||
}
|
||||
let exprs = statement_expressions(source);
|
||||
let stmts = statement_children(source);
|
||||
if node.expressions.len() != exprs.len() || node.statements.len() != stmts.len() {
|
||||
return Err("loaded AST statement child inventory differs".into());
|
||||
}
|
||||
for (expr, id) in exprs.iter().zip(&node.expressions) {
|
||||
self.expression(expr, *id)?;
|
||||
}
|
||||
for (stmt, id) in stmts.iter().zip(&node.statements) {
|
||||
self.statement(stmt, *id)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub fn verify(fun: &FnChunk) -> Result<(), String> {
|
||||
let source = &fun.source_ast;
|
||||
if source.contract != fun.contract || source.var_contracts != fun.var_contracts {
|
||||
return Err("loaded AST contract metadata differs".into());
|
||||
}
|
||||
if source.name != fun.name
|
||||
|| source.line != fun.declaration_span.line
|
||||
|| source.params.iter().map(|p| &p.name).ne(fun.params.iter())
|
||||
|| source.receiver_aliases != fun.receiver_aliases
|
||||
|| source.has_contract != fun.contract.is_some()
|
||||
|| source.binding_contracts.len() != fun.var_contracts.len()
|
||||
|| source
|
||||
.binding_contracts
|
||||
.iter()
|
||||
.any(|name| !fun.var_contracts.contains_key(name))
|
||||
{
|
||||
return Err("loaded AST function metadata differs".into());
|
||||
}
|
||||
let mut walk = Walk {
|
||||
fun,
|
||||
expressions: vec![false; fun.expressions.len()],
|
||||
statements: vec![false; fun.statements.len()],
|
||||
};
|
||||
if source.params.len() != fun.default_expressions.len()
|
||||
|| source.body.len() != fun.statement_roots.len()
|
||||
{
|
||||
return Err("loaded AST function root inventory differs".into());
|
||||
}
|
||||
for (param, id) in source.params.iter().zip(&fun.default_expressions) {
|
||||
match (¶m.default, id) {
|
||||
(Some(expr), Some(id)) => walk.expression(expr, *id)?,
|
||||
(None, None) => {}
|
||||
_ => return Err("loaded AST default presence differs".into()),
|
||||
}
|
||||
}
|
||||
for (stmt, id) in source.body.iter().zip(&fun.statement_roots) {
|
||||
walk.statement(stmt, *id)?;
|
||||
}
|
||||
if walk.expressions.iter().any(|seen| !seen) || walk.statements.iter().any(|seen| !seen) {
|
||||
return Err("emission witness has no loaded AST owner".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,365 @@
|
||||
//! Source-directed statement and control-flow templates. These checks validate
|
||||
//! emitted templates; they do not prove the parser or source-witness exporter.
|
||||
use super::expression::Cursor;
|
||||
use super::{FnChunk, Op};
|
||||
use crate::ast::Stmt;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum StatementRule {
|
||||
Assign(String),
|
||||
Return,
|
||||
Call,
|
||||
Expression,
|
||||
Branch(Vec<(bool, usize)>),
|
||||
While(usize),
|
||||
ForEach {
|
||||
item: String,
|
||||
collection: String,
|
||||
body: usize,
|
||||
},
|
||||
}
|
||||
impl StatementRule {
|
||||
pub fn from_source(stmt: &Stmt) -> Self {
|
||||
match stmt {
|
||||
Stmt::Assign { name, .. } => Self::Assign(name.clone()),
|
||||
Stmt::Return { .. } => Self::Return,
|
||||
Stmt::Call { .. } => Self::Call,
|
||||
Stmt::Expr { .. } => Self::Expression,
|
||||
Stmt::Branch { arms, .. } => Self::Branch(
|
||||
arms.iter()
|
||||
.map(|a| (a.condition.is_some(), a.body.len()))
|
||||
.collect(),
|
||||
),
|
||||
Stmt::While { body, .. } => Self::While(body.len()),
|
||||
Stmt::ForEach {
|
||||
item,
|
||||
collection,
|
||||
body,
|
||||
..
|
||||
} => Self::ForEach {
|
||||
item: item.clone(),
|
||||
collection: collection.clone(),
|
||||
body: body.len(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StatementEmission {
|
||||
pub rule: StatementRule,
|
||||
pub start: usize,
|
||||
pub end: usize,
|
||||
pub statements: Vec<usize>,
|
||||
pub expressions: Vec<usize>,
|
||||
pub auxiliary: Vec<usize>,
|
||||
}
|
||||
fn statement(c: &mut Cursor<'_>, id: usize) -> Result<(), String> {
|
||||
let child = c.fun.statements.get(id).ok_or("missing statement child")?;
|
||||
if child.start != c.position {
|
||||
return Err("source statement order/range differs".into());
|
||||
}
|
||||
c.position = child.end;
|
||||
Ok(())
|
||||
}
|
||||
pub fn verify(fun: &FnChunk) -> Result<(), String> {
|
||||
if fun.defaults.len() != fun.params.len() || fun.default_expressions.len() != fun.params.len() {
|
||||
return Err("default expression inventory".into());
|
||||
}
|
||||
let mut prefix = 0;
|
||||
for (parameter, default) in fun.default_expressions.iter().enumerate() {
|
||||
if fun.defaults[parameter] != default.is_some() {
|
||||
return Err("default source presence differs".into());
|
||||
}
|
||||
if let Some(id) = default {
|
||||
let expr = fun
|
||||
.expressions
|
||||
.get(*id)
|
||||
.ok_or("default source expression missing")?;
|
||||
if expr.start != prefix + 1
|
||||
|| fun.code.get(prefix) != Some(&Op::JumpIfProvided(parameter, expr.end + 2))
|
||||
|| fun.code.get(expr.end..expr.end + 2)
|
||||
!= Some([Op::SetLocal(parameter), Op::Pop].as_slice())
|
||||
{
|
||||
return Err("source default prologue differs".into());
|
||||
}
|
||||
prefix = expr.end + 2;
|
||||
}
|
||||
}
|
||||
if fun.code.get(prefix) != Some(&Op::FillMissingParameters) {
|
||||
return Err("missing parameter-fill boundary".into());
|
||||
}
|
||||
prefix += 1;
|
||||
if fun.contract.is_some() {
|
||||
if fun.code.get(prefix) != Some(&Op::CheckArguments) {
|
||||
return Err("missing input contract boundary".into());
|
||||
}
|
||||
prefix += 1;
|
||||
}
|
||||
if fun.receiver_aliases {
|
||||
if fun.code.get(prefix)!=Some(&Op::RestoreReceiver){return Err("missing receiver restoration".into());}prefix+=1;
|
||||
if fun.contract.is_some(){if fun.code.get(prefix)!=Some(&Op::CheckFrameArguments){return Err("missing frame contract boundary".into());}prefix+=1;}
|
||||
}
|
||||
if prefix != fun.body_start {
|
||||
return Err("function prologue/body boundary differs".into());
|
||||
}
|
||||
let mut protected = fun
|
||||
.params
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect::<std::collections::HashSet<_>>();
|
||||
protected.extend(["self".to_string(), "自".to_string()]);
|
||||
for expression in &fun.expressions {
|
||||
use super::ExpressionRule;
|
||||
match &expression.rule {
|
||||
ExpressionRule::Read(name) => {
|
||||
protected.insert(name.clone());
|
||||
}
|
||||
ExpressionRule::CallBoundary(names) => protected.extend(names.iter().cloned()),
|
||||
ExpressionRule::Interpolation(parts) => {
|
||||
for part in parts {
|
||||
match part {
|
||||
crate::ast::InterpPart::Var(name)
|
||||
| crate::ast::InterpPart::Index { base: name, .. } => {
|
||||
protected.insert(name.clone());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
ExpressionRule::Index {
|
||||
label,
|
||||
base_variable,
|
||||
} => {
|
||||
if let Some(name) = base_variable {
|
||||
protected.insert(name.clone());
|
||||
}
|
||||
if let crate::ast::IndexKey::Var(name) = label {
|
||||
protected.insert(name.clone());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
for statement in &fun.statements {
|
||||
match &statement.rule {
|
||||
StatementRule::Assign(name) => {
|
||||
protected.insert(name.clone());
|
||||
}
|
||||
StatementRule::ForEach {
|
||||
item, collection, ..
|
||||
} => {
|
||||
protected.insert(item.clone());
|
||||
protected.insert(collection.clone());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut auxiliary_slots = std::collections::HashSet::new();
|
||||
let mut parent_counts = vec![0usize; fun.statements.len()];
|
||||
for (id, node) in fun.statements.iter().enumerate() {
|
||||
if node.start > node.end || node.end > fun.code.len() {
|
||||
return Err("invalid statement interval".into());
|
||||
}
|
||||
for &child in &node.statements {
|
||||
if child <= id || child >= fun.statements.len() {
|
||||
return Err("invalid statement child".into());
|
||||
}
|
||||
parent_counts[child] += 1;
|
||||
let child = &fun.statements[child];
|
||||
if child.start < node.start || child.end > node.end {
|
||||
return Err("statement child outside parent".into());
|
||||
}
|
||||
}
|
||||
for &expr in &node.expressions {
|
||||
let expr = fun
|
||||
.expressions
|
||||
.get(expr)
|
||||
.ok_or("invalid statement expression")?;
|
||||
if expr.start < node.start || expr.end > node.end {
|
||||
return Err("expression outside statement".into());
|
||||
}
|
||||
}
|
||||
let mut c = Cursor {
|
||||
fun,
|
||||
position: node.start,
|
||||
end: node.end,
|
||||
};
|
||||
match &node.rule {
|
||||
StatementRule::Assign(_)
|
||||
| StatementRule::Return
|
||||
| StatementRule::Call
|
||||
| StatementRule::Expression => {
|
||||
if !node.statements.is_empty() || node.expressions.len() != 1 {
|
||||
return Err("statement source arity".into());
|
||||
}
|
||||
c.child(node.expressions[0])?;
|
||||
match &node.rule {
|
||||
StatementRule::Assign(name) => {
|
||||
let slot = fun
|
||||
.locals
|
||||
.iter()
|
||||
.position(|n| n == name)
|
||||
.ok_or("assignment slot")?;
|
||||
if fun.var_contracts.contains_key(name) {
|
||||
c.op(Op::CheckBinding(slot))?;
|
||||
}
|
||||
c.op(Op::SetLocal(slot))?;
|
||||
c.op(Op::Pop)?;
|
||||
}
|
||||
StatementRule::Return => {
|
||||
if fun.contract.is_some() {
|
||||
c.op(Op::CheckReturn)?;
|
||||
}
|
||||
c.op(Op::Return)?;
|
||||
}
|
||||
StatementRule::Call | StatementRule::Expression => c.op(Op::Pop)?,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
StatementRule::While(count) => {
|
||||
if node.expressions.len() != 1 || node.statements.len() != *count {
|
||||
return Err("while source arity".into());
|
||||
}
|
||||
c.op(Op::ResetLoop(id))?;
|
||||
let head = c.position;
|
||||
c.child(node.expressions[0])?;
|
||||
c.op(Op::JumpIfFalse(node.end))?;
|
||||
c.op(Op::CheckLoop(id))?;
|
||||
for &child in &node.statements {
|
||||
statement(&mut c, child)?;
|
||||
}
|
||||
c.op(Op::Jump(head))?;
|
||||
}
|
||||
StatementRule::Branch(arms) => {
|
||||
if node.expressions.len() != arms.iter().filter(|(condition, _)| *condition).count()
|
||||
|| node.statements.len() != arms.iter().map(|(_, n)| n).sum::<usize>()
|
||||
{
|
||||
return Err("branch source arity".into());
|
||||
}
|
||||
let (mut expr, mut stmt) = (0, 0);
|
||||
for (index, (condition, count)) in arms.iter().enumerate() {
|
||||
let guard = if *condition {
|
||||
c.child(node.expressions[expr])?;
|
||||
expr += 1;
|
||||
let pc = c.position;
|
||||
c.position += 1;
|
||||
Some(pc)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
for _ in 0..*count {
|
||||
statement(&mut c, node.statements[stmt])?;
|
||||
stmt += 1;
|
||||
}
|
||||
if index + 1 < arms.len() {
|
||||
c.op(Op::Jump(node.end))?;
|
||||
}
|
||||
if let Some(guard) = guard {
|
||||
if fun.code.get(guard) != Some(&Op::JumpIfFalse(c.position)) {
|
||||
return Err("source branch false target differs".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
StatementRule::ForEach {
|
||||
item,
|
||||
collection,
|
||||
body,
|
||||
} => {
|
||||
if node.expressions.len() != 1
|
||||
|| node.statements.len() != *body
|
||||
|| node.auxiliary.len() != 2
|
||||
{
|
||||
return Err("foreach source arity".into());
|
||||
}
|
||||
let (coll, index) = (node.auxiliary[0], node.auxiliary[1]);
|
||||
if coll == index || coll >= fun.locals.len() || index >= fun.locals.len() {
|
||||
return Err("foreach auxiliary slots".into());
|
||||
}
|
||||
for slot in [coll, index] {
|
||||
if protected.contains(&fun.locals[slot]) || !auxiliary_slots.insert(slot) {
|
||||
return Err("loop temporary aliases source or another loop".into());
|
||||
}
|
||||
}
|
||||
// The initial expression must read the authored collection.
|
||||
if !matches!(&fun.expressions[node.expressions[0]].rule,super::ExpressionRule::Read(name) if name==collection)
|
||||
{
|
||||
return Err("foreach source collection differs".into());
|
||||
}
|
||||
c.child(node.expressions[0])?;
|
||||
c.op(Op::IterItems)?;
|
||||
c.op(Op::SetLocal(coll))?;
|
||||
c.op(Op::Pop)?;
|
||||
c.literal(&crate::value::Value::Int(0))?;
|
||||
c.op(Op::SetLocal(index))?;
|
||||
c.op(Op::Pop)?;
|
||||
let head = c.position;
|
||||
for op in [
|
||||
Op::GetLocal(index),
|
||||
Op::GetLocal(coll),
|
||||
Op::Len,
|
||||
Op::Less,
|
||||
Op::JumpIfFalse(node.end),
|
||||
Op::GetLocal(coll),
|
||||
Op::GetLocal(index),
|
||||
Op::GetIndex,
|
||||
] {
|
||||
c.op(op)?;
|
||||
}
|
||||
let item = fun
|
||||
.locals
|
||||
.iter()
|
||||
.position(|n| n == item)
|
||||
.ok_or("foreach item slot")?;
|
||||
c.op(Op::SetLocal(item))?;
|
||||
c.op(Op::Pop)?;
|
||||
for &child in &node.statements {
|
||||
statement(&mut c, child)?;
|
||||
}
|
||||
c.op(Op::GetLocal(index))?;
|
||||
c.literal(&crate::value::Value::Int(1))?;
|
||||
for op in [Op::Add, Op::SetLocal(index), Op::Pop, Op::Jump(head)] {
|
||||
c.op(op)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
if c.position != node.end {
|
||||
return Err("statement instruction coverage differs".into());
|
||||
}
|
||||
}
|
||||
let mut cursor = fun.body_start;
|
||||
for &root in &fun.statement_roots {
|
||||
let node = fun.statements.get(root).ok_or("statement root missing")?;
|
||||
if parent_counts[root] != 0 || node.start != cursor {
|
||||
return Err("source body root order differs".into());
|
||||
}
|
||||
parent_counts[root] += 1;
|
||||
cursor = node.end;
|
||||
}
|
||||
if parent_counts.iter().any(|n| *n != 1) {
|
||||
return Err("statement forest coverage differs".into());
|
||||
}
|
||||
if fun.code.get(cursor..) != Some([Op::None_, Op::Return].as_slice()) {
|
||||
return Err("implicit function return differs".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl StatementRule {
|
||||
pub fn evidence(&self) -> serde_json::Value {
|
||||
use serde_json::json;
|
||||
match self {
|
||||
Self::Assign(name) => json!({"kind":"assign","name":name}),
|
||||
Self::Return => json!({"kind":"return"}),
|
||||
Self::Call => json!({"kind":"call"}),
|
||||
Self::Expression => json!({"kind":"expression"}),
|
||||
Self::Branch(arms) => json!({"kind":"branch","arms":arms}),
|
||||
Self::While(body) => json!({"kind":"while","body":body}),
|
||||
Self::ForEach {
|
||||
item,
|
||||
collection,
|
||||
body,
|
||||
} => json!({"kind":"foreach","item":item,"collection":collection,"body":body}),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
//! Checks every emitted instruction and a control-flow stack-height certificate.
|
||||
//! This is structural validation, not source/bytecode semantic equivalence.
|
||||
use super::{FnChunk, Op, Program};
|
||||
use crate::value::Value;
|
||||
use std::collections::{HashSet, VecDeque};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StackCertificate {
|
||||
pub heights: Vec<Option<usize>>,
|
||||
}
|
||||
|
||||
impl Program {
|
||||
pub fn verify(&self) -> Result<Vec<StackCertificate>, String> {
|
||||
let n = self.functions.len();
|
||||
if self.entry >= n || self.parents.len() != n || self.children.len() != n {
|
||||
return Err("invalid program function tables".into());
|
||||
}
|
||||
for (i, parent) in self.parents.iter().enumerate() {
|
||||
if parent.is_some_and(|p| p >= n || p == i) {
|
||||
return Err("invalid function parent".into());
|
||||
}
|
||||
for &child in &self.children[i] {
|
||||
if child >= n || self.parents[child] != Some(i) {
|
||||
return Err("inconsistent function child".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
check_forest(&self.parents)?;
|
||||
for (child, parent) in self.parents.iter().enumerate() {
|
||||
if let Some(parent) = parent {
|
||||
if !self.children[*parent].contains(&child)
|
||||
|| self.functions[*parent].module_path != self.functions[child].module_path
|
||||
{
|
||||
return Err("inconsistent parent owner".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
super::names::verify(self)?;
|
||||
if self.objects.len() != self.object_bases.len() {
|
||||
return Err("invalid object tables".into());
|
||||
}
|
||||
for (i, (_, fid)) in self.objects.iter().enumerate() {
|
||||
if *fid >= n
|
||||
|| self.object_bases[i]
|
||||
.is_some_and(|base| base >= n || !self.objects.iter().any(|(_, f)| *f == base))
|
||||
{
|
||||
return Err("invalid object reference".into());
|
||||
}
|
||||
}
|
||||
if self.methods.iter().any(|(owner, _, fid)| {
|
||||
*owner >= n || *fid >= n || !self.objects.iter().any(|(_, f)| f == owner)
|
||||
}) || self.path_index.values().any(|id| *id >= n)
|
||||
{
|
||||
return Err("invalid method/export reference".into());
|
||||
}
|
||||
let object_ids = self
|
||||
.objects
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, (_, fid))| (*fid, i))
|
||||
.collect::<std::collections::HashMap<_, _>>();
|
||||
if object_ids.len() != self.objects.len() {
|
||||
return Err("duplicate object reference".into());
|
||||
}
|
||||
let bases = self
|
||||
.object_bases
|
||||
.iter()
|
||||
.map(|base| base.map(|fid| object_ids[&fid]))
|
||||
.collect::<Vec<_>>();
|
||||
check_forest(&bases)?;
|
||||
if self
|
||||
.methods
|
||||
.iter()
|
||||
.any(|(owner, _, fid)| self.parents[*fid] != Some(*owner))
|
||||
{
|
||||
return Err("inconsistent method owner".into());
|
||||
}
|
||||
self.functions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, f)| {
|
||||
verify_chunk(self, i, f)
|
||||
.map_err(|e| format!("bytecode verification in {}: {e}", f.name))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn verify_chunk(program: &Program, _fid: usize, fun: &FnChunk) -> Result<StackCertificate, String> {
|
||||
super::expression::verify(fun)?;
|
||||
super::statement::verify(fun)?;
|
||||
super::call::verify(program,fun)?;
|
||||
super::source::verify(fun)?;
|
||||
let size = fun.code.len();
|
||||
if size == 0 || fun.spans.len() != size {
|
||||
return Err("invalid code/span table".into());
|
||||
}
|
||||
if fun.defaults.len() != fun.params.len()
|
||||
|| fun.locals.get(..fun.params.len()) != Some(fun.params.as_slice())
|
||||
{
|
||||
return Err("parameters must be a local-slot prefix".into());
|
||||
}
|
||||
if fun.locals.iter().collect::<HashSet<_>>().len() != fun.locals.len() {
|
||||
return Err("duplicate local slot name".into());
|
||||
}
|
||||
let local = |i: usize| {
|
||||
if i < fun.locals.len() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("invalid local reference".to_string())
|
||||
}
|
||||
};
|
||||
let target = |i: usize| {
|
||||
if i < size {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("invalid jump target".to_string())
|
||||
}
|
||||
};
|
||||
let function = |i: usize| {
|
||||
if i < program.functions.len() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("invalid call target".to_string())
|
||||
}
|
||||
};
|
||||
let text = |i: usize| {
|
||||
if matches!(fun.constants.get(i), Some(Value::Text(_))) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("invalid text constant reference".to_string())
|
||||
}
|
||||
};
|
||||
let mut shapes = Vec::with_capacity(size);
|
||||
for (ip, op) in fun.code.iter().copied().enumerate() {
|
||||
// pop count, push count, successor PCs. All opcodes are handled.
|
||||
let next = vec![ip + 1];
|
||||
let shape = match op {
|
||||
Op::Constant(i) => {
|
||||
if i >= fun.constants.len() {
|
||||
return Err("invalid constant reference".into());
|
||||
}
|
||||
(0, 1, next)
|
||||
}
|
||||
Op::True | Op::False | Op::None_ => (0, 1, next),
|
||||
Op::GetLocal(i) => {
|
||||
local(i)?;
|
||||
(0, 1, next)
|
||||
}
|
||||
Op::SetLocal(i) | Op::CheckBinding(i) => {
|
||||
local(i)?;
|
||||
(1, 1, next)
|
||||
}
|
||||
Op::Pop | Op::Print => (1, 0, next),
|
||||
Op::Add
|
||||
| Op::Sub
|
||||
| Op::Mul
|
||||
| Op::Div
|
||||
| Op::Equal
|
||||
| Op::NotEqual
|
||||
| Op::Greater
|
||||
| Op::GreaterEqual
|
||||
| Op::Less
|
||||
| Op::Lessequal
|
||||
| Op::Split
|
||||
| Op::Join
|
||||
| Op::GetIndex
|
||||
| Op::FootnoteGet => (2, 1, next),
|
||||
Op::ContractFootnoteGet(i) => {
|
||||
local(i)?;
|
||||
(2, 1, next)
|
||||
}
|
||||
Op::Negate
|
||||
| Op::Not
|
||||
| Op::Input
|
||||
| Op::Len
|
||||
| Op::Str
|
||||
| Op::Int
|
||||
| Op::TypeOf
|
||||
| Op::Trim
|
||||
| Op::IterItems
|
||||
| Op::CallFn => (1, 1, next),
|
||||
Op::BuildList(n) => (n, 1, next),
|
||||
Op::BuildMap(n) => (n.checked_mul(2).ok_or("map arity overflow")?, 1, next),
|
||||
Op::Jump(t) => {
|
||||
target(t)?;
|
||||
(0, 0, vec![t])
|
||||
}
|
||||
Op::JumpIfFalse(t) => {
|
||||
target(t)?;
|
||||
(1, 0, vec![ip + 1, t])
|
||||
}
|
||||
Op::JumpIfProvided(p, t) => {
|
||||
target(t)?;
|
||||
if p >= fun.params.len() {
|
||||
return Err("invalid presence reference".into());
|
||||
}
|
||||
(0, 0, vec![ip + 1, t])
|
||||
}
|
||||
Op::ResetLoop(id)|Op::CheckLoop(id)=>{
|
||||
if !matches!(fun.statements.get(id).map(|s|&s.rule),Some(super::StatementRule::While(_))){return Err("invalid source loop reference".into());}
|
||||
(0,0,next)
|
||||
},
|
||||
Op::FillMissingParameters | Op::CheckArguments | Op::RestoreReceiver | Op::CheckFrameArguments => (0, 0, next),
|
||||
Op::CheckReturn => (1, 1, next),
|
||||
Op::Call(f, n) => {
|
||||
function(f)?;
|
||||
(n, 1, next)
|
||||
}
|
||||
Op::CallArgs(f, l) => {
|
||||
function(f)?;
|
||||
let layout = fun.call_layouts.get(l).ok_or("invalid call layout")?;
|
||||
(layout.len(), 1, next)
|
||||
}
|
||||
Op::BuiltinCallArgs(name,l)|Op::PluginCallArgs(name,l)=>{
|
||||
text(name)?;
|
||||
if matches!(op,Op::BuiltinCallArgs(_,_)) && !matches!(&fun.constants[name],Value::Text(name) if crate::arguments::builtin_signature(name).is_some()){return Err("unknown builtin signature".into());}
|
||||
let layout=fun.call_layouts.get(l).ok_or("invalid call layout")?;(layout.len(),1,next)
|
||||
},
|
||||
Op::HostCallArgs(id,l)=>{
|
||||
if crate::host::HostFn::from_u16(id).is_none(){return Err("unknown host id".into());}
|
||||
let layout=fun.call_layouts.get(l).ok_or("invalid call layout")?;(layout.len(),1,next)
|
||||
},
|
||||
Op::PathCallArgs(id,l)=>{
|
||||
if !matches!(fun.calls.get(id).and_then(|c|c.dispatch.as_ref()),Some(super::CallDispatch::Path{..})){return Err("invalid path call record".into());}
|
||||
let layout=fun.call_layouts.get(l).ok_or("invalid path call layout")?;(layout.len(),1,next)
|
||||
},
|
||||
Op::RejectCallResolution(index)=>{text(index)?;(0,0,vec![])},
|
||||
Op::MethodCall(name, n) => {
|
||||
text(name)?;
|
||||
(n.checked_add(1).ok_or("method arity overflow")?, 1, next)
|
||||
}
|
||||
Op::MethodCallArgs(name, l) => {
|
||||
text(name)?;
|
||||
let layout = fun.call_layouts.get(l).ok_or("invalid method layout")?;
|
||||
(
|
||||
layout.len().checked_add(1).ok_or("method arity overflow")?,
|
||||
1,
|
||||
next,
|
||||
)
|
||||
}
|
||||
Op::PluginCall(name, n) => {
|
||||
text(name)?;
|
||||
if let Value::Text(s) = &fun.constants[name] {
|
||||
if s.starts_with('@') && n % 2 != 0 {
|
||||
return Err("odd named plugin arity".into());
|
||||
}
|
||||
}
|
||||
(n, 1, next)
|
||||
}
|
||||
Op::HostCall(id, n) => {
|
||||
if crate::host::HostFn::from_u16(id).is_none() {
|
||||
return Err("invalid host reference".into());
|
||||
}
|
||||
(n, 1, next)
|
||||
}
|
||||
Op::TagInstance(f) => {
|
||||
function(f)?;
|
||||
if !program.objects.iter().any(|(_, id)| *id == f) {
|
||||
return Err("invalid constructor reference".into());
|
||||
}
|
||||
(1, 1, next)
|
||||
}
|
||||
Op::RejectDuplicateArgument(name) | Op::RejectArgumentExpansion(name) => {
|
||||
text(name)?;
|
||||
(0, 0, vec![])
|
||||
}
|
||||
Op::Return => (1, 0, vec![]),
|
||||
};
|
||||
shapes.push(shape);
|
||||
}
|
||||
let mut heights = vec![None; size];
|
||||
heights[0] = Some(0);
|
||||
let mut queue = VecDeque::from([0]);
|
||||
while let Some(ip) = queue.pop_front() {
|
||||
let height = heights[ip].unwrap();
|
||||
let (pop, push, edges) = &shapes[ip];
|
||||
if height < *pop {
|
||||
return Err(format!("stack underflow at {ip}"));
|
||||
}
|
||||
if matches!(fun.code[ip], Op::Return) && height != 1 {
|
||||
return Err(format!("return leaks stack values at {ip}"));
|
||||
}
|
||||
let after = (height - pop)
|
||||
.checked_add(*push)
|
||||
.ok_or("stack height overflow")?;
|
||||
for &to in edges {
|
||||
target(to)?;
|
||||
match heights[to] {
|
||||
Some(known) if known != after => {
|
||||
return Err(format!("inconsistent stack join at {to}"))
|
||||
}
|
||||
Some(_) => {}
|
||||
None => {
|
||||
heights[to] = Some(after);
|
||||
queue.push_back(to);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(StackCertificate { heights })
|
||||
}
|
||||
|
||||
fn check_forest(parents: &[Option<usize>]) -> Result<(), String> {
|
||||
let mut colors = vec![0u8; parents.len()];
|
||||
for start in 0..parents.len() {
|
||||
if colors[start] != 0 {
|
||||
continue;
|
||||
}
|
||||
let mut cursor = Some(start);
|
||||
let mut path = Vec::new();
|
||||
while let Some(node) = cursor {
|
||||
if node >= parents.len() {
|
||||
return Err("invalid forest reference".into());
|
||||
}
|
||||
if colors[node] == 1 {
|
||||
return Err("cyclic bytecode declaration graph".into());
|
||||
}
|
||||
if colors[node] == 2 {
|
||||
break;
|
||||
}
|
||||
colors[node] = 1;
|
||||
path.push(node);
|
||||
cursor = parents[node];
|
||||
}
|
||||
for node in path {
|
||||
colors[node] = 2;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
+417
-288
@@ -5,14 +5,13 @@ use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::builtin::{
|
||||
builtin_at, builtin_footnote_get, builtin_int, builtin_iter_items, builtin_join,
|
||||
builtin_len, builtin_split, builtin_str, builtin_trim,
|
||||
builtin_type,
|
||||
builtin_at, builtin_footnote_get, builtin_int, builtin_iter_items, builtin_join, builtin_len,
|
||||
builtin_split, builtin_str, builtin_trim, builtin_type,
|
||||
};
|
||||
use crate::host::{call_host, HostContext, HostFn};
|
||||
use crate::bytecode::{Op, Program};
|
||||
use crate::bytecode::{FnChunk, Op, Program};
|
||||
use crate::debug::emit_trace;
|
||||
use crate::diagnostics::{bail_at, Span};
|
||||
use crate::diagnostics::{Diagnostic, DiagnosticFrame, SourceIdentity, Span};
|
||||
use crate::host::{call_host, HostContext, HostFn};
|
||||
use crate::input_feed::InputFeed;
|
||||
use crate::value::Value;
|
||||
use std::collections::HashMap;
|
||||
@@ -24,14 +23,32 @@ pub struct Vm {
|
||||
input: InputFeed,
|
||||
trace: bool,
|
||||
host: HostContext,
|
||||
current_source: SourceIdentity,
|
||||
current_scope: Option<u32>,
|
||||
diagnostic_frames: Vec<DiagnosticFrame>,
|
||||
}
|
||||
|
||||
struct Frame {
|
||||
fn_idx: usize,
|
||||
ip: usize,
|
||||
slots: Vec<Value>,
|
||||
slots: Vec<Option<Value>>,
|
||||
provided: Vec<bool>,
|
||||
loop_counts: HashMap<usize,u32>,
|
||||
receiver: Option<Value>,
|
||||
defaults_active: bool,
|
||||
/// When set, retag matrix-like return to this `_type` (method chaining).
|
||||
retag_matrix: Option<String>,
|
||||
retag_matrix: Option<(String, Option<Vec<String>>)>,
|
||||
has_call_site: bool,
|
||||
retag_instance: Option<usize>,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
fn read(&self, fun: &FnChunk, index: usize) -> Option<Value> {
|
||||
let name=&fun.locals[index];
|
||||
let overlay=self.defaults_active && self.receiver.is_some() && (name=="self" || name=="自") &&
|
||||
(index>=fun.params.len() || self.provided.get(index).copied().unwrap_or(false) || self.slots[index].is_none());
|
||||
if overlay {self.receiver.clone()} else {self.slots[index].clone()}
|
||||
}
|
||||
}
|
||||
|
||||
impl Vm {
|
||||
@@ -43,6 +60,12 @@ impl Vm {
|
||||
input: InputFeed::new(false, Vec::new()),
|
||||
trace: false,
|
||||
host: HostContext::for_run(path, Default::default(), Vec::new()),
|
||||
current_source: SourceIdentity {
|
||||
path: path.map(Path::to_path_buf),
|
||||
sha256: None,
|
||||
},
|
||||
current_scope: None,
|
||||
diagnostic_frames: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +77,12 @@ impl Vm {
|
||||
input: InputFeed::new(true, Vec::new()),
|
||||
trace: false,
|
||||
host: HostContext::for_capture(path, Default::default()),
|
||||
current_source: SourceIdentity {
|
||||
path: path.map(Path::to_path_buf),
|
||||
sha256: None,
|
||||
},
|
||||
current_scope: None,
|
||||
diagnostic_frames: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +105,50 @@ impl Vm {
|
||||
std::mem::take(&mut self.host.plots)
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "exec-host"))]
|
||||
pub(crate) fn foreign_source_evidence(
|
||||
&mut self,
|
||||
) -> (
|
||||
Vec<Value>,
|
||||
Vec<Value>,
|
||||
Vec<crate::host::foreign_session::ProtocolStep>,
|
||||
) {
|
||||
crate::source_refinement_tests::boundary_evidence(&mut self.host)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn diagnostic_scope_state(
|
||||
&self,
|
||||
) -> (SourceIdentity, usize, Vec<String>, Vec<u32>, Option<u32>) {
|
||||
(
|
||||
self.current_source.clone(),
|
||||
self.diagnostic_frames.len(),
|
||||
self.host.call_stack.clone(),
|
||||
self.host.call_site_lines.clone(),
|
||||
self.current_scope,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "exec-host"))]
|
||||
pub(crate) fn causal_replay_evidence(&mut self) -> serde_json::Value {
|
||||
crate::causal_replay_tests::evidence(&mut self.host)
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "exec-host"))]
|
||||
pub(crate) fn application_evidence(
|
||||
&self,
|
||||
) -> (
|
||||
Vec<Value>,
|
||||
Vec<crate::host::foreign_session::ProtocolStep>,
|
||||
usize,
|
||||
) {
|
||||
(
|
||||
self.host.oracle_observations.clone(),
|
||||
self.host.foreign_protocol_steps.clone(),
|
||||
self.host.foreign_sessions.len(),
|
||||
)
|
||||
}
|
||||
|
||||
fn emit_line(&mut self, text: &str) {
|
||||
if self.capture {
|
||||
self.captured_stdout.push_str(text);
|
||||
@@ -98,21 +171,34 @@ impl Vm {
|
||||
}
|
||||
|
||||
fn err_at(&self, span: Span, message: impl Into<String>) -> anyhow::Error {
|
||||
bail_at(self.path.as_deref(), span, message)
|
||||
Diagnostic::new(self.current_source.path.as_deref(), span, message)
|
||||
.with_source(&self.current_source)
|
||||
.with_frames(&self.diagnostic_frames)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn push_source_frame(&mut self, callee: &FnChunk, call_span: Option<Span>) {
|
||||
let mut source = callee.source.clone();
|
||||
if source.path.is_none() {
|
||||
if self.diagnostic_frames.is_empty() {
|
||||
source.path = self.path.clone();
|
||||
} else if self.current_scope == Some(callee.module_scope) {
|
||||
source.path = self.current_source.path.clone();
|
||||
}
|
||||
}
|
||||
self.diagnostic_frames.push(DiagnosticFrame {
|
||||
function: callee.name.clone(),
|
||||
declaration: source.at(callee.declaration_span),
|
||||
call_site: call_span.map(|span| self.current_source.at(span)),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn run(&mut self, program: &Program) -> Result<Value> {
|
||||
let entry = program.entry;
|
||||
let arity = program.functions[entry].params.len();
|
||||
if arity != 0 {
|
||||
let span = program.functions[entry]
|
||||
.spans
|
||||
.first()
|
||||
.copied()
|
||||
.unwrap_or(Span::new(1, 1));
|
||||
return Err(self.err_at(span, "bytecode: entry function needs arguments"));
|
||||
}
|
||||
self.execute(program, entry, &[])
|
||||
// Entry execution, like Interpreter::run_function, seeds unbound locals
|
||||
// with None. Inferred prose parameters do not require a program caller.
|
||||
let mut seen=std::collections::HashSet::new();
|
||||
let metadata=program.entry_metadata.iter().filter(|(name,_)|seen.insert(name.clone())).cloned().collect::<Vec<_>>();
|
||||
self.execute(program, program.entry, &metadata)
|
||||
}
|
||||
|
||||
/// Run `fn_idx` with named arguments already bound to local slots.
|
||||
@@ -137,9 +223,35 @@ impl Vm {
|
||||
program: &Program,
|
||||
fn_idx: usize,
|
||||
args: &[(String, Value)],
|
||||
) -> Result<Value> {
|
||||
program.verify().map_err(|error|self.err_at(Span::new(1,1),error))?;
|
||||
let previous_source = self.current_source.clone();
|
||||
let previous_scope = self.current_scope;
|
||||
let previous_depth = self.diagnostic_frames.len();
|
||||
let previous_calls = self.host.call_stack.clone();
|
||||
let previous_sites = self.host.call_site_lines.clone();
|
||||
let previous_line = self.host.current_line;
|
||||
// Scope numbers belong to one compiled program; never carry them into
|
||||
// a new external invocation, even when it preserves an outer prefix.
|
||||
self.current_scope = None;
|
||||
let result = self.execute_inner(program, fn_idx, args);
|
||||
self.diagnostic_frames.truncate(previous_depth);
|
||||
self.current_source = previous_source;
|
||||
self.current_scope = previous_scope;
|
||||
self.host.call_stack = previous_calls;
|
||||
self.host.call_site_lines = previous_sites;
|
||||
self.host.current_line = previous_line;
|
||||
result
|
||||
}
|
||||
|
||||
fn execute_inner(
|
||||
&mut self,
|
||||
program: &Program,
|
||||
fn_idx: usize,
|
||||
args: &[(String, Value)],
|
||||
) -> Result<Value> {
|
||||
let mut stack: Vec<Value> = Vec::new();
|
||||
let mut slots = vec![Value::None; program.functions[fn_idx].locals.len().max(1)];
|
||||
let mut slots = vec![None; program.functions[fn_idx].locals.len().max(1)];
|
||||
for (name, value) in args {
|
||||
if let Some(i) = program.functions[fn_idx]
|
||||
.locals
|
||||
@@ -147,45 +259,89 @@ impl Vm {
|
||||
.position(|local| local == name)
|
||||
{
|
||||
if i < slots.len() {
|
||||
slots[i] = value.clone();
|
||||
slots[i] = Some(value.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut frames = vec![Frame {
|
||||
fn_idx,
|
||||
loop_counts: HashMap::new(),
|
||||
receiver: None, defaults_active: true,
|
||||
ip: 0,
|
||||
provided: initial_presence(&slots,program.functions[fn_idx].params.len()),
|
||||
slots,
|
||||
retag_matrix: None,
|
||||
has_call_site: false,
|
||||
retag_instance: None,
|
||||
}];
|
||||
self.push_source_frame(&program.functions[fn_idx], None);
|
||||
self.host
|
||||
.push_call_frame(&program.functions[fn_idx].name.clone());
|
||||
|
||||
let mut source_owner = None;
|
||||
loop {
|
||||
let frame = frames.last_mut().unwrap();
|
||||
let fun = &program.functions[frame.fn_idx];
|
||||
if source_owner != Some(frame.fn_idx) {
|
||||
self.current_source = self
|
||||
.diagnostic_frames
|
||||
.last()
|
||||
.expect("active source frame")
|
||||
.declaration
|
||||
.source
|
||||
.clone();
|
||||
self.current_scope = Some(fun.module_scope);
|
||||
source_owner = Some(frame.fn_idx);
|
||||
}
|
||||
if frame.ip >= fun.code.len() {
|
||||
let span = fun.spans.last().copied().unwrap_or(Span::new(1, 1));
|
||||
return Err(self.err_at(span, format!("bytecode: ip out of range in {}", fun.name)));
|
||||
}
|
||||
let ip = frame.ip;
|
||||
let op = fun.code[ip];
|
||||
let mut op = fun.code[ip];
|
||||
let span = fun.spans.get(ip).copied().unwrap_or(Span::new(1, 1));
|
||||
frame.ip += 1;
|
||||
self.host.current_line = span.line;
|
||||
|
||||
let mut print_returns_none=false;
|
||||
let mut method_name_override=None;
|
||||
let mut constructor_tag=None;
|
||||
if let Op::PathCallArgs(owner,layout)=op {
|
||||
let call=&fun.calls[owner];
|
||||
let Some(super::CallDispatch::Path{receiver,check_receiver,fallback})=&call.dispatch else{unreachable!("verified path dispatch")};
|
||||
let value=if *check_receiver {fun.locals.iter().position(|n|n==receiver).and_then(|slot|frame.read(fun,slot))}else{None};
|
||||
if let Some(value)=value {
|
||||
stack.push(value);method_name_override=Some(call.source.callee.clone());op=Op::MethodCallArgs(0,layout);
|
||||
} else {match fallback {
|
||||
super::PathFallback::User{target,tag_instance}=>{op=Op::CallArgs(*target,layout);if *tag_instance{constructor_tag=Some(*target);}},
|
||||
super::PathFallback::Error{message}=>return Err(self.err_at(span,message)),
|
||||
}}
|
||||
}
|
||||
if let Op::BuiltinCallArgs(name,layout)=op {
|
||||
let name=fun.constants[name].as_display();let names=&fun.call_layouts[layout];
|
||||
let values=take_actuals(&mut stack,names.len()).map_err(|m|self.err_at(span,m))?;
|
||||
let (params,allow_missing)=crate::arguments::builtin_signature(&name).ok_or_else(||self.err_at(span,"unknown builtin signature"))?;
|
||||
let parameters=params.iter().map(|p|p.to_string()).collect::<Vec<_>>();
|
||||
let bound=crate::arguments::bind(¶meters,names,&values,allow_missing).map_err(|m|self.err_at(span,m))?;
|
||||
for param in params {
|
||||
stack.push(bound.get(*param).cloned().unwrap_or_else(||Value::Text(String::new())));
|
||||
}
|
||||
op=match name.as_str(){"print"=>{print_returns_none=true;Op::Print},"input"=>Op::Input,"len"=>Op::Len,"str"=>Op::Str,"int"=>Op::Int,"type"=>Op::TypeOf,"trim"=>Op::Trim,"split"=>Op::Split,"join"=>Op::Join,"at"=>Op::GetIndex,"call_fn"=>Op::CallFn,_=>unreachable!()};
|
||||
}
|
||||
|
||||
if self.trace {
|
||||
match op {
|
||||
Op::Print | Op::Input | Op::Call(_, _) | Op::Return => {
|
||||
Op::Print | Op::Input | Op::Call(_, _) | Op::CallArgs(_, _) | Op::Return => {
|
||||
let kind = match op {
|
||||
Op::Print => "print",
|
||||
Op::Input => "input",
|
||||
Op::Call(_, _) => "call",
|
||||
Op::Call(_, _) | Op::CallArgs(_, _) => "call",
|
||||
Op::Return => "return",
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let ip_s = ip.to_string();
|
||||
emit_trace(
|
||||
self.path.as_deref(),
|
||||
self.current_source.path.as_deref(),
|
||||
Some(span),
|
||||
"op",
|
||||
&[
|
||||
@@ -200,6 +356,61 @@ impl Vm {
|
||||
}
|
||||
|
||||
match op {
|
||||
Op::CheckArguments => {
|
||||
if let Some(contract)=&fun.contract {
|
||||
let bound=fun.locals.iter().zip(&frame.slots).filter_map(|(name,value)|
|
||||
value.clone().map(|value|(name.clone(),value))).collect();
|
||||
// Internal call errors identify the authored call site, like Tree.
|
||||
let site=self.diagnostic_frames.last().and_then(|f|f.call_site.as_ref());
|
||||
let call_site=site.cloned().unwrap_or_else(||self.current_source.at(span));
|
||||
if let Some(diag)=crate::contract::call_arg_diag_in(contract,&fun.name,&bound,&call_site,&fun.source) {
|
||||
let depth=self.diagnostic_frames.len()-usize::from(site.is_some());
|
||||
return Err(diag.with_frames(&self.diagnostic_frames[..depth]).into());
|
||||
}
|
||||
}
|
||||
}
|
||||
Op::CheckReturn => {
|
||||
if let Some(contract)=&fun.contract {
|
||||
let value=stack.last().cloned().unwrap_or(Value::None);
|
||||
if let Some(diag)=crate::contract::return_diag(contract,&fun.name,&value,self.current_source.path.as_deref(),span) {
|
||||
return Err(diag.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
Op::CheckBinding(slot) => {
|
||||
let name=&fun.locals[slot];
|
||||
if let Some(contract)=fun.var_contracts.get(name) {
|
||||
let value=stack.last().cloned().unwrap_or(Value::None);
|
||||
if let Some(diag)=crate::contract::fields_diag(contract,name,&value,self.current_source.path.as_deref(),span) {
|
||||
return Err(diag.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
Op::RestoreReceiver => {
|
||||
if let Some(receiver)=&frame.receiver {
|
||||
for (index,name) in fun.locals.iter().enumerate() {
|
||||
if name=="self" || name=="自" {frame.slots[index]=Some(receiver.clone());}
|
||||
}
|
||||
}
|
||||
frame.defaults_active=false;
|
||||
}
|
||||
Op::CheckFrameArguments => {
|
||||
if let Some(contract)=&fun.contract {
|
||||
let bound=fun.locals.iter().zip(&frame.slots).filter_map(|(name,value)|value.clone().map(|value|(name.clone(),value))).collect();
|
||||
if let Some(diag)=crate::contract::call_arg_diag(contract,&fun.name,&bound,self.current_source.path.as_deref(),fun.declaration_span){return Err(diag.with_source(&self.current_source).with_frames(&self.diagnostic_frames).into());}
|
||||
}
|
||||
}
|
||||
Op::ResetLoop(id) => {frame.loop_counts.insert(id,0);}
|
||||
Op::CheckLoop(id) => {
|
||||
let counter=frame.loop_counts.get_mut(&id).ok_or_else(||self.err_at(span,"uninitialized loop counter"))?;
|
||||
if *counter>=1_000_000 {return Err(self.err_at(span,"while loop exceeded iteration limit"));}
|
||||
*counter+=1;
|
||||
}
|
||||
Op::FillMissingParameters => {
|
||||
for index in 0..fun.params.len() {
|
||||
if frame.slots[index].is_none() { frame.slots[index]=Some(Value::None); }
|
||||
}
|
||||
}
|
||||
Op::Constant(i) => stack.push(fun.constants[i as usize].clone()),
|
||||
Op::True => stack.push(Value::Bool(true)),
|
||||
Op::False => stack.push(Value::Bool(false)),
|
||||
@@ -208,7 +419,10 @@ impl Vm {
|
||||
stack.pop();
|
||||
}
|
||||
Op::GetLocal(i) => {
|
||||
let v = frames.last().unwrap().slots[i as usize].clone();
|
||||
let current=frames.last().unwrap();
|
||||
let index=i as usize;let name=&fun.locals[index];
|
||||
let value=current.read(fun,index);
|
||||
let v=value.ok_or_else(||self.err_at(span,format!("undefined variable `{name}`")))?;
|
||||
stack.push(v);
|
||||
}
|
||||
Op::SetLocal(i) => {
|
||||
@@ -216,130 +430,32 @@ impl Vm {
|
||||
let slot = i as usize;
|
||||
let fr = frames.last_mut().unwrap();
|
||||
if fr.slots.len() <= slot {
|
||||
fr.slots.resize(slot + 1, Value::None);
|
||||
fr.slots.resize(slot + 1, None);
|
||||
}
|
||||
fr.slots[slot] = v;
|
||||
fr.slots[slot] = Some(v);
|
||||
}
|
||||
Op::Add => {
|
||||
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
if let Some(v) = crate::linalg_ops::try_matrix_binary(
|
||||
&mut self.host,
|
||||
crate::ast::BinaryOp::Add,
|
||||
&a,
|
||||
&b,
|
||||
)
|
||||
.map_err(|m| self.err_at(span, m))?
|
||||
{
|
||||
stack.push(v);
|
||||
} else {
|
||||
stack.push(add(a, b).map_err(|m| self.err_at(span, m))?);
|
||||
}
|
||||
Op::Add | Op::Sub | Op::Mul | Op::Div | Op::Equal | Op::NotEqual |
|
||||
Op::Greater | Op::GreaterEqual | Op::Less | Op::Lessequal => {
|
||||
use crate::ast::BinaryOp;
|
||||
let operation=match op {
|
||||
Op::Add=>BinaryOp::Add,Op::Sub=>BinaryOp::Sub,Op::Mul=>BinaryOp::Mul,Op::Div=>BinaryOp::Div,
|
||||
Op::Equal=>BinaryOp::Eq,Op::NotEqual=>BinaryOp::Ne,Op::Greater=>BinaryOp::Gt,
|
||||
Op::GreaterEqual=>BinaryOp::Ge,Op::Less=>BinaryOp::Lt,Op::Lessequal=>BinaryOp::Le,
|
||||
_=>unreachable!(),
|
||||
};
|
||||
let right=pop(&mut stack).map_err(|m|self.err_at(span,m))?;
|
||||
let left=pop(&mut stack).map_err(|m|self.err_at(span,m))?;
|
||||
let value=if let Some(value)=crate::linalg_ops::try_matrix_binary(&mut self.host,operation,&left,&right)
|
||||
.map_err(|m|self.err_at(span,m))? {value} else {
|
||||
crate::builtin::eval_binary(operation,&left,&right).map_err(|m|self.err_at(span,m))?
|
||||
};
|
||||
stack.push(value);
|
||||
}
|
||||
Op::Sub => {
|
||||
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
if let Some(v) = crate::linalg_ops::try_matrix_binary(
|
||||
&mut self.host,
|
||||
crate::ast::BinaryOp::Sub,
|
||||
&a,
|
||||
&b,
|
||||
)
|
||||
.map_err(|m| self.err_at(span, m))?
|
||||
{
|
||||
stack.push(v);
|
||||
} else {
|
||||
match (a, b) {
|
||||
(Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x - y)),
|
||||
(Value::Num(x), Value::Num(y)) => stack.push(Value::Num(x - y)),
|
||||
(Value::Int(x), Value::Num(y)) => {
|
||||
stack.push(Value::Num(x as f64 - y))
|
||||
}
|
||||
(Value::Num(x), Value::Int(y)) => {
|
||||
stack.push(Value::Num(x - y as f64))
|
||||
}
|
||||
_ => return Err(self.err_at(span, "`-` needs ints or nums")),
|
||||
}
|
||||
}
|
||||
Op::Negate | Op::Not => {
|
||||
let value=pop(&mut stack).map_err(|m|self.err_at(span,m))?;
|
||||
let operation=if matches!(op,Op::Negate){crate::ast::UnaryOp::Neg}else{crate::ast::UnaryOp::Not};
|
||||
stack.push(crate::builtin::eval_unary(operation,&value).map_err(|m|self.err_at(span,m))?);
|
||||
}
|
||||
Op::Mul => {
|
||||
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
if let Some(v) = crate::linalg_ops::try_matrix_binary(
|
||||
&mut self.host,
|
||||
crate::ast::BinaryOp::Mul,
|
||||
&a,
|
||||
&b,
|
||||
)
|
||||
.map_err(|m| self.err_at(span, m))?
|
||||
{
|
||||
stack.push(v);
|
||||
} else {
|
||||
match (a, b) {
|
||||
(Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x * y)),
|
||||
(Value::Num(x), Value::Num(y)) => stack.push(Value::Num(x * y)),
|
||||
(Value::Int(x), Value::Num(y)) => {
|
||||
stack.push(Value::Num(x as f64 * y))
|
||||
}
|
||||
(Value::Num(x), Value::Int(y)) => {
|
||||
stack.push(Value::Num(x * y as f64))
|
||||
}
|
||||
_ => return Err(self.err_at(span, "`*` needs ints or nums")),
|
||||
}
|
||||
}
|
||||
}
|
||||
Op::Div => {
|
||||
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
match (a, b) {
|
||||
(Value::Int(_), Value::Int(0)) => {
|
||||
return Err(self.err_at(span, "division by zero"));
|
||||
}
|
||||
(Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x / y)),
|
||||
(Value::Num(_), Value::Num(y)) if y == 0.0 => {
|
||||
return Err(self.err_at(span, "division by zero"));
|
||||
}
|
||||
(Value::Int(_), Value::Num(y)) if y == 0.0 => {
|
||||
return Err(self.err_at(span, "division by zero"));
|
||||
}
|
||||
(Value::Num(_), Value::Int(0)) => {
|
||||
return Err(self.err_at(span, "division by zero"));
|
||||
}
|
||||
(Value::Num(x), Value::Num(y)) => stack.push(Value::Num(x / y)),
|
||||
(Value::Int(x), Value::Num(y)) => {
|
||||
stack.push(Value::Num(x as f64 / y))
|
||||
}
|
||||
(Value::Num(x), Value::Int(y)) => {
|
||||
stack.push(Value::Num(x / y as f64))
|
||||
}
|
||||
_ => return Err(self.err_at(span, "`/` needs ints or nums")),
|
||||
}
|
||||
}
|
||||
Op::Negate => match pop(&mut stack).map_err(|m| self.err_at(span, m))? {
|
||||
Value::Int(n) => stack.push(Value::Int(-n)),
|
||||
Value::Num(n) => stack.push(Value::Num(-n)),
|
||||
_ => return Err(self.err_at(span, "unary `-` needs int or num")),
|
||||
},
|
||||
Op::Not => {
|
||||
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
stack.push(Value::Bool(!v.truthy()));
|
||||
}
|
||||
Op::Equal => {
|
||||
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
stack.push(Value::Bool(a == b));
|
||||
}
|
||||
Op::NotEqual => {
|
||||
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
stack.push(Value::Bool(a != b));
|
||||
}
|
||||
Op::Greater => cmp(&mut stack, |o| o.is_gt()).map_err(|m| self.err_at(span, m))?,
|
||||
Op::GreaterEqual => {
|
||||
cmp(&mut stack, |o| o.is_ge()).map_err(|m| self.err_at(span, m))?
|
||||
}
|
||||
Op::Less => cmp(&mut stack, |o| o.is_lt()).map_err(|m| self.err_at(span, m))?,
|
||||
Op::Lessequal => cmp(&mut stack, |o| o.is_le()).map_err(|m| self.err_at(span, m))?,
|
||||
Op::Jump(t) => {
|
||||
frames.last_mut().unwrap().ip = t as usize;
|
||||
}
|
||||
@@ -444,7 +560,7 @@ impl Vm {
|
||||
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
stack.push(builtin_iter_items(&v).map_err(|m| self.err_at(span, m))?);
|
||||
}
|
||||
Op::FootnoteGet => {
|
||||
Op::FootnoteGet | Op::ContractFootnoteGet(_) => {
|
||||
let label = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let base = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let label = match label {
|
||||
@@ -453,10 +569,27 @@ impl Vm {
|
||||
return Err(self.err_at(span, "footnote label must be text"));
|
||||
}
|
||||
};
|
||||
stack.push(
|
||||
builtin_footnote_get(&base, &label).map_err(|m| self.err_at(span, m))?,
|
||||
);
|
||||
let contract=if let Op::ContractFootnoteGet(slot)=op {
|
||||
let name=&fun.locals[slot];fun.var_contracts.get(name).map(|c|(name,c))
|
||||
} else {None};
|
||||
if let Some((name,c))=contract {
|
||||
if let Some(diag)=crate::contract::index_diag(c,name,&label,&Value::None,self.current_source.path.as_deref(),span) {return Err(diag.into());}
|
||||
}
|
||||
let value=builtin_footnote_get(&base,&label).map_err(|m|self.err_at(span,m))?;
|
||||
if let Some((name,c))=contract {
|
||||
if let Some(diag)=crate::contract::index_diag(c,name,&label,&value,self.current_source.path.as_deref(),span) {return Err(diag.into());}
|
||||
}
|
||||
stack.push(value);
|
||||
}
|
||||
Op::HostCallArgs(id, layout) => {
|
||||
let hf=HostFn::from_u16(id).ok_or_else(||self.err_at(span,"unknown host id"))?;
|
||||
let names=&fun.call_layouts[layout];let values=take_actuals(&mut stack,names.len()).map_err(|m|self.err_at(span,m))?;
|
||||
let params=hf.all_params().iter().map(|p|p.to_string()).collect::<Vec<_>>();
|
||||
let bound=crate::arguments::bind(¶ms,names,&values,true).map_err(|m|self.err_at(span,m))?;
|
||||
for req in hf.required_params(){if !bound.contains_key(*req){return Err(self.err_at(span,format!("{} requires `{req}`",hf.name())));}}
|
||||
stack.push(call_host(&mut self.host,hf,&bound).map_err(|m|self.err_at(span,m))?);
|
||||
}
|
||||
Op::BuiltinCallArgs(_,_)=>unreachable!("builtin dispatch was prepared above"),
|
||||
Op::HostCall(id, argc) => {
|
||||
let hf = HostFn::from_u16(id)
|
||||
.ok_or_else(|| self.err_at(span, format!("unknown host id {id}")))?;
|
||||
@@ -476,71 +609,105 @@ impl Vm {
|
||||
}
|
||||
for req in hf.required_params() {
|
||||
if !bound.contains_key(*req) {
|
||||
return Err(self.err_at(
|
||||
span,
|
||||
format!("{} requires `{req}`", hf.name()),
|
||||
));
|
||||
return Err(
|
||||
self.err_at(span, format!("{} requires `{req}`", hf.name()))
|
||||
);
|
||||
}
|
||||
}
|
||||
let result =
|
||||
call_host(&mut self.host, hf, &bound).map_err(|m| self.err_at(span, m))?;
|
||||
stack.push(result);
|
||||
}
|
||||
Op::Call(fid, argc) => {
|
||||
Op::RejectDuplicateArgument(index) => {
|
||||
let name=fun.constants[index].as_display();
|
||||
return Err(self.err_at(span,format!("duplicate named argument `{name}`")));
|
||||
}
|
||||
Op::RejectArgumentExpansion(index) => {
|
||||
return Err(self.err_at(span,fun.constants[index].as_display()));
|
||||
}
|
||||
Op::RejectCallResolution(index)=>return Err(self.err_at(span,fun.constants[index].as_display())),
|
||||
Op::PathCallArgs(_,_)=>unreachable!("path dispatch was selected above"),
|
||||
Op::JumpIfProvided(index, target) => {
|
||||
if frame.provided.get(index as usize).copied().unwrap_or(false) {
|
||||
frame.ip = target as usize;
|
||||
}
|
||||
}
|
||||
Op::Call(_, _) | Op::CallArgs(_, _) => {
|
||||
let (fid, argc) = match op {
|
||||
Op::Call(fid, argc) => (fid, argc as usize),
|
||||
Op::CallArgs(fid, layout) => (fid, layout),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let fid = fid as usize;
|
||||
let argc = argc as usize;
|
||||
let callee = &program.functions[fid];
|
||||
if callee.params.len() != argc {
|
||||
return Err(self.err_at(span, "call argument count mismatch"));
|
||||
}
|
||||
let mut slots = vec![Value::None; callee.locals.len().max(argc).max(1)];
|
||||
for i in (0..argc).rev() {
|
||||
slots[i] = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
}
|
||||
let layout = if matches!(op, Op::CallArgs(_, _)) {
|
||||
fun.call_layouts[argc as usize].clone()
|
||||
} else { vec![None; argc as usize] };
|
||||
let mut values = Vec::new();
|
||||
for _ in &layout { values.push(pop(&mut stack).map_err(|m| self.err_at(span, m))?); }
|
||||
values.reverse();
|
||||
let (slots, provided) = bind_call_values(callee, &layout, values).map_err(|m| self.err_at(span, m))?;
|
||||
let fname = program.functions[fid].name.clone();
|
||||
frames.push(Frame {
|
||||
fn_idx: fid,
|
||||
loop_counts: HashMap::new(),
|
||||
receiver: None, defaults_active: true,
|
||||
ip: 0,
|
||||
provided,
|
||||
slots,
|
||||
retag_matrix: None,
|
||||
has_call_site: true,
|
||||
retag_instance: constructor_tag,
|
||||
});
|
||||
self.push_source_frame(callee, Some(span));
|
||||
self.host.push_call_frame(&fname);
|
||||
self.host.push_call_site_line(span.line);
|
||||
}
|
||||
Op::TagInstance(idx) => {
|
||||
let type_name = match fun.constants.get(idx as usize) {
|
||||
Some(Value::Text(s)) => s.clone(),
|
||||
_ => {
|
||||
return Err(self.err_at(span, "TagInstance needs type name constant"));
|
||||
}
|
||||
};
|
||||
let class = program.functions.get(idx).ok_or_else(|| self.err_at(span,"TagInstance needs an object function"))?;
|
||||
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
stack.push(tag_instance_value(&type_name, v));
|
||||
stack.push(crate::value::tag_instance(&class.name, &class.module_path, v));
|
||||
}
|
||||
Op::MethodCall(name_idx, argc) => {
|
||||
let argc = argc as usize;
|
||||
let method_name = match fun.constants.get(name_idx as usize) {
|
||||
Op::MethodCall(_, _) | Op::MethodCallArgs(_, _) => {
|
||||
let (name_idx, argc) = match op {
|
||||
Op::MethodCall(name, argc) => (name, argc as usize),
|
||||
Op::MethodCallArgs(name, layout) => (name, layout),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let layout = if matches!(op, Op::MethodCallArgs(_, _)) {
|
||||
fun.call_layouts[argc as usize].clone()
|
||||
} else { vec![None; argc as usize] };
|
||||
let argc = layout.len();
|
||||
let method_name = if let Some(name)=method_name_override {name} else {match fun.constants.get(name_idx as usize) {
|
||||
Some(Value::Text(s)) => s.clone(),
|
||||
_ => {
|
||||
return Err(self.err_at(span, "MethodCall needs method name constant"));
|
||||
}
|
||||
};
|
||||
}};
|
||||
let recv = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let mut vals = Vec::with_capacity(argc);
|
||||
for _ in 0..argc {
|
||||
vals.push(pop(&mut stack).map_err(|m| self.err_at(span, m))?);
|
||||
}
|
||||
vals.reverse();
|
||||
let type_name = instance_type_name_bc(&recv).map_err(|m| self.err_at(span, m))?;
|
||||
let start_oi = program
|
||||
.objects
|
||||
.iter()
|
||||
.position(|(n, _)| n == &type_name)
|
||||
let type_name =
|
||||
instance_type_name_bc(&recv).map_err(|m| self.err_at(span, m))?;
|
||||
let namespace=crate::value::instance_module(&recv).map_err(|m| self.err_at(span,m))?;
|
||||
let start_oi = if let Some(namespace)=&namespace {
|
||||
program.objects.iter().position(|(name,id)| name==&type_name && &program.functions[*id].module_path==namespace)
|
||||
} else {
|
||||
let local=program.objects.iter().position(|(name,id)| name==&type_name && program.functions[*id].module_scope==fun.module_scope);
|
||||
if local.is_some() { local } else {
|
||||
let candidates=program.objects.iter().enumerate().filter(|(_, (name,_))| name==&type_name).map(|(index,_)|index).collect::<Vec<_>>();
|
||||
if candidates.len()>1 { return Err(self.err_at(span,format!("ambiguous object type `{type_name}`: a module tag is required"))); }
|
||||
candidates.first().copied()
|
||||
}
|
||||
}
|
||||
.ok_or_else(|| {
|
||||
self.err_at(span, format!("unknown object type `{type_name}`"))
|
||||
})?;
|
||||
let method_fid = resolve_method_fid(program, start_oi, &method_name).ok_or_else(
|
||||
|| {
|
||||
let method_fid = resolve_method_fid(program, start_oi, &method_name)
|
||||
.ok_or_else(|| {
|
||||
let mut chain = Vec::new();
|
||||
let mut oi = Some(start_oi);
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
@@ -562,36 +729,37 @@ impl Vm {
|
||||
chain.join(" → ")
|
||||
),
|
||||
)
|
||||
},
|
||||
)?;
|
||||
})?;
|
||||
let callee = &program.functions[method_fid];
|
||||
if callee.params.len() != argc {
|
||||
return Err(self.err_at(span, "method argument count mismatch"));
|
||||
}
|
||||
let mut slots = vec![Value::None; callee.locals.len().max(1)];
|
||||
for (i, v) in vals.into_iter().enumerate() {
|
||||
slots[i] = v;
|
||||
}
|
||||
for (i, name) in callee.locals.iter().enumerate() {
|
||||
if name == "自" || name == "self" {
|
||||
slots[i] = recv.clone();
|
||||
}
|
||||
}
|
||||
let (slots, provided) = bind_call_values(callee, &layout, vals).map_err(|m| self.err_at(span, m))?;
|
||||
let retag_matrix = if crate::linalg_ops::is_matrix_value(&recv) {
|
||||
Some(type_name.clone())
|
||||
Some((type_name.clone(), namespace))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let fname = callee.name.clone();
|
||||
frames.push(Frame {
|
||||
fn_idx: method_fid,
|
||||
loop_counts: HashMap::new(),
|
||||
receiver: Some(recv.clone()), defaults_active: true,
|
||||
ip: 0,
|
||||
provided,
|
||||
slots,
|
||||
retag_matrix,
|
||||
has_call_site: true,
|
||||
retag_instance: None,
|
||||
});
|
||||
self.push_source_frame(callee, Some(span));
|
||||
self.host.push_call_frame(&fname);
|
||||
self.host.push_call_site_line(span.line);
|
||||
}
|
||||
Op::PluginCallArgs(name_idx,layout) => {
|
||||
let name=fun.constants[name_idx].as_display();let names=&fun.call_layouts[layout];
|
||||
let values=take_actuals(&mut stack,names.len()).map_err(|m|self.err_at(span,m))?;
|
||||
let reg=self.host.plugins.get(&name).cloned().ok_or_else(||self.err_at(span,format!("unknown function `{name}`")))?;
|
||||
let bound=crate::arguments::bind(®.params,names,&values,false).map_err(|m|self.err_at(span,m))?;
|
||||
stack.push(crate::host::plugin::call_registered(&mut self.host,&name,&bound).map_err(|m|self.err_at(span,m))?);
|
||||
}
|
||||
Op::PluginCall(name_idx, argc) => {
|
||||
let argc = argc as usize;
|
||||
let raw_name = match fun.constants.get(name_idx as usize) {
|
||||
@@ -628,14 +796,9 @@ impl Vm {
|
||||
vals.push(pop(&mut stack).map_err(|m| self.err_at(span, m))?);
|
||||
}
|
||||
vals.reverse();
|
||||
let reg = self
|
||||
.host
|
||||
.plugins
|
||||
.get(&raw_name)
|
||||
.cloned()
|
||||
.ok_or_else(|| {
|
||||
self.err_at(span, format!("unknown function `{raw_name}`"))
|
||||
})?;
|
||||
let reg = self.host.plugins.get(&raw_name).cloned().ok_or_else(|| {
|
||||
self.err_at(span, format!("unknown function `{raw_name}`"))
|
||||
})?;
|
||||
if reg.params.len() != vals.len() {
|
||||
return Err(self.err_at(
|
||||
span,
|
||||
@@ -666,41 +829,60 @@ impl Vm {
|
||||
.map_err(|m| self.err_at(span, m))?;
|
||||
let callee = &program.functions[fid];
|
||||
if !callee.params.is_empty() {
|
||||
return Err(self.err_at(
|
||||
span,
|
||||
format!("call_fn: `{name}` must have no parameters"),
|
||||
));
|
||||
return Err(
|
||||
self.err_at(span, format!("call_fn: `{name}` must have no parameters"))
|
||||
);
|
||||
}
|
||||
let slots = vec![Value::None; callee.locals.len().max(1)];
|
||||
let slots = vec![None; callee.locals.len().max(1)];
|
||||
let fname = callee.name.clone();
|
||||
frames.push(Frame {
|
||||
fn_idx: fid,
|
||||
loop_counts: HashMap::new(),
|
||||
receiver: None, defaults_active: true,
|
||||
ip: 0,
|
||||
provided: Vec::new(),
|
||||
slots,
|
||||
retag_matrix: None,
|
||||
has_call_site: true,
|
||||
retag_instance: None,
|
||||
});
|
||||
self.push_source_frame(callee, Some(span));
|
||||
self.host.push_call_frame(&fname);
|
||||
self.host.push_call_site_line(span.line);
|
||||
}
|
||||
Op::Return => {
|
||||
let ret = pop(&mut stack).unwrap_or(Value::None);
|
||||
let frame = frames.pop();
|
||||
let constructor=frame.as_ref().and_then(|f|f.retag_instance);
|
||||
self.diagnostic_frames.pop();
|
||||
self.host.pop_call_frame();
|
||||
self.host.pop_call_site_line();
|
||||
if frame.as_ref().is_some_and(|f| f.has_call_site) {
|
||||
self.host.pop_call_site_line();
|
||||
}
|
||||
let ret = match frame.and_then(|f| f.retag_matrix) {
|
||||
Some(ty) => crate::linalg_ops::retag_matrix_like(&ty, ret),
|
||||
Some((ty,namespace)) => {
|
||||
let result=crate::linalg_ops::retag_matrix_like(&ty,ret);
|
||||
if let Some(namespace)=namespace { crate::value::tag_instance(&ty,&namespace,result) } else { result }
|
||||
},
|
||||
None => ret,
|
||||
};
|
||||
let ret=if let Some(id)=constructor {let class=&program.functions[id];crate::value::tag_instance(&class.name,&class.module_path,ret)}else{ret};
|
||||
if frames.is_empty() {
|
||||
return Ok(ret);
|
||||
}
|
||||
stack.push(ret);
|
||||
}
|
||||
}
|
||||
if print_returns_none{stack.push(Value::None);}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn take_actuals(stack:&mut Vec<Value>,count:usize)->Result<Vec<Value>,String>{
|
||||
if stack.len()<count{return Err("bytecode argument stack underflow".into());}
|
||||
Ok(stack.split_off(stack.len()-count))
|
||||
}
|
||||
|
||||
fn resolve_method_fid(program: &Program, start_oi: usize, method_name: &str) -> Option<usize> {
|
||||
let mut oi = Some(start_oi);
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
@@ -716,9 +898,12 @@ fn resolve_method_fid(program: &Program, start_oi: usize, method_name: &str) ->
|
||||
{
|
||||
return Some(*fid);
|
||||
}
|
||||
oi = program.object_bases.get(i).copied().flatten().and_then(|base_flat| {
|
||||
program.objects.iter().position(|(_, f)| *f == base_flat)
|
||||
});
|
||||
oi = program
|
||||
.object_bases
|
||||
.get(i)
|
||||
.copied()
|
||||
.flatten()
|
||||
.and_then(|base_flat| program.objects.iter().position(|(_, f)| *f == base_flat));
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -729,44 +914,6 @@ fn pop(stack: &mut Vec<Value>) -> Result<Value, String> {
|
||||
.ok_or_else(|| "bytecode: stack underflow".to_string())
|
||||
}
|
||||
|
||||
fn add(a: Value, b: Value) -> Result<Value, String> {
|
||||
match (a, b) {
|
||||
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x + y)),
|
||||
(Value::Num(x), Value::Num(y)) => Ok(Value::Num(x + y)),
|
||||
(Value::Int(x), Value::Num(y)) => Ok(Value::Num(x as f64 + y)),
|
||||
(Value::Num(x), Value::Int(y)) => Ok(Value::Num(x + y as f64)),
|
||||
(Value::Text(x), Value::Text(y)) => Ok(Value::Text(format!("{x}{y}"))),
|
||||
(Value::Text(x), Value::Secret(y)) => Ok(Value::Text(format!("{x}{y}"))),
|
||||
(Value::Secret(x), Value::Text(y)) => Ok(Value::Text(format!("{x}{y}"))),
|
||||
(Value::Secret(x), Value::Secret(y)) => Ok(Value::Text(format!("{x}{y}"))),
|
||||
(Value::Text(x), y) => Ok(Value::Text(format!("{x}{}", y.as_display()))),
|
||||
(x, Value::Text(y)) => Ok(Value::Text(format!("{}{y}", x.as_display()))),
|
||||
(Value::Int(x), y) => Ok(Value::Text(format!("{}{}", x, y.as_display()))),
|
||||
(x, Value::Int(y)) => Ok(Value::Text(format!("{}{y}", x.as_display()))),
|
||||
_ => Err("`+` needs ints, nums, or text".into()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn cmp(stack: &mut Vec<Value>, pred: impl Fn(std::cmp::Ordering) -> bool) -> Result<(), String> {
|
||||
let b = pop(stack)?;
|
||||
let a = pop(stack)?;
|
||||
let ord = match (&a, &b) {
|
||||
(Value::Int(x), Value::Int(y)) => x.cmp(y),
|
||||
(Value::Num(x), Value::Num(y)) => x.partial_cmp(y).unwrap_or(std::cmp::Ordering::Equal),
|
||||
(Value::Int(x), Value::Num(y)) => {
|
||||
(*x as f64).partial_cmp(y).unwrap_or(std::cmp::Ordering::Equal)
|
||||
}
|
||||
(Value::Num(x), Value::Int(y)) => {
|
||||
x.partial_cmp(&(*y as f64)).unwrap_or(std::cmp::Ordering::Equal)
|
||||
}
|
||||
(Value::Text(x), Value::Text(y)) => x.cmp(y),
|
||||
_ => return Err("comparison needs two ints/nums or two texts".into()),
|
||||
};
|
||||
stack.push(Value::Bool(pred(ord)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn instance_type_name_bc(v: &Value) -> Result<String, String> {
|
||||
match v {
|
||||
Value::Map(entries) => entries
|
||||
@@ -781,42 +928,24 @@ fn instance_type_name_bc(v: &Value) -> Result<String, String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn tag_instance_value(type_name: &str, value: Value) -> Value {
|
||||
match value {
|
||||
Value::Map(mut entries) => {
|
||||
// Always stamp the constructed type so an explicit `` `self` = > Parent ``
|
||||
// inside a child constructor still yields the most specific `_type`.
|
||||
entries.retain(|(k, _)| k != "_type");
|
||||
entries.insert(0, ("_type".into(), Value::Text(type_name.into())));
|
||||
Value::Map(entries)
|
||||
}
|
||||
Value::None => Value::Map(vec![("_type".into(), Value::Text(type_name.into()))]),
|
||||
other => Value::Map(vec![
|
||||
("_type".into(), Value::Text(type_name.into())),
|
||||
("value".into(), other),
|
||||
]),
|
||||
}
|
||||
fn resolve_fn_dynamic(program: &Program, current: usize, name: &str) -> Result<usize, String> {
|
||||
super::names::lookup(program,current,name).ok_or_else(||format!("call_fn: unknown function `{name}`"))
|
||||
}
|
||||
|
||||
fn resolve_fn_dynamic(program: &Program, current: usize, name: &str) -> Result<usize, String> {
|
||||
for &cid in &program.children[current] {
|
||||
if program.functions[cid].name == name {
|
||||
return Ok(cid);
|
||||
}
|
||||
/// Bind already evaluated actuals; no expression or foreign effect runs here.
|
||||
fn bind_call_values(callee: &FnChunk, layout: &[Option<String>], values: Vec<Value>)
|
||||
-> std::result::Result<(Vec<Option<Value>>, Vec<bool>), String> {
|
||||
if layout.len()!=values.len(){return Err("argument layout/value count mismatch".into());}
|
||||
let plan=crate::arguments::plan_with_omissions(&callee.params,layout,&callee.defaults)?;
|
||||
let mut slots = vec![None; callee.locals.len().max(1)];
|
||||
for (name,actual) in plan.assignments {
|
||||
if let Some(index)=callee.locals.iter().position(|local|local==&name){slots[index]=Some(values[actual].clone());}
|
||||
}
|
||||
let mut parent = program.parents[current];
|
||||
while let Some(pid) = parent {
|
||||
for &cid in &program.children[pid] {
|
||||
if program.functions[cid].name == name {
|
||||
return Ok(cid);
|
||||
}
|
||||
}
|
||||
parent = program.parents[pid];
|
||||
}
|
||||
for (i, f) in program.functions.iter().enumerate() {
|
||||
if program.parents[i].is_none() && f.name == name {
|
||||
return Ok(i);
|
||||
}
|
||||
}
|
||||
Err(format!("call_fn: unknown function `{name}`"))
|
||||
let provided=initial_presence(&slots,callee.params.len());
|
||||
Ok((slots, provided))
|
||||
}
|
||||
|
||||
/// Presence is derived once, before defaults, rather than independently rebound.
|
||||
fn initial_presence(slots:&[Option<Value>],parameters:usize)->Vec<bool> {
|
||||
slots.iter().take(parameters).map(Option::is_some).collect()
|
||||
}
|
||||
Reference in new issue
Block a user