Check source function annotations and unify managed function identity

This commit is contained in:
2026-10-21 22:38:22 +08:00
parent eb60212428
commit 6346041f77
27 changed files with 568 additions and 40 deletions
+56
View File
@@ -135,6 +135,10 @@ impl Vm {
}
self.release(frame.function)
.expect("frame owns its function root");
if let Some(reference) = frame.return_type.and_then(|kind| kind.reference()) {
self.release(reference)
.expect("frame owns its return type root");
}
}
fn prepare_frame(
@@ -161,8 +165,45 @@ impl Vm {
for value in arguments {
self.validate_value(value)?;
}
let mut return_type = None;
if self.source_type_checks
&& let Some(signature) = executable.source_signature()
{
let receiver = arguments.first().and_then(|value| match value {
Value::Object(object) => Some(*object),
_ => None,
});
for (index, expected_name) in signature.parameters.iter().enumerate() {
let value = arguments
.get(index)
.unwrap_or_else(|| &data.defaults()[index - required]);
let expected = self
.objects
.annotation_type(function, expected_name, receiver)?;
if !self.objects.matches_annotation(value, expected)? {
return Err(VmError::TypeMismatch {
function: data.name().into(),
position: format!("parameter {}", executable.parameters()[index]),
expected: expected_name.clone(),
actual: self.objects.value_type_name(value)?,
});
}
}
return_type = Some(self.objects.annotation_type(
function,
&signature.return_type,
receiver,
)?);
}
self.retain(function)?;
if let Some(reference) = return_type.and_then(|kind| kind.reference())
&& let Err(error) = self.retain(reference)
{
self.release(function)?;
return Err(error);
}
let mut frame = EndorFrame {
return_type,
function,
previous: None,
code: data.code(),
@@ -614,6 +655,21 @@ impl Vm {
}
Opcode::ReturnValue => {
let result = Self::top_value(frame)?;
if let Some(expected) = frame.return_type
&& !self.objects.matches_annotation(&result, expected)?
{
return Err(VmError::TypeMismatch {
function: frame.name.clone(),
position: "return".into(),
expected: frame
.executable
.source_signature()
.expect("checked source signature")
.return_type
.clone(),
actual: self.objects.value_type_name(&result)?,
});
}
frame.stack.pop();
self.clear_frame(frames.pop().unwrap());
if let Some(caller) = frames.last_mut() {