Enforce native type signatures and effects across delegated calls
This commit is contained in:
@@ -50,10 +50,141 @@ impl CallArity {
|
||||
}
|
||||
}
|
||||
|
||||
/// Declared host effect. ReadOnly may allocate results but must not mutate
|
||||
/// existing objects or perform external I/O. Unknown legacy calls remain unknown.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum CallEffect {
|
||||
ReadOnly,
|
||||
Mutating,
|
||||
External,
|
||||
}
|
||||
|
||||
/// Immutable native contract, including an explicit receiver where applicable.
|
||||
/// Missing fixed slots are covered by variadic; optionality comes from arity.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CallSignature {
|
||||
pub arity: CallArity,
|
||||
pub parameters: Vec<crate::AnnotationType>,
|
||||
pub variadic: Option<crate::AnnotationType>,
|
||||
pub returns: crate::AnnotationType,
|
||||
pub effect: CallEffect,
|
||||
}
|
||||
impl CallSignature {
|
||||
pub fn exact(
|
||||
parameters: Vec<crate::AnnotationType>,
|
||||
returns: crate::AnnotationType,
|
||||
effect: CallEffect,
|
||||
) -> Self {
|
||||
Self {
|
||||
arity: CallArity::exact(parameters.len()),
|
||||
parameters,
|
||||
variadic: None,
|
||||
returns,
|
||||
effect,
|
||||
}
|
||||
}
|
||||
pub fn references(&self) -> impl Iterator<Item = ObjectRef> + '_ {
|
||||
self.parameters
|
||||
.iter()
|
||||
.copied()
|
||||
.chain(self.variadic)
|
||||
.chain([self.returns])
|
||||
.filter_map(crate::AnnotationType::reference)
|
||||
}
|
||||
pub fn validate(&self, store: &ObjectStore) -> Result<(), VmError> {
|
||||
self.arity.validate()?;
|
||||
if self.parameters.len() > self.arity.maximum
|
||||
|| (self.variadic.is_none() && self.parameters.len() != self.arity.maximum)
|
||||
{
|
||||
return Err(VmError::InvalidArgument("incomplete native signature"));
|
||||
}
|
||||
for expected in self
|
||||
.parameters
|
||||
.iter()
|
||||
.copied()
|
||||
.chain(self.variadic)
|
||||
.chain([self.returns])
|
||||
{
|
||||
match expected {
|
||||
crate::AnnotationType::Nominal(reference) => {
|
||||
store.payload::<crate::EndorTypeObject>(reference, "Type")?;
|
||||
}
|
||||
crate::AnnotationType::Capability(reference) => {
|
||||
store.payload::<crate::EndorTraitObject>(reference, "Trait")?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn check_arguments(
|
||||
&self,
|
||||
store: &ObjectStore,
|
||||
name: &str,
|
||||
arguments: &[Value],
|
||||
) -> Result<(), VmError> {
|
||||
self.validate(store)?;
|
||||
self.arity.check(name.into(), arguments.len())?;
|
||||
if store.is_read_only() && self.effect != CallEffect::ReadOnly {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"native effect is forbidden during read-only execution",
|
||||
));
|
||||
}
|
||||
for (index, value) in arguments.iter().enumerate() {
|
||||
let expected = self
|
||||
.parameters
|
||||
.get(index)
|
||||
.copied()
|
||||
.or(self.variadic)
|
||||
.ok_or(VmError::InvalidArgument("incomplete native signature"))?;
|
||||
self.check_value(
|
||||
store,
|
||||
name,
|
||||
format!("parameter {}", index + 1),
|
||||
value,
|
||||
expected,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn check_return(
|
||||
&self,
|
||||
store: &ObjectStore,
|
||||
name: &str,
|
||||
value: &Value,
|
||||
) -> Result<(), VmError> {
|
||||
self.check_value(store, name, "return".into(), value, self.returns)
|
||||
}
|
||||
fn check_value(
|
||||
&self,
|
||||
store: &ObjectStore,
|
||||
name: &str,
|
||||
position: String,
|
||||
value: &Value,
|
||||
expected: crate::AnnotationType,
|
||||
) -> Result<(), VmError> {
|
||||
if !store.matches_annotation(value, expected)? {
|
||||
return Err(VmError::TypeMismatch {
|
||||
function: name.into(),
|
||||
position,
|
||||
expected: store.annotation_type_name(expected)?,
|
||||
actual: store.value_type_name(value)?,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait NativeCallable: fmt::Debug + Send + Sync + 'static {
|
||||
/// Immutable resolved types; referenced definitions must be traced by the
|
||||
/// payload. ContextNativeFunction snapshots and traces them automatically.
|
||||
fn signature(&self) -> Option<CallSignature> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Optional positional contract. Missing metadata remains an explicit legacy gap.
|
||||
fn arity(&self) -> Option<CallArity> {
|
||||
None
|
||||
self.signature().map(|signature| signature.arity)
|
||||
}
|
||||
|
||||
fn invoke(
|
||||
@@ -197,16 +328,29 @@ impl<'a> NativeContext<'a> {
|
||||
arguments.len(),
|
||||
)?;
|
||||
}
|
||||
let signature = store.native_signature(function)?;
|
||||
let name = store.object(function)?.payload().kind_name().to_owned();
|
||||
if let Some(signature) = &signature {
|
||||
signature.check_arguments(store, &name, arguments)?;
|
||||
}
|
||||
let mut context = Self {
|
||||
store,
|
||||
roots: Vec::new(),
|
||||
callee: function,
|
||||
};
|
||||
context.protect(&Value::Object(function))?;
|
||||
if let Some(signature) = &signature {
|
||||
for reference in signature.references() {
|
||||
context.protect(&Value::Object(reference))?;
|
||||
}
|
||||
}
|
||||
for argument in arguments {
|
||||
context.protect(argument)?;
|
||||
}
|
||||
let result = callable.invoke(&mut context, arguments)?;
|
||||
if let (Some(signature), NativeResult::Return(value)) = (&signature, &result) {
|
||||
signature.check_return(context.store(), &name, value)?;
|
||||
}
|
||||
let transferred_from = context.roots.len();
|
||||
match &result {
|
||||
NativeResult::Return(value) => context.protect(value)?,
|
||||
@@ -242,6 +386,7 @@ pub struct ContextNativeFunction {
|
||||
name: &'static str,
|
||||
callable: Arc<dyn NativeCallable>,
|
||||
captures: Vec<ObjectRef>,
|
||||
signature: Option<CallSignature>,
|
||||
}
|
||||
|
||||
impl ContextNativeFunction {
|
||||
@@ -252,6 +397,7 @@ impl ContextNativeFunction {
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
signature: callable.signature(),
|
||||
callable: Arc::new(callable),
|
||||
captures,
|
||||
}
|
||||
@@ -272,10 +418,18 @@ impl ObjectPayload for ContextNativeFunction {
|
||||
self.name
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
if let Some(signature) = &self.signature {
|
||||
for reference in signature.references() {
|
||||
visit(reference);
|
||||
}
|
||||
}
|
||||
for capture in &self.captures {
|
||||
visit(*capture);
|
||||
}
|
||||
}
|
||||
fn call_signature(&self) -> Option<CallSignature> {
|
||||
self.signature.clone()
|
||||
}
|
||||
fn native_callable(&self) -> Option<Arc<dyn NativeCallable>> {
|
||||
Some(self.callable.clone())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user