Check source function annotations and unify managed function identity
This commit is contained in:
@@ -5,9 +5,9 @@
|
||||
| 文件 | 职责 |
|
||||
| --- | --- |
|
||||
| [lib.rs](lib.rs) | VM 类型导出、objects 类型兼容重导出、宿主调用与字节码 rustdoc 示例 |
|
||||
| [vm.rs](vm.rs) | Vm 拥有 ObjectStore 与错误位置记录;对象操作和宿主 new_class 和显式Trait注册/关系查询显式委托给 objects |
|
||||
| [frame.rs](frame.rs) | EndorFrame、栈条目、执行位置元数据、ExecutionLimits |
|
||||
| [eval.rs](eval.rs) | execute/call、只读调用与依赖结果、参数绑定、帧切换、取指分派、MakeFunction、结果根转移与失败清理 |
|
||||
| [vm.rs](vm.rs) | Vm 拥有 ObjectStore 与错误位置记录;对象操作和宿主 new_class 和显式Trait注册/关系查询显式委托给 objects;源码检查开关与函数类型配置 |
|
||||
| [frame.rs](frame.rs) | EndorFrame、栈条目、返回类型执行根、执行位置元数据、ExecutionLimits |
|
||||
| [eval.rs](eval.rs) | execute/call、只读调用与依赖结果、参数绑定、可选注解检查、帧切换、取指分派、MakeFunction、结果根转移与失败清理 |
|
||||
|
||||
## 常见修改入口
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -15,6 +15,7 @@ pub(crate) enum StackEntry {
|
||||
#[derive(Debug)]
|
||||
pub struct EndorFrame {
|
||||
pub(crate) function: ObjectRef,
|
||||
pub(crate) return_type: Option<crate::AnnotationType>,
|
||||
/// Index of the caller in the execution's frame vector, never a pointer.
|
||||
pub(crate) previous: Option<usize>,
|
||||
pub(crate) code: ObjectRef,
|
||||
|
||||
@@ -45,16 +45,17 @@ mod vm;
|
||||
pub use frame::{EndorFrame, ExecutionLimits, ExecutionLocation};
|
||||
// Preserve existing embedding imports; objects owns these definitions.
|
||||
pub use objects::ObjectStore;
|
||||
pub use objects::{
|
||||
AnnotationType, ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement, RootTypes,
|
||||
TypeConstructor,
|
||||
};
|
||||
pub use objects::{
|
||||
ChangeBatch, ChangeKey, CodeLayout, CompareOp, ContextNativeFunction, DecodedInstruction,
|
||||
EndorBaseExceptionObject, EndorCellObject, EndorCode, EndorCodeObject, EndorDecimal,
|
||||
EndorDictObject, EndorFunctionObject, EndorInteger, EndorNativeCall, EndorNativeFunctionObject,
|
||||
EndorNumber, EndorNumberObject, EndorObject, EndorRational, EndorReal, EndorStringObject,
|
||||
EndorTracebackEntry, Instruction, NativeCall, NativeCallable, NativeContext, NativeFunction,
|
||||
NativeResult, ObjectChange, ObjectHeader, ObjectPayload, ObjectRef, Opcode, Record, Value,
|
||||
VmError, WeakObjectRef,
|
||||
};
|
||||
pub use objects::{
|
||||
ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement, RootTypes, TypeConstructor,
|
||||
NativeResult, ObjectChange, ObjectHeader, ObjectPayload, ObjectRef, Opcode, Record,
|
||||
SourceSignature, Value, VmError, WeakObjectRef,
|
||||
};
|
||||
pub use vm::Vm;
|
||||
|
||||
@@ -11,9 +11,23 @@ use crate::{
|
||||
pub struct Vm {
|
||||
pub(crate) objects: ObjectStore,
|
||||
pub(crate) last_traceback: Vec<ExecutionLocation>,
|
||||
pub(crate) source_type_checks: bool,
|
||||
}
|
||||
|
||||
impl Vm {
|
||||
pub fn set_source_type_checks(&mut self, enabled: bool) -> Result<(), VmError> {
|
||||
if self.objects.is_read_only() {
|
||||
return Err(VmError::UnsupportedOperation(
|
||||
"configure source type checks during read-only call",
|
||||
));
|
||||
}
|
||||
self.source_type_checks = enabled;
|
||||
Ok(())
|
||||
}
|
||||
pub fn set_function_type(&mut self, definition: ObjectRef) -> Result<(), VmError> {
|
||||
self.objects.set_function_type(definition)
|
||||
}
|
||||
|
||||
pub fn new_trait(
|
||||
&mut self,
|
||||
name: impl Into<String>,
|
||||
@@ -134,6 +148,7 @@ impl Vm {
|
||||
Ok(Self {
|
||||
objects: ObjectStore::new()?,
|
||||
last_traceback: Vec::new(),
|
||||
source_type_checks: false,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user