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
+1 -1
View File
@@ -4,6 +4,6 @@
| --- | --- |
| [lib.rs](lib.rs) | CompileOptions、公开分析/编译入口、AST 原文范围检查与带来源诊断 |
| [analysis.rs](analysis.rs) | 模块/函数/类方法/表达式/单次交互作用域、局部槽、全局名称、逐引用绑定和初始化检查 |
| [codegen.rs](codegen.rs) | AST 到函数与类创建指令、嵌套代码模板、EndorCode 验证、显式 VM 装载与临时根释放 |
| [codegen.rs](codegen.rs) | AST 到函数与类创建指令、嵌套代码模板与源码签名保留、EndorCode 验证、显式 VM 装载与临时根释放 |
节点定义在 frontend/Endor.asdl;这里匹配生成的 AST,不复制节点类型。源码和调用方提供的 AST 都先分析后生成,不执行 AST。修改名称语义、代码布局或所有权时同步 [编译契约](../../../docs/runtime/compiler.md)、[检查](../tests/README.md) 与 [示例](../examples/README.md)。返回 [compiler](../README.md)。
+23 -4
View File
@@ -25,6 +25,9 @@ impl CompiledCode {
pub fn name(&self) -> &str {
self.code.name()
}
pub fn source_signature(&self) -> Option<&endor_core::SourceSignature> {
self.code.source_signature()
}
pub fn filename(&self) -> &str {
self.code.filename()
}
@@ -47,7 +50,7 @@ impl CompiledCode {
roots.push(reference);
constants[*index] = Value::Object(reference);
}
let code = EndorCode::with_layout(
let mut code = EndorCode::with_layout(
self.code.bytecode().to_vec(),
constants,
self.code.layout().clone(),
@@ -57,6 +60,9 @@ impl CompiledCode {
self.code.filename(),
self.code.first_line(),
);
if let Some(signature) = self.code.source_signature() {
code = code.with_source_signature(signature.clone())?;
}
vm.new_code(code)
})();
for reference in roots {
@@ -152,7 +158,7 @@ impl<'a> Emitter<'a> {
fn declaration(&mut self, declaration: &Decl, symbols: &SymbolTable) -> Result<(), Diagnostic> {
match &declaration.kind {
DeclKind::Function(function) => {
self.function_value(function, symbols)?;
self.function_value(function, symbols, None)?;
self.emit(Opcode::StoreGlobal, self.name_index(&function.name.text))
}
DeclKind::Class(class) => {
@@ -169,7 +175,7 @@ impl<'a> Emitter<'a> {
self.emit(Opcode::BuildMap, class.fields.len())?;
for method in &class.methods {
self.constant(Value::Text(method.name.text.clone()))?;
self.function_value(method, symbols)?;
self.function_value(method, symbols, Some(&class.name.text))?;
}
self.emit(Opcode::BuildMap, class.methods.len())?;
self.simple(Opcode::BuildClass);
@@ -182,6 +188,7 @@ impl<'a> Emitter<'a> {
&mut self,
function: &frontend::ast::Function,
symbols: &SymbolTable,
owner: Option<&str>,
) -> Result<(), Diagnostic> {
let scope = symbols
.functions
@@ -203,7 +210,19 @@ impl<'a> Emitter<'a> {
body.constant(Value::Nil)?;
body.simple(Opcode::ReturnValue);
}
let child = body.finish()?;
let mut child = body.finish()?;
child.code = child
.code
.with_source_signature(endor_core::SourceSignature {
parameters: function
.parameters
.iter()
.map(|p| p.type_name.text.clone())
.collect(),
return_type: function.return_type.text.clone(),
method_owner: owner.map(str::to_owned),
})
.map_err(|failure| error(function.span, failure.to_string()))?;
let index = self.constants.len();
self.children.push((index, child));
self.constant(Value::Nil)?; // Replaced by a managed code object during load().
+1 -1
View File
@@ -2,7 +2,7 @@
[classes.rs](classes.rs) 覆盖源码 class、NativeTyped 实际原生载荷继承和多层字段构造、继承字段构造、动态方法覆盖、绑定接收者保活/回收、绑定后参数数量和重复成员/缺少 self 的预执行诊断。只编译,不在本地运行。
[pipeline.rs](pipeline.rs) 将真实 Endor 示例交给 reader/parser/名称分析/代码生成/VM,检查函数值、前向引用、局部遮蔽、精确数字常量、语义诊断、宿主全局与属性回调、对象结果保活、跨 VM 模板装载、反复调用、错误帧清理、递归限额、共享交互命名空间及超过 255 的操作数。
[pipeline.rs](pipeline.rs) 将真实 Endor 示例交给 reader/parser/名称分析/代码生成/VM,检查函数值、前向引用、局部遮蔽、精确数字常量、语义诊断、宿主全局与属性回调、对象结果保活、跨 VM 模板装载、源码签名保留和参数/返回失败清理、反复调用、错误帧清理、递归限额、共享交互命名空间及超过 255 的操作数。
裸赋值检查覆盖跨交互输入创建/更新 globals、参数重赋值、局部槽复用及未初始化读取;执行结束检查空堆。
+36
View File
@@ -446,3 +446,39 @@ fn assignments_update_parameters_and_allocate_one_local_slot_per_target() {
.is_err()
);
}
#[test]
fn source_signatures_survive_template_load_and_enable_runtime_checks() {
let compiled = compile(
"function identity(value: Number): Number { return value }\nfunction bad(): Number { return \"wrong\" }",
ParseMode::Module,
);
let signature = compiled
.children()
.next()
.unwrap()
.source_signature()
.unwrap();
assert_eq!(signature.parameters, ["Number"]);
assert_eq!(signature.return_type, "Number");
let mut vm = Vm::new().unwrap();
vm.set_source_type_checks(true).unwrap();
let (code, globals) = module(&mut vm, &compiled);
let identity = function(&vm, globals, "identity");
assert_eq!(
vm.call(identity, &[Value::Number(7.into())]).unwrap(),
Value::Number(7.into())
);
assert!(matches!(
vm.call(identity, &[Value::Text("wrong".into())]),
Err(VmError::TypeMismatch { .. })
));
let bad = function(&vm, globals, "bad");
let baseline = vm.object_count();
assert!(matches!(
vm.call(bad, &[]),
Err(VmError::TypeMismatch { .. })
));
assert_eq!(vm.object_count(), baseline);
cleanup(&mut vm, code, globals);
}