Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5ff1c68f5f | |||
| f3e1670c58 | |||
| 1aecc7e952 | |||
| ee68a0c54c | |||
| 42c27d083d | |||
| a775d5c827 | |||
| 89b817447e | |||
| ae75c54707 | |||
| 8a2135897e | |||
| 16816c38c7 | |||
| b33d322690 | |||
| e6b27ba7b6 | |||
| 1c729b4128 | |||
| e511e393d9 | |||
| ae821aea2e | |||
| cd25f819f2 | |||
| 0513b96dfb | |||
| c96dce118b | |||
| 100bf637f9 | |||
| e26e227dd0 |
Generated
+51
-1
@@ -2,6 +2,12 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "compiler"
|
name = "compiler"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@@ -38,9 +44,53 @@ version = "2.8.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-bigint"
|
||||||
|
version = "0.4.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367"
|
||||||
|
dependencies = [
|
||||||
|
"num-integer",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-integer"
|
||||||
|
version = "0.1.47"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-rational"
|
||||||
|
version = "0.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
|
||||||
|
dependencies = [
|
||||||
|
"num-bigint",
|
||||||
|
"num-integer",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "objects"
|
name = "objects"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"num-bigint",
|
||||||
|
"num-rational",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "peg"
|
name = "peg"
|
||||||
@@ -114,7 +164,7 @@ checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 3.0.9",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -7,3 +7,5 @@
|
|||||||
| [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)。
|
节点定义在 frontend/Endor.asdl;这里匹配生成的 AST,不复制节点类型。源码和调用方提供的 AST 都先分析后生成,不执行 AST。修改名称语义、代码布局或所有权时同步 [编译契约](../../../docs/runtime/compiler.md)、[检查](../tests/README.md) 与 [示例](../examples/README.md)。返回 [compiler](../README.md)。
|
||||||
|
|
||||||
|
analysis.rs遍历Await操作数,codegen.rs发出Await字节码;运行时等待交给VM及宿主。
|
||||||
|
|||||||
+124
-45
@@ -167,6 +167,16 @@ pub fn analyze(root: &AstRoot, options: &CompileOptions) -> Result<SymbolTable,
|
|||||||
StmtKind::Expression(expression) => {
|
StmtKind::Expression(expression) => {
|
||||||
root_scope.expression(expression, &globals)?
|
root_scope.expression(expression, &globals)?
|
||||||
}
|
}
|
||||||
|
StmtKind::If(_) | StmtKind::While(_) => {
|
||||||
|
let statements = std::slice::from_ref(statement);
|
||||||
|
collect_locals(
|
||||||
|
statements,
|
||||||
|
&mut declared,
|
||||||
|
&mut root_scope.scope.layout.names,
|
||||||
|
)?;
|
||||||
|
globals.extend(root_scope.scope.layout.names.iter().cloned());
|
||||||
|
root_scope.statements(statements, &globals)?;
|
||||||
|
}
|
||||||
StmtKind::Return(_) => {
|
StmtKind::Return(_) => {
|
||||||
return Err(error(statement.span, "return outside a function"));
|
return Err(error(statement.span, "return outside a function"));
|
||||||
}
|
}
|
||||||
@@ -207,21 +217,38 @@ fn function_scope(function: &Function, globals: &BTreeSet<String>) -> Result<Sco
|
|||||||
.push(parameter.name.text.clone());
|
.push(parameter.name.text.clone());
|
||||||
builder.initialized.insert(parameter.name.text.clone());
|
builder.initialized.insert(parameter.name.text.clone());
|
||||||
}
|
}
|
||||||
for statement in &function.body.statements {
|
collect_locals(
|
||||||
|
&function.body.statements,
|
||||||
|
&mut locals,
|
||||||
|
&mut builder.scope.layout.locals,
|
||||||
|
)?;
|
||||||
|
builder.statements(&function.body.statements, globals)?;
|
||||||
|
Ok(builder.scope)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn collect_locals(
|
||||||
|
statements: &[frontend::ast::Stmt],
|
||||||
|
locals: &mut BTreeSet<String>,
|
||||||
|
layout: &mut Vec<String>,
|
||||||
|
) -> Result<(), Diagnostic> {
|
||||||
|
for statement in statements {
|
||||||
match &statement.kind {
|
match &statement.kind {
|
||||||
StmtKind::Binding(binding) => {
|
StmtKind::Binding(b) => {
|
||||||
declare(&mut locals, &binding.name.text, binding.name.span)?;
|
declare(locals, &b.name.text, b.name.span)?;
|
||||||
builder.scope.layout.locals.push(binding.name.text.clone());
|
layout.push(b.name.text.clone());
|
||||||
}
|
}
|
||||||
StmtKind::Assignment(assignment) => {
|
StmtKind::Assignment(a) => {
|
||||||
if locals.insert(assignment.name.text.clone()) {
|
if locals.insert(a.name.text.clone()) {
|
||||||
builder
|
layout.push(a.name.text.clone());
|
||||||
.scope
|
|
||||||
.layout
|
|
||||||
.locals
|
|
||||||
.push(assignment.name.text.clone());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
StmtKind::If(b) => {
|
||||||
|
collect_locals(&b.body.statements, locals, layout)?;
|
||||||
|
if let Some(other) = &b.otherwise {
|
||||||
|
collect_locals(&other.statements, locals, layout)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StmtKind::While(b) => collect_locals(&b.body.statements, locals, layout)?,
|
||||||
StmtKind::Declaration(_) => {
|
StmtKind::Declaration(_) => {
|
||||||
return Err(error(
|
return Err(error(
|
||||||
statement.span,
|
statement.span,
|
||||||
@@ -231,39 +258,7 @@ fn function_scope(function: &Function, globals: &BTreeSet<String>) -> Result<Sco
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut returned = false;
|
Ok(())
|
||||||
for statement in &function.body.statements {
|
|
||||||
if returned {
|
|
||||||
return Err(error(
|
|
||||||
statement.span,
|
|
||||||
"statement after return is unreachable",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
match &statement.kind {
|
|
||||||
StmtKind::Binding(binding) => {
|
|
||||||
builder.expression(&binding.value, globals)?;
|
|
||||||
builder.initialized.insert(binding.name.text.clone());
|
|
||||||
}
|
|
||||||
StmtKind::Assignment(assignment) => {
|
|
||||||
builder.expression(&assignment.value, globals)?;
|
|
||||||
builder.initialized.insert(assignment.name.text.clone());
|
|
||||||
}
|
|
||||||
StmtKind::Return(statement) => {
|
|
||||||
if let Some(value) = &statement.value {
|
|
||||||
builder.expression(value, globals)?;
|
|
||||||
}
|
|
||||||
returned = true;
|
|
||||||
}
|
|
||||||
StmtKind::MemberAssignment(assignment) => {
|
|
||||||
builder.expression(&assignment.object, globals)?;
|
|
||||||
builder.expression(&assignment.value, globals)?;
|
|
||||||
builder.intern(&assignment.name.text);
|
|
||||||
}
|
|
||||||
StmtKind::Expression(expression) => builder.expression(expression, globals)?,
|
|
||||||
StmtKind::Declaration(_) => unreachable!("nested declaration rejected above"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(builder.scope)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Builder {
|
struct Builder {
|
||||||
@@ -300,6 +295,88 @@ impl Builder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Function-scoped locals; branches merge definite initialization. A loop
|
||||||
|
/// may execute zero times, so its writes do not initialize later reads.
|
||||||
|
fn statements(
|
||||||
|
&mut self,
|
||||||
|
statements: &[frontend::ast::Stmt],
|
||||||
|
globals: &BTreeSet<String>,
|
||||||
|
) -> Result<bool, Diagnostic> {
|
||||||
|
let mut returned = false;
|
||||||
|
for statement in statements {
|
||||||
|
if returned {
|
||||||
|
return Err(error(
|
||||||
|
statement.span,
|
||||||
|
"statement after return is unreachable",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
match &statement.kind {
|
||||||
|
StmtKind::Binding(b) => {
|
||||||
|
self.expression(&b.value, globals)?;
|
||||||
|
self.initialized.insert(b.name.text.clone());
|
||||||
|
if self.scope.kind != ScopeKind::Function {
|
||||||
|
self.intern(&b.name.text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StmtKind::Assignment(a) => {
|
||||||
|
self.expression(&a.value, globals)?;
|
||||||
|
self.initialized.insert(a.name.text.clone());
|
||||||
|
if self.scope.kind != ScopeKind::Function {
|
||||||
|
self.intern(&a.name.text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StmtKind::MemberAssignment(a) => {
|
||||||
|
self.expression(&a.object, globals)?;
|
||||||
|
self.expression(&a.value, globals)?;
|
||||||
|
self.intern(&a.name.text);
|
||||||
|
}
|
||||||
|
StmtKind::Expression(e) => self.expression(e, globals)?,
|
||||||
|
StmtKind::Return(r) => {
|
||||||
|
if self.scope.kind != ScopeKind::Function {
|
||||||
|
return Err(error(statement.span, "return outside a function"));
|
||||||
|
}
|
||||||
|
if let Some(e) = &r.value {
|
||||||
|
self.expression(e, globals)?;
|
||||||
|
}
|
||||||
|
returned = true;
|
||||||
|
}
|
||||||
|
StmtKind::If(b) => {
|
||||||
|
self.expression(&b.condition, globals)?;
|
||||||
|
let before = self.initialized.clone();
|
||||||
|
let yes_return = self.statements(&b.body.statements, globals)?;
|
||||||
|
let yes = self.initialized.clone();
|
||||||
|
self.initialized = before;
|
||||||
|
let no_return = if let Some(other) = &b.otherwise {
|
||||||
|
self.statements(&other.statements, globals)?
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
self.initialized = if yes_return {
|
||||||
|
self.initialized.clone()
|
||||||
|
} else if no_return {
|
||||||
|
yes
|
||||||
|
} else {
|
||||||
|
yes.intersection(&self.initialized).cloned().collect()
|
||||||
|
};
|
||||||
|
returned = yes_return && no_return;
|
||||||
|
}
|
||||||
|
StmtKind::While(b) => {
|
||||||
|
self.expression(&b.condition, globals)?;
|
||||||
|
let before = self.initialized.clone();
|
||||||
|
self.statements(&b.body.statements, globals)?;
|
||||||
|
self.initialized = before;
|
||||||
|
}
|
||||||
|
StmtKind::Declaration(_) => {
|
||||||
|
return Err(error(
|
||||||
|
statement.span,
|
||||||
|
"nested declarations are not compiled yet",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(returned)
|
||||||
|
}
|
||||||
|
|
||||||
fn expression(
|
fn expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
expression: &Expr,
|
expression: &Expr,
|
||||||
@@ -332,7 +409,9 @@ impl Builder {
|
|||||||
binding,
|
binding,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
ExprKind::Unary { operand, .. } => self.expression(operand, globals)?,
|
ExprKind::Unary { operand, .. } | ExprKind::Await(operand) => {
|
||||||
|
self.expression(operand, globals)?
|
||||||
|
}
|
||||||
ExprKind::Binary { left, right, .. } => {
|
ExprKind::Binary { left, right, .. } => {
|
||||||
self.expression(left, globals)?;
|
self.expression(left, globals)?;
|
||||||
self.expression(right, globals)?;
|
self.expression(right, globals)?;
|
||||||
|
|||||||
+103
-16
@@ -2,7 +2,7 @@ use endor_core::{
|
|||||||
CodeLayout, DecodedInstruction, EndorCode, EndorNumber, Instruction, ObjectRef, Opcode, Value,
|
CodeLayout, DecodedInstruction, EndorCode, EndorNumber, Instruction, ObjectRef, Opcode, Value,
|
||||||
Vm, VmError,
|
Vm, VmError,
|
||||||
};
|
};
|
||||||
use frontend::ast::{AstRoot, Decl, DeclKind, Expr, ExprKind, Stmt, StmtKind, UnaryOp};
|
use frontend::ast::{AstRoot, BinaryOp, Decl, DeclKind, Expr, ExprKind, Stmt, StmtKind, UnaryOp};
|
||||||
use frontend::{Diagnostic, DiagnosticKind, SourceUnit, Span};
|
use frontend::{Diagnostic, DiagnosticKind, SourceUnit, Span};
|
||||||
|
|
||||||
use crate::{NameBinding, Scope, ScopeKind, SymbolTable, diagnostic};
|
use crate::{NameBinding, Scope, ScopeKind, SymbolTable, diagnostic};
|
||||||
@@ -115,6 +115,7 @@ struct Emitter<'a> {
|
|||||||
source: &'a SourceUnit,
|
source: &'a SourceUnit,
|
||||||
instructions: Vec<Instruction>,
|
instructions: Vec<Instruction>,
|
||||||
constants: Vec<Value>,
|
constants: Vec<Value>,
|
||||||
|
jumps: Vec<(usize, usize)>,
|
||||||
children: Vec<(usize, CompiledCode)>,
|
children: Vec<(usize, CompiledCode)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,6 +126,7 @@ impl<'a> Emitter<'a> {
|
|||||||
source,
|
source,
|
||||||
instructions: Vec::new(),
|
instructions: Vec::new(),
|
||||||
constants: Vec::new(),
|
constants: Vec::new(),
|
||||||
|
jumps: Vec::new(),
|
||||||
children: Vec::new(),
|
children: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -249,6 +251,36 @@ impl<'a> Emitter<'a> {
|
|||||||
|
|
||||||
fn statement(&mut self, statement: &Stmt, symbols: &SymbolTable) -> Result<(), Diagnostic> {
|
fn statement(&mut self, statement: &Stmt, symbols: &SymbolTable) -> Result<(), Diagnostic> {
|
||||||
match &statement.kind {
|
match &statement.kind {
|
||||||
|
StmtKind::If(branch) => {
|
||||||
|
self.expression(&branch.condition)?;
|
||||||
|
let conditional = self.instructions.len();
|
||||||
|
self.emit(Opcode::PopJumpIfFalse, 0)?;
|
||||||
|
for statement in &branch.body.statements {
|
||||||
|
self.statement(statement, symbols)?;
|
||||||
|
}
|
||||||
|
let end = self.instructions.len();
|
||||||
|
self.emit(Opcode::JumpForward, 0)?;
|
||||||
|
self.jumps.push((conditional, self.instructions.len()));
|
||||||
|
if let Some(otherwise) = &branch.otherwise {
|
||||||
|
for statement in &otherwise.statements {
|
||||||
|
self.statement(statement, symbols)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.jumps.push((end, self.instructions.len()));
|
||||||
|
}
|
||||||
|
StmtKind::While(loop_) => {
|
||||||
|
let start = self.instructions.len();
|
||||||
|
self.expression(&loop_.condition)?;
|
||||||
|
let exit = self.instructions.len();
|
||||||
|
self.emit(Opcode::PopJumpIfFalse, 0)?;
|
||||||
|
for statement in &loop_.body.statements {
|
||||||
|
self.statement(statement, symbols)?;
|
||||||
|
}
|
||||||
|
let back = self.instructions.len();
|
||||||
|
self.emit(Opcode::JumpBackward, 0)?;
|
||||||
|
self.jumps.push((back, start));
|
||||||
|
self.jumps.push((exit, self.instructions.len()));
|
||||||
|
}
|
||||||
StmtKind::Binding(binding) => self.store_binding(&binding.name.text, &binding.value)?,
|
StmtKind::Binding(binding) => self.store_binding(&binding.name.text, &binding.value)?,
|
||||||
StmtKind::Assignment(assignment) => {
|
StmtKind::Assignment(assignment) => {
|
||||||
self.store_binding(&assignment.name.text, &assignment.value)?
|
self.store_binding(&assignment.name.text, &assignment.value)?
|
||||||
@@ -300,26 +332,46 @@ impl<'a> Emitter<'a> {
|
|||||||
}
|
}
|
||||||
self.emit(Opcode::Call, arguments.len())?;
|
self.emit(Opcode::Call, arguments.len())?;
|
||||||
}
|
}
|
||||||
|
ExprKind::Await(operand) => {
|
||||||
|
self.expression(operand)?;
|
||||||
|
self.simple(Opcode::Await);
|
||||||
|
}
|
||||||
ExprKind::Member { object, name } => {
|
ExprKind::Member { object, name } => {
|
||||||
self.expression(object)?;
|
self.expression(object)?;
|
||||||
self.emit(Opcode::LoadAttr, self.name_index(&name.text))?;
|
self.emit(Opcode::LoadAttr, self.name_index(&name.text))?;
|
||||||
}
|
}
|
||||||
ExprKind::Unary { .. } => {
|
ExprKind::Unary { operand, .. } => {
|
||||||
// Literal sign folding preserves arbitrarily large integers
|
if let Some(spelling) = signed_literal(expression, false) {
|
||||||
// and decimal spelling; this is not general numeric arithmetic.
|
|
||||||
let Some(spelling) = signed_literal(expression, false) else {
|
|
||||||
return Err(error(
|
|
||||||
expression.span,
|
|
||||||
"numeric unary operations are not implemented; only signed number literals are supported",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.number(&spelling, expression.span)?;
|
self.number(&spelling, expression.span)?;
|
||||||
|
} else {
|
||||||
|
self.expression(operand)?;
|
||||||
|
self.simple(Opcode::UnaryNegate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ExprKind::Binary {
|
||||||
|
operation,
|
||||||
|
left,
|
||||||
|
right,
|
||||||
|
} => {
|
||||||
|
self.expression(left)?;
|
||||||
|
self.expression(right)?;
|
||||||
|
match operation {
|
||||||
|
BinaryOp::Add => self.emit(Opcode::BinaryArithmetic, 0)?,
|
||||||
|
BinaryOp::Subtract => self.emit(Opcode::BinaryArithmetic, 1)?,
|
||||||
|
BinaryOp::Multiply => self.emit(Opcode::BinaryArithmetic, 2)?,
|
||||||
|
BinaryOp::Divide => self.emit(Opcode::BinaryArithmetic, 3)?,
|
||||||
|
_ => self
|
||||||
|
.instructions
|
||||||
|
.push(Instruction::compare(match operation {
|
||||||
|
BinaryOp::Equal => endor_core::CompareOp::Eq,
|
||||||
|
BinaryOp::NotEqual => endor_core::CompareOp::Ne,
|
||||||
|
BinaryOp::Less => endor_core::CompareOp::Lt,
|
||||||
|
BinaryOp::LessEqual => endor_core::CompareOp::Le,
|
||||||
|
BinaryOp::Greater => endor_core::CompareOp::Gt,
|
||||||
|
BinaryOp::GreaterEqual => endor_core::CompareOp::Ge,
|
||||||
|
_ => unreachable!(),
|
||||||
|
})),
|
||||||
}
|
}
|
||||||
ExprKind::Binary { .. } => {
|
|
||||||
return Err(error(
|
|
||||||
expression.span,
|
|
||||||
"numeric arithmetic bytecode is not implemented yet",
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
ExprKind::Symbol(_) => {
|
ExprKind::Symbol(_) => {
|
||||||
return Err(error(
|
return Err(error(
|
||||||
@@ -341,7 +393,42 @@ impl<'a> Emitter<'a> {
|
|||||||
self.constant(Value::Number(value))
|
self.constant(Value::Number(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish(self) -> Result<CompiledCode, Diagnostic> {
|
fn finish(mut self) -> Result<CompiledCode, Diagnostic> {
|
||||||
|
// Labels refer to logical instructions. Resolve relative code-unit
|
||||||
|
// offsets after EXTENDED_ARG expansion, to a fixed point.
|
||||||
|
for attempt in 0..64 {
|
||||||
|
let mut offsets = vec![0_usize];
|
||||||
|
for i in &self.instructions {
|
||||||
|
let units = if i.argument <= 0xff {
|
||||||
|
1
|
||||||
|
} else if i.argument <= 0xffff {
|
||||||
|
2
|
||||||
|
} else if i.argument <= 0xffffff {
|
||||||
|
3
|
||||||
|
} else {
|
||||||
|
4
|
||||||
|
};
|
||||||
|
offsets.push(offsets.last().unwrap() + units);
|
||||||
|
}
|
||||||
|
let mut changed = false;
|
||||||
|
for &(index, target) in &self.jumps {
|
||||||
|
let distance = if self.instructions[index].opcode == Opcode::JumpBackward {
|
||||||
|
offsets[index + 1] - offsets[target]
|
||||||
|
} else {
|
||||||
|
offsets[target] - offsets[index + 1]
|
||||||
|
};
|
||||||
|
let operand = u32::try_from(distance)
|
||||||
|
.map_err(|_| error(self.scope.span, "jump exceeds u32"))?;
|
||||||
|
changed |= self.instructions[index].argument != operand;
|
||||||
|
self.instructions[index].argument = operand;
|
||||||
|
}
|
||||||
|
if !changed {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if attempt == 63 {
|
||||||
|
return Err(error(self.scope.span, "jump layout did not converge"));
|
||||||
|
}
|
||||||
|
}
|
||||||
let line = self
|
let line = self
|
||||||
.scope
|
.scope
|
||||||
.span
|
.span
|
||||||
|
|||||||
@@ -262,11 +262,22 @@ fn declared_field_checks_cover_construction_inheritance_writes_and_gc() {
|
|||||||
r#"
|
r#"
|
||||||
class Base { count: Number }
|
class Base { count: Number }
|
||||||
class Child: Base { label: String }
|
class Child: Base { label: String }
|
||||||
value = Child(9, "ok")
|
|
||||||
class Holder { child: Base }
|
class Holder { child: Base }
|
||||||
holder = Holder(value)
|
|
||||||
class Unknown { value: Missing }
|
class Unknown { value: Missing }
|
||||||
"#,
|
"#,
|
||||||
|
ParseMode::Module,
|
||||||
|
);
|
||||||
|
// Interactive intentionally accepts one complete statement per source unit.
|
||||||
|
execute(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
r#"value = Child(9, "ok")"#,
|
||||||
|
ParseMode::Interactive,
|
||||||
|
);
|
||||||
|
execute(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"holder = Holder(value)",
|
||||||
ParseMode::Interactive,
|
ParseMode::Interactive,
|
||||||
);
|
);
|
||||||
let Value::Object(value) = vm
|
let Value::Object(value) = vm
|
||||||
@@ -334,7 +345,7 @@ fn checked_fields_keep_declaring_self_type_and_can_be_opted_out() {
|
|||||||
class Link { next: Link }
|
class Link { next: Link }
|
||||||
class Seed: Link {}
|
class Seed: Link {}
|
||||||
"#,
|
"#,
|
||||||
ParseMode::Interactive,
|
ParseMode::Module,
|
||||||
);
|
);
|
||||||
// A recursive nonnullable field can be initialized by the trusted host in two steps.
|
// A recursive nonnullable field can be initialized by the trusted host in two steps.
|
||||||
let Value::Object(link) = vm
|
let Value::Object(link) = vm
|
||||||
|
|||||||
@@ -345,19 +345,11 @@ fn extended_operands_work_for_large_local_and_constant_tables() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unsupported_execution_features_are_compile_errors_instead_of_fake_results() {
|
fn unsupported_execution_features_are_compile_errors_instead_of_fake_results() {
|
||||||
for (text, mode) in [
|
let source = EndorReader::from_text("<unsupported>", ".title");
|
||||||
("1 + 2", ParseMode::Expression),
|
let error =
|
||||||
(".title", ParseMode::Expression),
|
compile_source(&source, ParseMode::Expression, &CompileOptions::default()).unwrap_err();
|
||||||
(
|
|
||||||
"function f(value: Number): Number { return -value }",
|
|
||||||
ParseMode::Module,
|
|
||||||
),
|
|
||||||
] {
|
|
||||||
let source = EndorReader::from_text("<unsupported>", text);
|
|
||||||
let error = compile_source(&source, mode, &CompileOptions::default()).unwrap_err();
|
|
||||||
assert_eq!(error.diagnostic.kind, DiagnosticKind::Compilation);
|
assert_eq!(error.diagnostic.kind, DiagnosticKind::Compilation);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compiling_ast_rejects_invalid_source_ranges() {
|
fn compiling_ast_rejects_invalid_source_ranges() {
|
||||||
@@ -482,3 +474,79 @@ fn source_signatures_survive_template_load_and_enable_runtime_checks() {
|
|||||||
assert_eq!(vm.object_count(), baseline);
|
assert_eq!(vm.object_count(), baseline);
|
||||||
cleanup(&mut vm, code, globals);
|
cleanup(&mut vm, code, globals);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn source_control_flow_and_exact_arithmetic_execute_real_bytecode() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let text = "function main(): Number { i=0 total=0 while i<20 { if i<10 {total=total+i} else {total=total-i} i=i+1 } return total }";
|
||||||
|
let (code, globals) = module(&mut vm, &compile(text, ParseMode::Module));
|
||||||
|
let main = function(&vm, globals, "main");
|
||||||
|
let result = vm.call(main, &[]).unwrap();
|
||||||
|
assert_eq!(result, Value::Number((-100_i64).into()));
|
||||||
|
vm.release_value(result).unwrap();
|
||||||
|
cleanup(&mut vm, code, globals);
|
||||||
|
for (text, expected) in [
|
||||||
|
("0.1 + 0.2 == 0.3", Value::Bool(true)),
|
||||||
|
(
|
||||||
|
"100000000000000000000 + 1 > 100000000000000000000",
|
||||||
|
Value::Bool(true),
|
||||||
|
),
|
||||||
|
("1 / 3 + 1 / 3 + 1 / 3", Value::Number(1_i64.into())),
|
||||||
|
("-(2 + 3)", Value::Number((-5_i64).into())),
|
||||||
|
] {
|
||||||
|
let code = compile(text, ParseMode::Expression).load(&mut vm).unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let result = vm.execute(code, globals).unwrap();
|
||||||
|
assert_eq!(result, expected);
|
||||||
|
vm.release_value(result).unwrap();
|
||||||
|
vm.release(code).unwrap();
|
||||||
|
vm.release(globals).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn control_flow_definite_initialization_large_jumps_and_loop_budgets() {
|
||||||
|
let mut body = String::from("function main(): Number { i=0 while i<2 { if i==0 {");
|
||||||
|
for _ in 0..300 {
|
||||||
|
body.push_str("i=i+0 ");
|
||||||
|
}
|
||||||
|
body.push_str("} i=i+1 } return i }");
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let (code, globals) = module(&mut vm, &compile(&body, ParseMode::Module));
|
||||||
|
let result = vm.call(function(&vm, globals, "main"), &[]).unwrap();
|
||||||
|
assert_eq!(result, Value::Number(2_i64.into()));
|
||||||
|
vm.release_value(result).unwrap();
|
||||||
|
cleanup(&mut vm, code, globals);
|
||||||
|
for text in [
|
||||||
|
"function f(): Number { if 1 {x=2} return x }",
|
||||||
|
"function f(): Number {while 0 {x=2} return x}",
|
||||||
|
] {
|
||||||
|
assert!(
|
||||||
|
compile_source(
|
||||||
|
&EndorReader::from_text("<test>", text),
|
||||||
|
ParseMode::Module,
|
||||||
|
&CompileOptions::default()
|
||||||
|
)
|
||||||
|
.is_err()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let (code, globals) = module(
|
||||||
|
&mut vm,
|
||||||
|
&compile(
|
||||||
|
"function main(): Unit { while 1 { value=2 } }",
|
||||||
|
ParseMode::Module,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
assert!(matches!(
|
||||||
|
vm.call_with_limits(
|
||||||
|
function(&vm, globals, "main"),
|
||||||
|
&[],
|
||||||
|
ExecutionLimits {
|
||||||
|
max_frames: 10,
|
||||||
|
max_instructions: 100
|
||||||
|
}
|
||||||
|
),
|
||||||
|
Err(VmError::ExecutionLimit("instructions"))
|
||||||
|
));
|
||||||
|
cleanup(&mut vm, code, globals);
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
| 文件 | 职责 |
|
| 文件 | 职责 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| [lib.rs](lib.rs) | VM 类型导出、objects 类型兼容重导出(含 ReadAccess/CallSignature/CallType/CallEffect)、宿主调用与字节码 rustdoc 示例 |
|
| [lib.rs](lib.rs) | VM 类型导出、objects 类型兼容重导出(含 ReadAccess/CallSignature/CallType/CallEffect)、宿主调用与字节码 rustdoc 示例 |
|
||||||
| [vm.rs](vm.rs) | Vm 拥有 ObjectStore 与错误位置记录;对象操作和宿主 new_class 和显式Trait注册/关系查询显式委托给 objects;函数/字段检查统一开关、统一参数区间查询与函数类型配置 |
|
| [vm.rs](vm.rs) | Vm 拥有 ObjectStore、错误位置记录与挂起的只读执行状态;对象操作和宿主 new_class 和显式/类型化Trait注册/关系查询显式委托给 objects;函数/字段检查统一开关、统一参数区间/已知调用效果查询与函数类型配置 |
|
||||||
| [frame.rs](frame.rs) | EndorFrame、栈条目、源码与原生委托返回契约执行根、执行位置元数据、ExecutionLimits |
|
| [frame.rs](frame.rs) | EndorFrame、栈条目、源码与原生委托返回契约执行根、执行位置元数据、ExecutionLimits、ExecutionProgress 与 VM 内续算帧状态 |
|
||||||
| [eval.rs](eval.rs) | execute/call、只读调用与依赖结果、参数绑定、原生实现前数量/类型/效果检查和委托返回检查、可选注解检查、源码类字段环境绑定、帧切换、取指分派、MakeFunction、结果根转移与失败清理 |
|
| [eval.rs](eval.rs) | execute/call、只读调用/可恢复分片/取消与依赖结果、参数绑定、原生实现前数量/类型/效果检查和原生/类型化Trait委托返回检查、可选注解/动态覆写兼容检查、源码类字段环境绑定、帧切换、取指分派、MakeFunction、结果根转移与失败清理 |
|
||||||
|
|
||||||
## 常见修改入口
|
## 常见修改入口
|
||||||
|
|
||||||
@@ -19,3 +19,9 @@
|
|||||||
| 新 VM 公开 API | vm.rs/eval.rs 与 lib.rs 导出 | 所属主题、示例、[tests 导航](../tests/README.md) |
|
| 新 VM 公开 API | vm.rs/eval.rs 与 lib.rs 导出 | 所属主题、示例、[tests 导航](../tests/README.md) |
|
||||||
|
|
||||||
core 不访问对象私有字段或 Heap;执行器只通过受校验的对象存储操作维护帧根。返回 [core](../README.md)。
|
core 不访问对象私有字段或 Heap;执行器只通过受校验的对象存储操作维护帧根。返回 [core](../README.md)。
|
||||||
|
|
||||||
|
独立托管调用帧:frame.rs 定义 CoroutineProgress 和私有可追踪帧载荷;eval.rs 提供 new_coroutine/resume_coroutine/cancel_coroutine,暂停帧以托管边保活,恢复时转回执行根。与跨量子只读续算分开,所有权及边界见 docs/runtime/bytecode.md。
|
||||||
|
|
||||||
|
eval.rs的LoadGlobal及可恢复只读结果支持ReadDependency;frame.rs携带精确观察,lib.rs重导出只读观察类型,兼容旧reads对象列表。
|
||||||
|
|
||||||
|
同步call_read_only_precise_with_limits同样返回ReadSet,供宿主精确校验缓存;原call_read_only_with_limits继续返回保守objects列表,预算、结果拥有式引用和失败关闭只读作用域的语义不变。
|
||||||
|
|||||||
+569
-25
@@ -1,9 +1,11 @@
|
|||||||
use crate::frame::{NativeReturn, StackEntry};
|
use crate::ExecutionProgress;
|
||||||
|
use crate::frame::{Coroutine, CoroutineState, NativeReturn, StackEntry, SuspendedCall};
|
||||||
use crate::{
|
use crate::{
|
||||||
CompareOp, EndorCellObject, EndorCodeObject, EndorFrame, EndorFunctionObject,
|
CompareOp, EndorCellObject, EndorCodeObject, EndorFrame, EndorFunctionObject,
|
||||||
EndorStringObject, ExecutionLimits, ExecutionLocation, Instruction, ObjectRef, Opcode, Value,
|
EndorStringObject, ExecutionLimits, ExecutionLocation, Instruction, ObjectRef, Opcode, Value,
|
||||||
Vm, VmError,
|
Vm, VmError,
|
||||||
};
|
};
|
||||||
|
use crate::{CoroutineProgress, ObjectPayload};
|
||||||
use crate::{NativeContext, NativeResult};
|
use crate::{NativeContext, NativeResult};
|
||||||
|
|
||||||
enum PreparedCall {
|
enum PreparedCall {
|
||||||
@@ -57,9 +59,23 @@ impl Vm {
|
|||||||
arguments: &[Value],
|
arguments: &[Value],
|
||||||
limits: ExecutionLimits,
|
limits: ExecutionLimits,
|
||||||
) -> Result<(Value, Vec<ObjectRef>), VmError> {
|
) -> Result<(Value, Vec<ObjectRef>), VmError> {
|
||||||
|
let (value, reads) =
|
||||||
|
self.call_read_only_precise_with_limits(function, arguments, limits)?;
|
||||||
|
Ok((value, reads.objects))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Synchronous read-only call with precise observations as well as the
|
||||||
|
/// conservative object list. Result ownership and hard limits match call().
|
||||||
|
/// ReadSet and all identities/value snapshots inside it are borrowed only.
|
||||||
|
pub fn call_read_only_precise_with_limits(
|
||||||
|
&mut self,
|
||||||
|
function: ObjectRef,
|
||||||
|
arguments: &[Value],
|
||||||
|
limits: ExecutionLimits,
|
||||||
|
) -> Result<(Value, crate::ReadSet), VmError> {
|
||||||
let scope = self.objects.begin_read_only();
|
let scope = self.objects.begin_read_only();
|
||||||
let result = self.call_with_limits(function, arguments, limits);
|
let result = self.call_with_limits(function, arguments, limits);
|
||||||
let reads = self.objects.end_read_only(scope)?;
|
let reads = self.objects.end_read_only_precise(scope)?;
|
||||||
result.map(|value| (value, reads))
|
result.map(|value| (value, reads))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,6 +85,11 @@ impl Vm {
|
|||||||
arguments: &[Value],
|
arguments: &[Value],
|
||||||
limits: ExecutionLimits,
|
limits: ExecutionLimits,
|
||||||
) -> Result<Value, VmError> {
|
) -> Result<Value, VmError> {
|
||||||
|
if self.suspended_call.is_some() {
|
||||||
|
return Err(VmError::UnsupportedOperation(
|
||||||
|
"call while read-only execution is suspended",
|
||||||
|
));
|
||||||
|
}
|
||||||
self.last_traceback.clear();
|
self.last_traceback.clear();
|
||||||
let mut executed = 0;
|
let mut executed = 0;
|
||||||
let frame = match self.prepare_call(function, arguments.to_vec(), &mut executed, limits)? {
|
let frame = match self.prepare_call(function, arguments.to_vec(), &mut executed, limits)? {
|
||||||
@@ -80,7 +101,9 @@ impl Vm {
|
|||||||
return Err(VmError::ExecutionLimit("frames"));
|
return Err(VmError::ExecutionLimit("frames"));
|
||||||
}
|
}
|
||||||
let mut frames = vec![frame];
|
let mut frames = vec![frame];
|
||||||
let result = self.eval_frames(&mut frames, limits, &mut executed);
|
let result = self
|
||||||
|
.eval_frames(&mut frames, limits, &mut executed, usize::MAX, false)
|
||||||
|
.map(|value| value.expect("unbounded slice completes or fails"));
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
self.last_traceback = frames
|
self.last_traceback = frames
|
||||||
.iter()
|
.iter()
|
||||||
@@ -98,6 +121,384 @@ impl Vm {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Begin a resumable read-only call. One VM owns at most one continuation.
|
||||||
|
/// A slice is a soft instruction quantum: native callbacks and delegation
|
||||||
|
/// complete atomically and remain subject to the cumulative hard limits.
|
||||||
|
pub fn start_read_only_call(
|
||||||
|
&mut self,
|
||||||
|
function: ObjectRef,
|
||||||
|
arguments: &[Value],
|
||||||
|
limits: ExecutionLimits,
|
||||||
|
slice: usize,
|
||||||
|
) -> Result<ExecutionProgress, VmError> {
|
||||||
|
if slice == 0 {
|
||||||
|
return Err(VmError::InvalidArgument("execution slice must be positive"));
|
||||||
|
}
|
||||||
|
if self.suspended_call.is_some() || self.objects.is_read_only() {
|
||||||
|
return Err(VmError::UnsupportedOperation(
|
||||||
|
"start suspended call inside another execution",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
self.last_traceback.clear();
|
||||||
|
let scope = self.objects.begin_read_only();
|
||||||
|
let mut executed = 0;
|
||||||
|
let prepared = self.prepare_call(function, arguments.to_vec(), &mut executed, limits);
|
||||||
|
match prepared {
|
||||||
|
Ok(PreparedCall::Value(value)) => {
|
||||||
|
let reads = match self.objects.end_read_only_precise(scope) {
|
||||||
|
Ok(reads) => reads,
|
||||||
|
Err(error) => {
|
||||||
|
self.drop_root(value);
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(ExecutionProgress::Complete {
|
||||||
|
value,
|
||||||
|
dependencies: reads.dependencies,
|
||||||
|
reads: reads.objects,
|
||||||
|
instructions: executed,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Ok(PreparedCall::Frame(frame)) => {
|
||||||
|
if limits.max_frames == 0 {
|
||||||
|
self.clear_frame(frame);
|
||||||
|
self.objects.end_read_only(scope)?;
|
||||||
|
return Err(VmError::ExecutionLimit("frames"));
|
||||||
|
}
|
||||||
|
self.suspended_call = Some(SuspendedCall {
|
||||||
|
frames: vec![frame],
|
||||||
|
scope,
|
||||||
|
limits,
|
||||||
|
executed,
|
||||||
|
});
|
||||||
|
// Preparation counts toward the first quantum as well.
|
||||||
|
self.resume_read_only_to(slice)
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
self.objects.end_read_only(scope)?;
|
||||||
|
Err(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_suspended_call(&self) -> bool {
|
||||||
|
self.suspended_call.is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resume existing frames, never replay earlier instructions. The read-only
|
||||||
|
/// scope remains active across Pending; preexisting heap writes stay barred.
|
||||||
|
pub fn resume_read_only_call(&mut self, slice: usize) -> Result<ExecutionProgress, VmError> {
|
||||||
|
if slice == 0 {
|
||||||
|
return Err(VmError::InvalidArgument("execution slice must be positive"));
|
||||||
|
}
|
||||||
|
let executed = self
|
||||||
|
.suspended_call
|
||||||
|
.as_ref()
|
||||||
|
.ok_or(VmError::InvalidArgument("no suspended call"))?
|
||||||
|
.executed;
|
||||||
|
self.resume_read_only_to(executed.saturating_add(slice))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resume_read_only_to(&mut self, slice_end: usize) -> Result<ExecutionProgress, VmError> {
|
||||||
|
let mut call = self
|
||||||
|
.suspended_call
|
||||||
|
.take()
|
||||||
|
.ok_or(VmError::InvalidArgument("no suspended call"))?;
|
||||||
|
let result = self.eval_frames(
|
||||||
|
&mut call.frames,
|
||||||
|
call.limits,
|
||||||
|
&mut call.executed,
|
||||||
|
slice_end,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
if let Ok(None) = result {
|
||||||
|
let instructions = call.executed;
|
||||||
|
self.suspended_call = Some(call);
|
||||||
|
return Ok(ExecutionProgress::Pending { instructions });
|
||||||
|
}
|
||||||
|
if result.is_err() {
|
||||||
|
self.last_traceback = call
|
||||||
|
.frames
|
||||||
|
.iter()
|
||||||
|
.map(|frame| ExecutionLocation {
|
||||||
|
function: frame.name.clone(),
|
||||||
|
filename: frame.executable.filename().into(),
|
||||||
|
first_line: frame.executable.first_line(),
|
||||||
|
instruction_offset: frame.current_offset,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
while let Some(frame) = call.frames.pop() {
|
||||||
|
self.clear_frame(frame);
|
||||||
|
}
|
||||||
|
let reads = self.objects.end_read_only_precise(call.scope);
|
||||||
|
match (result, reads) {
|
||||||
|
(Ok(Some(value)), Ok(reads)) => Ok(ExecutionProgress::Complete {
|
||||||
|
value,
|
||||||
|
dependencies: reads.dependencies,
|
||||||
|
reads: reads.objects,
|
||||||
|
instructions: call.executed,
|
||||||
|
}),
|
||||||
|
(Ok(Some(value)), Err(error)) => {
|
||||||
|
self.drop_root(value);
|
||||||
|
Err(error)
|
||||||
|
}
|
||||||
|
(Err(error), _) => Err(error),
|
||||||
|
(Ok(None), _) => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Release all retained execution roots and close the read-only scope.
|
||||||
|
pub fn cancel_read_only_call(&mut self) -> Result<bool, VmError> {
|
||||||
|
let Some(mut call) = self.suspended_call.take() else {
|
||||||
|
return Ok(false);
|
||||||
|
};
|
||||||
|
while let Some(frame) = call.frames.pop() {
|
||||||
|
self.clear_frame(frame);
|
||||||
|
}
|
||||||
|
self.objects.end_read_only(call.scope)?;
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Prepare a managed independently resumable call. Native delegation may
|
||||||
|
/// run during preparation; source instructions start only on resume.
|
||||||
|
/// The returned handle owns one external reference, like every allocation.
|
||||||
|
pub fn new_coroutine(
|
||||||
|
&mut self,
|
||||||
|
function: ObjectRef,
|
||||||
|
arguments: &[Value],
|
||||||
|
limits: ExecutionLimits,
|
||||||
|
) -> Result<ObjectRef, VmError> {
|
||||||
|
self.check_coroutine_entry()?;
|
||||||
|
self.last_traceback.clear();
|
||||||
|
let mut executed = 0;
|
||||||
|
let prepared = self.prepare_call(function, arguments.to_vec(), &mut executed, limits)?;
|
||||||
|
let state = match prepared {
|
||||||
|
PreparedCall::Frame(frame) => {
|
||||||
|
if limits.max_frames == 0 {
|
||||||
|
self.clear_frame(frame);
|
||||||
|
return Err(VmError::ExecutionLimit("frames"));
|
||||||
|
}
|
||||||
|
CoroutineState::Frames(std::sync::Arc::new(vec![frame]))
|
||||||
|
}
|
||||||
|
PreparedCall::Value(value) => CoroutineState::Ready(value),
|
||||||
|
};
|
||||||
|
// Allocate traced edges before releasing the original execution roots.
|
||||||
|
let coroutine = Coroutine {
|
||||||
|
state,
|
||||||
|
limits,
|
||||||
|
executed,
|
||||||
|
};
|
||||||
|
let mut roots = Vec::new();
|
||||||
|
coroutine.trace(&mut |reference| roots.push(reference));
|
||||||
|
let result = self.allocate(coroutine);
|
||||||
|
for root in roots {
|
||||||
|
self.release(root)?;
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_coroutine_entry(&self) -> Result<(), VmError> {
|
||||||
|
if self.has_suspended_call() || self.objects.is_read_only() {
|
||||||
|
return Err(VmError::UnsupportedOperation(
|
||||||
|
"coroutine operation inside read-only execution",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resume one managed call, preserving cumulative hard limits. Other calls
|
||||||
|
/// and heap writes may execute between Pending results. No prior instruction
|
||||||
|
/// is replayed. Completion returns an owned value and consumes the frames.
|
||||||
|
pub fn resume_coroutine(
|
||||||
|
&mut self,
|
||||||
|
reference: ObjectRef,
|
||||||
|
slice: usize,
|
||||||
|
) -> Result<CoroutineProgress, VmError> {
|
||||||
|
self.retain(reference)?;
|
||||||
|
let result = self.resume_coroutine_inner(reference, slice);
|
||||||
|
self.release(reference)?;
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resume_coroutine_inner(
|
||||||
|
&mut self,
|
||||||
|
reference: ObjectRef,
|
||||||
|
slice: usize,
|
||||||
|
) -> Result<CoroutineProgress, VmError> {
|
||||||
|
self.check_coroutine_entry()?;
|
||||||
|
if slice == 0 {
|
||||||
|
return Err(VmError::InvalidArgument("execution slice must be positive"));
|
||||||
|
}
|
||||||
|
let coroutine = self.payload::<Coroutine>(reference, "coroutine")?;
|
||||||
|
let limits = coroutine.limits;
|
||||||
|
let mut executed = coroutine.executed;
|
||||||
|
let state = match &coroutine.state {
|
||||||
|
CoroutineState::Frames(frames) => CoroutineState::Frames(frames.clone()),
|
||||||
|
CoroutineState::Ready(value) => CoroutineState::Ready(value.clone()),
|
||||||
|
_ => return Err(VmError::InvalidArgument("coroutine is running or finished")),
|
||||||
|
};
|
||||||
|
let mut roots = Vec::new();
|
||||||
|
match &state {
|
||||||
|
CoroutineState::Frames(frames) => {
|
||||||
|
for frame in frames.iter() {
|
||||||
|
frame.trace_roots(&mut |root| roots.push(root));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CoroutineState::Ready(Value::Object(root)) => roots.push(*root),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
// Transfer edges to execution roots transactionally, before replacing
|
||||||
|
// the managed payload. Duplicate edges must retain duplicate roots.
|
||||||
|
let mut retained = Vec::new();
|
||||||
|
for root in roots {
|
||||||
|
if let Err(error) = self.retain(root) {
|
||||||
|
for root in retained {
|
||||||
|
self.release(root)?;
|
||||||
|
}
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
retained.push(root);
|
||||||
|
}
|
||||||
|
if let Err(error) = self.replace_payload(
|
||||||
|
reference,
|
||||||
|
Coroutine {
|
||||||
|
state: CoroutineState::Running,
|
||||||
|
limits,
|
||||||
|
executed,
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
for root in retained {
|
||||||
|
self.release(root)?;
|
||||||
|
}
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
self.last_traceback.clear();
|
||||||
|
let mut frames = match state {
|
||||||
|
CoroutineState::Frames(frames) => {
|
||||||
|
std::sync::Arc::try_unwrap(frames).expect("private frame storage has one owner")
|
||||||
|
}
|
||||||
|
CoroutineState::Ready(value) => {
|
||||||
|
if let Err(error) = self.replace_payload(
|
||||||
|
reference,
|
||||||
|
Coroutine {
|
||||||
|
state: CoroutineState::Finished,
|
||||||
|
limits,
|
||||||
|
executed,
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
self.drop_root(value);
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
return Ok(CoroutineProgress::Complete {
|
||||||
|
value,
|
||||||
|
instructions: executed,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
let slice_end = executed.saturating_add(slice);
|
||||||
|
let result = self.eval_frames(&mut frames, limits, &mut executed, slice_end, true);
|
||||||
|
if let Ok(None) = result {
|
||||||
|
let waiting = frames.last().filter(|frame| frame.waiting).map(|frame| {
|
||||||
|
match Self::top_value(frame).expect("await operand remains on stack") {
|
||||||
|
Value::Object(reference) => reference,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let frames = std::sync::Arc::new(frames);
|
||||||
|
let mut roots = Vec::new();
|
||||||
|
for frame in frames.iter() {
|
||||||
|
frame.trace_roots(&mut |root| roots.push(root));
|
||||||
|
}
|
||||||
|
let published = self.replace_payload(
|
||||||
|
reference,
|
||||||
|
Coroutine {
|
||||||
|
state: CoroutineState::Frames(frames),
|
||||||
|
limits,
|
||||||
|
executed,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
for root in roots {
|
||||||
|
self.release(root)?;
|
||||||
|
}
|
||||||
|
published?;
|
||||||
|
if let Some(awaitable) = waiting {
|
||||||
|
return Ok(CoroutineProgress::Waiting {
|
||||||
|
awaitable,
|
||||||
|
instructions: executed,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return Ok(CoroutineProgress::Pending {
|
||||||
|
instructions: executed,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if result.is_err() {
|
||||||
|
self.last_traceback = frames
|
||||||
|
.iter()
|
||||||
|
.map(|frame| ExecutionLocation {
|
||||||
|
function: frame.name.clone(),
|
||||||
|
filename: frame.executable.filename().into(),
|
||||||
|
first_line: frame.executable.first_line(),
|
||||||
|
instruction_offset: frame.current_offset,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
while let Some(frame) = frames.pop() {
|
||||||
|
self.clear_frame(frame);
|
||||||
|
}
|
||||||
|
let finished = self.replace_payload(
|
||||||
|
reference,
|
||||||
|
Coroutine {
|
||||||
|
state: CoroutineState::Finished,
|
||||||
|
limits,
|
||||||
|
executed,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
match result {
|
||||||
|
Ok(Some(value)) => {
|
||||||
|
if let Err(error) = finished {
|
||||||
|
self.drop_root(value);
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
Ok(CoroutineProgress::Complete {
|
||||||
|
value,
|
||||||
|
instructions: executed,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
finished?;
|
||||||
|
Err(error)
|
||||||
|
}
|
||||||
|
Ok(None) => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cancellation releases managed frame edges; dropping an unreachable
|
||||||
|
/// coroutine has the same ownership effect, including cycles through locals.
|
||||||
|
/// Writes performed by previous quanta are never rolled back.
|
||||||
|
pub fn cancel_coroutine(&mut self, reference: ObjectRef) -> Result<bool, VmError> {
|
||||||
|
self.check_coroutine_entry()?;
|
||||||
|
let coroutine = self.payload::<Coroutine>(reference, "coroutine")?;
|
||||||
|
if matches!(coroutine.state, CoroutineState::Running) {
|
||||||
|
return Err(VmError::InvalidArgument("coroutine is running"));
|
||||||
|
}
|
||||||
|
if matches!(coroutine.state, CoroutineState::Finished) {
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
let limits = coroutine.limits;
|
||||||
|
let executed = coroutine.executed;
|
||||||
|
self.replace_payload(
|
||||||
|
reference,
|
||||||
|
Coroutine {
|
||||||
|
state: CoroutineState::Finished,
|
||||||
|
limits,
|
||||||
|
executed,
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
|
||||||
/// Outer-to-inner execution locations from the last failed call/execute.
|
/// Outer-to-inner execution locations from the last failed call/execute.
|
||||||
/// These are copied metadata, not heap references or language exceptions.
|
/// These are copied metadata, not heap references or language exceptions.
|
||||||
pub fn last_traceback(&self) -> &[ExecutionLocation] {
|
pub fn last_traceback(&self) -> &[ExecutionLocation] {
|
||||||
@@ -166,14 +567,29 @@ impl Vm {
|
|||||||
for value in arguments {
|
for value in arguments {
|
||||||
self.validate_value(value)?;
|
self.validate_value(value)?;
|
||||||
}
|
}
|
||||||
let mut return_type = None;
|
let receiver = arguments
|
||||||
if self.source_type_checks
|
.first()
|
||||||
&& let Some(signature) = executable.source_signature()
|
.or_else(|| {
|
||||||
{
|
if required == 0 {
|
||||||
let receiver = arguments.first().and_then(|value| match value {
|
data.defaults().first()
|
||||||
Value::Object(object) => Some(*object),
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.and_then(|value| match value {
|
||||||
|
Value::Object(receiver) => Some(*receiver),
|
||||||
_ => None,
|
_ => None,
|
||||||
});
|
});
|
||||||
|
let typed_method = if let Some(receiver) = receiver {
|
||||||
|
self.objects
|
||||||
|
.check_method_contract_call(function, receiver, self.source_type_checks)?
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
let mut return_type = None;
|
||||||
|
if (self.source_type_checks || typed_method)
|
||||||
|
&& let Some(signature) = executable.source_signature()
|
||||||
|
{
|
||||||
for (index, expected_name) in signature.parameters.iter().enumerate() {
|
for (index, expected_name) in signature.parameters.iter().enumerate() {
|
||||||
let value = arguments
|
let value = arguments
|
||||||
.get(index)
|
.get(index)
|
||||||
@@ -214,11 +630,20 @@ impl Vm {
|
|||||||
name: data.name().into(),
|
name: data.name().into(),
|
||||||
instruction_offset: 0,
|
instruction_offset: 0,
|
||||||
current_offset: 0,
|
current_offset: 0,
|
||||||
|
waiting: false,
|
||||||
localsplus: vec![None; executable.slot_count()],
|
localsplus: vec![None; executable.slot_count()],
|
||||||
stack: Vec::with_capacity(executable.stack_size()),
|
stack: Vec::with_capacity(executable.stack_size()),
|
||||||
executable,
|
executable,
|
||||||
};
|
};
|
||||||
let initialized = (|| -> Result<(), VmError> {
|
let initialized = (|| -> Result<(), VmError> {
|
||||||
|
if let Some(receiver) = receiver {
|
||||||
|
for (name, expected) in self.objects.method_trait_returns(function, receiver)? {
|
||||||
|
self.push_native_return(
|
||||||
|
&mut frame.native_returns,
|
||||||
|
NativeReturn { name, expected },
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
let defaults = data.defaults().iter().skip(arguments.len() - required);
|
let defaults = data.defaults().iter().skip(arguments.len() - required);
|
||||||
for (slot, value) in frame
|
for (slot, value) in frame
|
||||||
.localsplus
|
.localsplus
|
||||||
@@ -262,6 +687,13 @@ impl Vm {
|
|||||||
for argument in arguments {
|
for argument in arguments {
|
||||||
self.validate_value(argument)?;
|
self.validate_value(argument)?;
|
||||||
}
|
}
|
||||||
|
if let Some(Value::Object(receiver)) = arguments.first() {
|
||||||
|
self.objects.check_method_contract_call(
|
||||||
|
function,
|
||||||
|
*receiver,
|
||||||
|
self.source_type_checks,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
if let Some(arity) = self.objects.call_arity(function)?
|
if let Some(arity) = self.objects.call_arity(function)?
|
||||||
&& !arity.accepts(arguments.len())
|
&& !arity.accepts(arguments.len())
|
||||||
{
|
{
|
||||||
@@ -305,7 +737,9 @@ impl Vm {
|
|||||||
.is::<EndorFunctionObject>()
|
.is::<EndorFunctionObject>()
|
||||||
{
|
{
|
||||||
let mut frame = self.prepare_frame(function, &arguments)?;
|
let mut frame = self.prepare_frame(function, &arguments)?;
|
||||||
|
let mut local = std::mem::take(&mut frame.native_returns);
|
||||||
frame.native_returns = std::mem::take(&mut returns);
|
frame.native_returns = std::mem::take(&mut returns);
|
||||||
|
frame.native_returns.append(&mut local);
|
||||||
return Ok(PreparedCall::Frame(frame));
|
return Ok(PreparedCall::Frame(frame));
|
||||||
}
|
}
|
||||||
if *executed >= limits.max_instructions {
|
if *executed >= limits.max_instructions {
|
||||||
@@ -319,17 +753,12 @@ impl Vm {
|
|||||||
name: self.object(function)?.payload().kind_name().into(),
|
name: self.object(function)?.payload().kind_name().into(),
|
||||||
expected: signature.returns,
|
expected: signature.returns,
|
||||||
};
|
};
|
||||||
let mut retained = Vec::new();
|
self.push_native_return(&mut returns, check)?;
|
||||||
for reference in check.expected.references() {
|
|
||||||
if let Err(error) = self.retain(reference) {
|
|
||||||
for reference in retained {
|
|
||||||
self.release(reference)?;
|
|
||||||
}
|
}
|
||||||
return Err(error);
|
if let Some(Value::Object(receiver)) = arguments.first() {
|
||||||
|
for (name, expected) in self.objects.method_trait_returns(function, *receiver)? {
|
||||||
|
self.push_native_return(&mut returns, NativeReturn { name, expected })?;
|
||||||
}
|
}
|
||||||
retained.push(reference);
|
|
||||||
}
|
|
||||||
returns.push(check);
|
|
||||||
}
|
}
|
||||||
match self.invoke_native(function, &arguments)? {
|
match self.invoke_native(function, &arguments)? {
|
||||||
NativeResult::Return(value) => {
|
NativeResult::Return(value) => {
|
||||||
@@ -368,6 +797,24 @@ impl Vm {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn push_native_return(
|
||||||
|
&mut self,
|
||||||
|
returns: &mut Vec<NativeReturn>,
|
||||||
|
check: NativeReturn,
|
||||||
|
) -> Result<(), VmError> {
|
||||||
|
let mut retained = Vec::new();
|
||||||
|
for reference in check.expected.references() {
|
||||||
|
if let Err(error) = self.retain(reference) {
|
||||||
|
for reference in retained {
|
||||||
|
self.release(reference)?;
|
||||||
|
}
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
retained.push(reference);
|
||||||
|
}
|
||||||
|
returns.push(check);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
fn clear_native_returns(&mut self, returns: Vec<NativeReturn>) {
|
fn clear_native_returns(&mut self, returns: Vec<NativeReturn>) {
|
||||||
for check in returns {
|
for check in returns {
|
||||||
for reference in check.expected.references() {
|
for reference in check.expected.references() {
|
||||||
@@ -454,14 +901,38 @@ impl Vm {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn number_value<'a>(
|
||||||
|
&'a self,
|
||||||
|
value: &'a Value,
|
||||||
|
) -> Result<Option<&'a crate::EndorNumber>, VmError> {
|
||||||
|
Ok(match value {
|
||||||
|
Value::Number(n) => Some(n),
|
||||||
|
Value::Object(o) => self
|
||||||
|
.object(*o)?
|
||||||
|
.payload()
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<crate::EndorNumberObject>()
|
||||||
|
.map(|n| n.value()),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn compare_values(
|
fn compare_values(
|
||||||
&self,
|
&self,
|
||||||
left: &Value,
|
left: &Value,
|
||||||
right: &Value,
|
right: &Value,
|
||||||
operation: CompareOp,
|
operation: CompareOp,
|
||||||
) -> Result<bool, VmError> {
|
) -> Result<bool, VmError> {
|
||||||
if matches!(left, Value::Number(_)) || matches!(right, Value::Number(_)) {
|
let (left_number, right_number) = (self.number_value(left)?, self.number_value(right)?);
|
||||||
return Err(VmError::UnsupportedOperation("numeric comparison"));
|
if let (Some(left), Some(right)) = (left_number, right_number) {
|
||||||
|
return left.numeric_compare(right, operation);
|
||||||
|
}
|
||||||
|
if left_number.is_some() || right_number.is_some() {
|
||||||
|
return match operation {
|
||||||
|
CompareOp::Eq => Ok(false),
|
||||||
|
CompareOp::Ne => Ok(true),
|
||||||
|
_ => Err(VmError::UnsupportedOperation("mixed numeric ordering")),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
if let (Some(left), Some(right)) = (self.text_value(left)?, self.text_value(right)?) {
|
if let (Some(left), Some(right)) = (self.text_value(left)?, self.text_value(right)?) {
|
||||||
return Ok(match operation {
|
return Ok(match operation {
|
||||||
@@ -568,11 +1039,16 @@ impl Vm {
|
|||||||
frames: &mut Vec<EndorFrame>,
|
frames: &mut Vec<EndorFrame>,
|
||||||
limits: ExecutionLimits,
|
limits: ExecutionLimits,
|
||||||
executed: &mut usize,
|
executed: &mut usize,
|
||||||
) -> Result<Value, VmError> {
|
slice_end: usize,
|
||||||
|
allow_wait: bool,
|
||||||
|
) -> Result<Option<Value>, VmError> {
|
||||||
loop {
|
loop {
|
||||||
if *executed >= limits.max_instructions {
|
if *executed >= limits.max_instructions {
|
||||||
return Err(VmError::ExecutionLimit("instructions"));
|
return Err(VmError::ExecutionLimit("instructions"));
|
||||||
}
|
}
|
||||||
|
if *executed >= slice_end {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
*executed += 1;
|
*executed += 1;
|
||||||
let frame = frames.last_mut().expect("an evaluation frame is active");
|
let frame = frames.last_mut().expect("an evaluation frame is active");
|
||||||
let decoded = frame.executable.instruction_at(frame.instruction_offset)?;
|
let decoded = frame.executable.instruction_at(frame.instruction_offset)?;
|
||||||
@@ -581,6 +1057,36 @@ impl Vm {
|
|||||||
let opcode = decoded.instruction.opcode;
|
let opcode = decoded.instruction.opcode;
|
||||||
let argument = decoded.instruction.argument as usize;
|
let argument = decoded.instruction.argument as usize;
|
||||||
match opcode {
|
match opcode {
|
||||||
|
Opcode::Await => {
|
||||||
|
if self.objects.is_read_only() {
|
||||||
|
return Err(VmError::UnsupportedOperation(
|
||||||
|
"await during read-only execution",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let Value::Object(awaitable) = Self::top_value(frame)? else {
|
||||||
|
return Err(VmError::ObjectKindMismatch("awaitable object"));
|
||||||
|
};
|
||||||
|
match self.object(awaitable)?.payload().poll_await()? {
|
||||||
|
crate::AwaitPoll::Pending => {
|
||||||
|
if !allow_wait {
|
||||||
|
return Err(VmError::UnsupportedOperation(
|
||||||
|
"pending await outside a coroutine",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
// Keep the evaluated operand, never replay its call
|
||||||
|
// or the statements before this await instruction.
|
||||||
|
frame.waiting = true;
|
||||||
|
frame.instruction_offset = decoded.offset;
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
crate::AwaitPoll::Ready(value) => {
|
||||||
|
self.retain_value(&value)?;
|
||||||
|
self.discard(frame, 1);
|
||||||
|
frame.stack.push(StackEntry::Value(value));
|
||||||
|
frame.waiting = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Opcode::Nop => {}
|
Opcode::Nop => {}
|
||||||
Opcode::LoadConst => {
|
Opcode::LoadConst => {
|
||||||
let value = frame.executable.constants()[argument].clone();
|
let value = frame.executable.constants()[argument].clone();
|
||||||
@@ -611,12 +1117,11 @@ impl Vm {
|
|||||||
}
|
}
|
||||||
Opcode::LoadGlobal => {
|
Opcode::LoadGlobal => {
|
||||||
let name = &frame.executable.layout().names[argument];
|
let name = &frame.executable.layout().names[argument];
|
||||||
let key = Value::Text(name.clone());
|
let mut value = self.objects.dict_get_name(frame.globals, name)?;
|
||||||
let mut value = self.dict_get(frame.globals, &key)?;
|
|
||||||
if value.is_none()
|
if value.is_none()
|
||||||
&& let Some(builtins) = frame.builtins
|
&& let Some(builtins) = frame.builtins
|
||||||
{
|
{
|
||||||
value = self.dict_get(builtins, &key)?;
|
value = self.objects.dict_get_name(builtins, name)?;
|
||||||
}
|
}
|
||||||
let value = value.ok_or_else(|| VmError::NameNotFound(name.clone()))?;
|
let value = value.ok_or_else(|| VmError::NameNotFound(name.clone()))?;
|
||||||
self.push_borrowed(frame, value)?;
|
self.push_borrowed(frame, value)?;
|
||||||
@@ -737,7 +1242,7 @@ impl Vm {
|
|||||||
if let Some(caller) = frames.last_mut() {
|
if let Some(caller) = frames.last_mut() {
|
||||||
caller.stack.push(StackEntry::Value(result));
|
caller.stack.push(StackEntry::Value(result));
|
||||||
} else {
|
} else {
|
||||||
return Ok(result);
|
return Ok(Some(result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Opcode::JumpForward | Opcode::JumpBackward => {
|
Opcode::JumpForward | Opcode::JumpBackward => {
|
||||||
@@ -761,6 +1266,45 @@ impl Vm {
|
|||||||
},
|
},
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
Opcode::BinaryArithmetic => {
|
||||||
|
if decoded.instruction.argument == 0 {
|
||||||
|
let left = Self::stack_value(frame, frame.stack.len() - 2)?;
|
||||||
|
let right = Self::top_value(frame)?;
|
||||||
|
if let (Value::Text(a), Value::Text(b)) = (left, right) {
|
||||||
|
if a.len().saturating_add(b.len()) > 1_048_576 {
|
||||||
|
return Err(VmError::InvalidArgument(
|
||||||
|
"text concatenation exceeds operation budget",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
self.discard(frame, 2);
|
||||||
|
frame.stack.push(StackEntry::Value(Value::Text(a + &b)));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let left = Self::stack_value(frame, frame.stack.len() - 2)?;
|
||||||
|
let right = Self::top_value(frame)?;
|
||||||
|
let left = self
|
||||||
|
.number_value(&left)?
|
||||||
|
.ok_or(VmError::ObjectKindMismatch("Number"))?;
|
||||||
|
let right = self
|
||||||
|
.number_value(&right)?
|
||||||
|
.ok_or(VmError::ObjectKindMismatch("Number"))?;
|
||||||
|
let result = left.arithmetic(
|
||||||
|
right,
|
||||||
|
crate::ArithmeticOp::from_argument(decoded.instruction.argument).unwrap(),
|
||||||
|
)?;
|
||||||
|
self.discard(frame, 2);
|
||||||
|
frame.stack.push(StackEntry::Value(Value::Number(result)));
|
||||||
|
}
|
||||||
|
Opcode::UnaryNegate => {
|
||||||
|
let value = Self::top_value(frame)?;
|
||||||
|
let value = self
|
||||||
|
.number_value(&value)?
|
||||||
|
.ok_or(VmError::ObjectKindMismatch("Number"))?;
|
||||||
|
let result = value.negated()?;
|
||||||
|
self.discard(frame, 1);
|
||||||
|
frame.stack.push(StackEntry::Value(Value::Number(result)));
|
||||||
|
}
|
||||||
Opcode::CompareOp => {
|
Opcode::CompareOp => {
|
||||||
let left = Self::stack_value(frame, frame.stack.len() - 2)?;
|
let left = Self::stack_value(frame, frame.stack.len() - 2)?;
|
||||||
let right = Self::top_value(frame)?;
|
let right = Self::top_value(frame)?;
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ pub struct EndorFrame {
|
|||||||
pub(crate) name: String,
|
pub(crate) name: String,
|
||||||
pub(crate) instruction_offset: usize,
|
pub(crate) instruction_offset: usize,
|
||||||
pub(crate) current_offset: usize,
|
pub(crate) current_offset: usize,
|
||||||
|
pub(crate) waiting: bool,
|
||||||
pub(crate) localsplus: Vec<Option<Value>>,
|
pub(crate) localsplus: Vec<Option<Value>>,
|
||||||
pub(crate) stack: Vec<StackEntry>,
|
pub(crate) stack: Vec<StackEntry>,
|
||||||
}
|
}
|
||||||
@@ -91,3 +92,106 @@ impl Default for ExecutionLimits {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A read-only call either retains its VM-owned continuation or returns an
|
||||||
|
/// owned value plus borrowed read dependencies. Instruction counts accumulate.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum ExecutionProgress {
|
||||||
|
Pending {
|
||||||
|
instructions: usize,
|
||||||
|
},
|
||||||
|
Complete {
|
||||||
|
value: Value,
|
||||||
|
reads: Vec<ObjectRef>,
|
||||||
|
dependencies: Vec<crate::ReadDependency>,
|
||||||
|
instructions: usize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct SuspendedCall {
|
||||||
|
pub frames: Vec<EndorFrame>,
|
||||||
|
pub scope: usize,
|
||||||
|
pub limits: ExecutionLimits,
|
||||||
|
pub executed: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Independently parked mutable execution. Unlike read-only slicing, it does
|
||||||
|
/// not keep a VM-wide heap scope active between quanta.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum CoroutineProgress {
|
||||||
|
Waiting {
|
||||||
|
awaitable: ObjectRef,
|
||||||
|
instructions: usize,
|
||||||
|
},
|
||||||
|
Pending {
|
||||||
|
instructions: usize,
|
||||||
|
},
|
||||||
|
Complete {
|
||||||
|
value: Value,
|
||||||
|
instructions: usize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) enum CoroutineState {
|
||||||
|
Frames(Arc<Vec<EndorFrame>>),
|
||||||
|
Ready(Value),
|
||||||
|
Running,
|
||||||
|
Finished,
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct Coroutine {
|
||||||
|
pub state: CoroutineState,
|
||||||
|
pub limits: ExecutionLimits,
|
||||||
|
pub executed: usize,
|
||||||
|
}
|
||||||
|
impl EndorFrame {
|
||||||
|
/// Exactly the references held as execution roots while this frame runs.
|
||||||
|
/// code/globals/builtins remain reachable through its owning function.
|
||||||
|
pub(crate) fn trace_roots(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||||
|
visit(self.function);
|
||||||
|
if let Some(reference) = self.return_type.and_then(|kind| kind.reference()) {
|
||||||
|
visit(reference);
|
||||||
|
}
|
||||||
|
for check in &self.native_returns {
|
||||||
|
for reference in check.expected.references() {
|
||||||
|
visit(reference);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for value in self
|
||||||
|
.localsplus
|
||||||
|
.iter()
|
||||||
|
.flatten()
|
||||||
|
.chain(self.stack.iter().filter_map(|entry| match entry {
|
||||||
|
StackEntry::Value(value) => Some(value),
|
||||||
|
StackEntry::Null => None,
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
if let Value::Object(reference) = value {
|
||||||
|
visit(*reference);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl crate::ObjectPayload for Coroutine {
|
||||||
|
fn as_any(&self) -> &dyn std::any::Any {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
fn kind_name(&self) -> &'static str {
|
||||||
|
"coroutine"
|
||||||
|
}
|
||||||
|
fn reference_assignment_only(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||||
|
match &self.state {
|
||||||
|
CoroutineState::Frames(frames) => {
|
||||||
|
for frame in frames.iter() {
|
||||||
|
frame.trace_roots(visit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CoroutineState::Ready(Value::Object(reference)) => visit(*reference),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+13
-11
@@ -42,21 +42,23 @@ mod eval;
|
|||||||
mod frame;
|
mod frame;
|
||||||
mod vm;
|
mod vm;
|
||||||
|
|
||||||
pub use frame::{EndorFrame, ExecutionLimits, ExecutionLocation};
|
pub use frame::{
|
||||||
|
CoroutineProgress, EndorFrame, ExecutionLimits, ExecutionLocation, ExecutionProgress,
|
||||||
|
};
|
||||||
// Preserve existing embedding imports; objects owns these definitions.
|
// Preserve existing embedding imports; objects owns these definitions.
|
||||||
pub use objects::ObjectStore;
|
pub use objects::ObjectStore;
|
||||||
pub use objects::{
|
pub use objects::{
|
||||||
AnnotationType, ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement, RootTypes,
|
AnnotationType, AwaitPoll, ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement,
|
||||||
TypeConstructor,
|
RootTypes, TypeConstructor, TypedMethodRequirement,
|
||||||
};
|
};
|
||||||
pub use objects::{
|
pub use objects::{
|
||||||
CallArity, CallEffect, CallSignature, CallType, ChangeBatch, ChangeKey, CodeLayout, CompareOp,
|
ArithmeticOp, CallArity, CallEffect, CallSignature, CallType, ChangeBatch, ChangeKey,
|
||||||
ContextNativeFunction, DecodedInstruction, EndorBaseExceptionObject, EndorCellObject,
|
CodeLayout, CompareOp, ContextNativeFunction, DecodedInstruction, EndorBaseExceptionObject,
|
||||||
EndorCode, EndorCodeObject, EndorDecimal, EndorDictObject, EndorFunctionObject, EndorInteger,
|
EndorCellObject, EndorCode, EndorCodeObject, EndorDecimal, EndorDictObject,
|
||||||
EndorNativeCall, EndorNativeFunctionObject, EndorNumber, EndorNumberObject, EndorObject,
|
EndorFunctionObject, EndorInteger, EndorNativeCall, EndorNativeFunctionObject, EndorNumber,
|
||||||
EndorRational, EndorReal, EndorStringObject, EndorTracebackEntry, Instruction, NativeCall,
|
EndorNumberObject, EndorObject, EndorRational, EndorReal, EndorStringObject,
|
||||||
NativeCallable, NativeContext, NativeFunction, NativeResult, ObjectChange, ObjectHeader,
|
EndorTracebackEntry, Instruction, NativeCall, NativeCallable, NativeContext, NativeFunction,
|
||||||
ObjectPayload, ObjectRef, Opcode, ReadAccess, Record, SourceSignature, Value, VmError,
|
NativeResult, ObjectChange, ObjectHeader, ObjectPayload, ObjectRef, Opcode, ReadAccess,
|
||||||
WeakObjectRef,
|
ReadDependency, ReadSet, Record, SourceSignature, TrackedReads, Value, VmError, WeakObjectRef,
|
||||||
};
|
};
|
||||||
pub use vm::Vm;
|
pub use vm::Vm;
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ pub struct Vm {
|
|||||||
pub(crate) objects: ObjectStore,
|
pub(crate) objects: ObjectStore,
|
||||||
pub(crate) last_traceback: Vec<ExecutionLocation>,
|
pub(crate) last_traceback: Vec<ExecutionLocation>,
|
||||||
pub(crate) source_type_checks: bool,
|
pub(crate) source_type_checks: bool,
|
||||||
|
pub(crate) suspended_call: Option<crate::frame::SuspendedCall>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vm {
|
impl Vm {
|
||||||
@@ -25,6 +26,11 @@ impl Vm {
|
|||||||
self.source_type_checks = enabled;
|
self.source_type_checks = enabled;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
/// Known whole-call effect; unproved source/delegated calls return None.
|
||||||
|
pub fn call_effect(&self, function: ObjectRef) -> Result<Option<objects::CallEffect>, VmError> {
|
||||||
|
self.objects.call_effect(function)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn call_arity(&self, function: ObjectRef) -> Result<Option<objects::CallArity>, VmError> {
|
pub fn call_arity(&self, function: ObjectRef) -> Result<Option<objects::CallArity>, VmError> {
|
||||||
self.objects.call_arity(function)
|
self.objects.call_arity(function)
|
||||||
}
|
}
|
||||||
@@ -40,6 +46,13 @@ impl Vm {
|
|||||||
) -> Result<ObjectRef, VmError> {
|
) -> Result<ObjectRef, VmError> {
|
||||||
self.objects.new_trait(name, requirements)
|
self.objects.new_trait(name, requirements)
|
||||||
}
|
}
|
||||||
|
pub fn new_typed_trait(
|
||||||
|
&mut self,
|
||||||
|
name: impl Into<String>,
|
||||||
|
requirements: Vec<crate::TypedMethodRequirement>,
|
||||||
|
) -> Result<ObjectRef, VmError> {
|
||||||
|
self.objects.new_typed_trait(name, requirements)
|
||||||
|
}
|
||||||
pub fn implement_trait(
|
pub fn implement_trait(
|
||||||
&mut self,
|
&mut self,
|
||||||
definition: ObjectRef,
|
definition: ObjectRef,
|
||||||
@@ -154,6 +167,7 @@ impl Vm {
|
|||||||
objects: ObjectStore::new()?,
|
objects: ObjectStore::new()?,
|
||||||
last_traceback: Vec::new(),
|
last_traceback: Vec::new(),
|
||||||
source_type_checks: false,
|
source_type_checks: false,
|
||||||
|
suspended_call: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,20 @@
|
|||||||
| --- | --- |
|
| --- | --- |
|
||||||
| [runtime.rs](runtime.rs) | 空堆启动、默认操作、引用/借用、属性原子性、稳定地址、长链/循环、原生回调、VM 销毁 |
|
| [runtime.rs](runtime.rs) | 空堆启动、默认操作、引用/借用、属性原子性、稳定地址、长链/循环、原生回调、VM 销毁 |
|
||||||
| [builtins.rs](builtins.rs) | 无损 Number、Unicode 字符串、Dict 顺序与键、Cell、函数环境/共享代码、异常数据 |
|
| [builtins.rs](builtins.rs) | 无损 Number、Unicode 字符串、Dict 顺序与键、Cell、函数环境/共享代码、异常数据 |
|
||||||
| [execution.rs](execution.rs) | wordcode 与校验、控制流、参数/默认值、嵌套帧、字节码函数创建/默认值/环境捕获、闭包、返回值存活、失败清理、限额 |
|
| [execution.rs](execution.rs) | wordcode 与校验、控制流、参数/默认值、嵌套帧、字节码函数创建/默认值/环境捕获、闭包、返回值存活、失败清理、限额、只读分片/取消/累计预算/暂停期间GC执行根 |
|
||||||
| [types.rs](types.rs) | 名义身份、继承与实例关系、类型边保活/循环回收、只读类型元数据与外来句柄、显式Trait身份/继承/参数约束及后代更新原子性;统一函数分类、原生数量元数据/副作用前拒绝/能力更新/默认参数与返回类型错误根清理;仅远端执行 |
|
| [types.rs](types.rs) | 名义身份、继承与实例关系、类型边保活/循环回收、只读类型元数据与外来句柄、显式Trait身份/继承/参数约束及后代更新原子性;统一函数分类、原生数量元数据/副作用前拒绝/能力更新/默认参数与返回类型错误根清理;仅远端执行 |
|
||||||
| [native_context.rs](native_context.rs) | 原生作用域根、分配失败清理、原生签名/联合与可空类型/效果和委托返回根、绑定方法元数据、委托、只读结果分配/写入拒绝/依赖/校验与普通读取区分及失败恢复、弱引用过期及有界变更;仅远端执行 |
|
| [typed_traits.rs](typed_traits.rs) | 类型化Trait登记、原子拒绝、源码检查关闭时的显式约束、宽参数调用、类型强边GC、体后实际能力结果检查与临时验证集合恢复;仅远端执行 |
|
||||||
|
| [method_contracts.rs](method_contracts.rs) | 源码/原生方法覆写参数逆变/返回协变、联合参数、已证明源码/调用图只读、全局/闭包/接收者重绑定、控制流汇合与原生效果及共享赋值保守分类、祖先替换原子性、动态名称重绑定/前向名称与体前拒绝/恢复及清理;仅远端执行 |
|
||||||
|
| [native_context.rs](native_context.rs) | 原生作用域根、分配失败清理、原生签名/联合与可空类型/固定与Delegated效果和委托返回根、绑定方法元数据、具体原生实现查询与载荷替换、委托、只读结果分配/写入拒绝/依赖/校验与普通读取区分及失败恢复、弱引用过期及有界变更;仅远端执行 |
|
||||||
|
|
||||||
核心对象现由 objects 定义,本目录保留既有 endor_core 导入与 Vm 方法的兼容性检查。独立对象存储检查见 [objects/tests](../../objects/tests/README.md)。堆内部伪造句柄与计数溢出检查在 objects 的 src/heap.rs,公开示例由 src/lib.rs rustdoc 提供。文件职责见 [src](../src/README.md),检查方式和结果边界见 [验证](../../../docs/operations/validation.md)。本地不执行测试;存在测试文件不代表某次提交已通过。返回 [core](../README.md)。
|
核心对象现由 objects 定义,本目录保留既有 endor_core 导入与 Vm 方法的兼容性检查。独立对象存储检查见 [objects/tests](../../objects/tests/README.md)。堆内部伪造句柄与计数溢出检查在 objects 的 src/heap.rs,公开示例由 src/lib.rs rustdoc 提供。文件职责见 [src](../src/README.md),检查方式和结果边界见 [验证](../../../docs/operations/validation.md)。本地不执行测试;存在测试文件不代表某次提交已通过。返回 [core](../README.md)。
|
||||||
|
|
||||||
execution.rs 同时覆盖宿主可选 assign_from 的全局/局部赋值、别名身份、拒绝替换与执行根清理;不在本地运行。
|
execution.rs 同时覆盖宿主可选 assign_from 的全局/局部赋值、别名身份、拒绝替换与执行根清理;不在本地运行。
|
||||||
|
|
||||||
|
execution.rs 的 managed_coroutine 用例覆盖交替恢复不重放写入、累计预算错误释放、取消幂等和暂停帧引用环回收;本地只经宿主Clippy编译,不运行测试。
|
||||||
|
|
||||||
|
execution.rs补AwaitGate,覆盖真实Waiting、前缀写入不重放、等待操作数保活及拥有式对象结果;本地仅编译不执行。
|
||||||
|
|
||||||
|
execution.rs的named_reads/precise_nested_regions覆盖无关键、缺失遮蔽、内建变化、区域传播、保守遍历及观察不保活;仅编译未本地运行。
|
||||||
|
|
||||||
|
named_reads同时比较同步精确入口与可恢复调用的观察,验证旧同步入口仍返回相同保守对象列表;本地仅编译。
|
||||||
|
|||||||
@@ -1386,3 +1386,469 @@ fn later_execution_failure_releases_a_new_function_and_its_temporary_default() {
|
|||||||
vm.release(globals).unwrap();
|
vm.release(globals).unwrap();
|
||||||
assert_eq!(vm.object_count(), 0);
|
assert_eq!(vm.object_count(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn suspended_read_only_call_keeps_roots_and_releases_them_on_completion() {
|
||||||
|
use endor_core::ExecutionProgress;
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let shared = vm.new_object().unwrap();
|
||||||
|
vm.set_attr(shared, "value", Value::Number(7_i64.into()))
|
||||||
|
.unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let mut instructions = vec![simple(Opcode::Nop); 16];
|
||||||
|
instructions.extend([op(Opcode::LoadConst, 0), simple(Opcode::ReturnValue)]);
|
||||||
|
let code = make_code(
|
||||||
|
&mut vm,
|
||||||
|
&instructions,
|
||||||
|
vec![Value::Object(shared)],
|
||||||
|
CodeLayout::default(),
|
||||||
|
);
|
||||||
|
let function = vm
|
||||||
|
.new_function("sliced", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
let mut progress = vm
|
||||||
|
.start_read_only_call(
|
||||||
|
function,
|
||||||
|
&[],
|
||||||
|
ExecutionLimits {
|
||||||
|
max_frames: 8,
|
||||||
|
max_instructions: 100,
|
||||||
|
},
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
progress,
|
||||||
|
ExecutionProgress::Pending { instructions: 1 }
|
||||||
|
));
|
||||||
|
assert!(
|
||||||
|
vm.set_attr(shared, "value", Value::Number(8_i64.into()))
|
||||||
|
.is_err()
|
||||||
|
);
|
||||||
|
assert!(vm.call(function, &[]).is_err());
|
||||||
|
vm.release(function).unwrap();
|
||||||
|
vm.release(code).unwrap();
|
||||||
|
vm.release(globals).unwrap();
|
||||||
|
vm.release(shared).unwrap();
|
||||||
|
vm.collect_cycles();
|
||||||
|
loop {
|
||||||
|
match progress {
|
||||||
|
ExecutionProgress::Pending { .. } => progress = vm.resume_read_only_call(2).unwrap(),
|
||||||
|
ExecutionProgress::Complete {
|
||||||
|
value,
|
||||||
|
instructions,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
assert_eq!(instructions, 18);
|
||||||
|
assert_eq!(value, Value::Object(shared));
|
||||||
|
assert_eq!(
|
||||||
|
vm.get_attr(shared, "value").unwrap(),
|
||||||
|
Value::Number(7_i64.into())
|
||||||
|
);
|
||||||
|
vm.release_value(value).unwrap();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(!vm.has_suspended_call());
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn suspended_call_cancellation_and_total_budget_restore_read_only_state() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let shared = vm.new_object().unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let mut instructions = vec![simple(Opcode::Nop); 8];
|
||||||
|
instructions.extend([op(Opcode::LoadConst, 0), simple(Opcode::ReturnValue)]);
|
||||||
|
let code = make_code(
|
||||||
|
&mut vm,
|
||||||
|
&instructions,
|
||||||
|
vec![Value::Object(shared)],
|
||||||
|
CodeLayout::default(),
|
||||||
|
);
|
||||||
|
let function = vm
|
||||||
|
.new_function("budget", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
let roots = vm.ref_count(function).unwrap();
|
||||||
|
vm.start_read_only_call(
|
||||||
|
function,
|
||||||
|
&[],
|
||||||
|
ExecutionLimits {
|
||||||
|
max_frames: 8,
|
||||||
|
max_instructions: 3,
|
||||||
|
},
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(vm.resume_read_only_call(0).is_err());
|
||||||
|
assert!(vm.has_suspended_call());
|
||||||
|
vm.resume_read_only_call(1).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
vm.resume_read_only_call(1).unwrap_err(),
|
||||||
|
VmError::ExecutionLimit("instructions")
|
||||||
|
);
|
||||||
|
assert!(!vm.has_suspended_call());
|
||||||
|
assert_eq!(vm.ref_count(function).unwrap(), roots);
|
||||||
|
vm.set_attr(shared, "after", Value::Bool(true)).unwrap();
|
||||||
|
vm.start_read_only_call(function, &[], ExecutionLimits::default(), 1)
|
||||||
|
.unwrap();
|
||||||
|
assert!(vm.cancel_read_only_call().unwrap());
|
||||||
|
assert!(!vm.cancel_read_only_call().unwrap());
|
||||||
|
assert_eq!(vm.ref_count(function).unwrap(), roots);
|
||||||
|
vm.set_attr(shared, "after", Value::Bool(false)).unwrap();
|
||||||
|
vm.release(function).unwrap();
|
||||||
|
vm.release(code).unwrap();
|
||||||
|
vm.release(globals).unwrap();
|
||||||
|
vm.release(shared).unwrap();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn managed_coroutines_interleave_without_replaying_writes() {
|
||||||
|
use endor_core::CoroutineProgress;
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let code = make_code(
|
||||||
|
&mut vm,
|
||||||
|
&[
|
||||||
|
op(Opcode::LoadConst, 0),
|
||||||
|
op(Opcode::StoreGlobal, 0),
|
||||||
|
op(Opcode::LoadConst, 1),
|
||||||
|
op(Opcode::StoreGlobal, 1),
|
||||||
|
op(Opcode::LoadConst, 2),
|
||||||
|
simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![text("first"), text("second"), Value::Nil],
|
||||||
|
CodeLayout {
|
||||||
|
names: vec!["x".into(), "y".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let function = vm
|
||||||
|
.new_function("work", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
let first = vm
|
||||||
|
.new_coroutine(function, &[], ExecutionLimits::default())
|
||||||
|
.unwrap();
|
||||||
|
let second = vm
|
||||||
|
.new_coroutine(function, &[], ExecutionLimits::default())
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.resume_coroutine(first, 2).unwrap(),
|
||||||
|
CoroutineProgress::Pending { instructions: 2 }
|
||||||
|
));
|
||||||
|
assert!(matches!(
|
||||||
|
vm.resume_coroutine(second, 2).unwrap(),
|
||||||
|
CoroutineProgress::Pending { instructions: 2 }
|
||||||
|
));
|
||||||
|
vm.dict_set(globals, text("x"), text("interleaved"))
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.resume_coroutine(first, 16).unwrap(),
|
||||||
|
CoroutineProgress::Complete {
|
||||||
|
value: Value::Nil,
|
||||||
|
instructions: 6
|
||||||
|
}
|
||||||
|
));
|
||||||
|
assert_eq!(
|
||||||
|
vm.dict_get(globals, &text("x")).unwrap(),
|
||||||
|
Some(text("interleaved"))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
vm.dict_get(globals, &text("y")).unwrap(),
|
||||||
|
Some(text("second"))
|
||||||
|
);
|
||||||
|
assert!(vm.cancel_coroutine(second).unwrap());
|
||||||
|
assert!(!vm.cancel_coroutine(second).unwrap());
|
||||||
|
assert!(vm.resume_coroutine(first, 1).is_err());
|
||||||
|
for object in [first, second, function, code, globals] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parked_coroutine_cycles_are_collectable_without_cancellation() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let code = return_constant(&mut vm, Value::Nil);
|
||||||
|
let function = vm
|
||||||
|
.new_function("cycle", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
let coroutine = vm
|
||||||
|
.new_coroutine(function, &[], ExecutionLimits::default())
|
||||||
|
.unwrap();
|
||||||
|
vm.dict_set(globals, text("task"), Value::Object(coroutine))
|
||||||
|
.unwrap();
|
||||||
|
for object in [coroutine, function, code, globals] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
assert!(vm.collect_cycles() > 0);
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn managed_coroutine_hard_budget_accumulates_and_error_releases_frames() {
|
||||||
|
use endor_core::CoroutineProgress;
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
// Reaching ReturnValue at the limit succeeds; keep a third instruction
|
||||||
|
// beyond the hard limit to exercise failure before the next Pending yield.
|
||||||
|
let code = vm
|
||||||
|
.new_code(
|
||||||
|
EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::simple(Opcode::Nop),
|
||||||
|
Instruction::new(Opcode::LoadConst, 0),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![Value::Nil],
|
||||||
|
CodeLayout::default(),
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let function = vm
|
||||||
|
.new_function("limited", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
let coroutine = vm
|
||||||
|
.new_coroutine(
|
||||||
|
function,
|
||||||
|
&[],
|
||||||
|
ExecutionLimits {
|
||||||
|
max_frames: 4,
|
||||||
|
// The second resume reaches the cumulative limit before
|
||||||
|
// the third instruction, without replaying the first.
|
||||||
|
max_instructions: 2,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.resume_coroutine(coroutine, 1).unwrap(),
|
||||||
|
CoroutineProgress::Pending { instructions: 1 }
|
||||||
|
));
|
||||||
|
assert!(matches!(
|
||||||
|
vm.resume_coroutine(coroutine, 1),
|
||||||
|
Err(VmError::ExecutionLimit("instructions"))
|
||||||
|
));
|
||||||
|
assert!(!vm.last_traceback().is_empty());
|
||||||
|
assert!(!vm.cancel_coroutine(coroutine).unwrap());
|
||||||
|
for object in [coroutine, function, code, globals] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct AwaitGate {
|
||||||
|
ready: Arc<std::sync::atomic::AtomicBool>,
|
||||||
|
value: Value,
|
||||||
|
}
|
||||||
|
impl ObjectPayload for AwaitGate {
|
||||||
|
fn as_any(&self) -> &dyn Any {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
fn kind_name(&self) -> &'static str {
|
||||||
|
"await-gate"
|
||||||
|
}
|
||||||
|
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||||
|
if let Value::Object(reference) = self.value {
|
||||||
|
visit(reference);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn poll_await(&self) -> Result<endor_core::AwaitPoll, VmError> {
|
||||||
|
Ok(if self.ready.load(Ordering::Acquire) {
|
||||||
|
endor_core::AwaitPoll::Ready(self.value.clone())
|
||||||
|
} else {
|
||||||
|
endor_core::AwaitPoll::Pending
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn await_keeps_operand_and_owned_object_result_without_replaying_prefix() {
|
||||||
|
use endor_core::CoroutineProgress;
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let ready = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||||
|
let value = vm.new_object().unwrap();
|
||||||
|
vm.set_attr(value, "marker", text("kept")).unwrap();
|
||||||
|
let gate = vm
|
||||||
|
.allocate(AwaitGate {
|
||||||
|
ready: ready.clone(),
|
||||||
|
value: Value::Object(value),
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let code = make_code(
|
||||||
|
&mut vm,
|
||||||
|
&[
|
||||||
|
op(Opcode::LoadConst, 0),
|
||||||
|
op(Opcode::StoreGlobal, 0),
|
||||||
|
op(Opcode::LoadConst, 1),
|
||||||
|
simple(Opcode::Await),
|
||||||
|
simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![text("prefix"), Value::Object(gate)],
|
||||||
|
CodeLayout {
|
||||||
|
names: vec!["step".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let function = vm
|
||||||
|
.new_function("awaited", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
let coroutine = vm
|
||||||
|
.new_coroutine(function, &[], ExecutionLimits::default())
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
matches!(vm.resume_coroutine(coroutine, 64).unwrap(), CoroutineProgress::Waiting { awaitable, .. } if awaitable == gate)
|
||||||
|
);
|
||||||
|
vm.release(value).unwrap();
|
||||||
|
vm.release(gate).unwrap();
|
||||||
|
vm.dict_set(globals, text("step"), text("interleaved"))
|
||||||
|
.unwrap();
|
||||||
|
vm.collect_cycles();
|
||||||
|
ready.store(true, Ordering::Release);
|
||||||
|
let CoroutineProgress::Complete { value: result, .. } =
|
||||||
|
vm.resume_coroutine(coroutine, 64).unwrap()
|
||||||
|
else {
|
||||||
|
panic!("ready await must complete")
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
vm.dict_get(globals, &text("step")).unwrap(),
|
||||||
|
Some(text("interleaved"))
|
||||||
|
);
|
||||||
|
for object in [coroutine, function, code, globals] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
let Value::Object(reference) = result else {
|
||||||
|
panic!("owned object result")
|
||||||
|
};
|
||||||
|
assert_eq!(vm.get_attr(reference, "marker").unwrap(), text("kept"));
|
||||||
|
vm.release_value(result).unwrap();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn named_reads_ignore_unrelated_globals_but_track_missing_shadow_and_payload_kind() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let builtins = vm.new_dict().unwrap();
|
||||||
|
vm.dict_set(builtins, text("name"), text("builtin"))
|
||||||
|
.unwrap();
|
||||||
|
vm.dict_set(globals, text("__builtins__"), Value::Object(builtins))
|
||||||
|
.unwrap();
|
||||||
|
let code = make_code(
|
||||||
|
&mut vm,
|
||||||
|
&[op(Opcode::LoadGlobal, 0), simple(Opcode::ReturnValue)],
|
||||||
|
vec![],
|
||||||
|
CodeLayout {
|
||||||
|
names: vec!["name".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let function = vm
|
||||||
|
.new_function("read-name", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
let endor_core::ExecutionProgress::Complete {
|
||||||
|
value,
|
||||||
|
reads,
|
||||||
|
dependencies,
|
||||||
|
..
|
||||||
|
} = vm
|
||||||
|
.start_read_only_call(function, &[], ExecutionLimits::default(), 64)
|
||||||
|
.unwrap()
|
||||||
|
else {
|
||||||
|
panic!("complete")
|
||||||
|
};
|
||||||
|
assert_eq!(value, text("builtin"));
|
||||||
|
assert!(reads.contains(&globals) && reads.contains(&builtins));
|
||||||
|
vm.release_value(value).unwrap();
|
||||||
|
vm.dict_set(globals, text("_"), text("unrelated")).unwrap();
|
||||||
|
let (value, synchronous) = vm
|
||||||
|
.call_read_only_precise_with_limits(function, &[], ExecutionLimits::default())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(value, text("builtin"));
|
||||||
|
assert_eq!(synchronous.dependencies, dependencies);
|
||||||
|
assert_eq!(synchronous.objects, reads);
|
||||||
|
vm.release_value(value).unwrap();
|
||||||
|
let (value, conservative) = vm
|
||||||
|
.call_read_only_with_limits(function, &[], ExecutionLimits::default())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(conservative, reads);
|
||||||
|
vm.release_value(value).unwrap();
|
||||||
|
assert!(
|
||||||
|
dependencies
|
||||||
|
.iter()
|
||||||
|
.all(|dependency| vm.object_store().read_dependency_valid(dependency))
|
||||||
|
);
|
||||||
|
vm.dict_set(globals, text("name"), text("shadow")).unwrap();
|
||||||
|
assert!(
|
||||||
|
dependencies
|
||||||
|
.iter()
|
||||||
|
.any(|dependency| !vm.object_store().read_dependency_valid(dependency))
|
||||||
|
);
|
||||||
|
vm.dict_delete(globals, &text("name")).unwrap();
|
||||||
|
assert!(
|
||||||
|
dependencies
|
||||||
|
.iter()
|
||||||
|
.all(|dependency| vm.object_store().read_dependency_valid(dependency))
|
||||||
|
);
|
||||||
|
vm.dict_set(builtins, text("name"), text("changed"))
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
dependencies
|
||||||
|
.iter()
|
||||||
|
.any(|dependency| !vm.object_store().read_dependency_valid(dependency))
|
||||||
|
);
|
||||||
|
for object in [function, code, globals, builtins] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
assert!(
|
||||||
|
dependencies
|
||||||
|
.iter()
|
||||||
|
.any(|dependency| !vm.object_store().read_dependency_valid(dependency))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn precise_nested_regions_replay_borrowed_dependencies_without_hiding_full_dict_reads() {
|
||||||
|
let mut vm = endor_core::ObjectStore::new().unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
vm.dict_set(globals, text("name"), text("value")).unwrap();
|
||||||
|
let scope = vm.begin_read_only();
|
||||||
|
let token = vm.begin_read_tracking().unwrap();
|
||||||
|
vm.dict_get_name(globals, "name").unwrap();
|
||||||
|
let tracked = vm.end_read_tracking_precise(token).unwrap();
|
||||||
|
assert!(
|
||||||
|
tracked
|
||||||
|
.accesses
|
||||||
|
.iter()
|
||||||
|
.any(|access| access.object == globals)
|
||||||
|
);
|
||||||
|
for dependency in &tracked.dependencies {
|
||||||
|
assert!(vm.read_dependency_valid(dependency));
|
||||||
|
}
|
||||||
|
let reads = vm.end_read_only_precise(scope).unwrap();
|
||||||
|
vm.dict_set(globals, text("other"), Value::Nil).unwrap();
|
||||||
|
assert!(
|
||||||
|
reads
|
||||||
|
.dependencies
|
||||||
|
.iter()
|
||||||
|
.all(|dependency| vm.read_dependency_valid(dependency))
|
||||||
|
);
|
||||||
|
let scope = vm.begin_read_only();
|
||||||
|
vm.dict_items(globals).unwrap();
|
||||||
|
let reads = vm.end_read_only_precise(scope).unwrap();
|
||||||
|
vm.dict_set(globals, text("third"), Value::Nil).unwrap();
|
||||||
|
assert!(
|
||||||
|
reads
|
||||||
|
.dependencies
|
||||||
|
.iter()
|
||||||
|
.any(|dependency| !vm.read_dependency_valid(dependency))
|
||||||
|
);
|
||||||
|
vm.release(globals).unwrap();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,876 @@
|
|||||||
|
//! Compile here; execute only in the designated remote validation environment.
|
||||||
|
use endor_core::{
|
||||||
|
AnnotationType as T, CallEffect, CallSignature, CallType, CodeLayout, ContextNativeFunction,
|
||||||
|
EndorCode, Instruction, NativeCallable, NativeContext, NativeResult, ObjectRef, Opcode,
|
||||||
|
SourceSignature, TypeConstructor, Value, Vm, VmError,
|
||||||
|
};
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
fn source(
|
||||||
|
vm: &mut Vm,
|
||||||
|
globals: ObjectRef,
|
||||||
|
owner: &str,
|
||||||
|
parameters: &[&str],
|
||||||
|
returns: &str,
|
||||||
|
result: Value,
|
||||||
|
) -> ObjectRef {
|
||||||
|
let code = EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::new(Opcode::LoadConst, 0),
|
||||||
|
Instruction::new(Opcode::StoreGlobal, 0),
|
||||||
|
Instruction::new(Opcode::LoadConst, 1),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![Value::Number(42.into()), result],
|
||||||
|
CodeLayout {
|
||||||
|
parameters: (0..parameters.len()).map(|i| format!("p{i}")).collect(),
|
||||||
|
locals: (0..parameters.len()).map(|i| format!("p{i}")).collect(),
|
||||||
|
names: vec!["ran".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.with_source_signature(SourceSignature {
|
||||||
|
parameters: parameters.iter().map(|s| (*s).into()).collect(),
|
||||||
|
return_type: returns.into(),
|
||||||
|
method_owner: Some(owner.into()),
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let code = vm.new_code(code).unwrap();
|
||||||
|
let function = vm
|
||||||
|
.new_function("check", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
vm.release(code).unwrap();
|
||||||
|
function
|
||||||
|
}
|
||||||
|
fn class(
|
||||||
|
vm: &mut Vm,
|
||||||
|
name: &str,
|
||||||
|
base: ObjectRef,
|
||||||
|
function: ObjectRef,
|
||||||
|
) -> Result<ObjectRef, VmError> {
|
||||||
|
vm.new_class(
|
||||||
|
name,
|
||||||
|
base,
|
||||||
|
vec![],
|
||||||
|
BTreeMap::from([("check".into(), function)]),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn source_parameters_are_contravariant_and_returns_covariant_at_class_creation() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
vm.set_source_type_checks(true).unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let base_fn = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Base",
|
||||||
|
&["Base", "Number"],
|
||||||
|
"Object",
|
||||||
|
Value::Nil,
|
||||||
|
);
|
||||||
|
let base = class(&mut vm, "Base", roots.object, base_fn).unwrap();
|
||||||
|
let good_fn = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Good",
|
||||||
|
&["Good", "Object"],
|
||||||
|
"Number",
|
||||||
|
Value::Number(7.into()),
|
||||||
|
);
|
||||||
|
let good = class(&mut vm, "Good", base, good_fn).unwrap();
|
||||||
|
let narrow = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Narrow",
|
||||||
|
&["Narrow", "String"],
|
||||||
|
"Number",
|
||||||
|
Value::Nil,
|
||||||
|
);
|
||||||
|
assert!(matches!(
|
||||||
|
class(&mut vm, "Narrow", base, narrow),
|
||||||
|
Err(VmError::OverrideViolation {
|
||||||
|
reason: "override narrows a parameter type",
|
||||||
|
..
|
||||||
|
})
|
||||||
|
));
|
||||||
|
let numeric = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Numeric",
|
||||||
|
&["Numeric", "Object"],
|
||||||
|
"Number",
|
||||||
|
Value::Number(0.into()),
|
||||||
|
);
|
||||||
|
let numeric_type = class(&mut vm, "Numeric", roots.object, numeric).unwrap();
|
||||||
|
let broad = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Broad",
|
||||||
|
&["Broad", "Object"],
|
||||||
|
"Object",
|
||||||
|
Value::Nil,
|
||||||
|
);
|
||||||
|
assert!(matches!(
|
||||||
|
class(&mut vm, "Broad", numeric_type, broad),
|
||||||
|
Err(VmError::OverrideViolation {
|
||||||
|
reason: "override widens the return type",
|
||||||
|
..
|
||||||
|
})
|
||||||
|
));
|
||||||
|
for object in [
|
||||||
|
good,
|
||||||
|
good_fn,
|
||||||
|
narrow,
|
||||||
|
broad,
|
||||||
|
numeric_type,
|
||||||
|
numeric,
|
||||||
|
base,
|
||||||
|
base_fn,
|
||||||
|
globals,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn replacing_an_ancestor_validates_overriding_descendants_before_mutation() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
vm.set_source_type_checks(true).unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let base_fn = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Base",
|
||||||
|
&["Base", "Number"],
|
||||||
|
"Object",
|
||||||
|
Value::Nil,
|
||||||
|
);
|
||||||
|
let base = class(&mut vm, "Base", roots.object, base_fn).unwrap();
|
||||||
|
let child_fn = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Child",
|
||||||
|
&["Child", "Number"],
|
||||||
|
"Number",
|
||||||
|
Value::Number(0.into()),
|
||||||
|
);
|
||||||
|
let child = class(&mut vm, "Child", base, child_fn).unwrap();
|
||||||
|
let replacement = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Base",
|
||||||
|
&["Base", "Object"],
|
||||||
|
"Object",
|
||||||
|
Value::Nil,
|
||||||
|
);
|
||||||
|
let version = vm.change_version(base).unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.define_method(base, "check", replacement),
|
||||||
|
Err(VmError::OverrideViolation { .. })
|
||||||
|
));
|
||||||
|
assert_eq!(vm.change_version(base).unwrap(), version);
|
||||||
|
assert_eq!(
|
||||||
|
vm.object_store().lookup_method(base, "check").unwrap(),
|
||||||
|
Some(base_fn)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
vm.object_store().lookup_method(child, "check").unwrap(),
|
||||||
|
Some(child_fn)
|
||||||
|
);
|
||||||
|
for object in [
|
||||||
|
replacement,
|
||||||
|
child,
|
||||||
|
child_fn,
|
||||||
|
base,
|
||||||
|
base_fn,
|
||||||
|
globals,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn global_rebinding_is_rechecked_before_the_source_body_and_recovers() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
vm.set_source_type_checks(true).unwrap();
|
||||||
|
let parent = vm
|
||||||
|
.new_type("Result", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let result_type = vm
|
||||||
|
.new_type("ResultChild", parent, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let other = vm
|
||||||
|
.new_type("Other", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let result = vm.new_object().unwrap();
|
||||||
|
vm.attach_type(result, result_type).unwrap();
|
||||||
|
let base_globals = vm.new_dict().unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
vm.dict_set(
|
||||||
|
base_globals,
|
||||||
|
Value::Text("Result".into()),
|
||||||
|
Value::Object(parent),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
vm.dict_set(
|
||||||
|
globals,
|
||||||
|
Value::Text("Actual".into()),
|
||||||
|
Value::Object(result_type),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
vm.dict_set(globals, Value::Text("ran".into()), Value::Number(0.into()))
|
||||||
|
.unwrap();
|
||||||
|
let base_fn = source(
|
||||||
|
&mut vm,
|
||||||
|
base_globals,
|
||||||
|
"Base",
|
||||||
|
&["Base"],
|
||||||
|
"Result",
|
||||||
|
Value::Nil,
|
||||||
|
);
|
||||||
|
let base = class(&mut vm, "Base", roots.object, base_fn).unwrap();
|
||||||
|
let child_fn = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Child",
|
||||||
|
&["Child"],
|
||||||
|
"Actual",
|
||||||
|
Value::Object(result),
|
||||||
|
);
|
||||||
|
let child = class(&mut vm, "Child", base, child_fn).unwrap();
|
||||||
|
let receiver = vm.new_object().unwrap();
|
||||||
|
vm.attach_type(receiver, child).unwrap();
|
||||||
|
vm.dict_set(globals, Value::Text("Actual".into()), Value::Object(other))
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.call(child_fn, &[Value::Object(receiver)]),
|
||||||
|
Err(VmError::OverrideViolation { .. })
|
||||||
|
));
|
||||||
|
assert_eq!(
|
||||||
|
vm.dict_get(globals, &Value::Text("ran".into())).unwrap(),
|
||||||
|
Some(Value::Number(0.into()))
|
||||||
|
);
|
||||||
|
vm.dict_set(
|
||||||
|
globals,
|
||||||
|
Value::Text("Actual".into()),
|
||||||
|
Value::Object(result_type),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let value = vm.call(child_fn, &[Value::Object(receiver)]).unwrap();
|
||||||
|
assert_eq!(value, Value::Object(result));
|
||||||
|
vm.release_value(value).unwrap();
|
||||||
|
for object in [
|
||||||
|
receiver,
|
||||||
|
child,
|
||||||
|
child_fn,
|
||||||
|
base,
|
||||||
|
base_fn,
|
||||||
|
globals,
|
||||||
|
base_globals,
|
||||||
|
result,
|
||||||
|
other,
|
||||||
|
result_type,
|
||||||
|
parent,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Native(CallSignature);
|
||||||
|
impl NativeCallable for Native {
|
||||||
|
fn signature(&self) -> Option<CallSignature> {
|
||||||
|
Some(self.0.clone())
|
||||||
|
}
|
||||||
|
fn invoke(&self, _: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
|
||||||
|
Ok(NativeResult::Return(Value::Number(0.into())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn native_overrides_preserve_union_inputs_and_do_not_expand_effects() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
vm.set_source_type_checks(true).unwrap();
|
||||||
|
let base = vm
|
||||||
|
.new_type("Base", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let mut signature = CallSignature::exact(
|
||||||
|
vec![T::Nominal(base), T::Any],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::ReadOnly,
|
||||||
|
);
|
||||||
|
signature.parameters[1] = CallType::Union(vec![T::Number, T::String]);
|
||||||
|
let base_fn = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"check",
|
||||||
|
Native(signature),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
vm.define_method(base, "check", base_fn).unwrap();
|
||||||
|
let child = vm.new_type("Child", base, TypeConstructor::Record).unwrap();
|
||||||
|
let effect_fn = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"effect",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![T::Nominal(child), T::Any],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::Mutating,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.define_method(child, "check", effect_fn),
|
||||||
|
Err(VmError::OverrideViolation {
|
||||||
|
reason: "override expands known effects",
|
||||||
|
..
|
||||||
|
})
|
||||||
|
));
|
||||||
|
let narrow_fn = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"narrow",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![T::Nominal(child), T::Number],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::ReadOnly,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.define_method(child, "check", narrow_fn),
|
||||||
|
Err(VmError::OverrideViolation {
|
||||||
|
reason: "override narrows a parameter type",
|
||||||
|
..
|
||||||
|
})
|
||||||
|
));
|
||||||
|
for object in [
|
||||||
|
narrow_fn,
|
||||||
|
effect_fn,
|
||||||
|
child,
|
||||||
|
base_fn,
|
||||||
|
base,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unknown_forward_names_defer_until_call_without_entering_the_body() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
vm.set_source_type_checks(true).unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
vm.dict_set(globals, Value::Text("ran".into()), Value::Number(0.into()))
|
||||||
|
.unwrap();
|
||||||
|
let base_fn = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Base",
|
||||||
|
&["Base", "Number"],
|
||||||
|
"Number",
|
||||||
|
Value::Number(0.into()),
|
||||||
|
);
|
||||||
|
let base = class(&mut vm, "Base", roots.object, base_fn).unwrap();
|
||||||
|
let child_fn = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Child",
|
||||||
|
&["Child", "Later"],
|
||||||
|
"Number",
|
||||||
|
Value::Number(0.into()),
|
||||||
|
);
|
||||||
|
let child = class(&mut vm, "Child", base, child_fn).unwrap();
|
||||||
|
let receiver = vm.new_object().unwrap();
|
||||||
|
vm.attach_type(receiver, child).unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.call(
|
||||||
|
child_fn,
|
||||||
|
&[Value::Object(receiver), Value::Number(1.into())]
|
||||||
|
),
|
||||||
|
Err(VmError::UnknownAnnotation(_))
|
||||||
|
));
|
||||||
|
let later = vm
|
||||||
|
.new_type("Later", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
vm.dict_set(globals, Value::Text("Later".into()), Value::Object(later))
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.call(
|
||||||
|
child_fn,
|
||||||
|
&[Value::Object(receiver), Value::Number(1.into())]
|
||||||
|
),
|
||||||
|
Err(VmError::OverrideViolation { .. })
|
||||||
|
));
|
||||||
|
assert_eq!(
|
||||||
|
vm.dict_get(globals, &Value::Text("ran".into())).unwrap(),
|
||||||
|
Some(Value::Number(0.into()))
|
||||||
|
);
|
||||||
|
for object in [
|
||||||
|
receiver,
|
||||||
|
child,
|
||||||
|
child_fn,
|
||||||
|
base,
|
||||||
|
base_fn,
|
||||||
|
later,
|
||||||
|
globals,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn verified_source_read_only_effects_constrain_native_overrides() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
vm.set_source_type_checks(true).unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let code = EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::new(Opcode::LoadFast, 1),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![],
|
||||||
|
CodeLayout {
|
||||||
|
parameters: vec!["self".into(), "value".into()],
|
||||||
|
locals: vec!["self".into(), "value".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.with_source_signature(SourceSignature {
|
||||||
|
parameters: vec!["PureBase".into(), "Number".into()],
|
||||||
|
return_type: "Number".into(),
|
||||||
|
method_owner: Some("PureBase".into()),
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
assert!(code.is_statically_read_only());
|
||||||
|
let code = vm.new_code(code).unwrap();
|
||||||
|
let source = vm
|
||||||
|
.new_function("check", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
let base = class(&mut vm, "PureBase", roots.object, source).unwrap();
|
||||||
|
let child = vm.new_type("Child", base, TypeConstructor::Record).unwrap();
|
||||||
|
assert_eq!(vm.call_effect(source).unwrap(), Some(CallEffect::ReadOnly));
|
||||||
|
let receiver = vm.new_object().unwrap();
|
||||||
|
vm.attach_type(receiver, child).unwrap();
|
||||||
|
let Value::Object(bound) = vm.bind_attr(receiver, "check").unwrap() else {
|
||||||
|
panic!("bound method")
|
||||||
|
};
|
||||||
|
assert_eq!(vm.call_effect(bound).unwrap(), Some(CallEffect::ReadOnly));
|
||||||
|
let mut functions = vec![];
|
||||||
|
for effect in [
|
||||||
|
CallEffect::Mutating,
|
||||||
|
CallEffect::External,
|
||||||
|
CallEffect::ReadOnly,
|
||||||
|
CallEffect::Delegated,
|
||||||
|
] {
|
||||||
|
let function = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"check",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![T::Any, T::Number],
|
||||||
|
T::Number,
|
||||||
|
effect,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
let version = vm.change_version(child).unwrap();
|
||||||
|
let update = vm.define_method(child, "check", function);
|
||||||
|
if matches!(effect, CallEffect::Mutating | CallEffect::External) {
|
||||||
|
assert!(matches!(
|
||||||
|
update,
|
||||||
|
Err(VmError::OverrideViolation {
|
||||||
|
reason: "override expands known effects",
|
||||||
|
..
|
||||||
|
})
|
||||||
|
));
|
||||||
|
assert_eq!(vm.change_version(child).unwrap(), version);
|
||||||
|
} else {
|
||||||
|
update.unwrap();
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
vm.call_effect(function).unwrap(),
|
||||||
|
(effect != CallEffect::Delegated).then_some(effect)
|
||||||
|
);
|
||||||
|
functions.push(function);
|
||||||
|
}
|
||||||
|
for object in functions.into_iter().chain([
|
||||||
|
bound,
|
||||||
|
receiver,
|
||||||
|
child,
|
||||||
|
base,
|
||||||
|
source,
|
||||||
|
code,
|
||||||
|
globals,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
]) {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn shared_local_assignment_and_dynamic_calls_are_not_inferred_pure() {
|
||||||
|
let assignment = EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::new(Opcode::LoadFast, 1),
|
||||||
|
Instruction::new(Opcode::StoreFast, 0),
|
||||||
|
Instruction::new(Opcode::LoadFast, 0),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![],
|
||||||
|
CodeLayout {
|
||||||
|
parameters: vec!["target".into(), "value".into()],
|
||||||
|
locals: vec!["target".into(), "value".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(!assignment.is_statically_read_only());
|
||||||
|
let captured = EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::new(Opcode::LoadConst, 0),
|
||||||
|
Instruction::new(Opcode::StoreDeref, 0),
|
||||||
|
Instruction::new(Opcode::LoadConst, 0),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![Value::Nil],
|
||||||
|
CodeLayout {
|
||||||
|
free_variables: vec!["captured".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(!captured.is_statically_read_only());
|
||||||
|
let call = EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::new(Opcode::LoadGlobal, 0),
|
||||||
|
Instruction::simple(Opcode::PushNull),
|
||||||
|
Instruction::new(Opcode::Call, 0),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![],
|
||||||
|
CodeLayout {
|
||||||
|
names: vec!["dynamic".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(!call.is_statically_read_only());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn global_caller(vm: &mut Vm, globals: ObjectRef, name: &str) -> ObjectRef {
|
||||||
|
let code = EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::new(Opcode::LoadGlobal, 0),
|
||||||
|
Instruction::simple(Opcode::PushNull),
|
||||||
|
Instruction::new(Opcode::Call, 0),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![],
|
||||||
|
CodeLayout {
|
||||||
|
names: vec![name.into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let code = vm.new_code(code).unwrap();
|
||||||
|
let function = vm
|
||||||
|
.new_function("caller", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
vm.release(code).unwrap();
|
||||||
|
function
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn transitive_effect_queries_follow_rebinding_closures_and_recursive_graphs() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let builtins = vm.new_dict().unwrap();
|
||||||
|
vm.dict_set(
|
||||||
|
globals,
|
||||||
|
Value::Text("__builtins__".into()),
|
||||||
|
Value::Object(builtins),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let pure = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"pure",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::ReadOnly,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
let impure = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"impure",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::Mutating,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
vm.dict_set(builtins, Value::Text("target".into()), Value::Object(pure))
|
||||||
|
.unwrap();
|
||||||
|
let wrapper = global_caller(&mut vm, globals, "target");
|
||||||
|
let outer = global_caller(&mut vm, globals, "wrapper");
|
||||||
|
vm.dict_set(
|
||||||
|
globals,
|
||||||
|
Value::Text("wrapper".into()),
|
||||||
|
Value::Object(wrapper),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(vm.call_effect(outer).unwrap(), Some(CallEffect::ReadOnly));
|
||||||
|
vm.dict_set(globals, Value::Text("target".into()), Value::Object(impure))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(vm.call_effect(outer).unwrap(), None);
|
||||||
|
vm.dict_delete(globals, &Value::Text("target".into()))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(vm.call_effect(outer).unwrap(), Some(CallEffect::ReadOnly));
|
||||||
|
let cell = vm.new_cell(Some(Value::Object(pure))).unwrap();
|
||||||
|
let code = EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::new(Opcode::LoadDeref, 0),
|
||||||
|
Instruction::simple(Opcode::PushNull),
|
||||||
|
Instruction::new(Opcode::Call, 0),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![],
|
||||||
|
CodeLayout {
|
||||||
|
free_variables: vec!["target".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let code = vm.new_code(code).unwrap();
|
||||||
|
let closure = vm
|
||||||
|
.new_function("closure", code, globals, vec![], vec![cell])
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(vm.call_effect(closure).unwrap(), Some(CallEffect::ReadOnly));
|
||||||
|
vm.cell_set(cell, Value::Object(impure)).unwrap();
|
||||||
|
assert_eq!(vm.call_effect(closure).unwrap(), None);
|
||||||
|
vm.cell_set(cell, Value::Object(pure)).unwrap();
|
||||||
|
assert_eq!(vm.call_effect(closure).unwrap(), Some(CallEffect::ReadOnly));
|
||||||
|
let recursive = global_caller(&mut vm, globals, "recursive");
|
||||||
|
vm.dict_set(
|
||||||
|
globals,
|
||||||
|
Value::Text("recursive".into()),
|
||||||
|
Value::Object(recursive),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
// No function body is executed, including this non-terminating call graph.
|
||||||
|
assert_eq!(
|
||||||
|
vm.call_effect(recursive).unwrap(),
|
||||||
|
Some(CallEffect::ReadOnly)
|
||||||
|
);
|
||||||
|
vm.dict_set(
|
||||||
|
globals,
|
||||||
|
Value::Text("recursive".into()),
|
||||||
|
Value::Object(impure),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(vm.call_effect(recursive).unwrap(), None);
|
||||||
|
for object in [
|
||||||
|
recursive, closure, code, cell, outer, wrapper, pure, impure, builtins, globals,
|
||||||
|
] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn effect_control_flow_joins_do_not_keep_a_stale_known_call_target() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let pure = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"pure",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::ReadOnly,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
let impure = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"impure",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::External,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
for name in ["left", "right"] {
|
||||||
|
vm.dict_set(globals, Value::Text(name.into()), Value::Object(pure))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
let code = EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::new(Opcode::LoadFast, 0),
|
||||||
|
Instruction::new(Opcode::PopJumpIfFalse, 2),
|
||||||
|
Instruction::new(Opcode::LoadGlobal, 0),
|
||||||
|
Instruction::new(Opcode::JumpForward, 1),
|
||||||
|
Instruction::new(Opcode::LoadGlobal, 1),
|
||||||
|
Instruction::simple(Opcode::PushNull),
|
||||||
|
Instruction::new(Opcode::Call, 0),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![],
|
||||||
|
CodeLayout {
|
||||||
|
parameters: vec!["condition".into()],
|
||||||
|
locals: vec!["condition".into()],
|
||||||
|
names: vec!["left".into(), "right".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let code = vm.new_code(code).unwrap();
|
||||||
|
let function = vm
|
||||||
|
.new_function("choose", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
vm.call_effect(function).unwrap(),
|
||||||
|
Some(CallEffect::ReadOnly)
|
||||||
|
);
|
||||||
|
vm.dict_set(globals, Value::Text("right".into()), Value::Object(impure))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(vm.call_effect(function).unwrap(), None);
|
||||||
|
vm.dict_set(globals, Value::Text("right".into()), Value::Object(pure))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
vm.call_effect(function).unwrap(),
|
||||||
|
Some(CallEffect::ReadOnly)
|
||||||
|
);
|
||||||
|
for object in [function, code, pure, impure, globals] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bound_source_effects_follow_the_actual_receiver_method_and_instance_field() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let definition = vm
|
||||||
|
.new_type("Receiver", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let pure = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"helper",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![T::Any],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::ReadOnly,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
let impure = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"helper",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![T::Any],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::Mutating,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
let code = EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::new(Opcode::LoadFast, 0),
|
||||||
|
Instruction::new(Opcode::LoadAttr, 0),
|
||||||
|
Instruction::simple(Opcode::PushNull),
|
||||||
|
Instruction::new(Opcode::Call, 0),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![],
|
||||||
|
CodeLayout {
|
||||||
|
parameters: vec!["self".into()],
|
||||||
|
locals: vec!["self".into()],
|
||||||
|
names: vec!["helper".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let code = vm.new_code(code).unwrap();
|
||||||
|
let function = vm
|
||||||
|
.new_function("check", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
vm.define_method(definition, "helper", pure).unwrap();
|
||||||
|
vm.define_method(definition, "check", function).unwrap();
|
||||||
|
let receiver = vm.new_object().unwrap();
|
||||||
|
vm.attach_type(receiver, definition).unwrap();
|
||||||
|
let Value::Object(bound) = vm.bind_attr(receiver, "check").unwrap() else {
|
||||||
|
panic!("bound")
|
||||||
|
};
|
||||||
|
assert_eq!(vm.call_effect(function).unwrap(), None);
|
||||||
|
assert_eq!(vm.call_effect(bound).unwrap(), Some(CallEffect::ReadOnly));
|
||||||
|
vm.define_method(definition, "helper", impure).unwrap();
|
||||||
|
assert_eq!(vm.call_effect(bound).unwrap(), None);
|
||||||
|
vm.define_method(definition, "helper", pure).unwrap();
|
||||||
|
assert_eq!(vm.call_effect(bound).unwrap(), Some(CallEffect::ReadOnly));
|
||||||
|
vm.set_attr(receiver, "helper", Value::Object(impure))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(vm.call_effect(bound).unwrap(), None);
|
||||||
|
vm.del_attr(receiver, "helper").unwrap();
|
||||||
|
assert_eq!(vm.call_effect(bound).unwrap(), Some(CallEffect::ReadOnly));
|
||||||
|
for object in [
|
||||||
|
bound,
|
||||||
|
receiver,
|
||||||
|
function,
|
||||||
|
code,
|
||||||
|
pure,
|
||||||
|
impure,
|
||||||
|
definition,
|
||||||
|
globals,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
@@ -57,6 +57,46 @@ impl NativeCallable for Delegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn native_implementation_inspection_follows_payload_replacement() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let function = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"allocate",
|
||||||
|
Allocate(false),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
let payload = vm
|
||||||
|
.payload::<ContextNativeFunction>(function, "native")
|
||||||
|
.unwrap();
|
||||||
|
assert!(payload.implementation::<Allocate>().is_some());
|
||||||
|
assert!(payload.implementation::<Delegate>().is_none());
|
||||||
|
let version = vm.change_version(function).unwrap();
|
||||||
|
let target = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"target",
|
||||||
|
Allocate(false),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
vm.replace_payload(
|
||||||
|
function,
|
||||||
|
ContextNativeFunction::new("delegate", Delegate(target), vec![target]),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let payload = vm
|
||||||
|
.payload::<ContextNativeFunction>(function, "native")
|
||||||
|
.unwrap();
|
||||||
|
assert!(payload.implementation::<Allocate>().is_none());
|
||||||
|
assert_eq!(payload.implementation::<Delegate>().unwrap().0, target);
|
||||||
|
assert!(vm.change_version(function).unwrap() > version);
|
||||||
|
for object in [function, target] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn native_delegation_shares_budget_and_cleans_owned_arguments() {
|
fn native_delegation_shares_budget_and_cleans_owned_arguments() {
|
||||||
let mut vm = Vm::new().unwrap();
|
let mut vm = Vm::new().unwrap();
|
||||||
@@ -719,3 +759,70 @@ fn native_union_and_nullable_contracts_accept_each_branch_and_reject_invalid_met
|
|||||||
vm.release(function).unwrap();
|
vm.release(function).unwrap();
|
||||||
assert_eq!(vm.object_count(), 0);
|
assert_eq!(vm.object_count(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn delegated_effects_keep_the_original_read_only_scope_and_check_the_actual_target() {
|
||||||
|
use endor_core::{AnnotationType as T, CallEffect, CallSignature};
|
||||||
|
use std::sync::{
|
||||||
|
Arc,
|
||||||
|
atomic::{AtomicUsize, Ordering},
|
||||||
|
};
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let calls = Arc::new(AtomicUsize::new(0));
|
||||||
|
let target_calls = Arc::new(AtomicUsize::new(0));
|
||||||
|
let target = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"target",
|
||||||
|
TypedNative {
|
||||||
|
signature: CallSignature::exact(vec![], T::Unit, CallEffect::Mutating),
|
||||||
|
calls: target_calls.clone(),
|
||||||
|
delegate: None,
|
||||||
|
allocate_result: false,
|
||||||
|
},
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
let wrapper = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"wrapper",
|
||||||
|
TypedNative {
|
||||||
|
signature: CallSignature::exact(vec![], T::Unit, CallEffect::Delegated),
|
||||||
|
calls: calls.clone(),
|
||||||
|
delegate: Some(target),
|
||||||
|
allocate_result: false,
|
||||||
|
},
|
||||||
|
vec![target],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
vm.call_read_only_with_limits(wrapper, &[], ExecutionLimits::default())
|
||||||
|
.is_err()
|
||||||
|
);
|
||||||
|
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||||
|
assert_eq!(target_calls.load(Ordering::SeqCst), 0);
|
||||||
|
assert_eq!(vm.call(wrapper, &[]).unwrap(), Value::Nil);
|
||||||
|
vm.replace_payload(
|
||||||
|
target,
|
||||||
|
ContextNativeFunction::new(
|
||||||
|
"pure",
|
||||||
|
TypedNative {
|
||||||
|
signature: CallSignature::exact(vec![], T::Unit, CallEffect::ReadOnly),
|
||||||
|
calls: target_calls.clone(),
|
||||||
|
delegate: None,
|
||||||
|
allocate_result: false,
|
||||||
|
},
|
||||||
|
vec![],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
vm.call_read_only_with_limits(wrapper, &[], ExecutionLimits::default())
|
||||||
|
.unwrap()
|
||||||
|
.0,
|
||||||
|
Value::Nil
|
||||||
|
);
|
||||||
|
for object in [wrapper, target] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,366 @@
|
|||||||
|
//! Remote-only coverage of explicit typed capability contracts.
|
||||||
|
use endor_core::{
|
||||||
|
AnnotationType as T, CallEffect, CallSignature, CodeLayout, ContextNativeFunction, EndorCode,
|
||||||
|
Instruction, NativeCallable, NativeContext, NativeResult, ObjectRef, Opcode, SourceSignature,
|
||||||
|
TypeConstructor, TypedMethodRequirement, Value, Vm, VmError,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn capability(
|
||||||
|
vm: &mut Vm,
|
||||||
|
name: &str,
|
||||||
|
parameters: Vec<T>,
|
||||||
|
returns: T,
|
||||||
|
effect: CallEffect,
|
||||||
|
) -> ObjectRef {
|
||||||
|
vm.new_typed_trait(
|
||||||
|
name,
|
||||||
|
vec![TypedMethodRequirement {
|
||||||
|
name: "check".into(),
|
||||||
|
signature: CallSignature::exact(parameters, returns, effect),
|
||||||
|
}],
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
fn source(
|
||||||
|
vm: &mut Vm,
|
||||||
|
globals: ObjectRef,
|
||||||
|
owner: &str,
|
||||||
|
types: &[&str],
|
||||||
|
returns: &str,
|
||||||
|
value: Value,
|
||||||
|
) -> ObjectRef {
|
||||||
|
let code = EndorCode::from_instructions(
|
||||||
|
&[
|
||||||
|
Instruction::new(Opcode::LoadConst, 0),
|
||||||
|
Instruction::new(Opcode::StoreGlobal, 0),
|
||||||
|
Instruction::new(Opcode::LoadConst, 1),
|
||||||
|
Instruction::simple(Opcode::ReturnValue),
|
||||||
|
],
|
||||||
|
vec![Value::Number(42.into()), value],
|
||||||
|
CodeLayout {
|
||||||
|
parameters: (0..types.len()).map(|i| format!("p{i}")).collect(),
|
||||||
|
locals: (0..types.len()).map(|i| format!("p{i}")).collect(),
|
||||||
|
names: vec!["ran".into()],
|
||||||
|
..CodeLayout::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.with_source_signature(SourceSignature {
|
||||||
|
parameters: types.iter().map(|s| (*s).into()).collect(),
|
||||||
|
return_type: returns.into(),
|
||||||
|
method_owner: Some(owner.into()),
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let code = vm.new_code(code).unwrap();
|
||||||
|
let function = vm
|
||||||
|
.new_function("check", code, globals, vec![], vec![])
|
||||||
|
.unwrap();
|
||||||
|
vm.release(code).unwrap();
|
||||||
|
function
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Native(CallSignature);
|
||||||
|
impl NativeCallable for Native {
|
||||||
|
fn signature(&self) -> Option<CallSignature> {
|
||||||
|
Some(self.0.clone())
|
||||||
|
}
|
||||||
|
fn invoke(&self, _: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
|
||||||
|
Ok(NativeResult::Return(Value::Number(7.into())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn typed_registration_is_atomic_and_wider_native_inputs_remain_callable() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
let definition = vm
|
||||||
|
.new_type("Model", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let contract = capability(
|
||||||
|
&mut vm,
|
||||||
|
"Numeric",
|
||||||
|
vec![T::Any, T::Number],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::ReadOnly,
|
||||||
|
);
|
||||||
|
for (argument, effect) in [
|
||||||
|
(T::String, CallEffect::ReadOnly),
|
||||||
|
(T::Any, CallEffect::Mutating),
|
||||||
|
] {
|
||||||
|
let function = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"bad",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![T::Nominal(definition), argument],
|
||||||
|
T::Number,
|
||||||
|
effect,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
vm.define_method(definition, "check", function).unwrap();
|
||||||
|
let version = vm.change_version(definition).unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.implement_trait(definition, contract),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
assert_eq!(vm.change_version(definition).unwrap(), version);
|
||||||
|
assert!(!vm.implements_trait(definition, contract).unwrap());
|
||||||
|
vm.release(function).unwrap();
|
||||||
|
}
|
||||||
|
let function = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"good",
|
||||||
|
Native(CallSignature::exact(
|
||||||
|
vec![T::Nominal(definition), T::Any],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::ReadOnly,
|
||||||
|
)),
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
vm.define_method(definition, "check", function).unwrap();
|
||||||
|
vm.implement_trait(definition, contract).unwrap();
|
||||||
|
let receiver = vm.new_object().unwrap();
|
||||||
|
vm.attach_type(receiver, definition).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
vm.call(
|
||||||
|
function,
|
||||||
|
&[
|
||||||
|
Value::Object(receiver),
|
||||||
|
Value::Text("extra supported input".into())
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
Value::Number(7.into())
|
||||||
|
);
|
||||||
|
for object in [
|
||||||
|
receiver,
|
||||||
|
function,
|
||||||
|
definition,
|
||||||
|
contract,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn typed_source_methods_enforce_their_annotations_even_when_global_checks_are_off() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
let definition = vm
|
||||||
|
.new_type("Model", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
let function = source(
|
||||||
|
&mut vm,
|
||||||
|
globals,
|
||||||
|
"Model",
|
||||||
|
&["Model", "Number"],
|
||||||
|
"Number",
|
||||||
|
Value::Bool(true),
|
||||||
|
);
|
||||||
|
vm.define_method(definition, "check", function).unwrap();
|
||||||
|
let receiver = vm.new_object().unwrap();
|
||||||
|
vm.attach_type(receiver, definition).unwrap();
|
||||||
|
// Existing untyped declarations retain the default-off compatibility mode.
|
||||||
|
assert_eq!(
|
||||||
|
vm.call(function, &[Value::Object(receiver), Value::Bool(false)])
|
||||||
|
.unwrap(),
|
||||||
|
Value::Bool(true)
|
||||||
|
);
|
||||||
|
let contract = capability(
|
||||||
|
&mut vm,
|
||||||
|
"Numeric",
|
||||||
|
vec![T::Any, T::Number],
|
||||||
|
T::Number,
|
||||||
|
CallEffect::ReadOnly,
|
||||||
|
);
|
||||||
|
vm.implement_trait(definition, contract).unwrap();
|
||||||
|
vm.dict_set(globals, Value::Text("ran".into()), Value::Number(0.into()))
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.call(function, &[Value::Object(receiver), Value::Bool(false)]),
|
||||||
|
Err(VmError::TypeMismatch { .. })
|
||||||
|
));
|
||||||
|
assert_eq!(
|
||||||
|
vm.dict_get(globals, &Value::Text("ran".into())).unwrap(),
|
||||||
|
Some(Value::Number(0.into()))
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
matches!(vm.call(function, &[Value::Object(receiver), Value::Number(1.into())]), Err(VmError::TypeMismatch { position, .. }) if position=="return")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
vm.dict_get(globals, &Value::Text("ran".into())).unwrap(),
|
||||||
|
Some(Value::Number(42.into()))
|
||||||
|
);
|
||||||
|
for object in [
|
||||||
|
receiver,
|
||||||
|
function,
|
||||||
|
definition,
|
||||||
|
contract,
|
||||||
|
globals,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn typed_trait_definitions_trace_their_signature_types_without_hidden_roots() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
let definition = vm
|
||||||
|
.new_type("Value", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let weak = vm.downgrade(definition).unwrap();
|
||||||
|
let contract = capability(
|
||||||
|
&mut vm,
|
||||||
|
"ValueContract",
|
||||||
|
vec![T::Any, T::Nominal(definition)],
|
||||||
|
T::Nominal(definition),
|
||||||
|
CallEffect::ReadOnly,
|
||||||
|
);
|
||||||
|
vm.release(definition).unwrap();
|
||||||
|
let retained = vm
|
||||||
|
.upgrade(weak)
|
||||||
|
.unwrap()
|
||||||
|
.expect("signature retains its type");
|
||||||
|
vm.release(retained).unwrap();
|
||||||
|
vm.release(contract).unwrap();
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert!(vm.upgrade(weak).unwrap().is_none());
|
||||||
|
for object in [roots.object, roots.type_type] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct RebindThenReturn {
|
||||||
|
signature: CallSignature,
|
||||||
|
globals: ObjectRef,
|
||||||
|
other: ObjectRef,
|
||||||
|
result: ObjectRef,
|
||||||
|
}
|
||||||
|
impl NativeCallable for RebindThenReturn {
|
||||||
|
fn signature(&self) -> Option<CallSignature> {
|
||||||
|
Some(self.signature.clone())
|
||||||
|
}
|
||||||
|
fn invoke(&self, cx: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
|
||||||
|
cx.dict_set(
|
||||||
|
self.globals,
|
||||||
|
Value::Text("Current".into()),
|
||||||
|
Value::Object(self.other),
|
||||||
|
)?;
|
||||||
|
Ok(NativeResult::Return(Value::Object(self.result)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn actual_trait_returns_revalidate_capabilities_after_body_side_effects_and_restore_guards() {
|
||||||
|
let mut vm = Vm::new().unwrap();
|
||||||
|
let roots = vm.bootstrap_types().unwrap();
|
||||||
|
let result_base = vm
|
||||||
|
.new_type("Result", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let result_child = vm
|
||||||
|
.new_type("ResultChild", result_base, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let other = vm
|
||||||
|
.new_type("Other", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let model = vm
|
||||||
|
.new_type("Model", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let globals = vm.new_dict().unwrap();
|
||||||
|
vm.dict_set(
|
||||||
|
globals,
|
||||||
|
Value::Text("Current".into()),
|
||||||
|
Value::Object(result_child),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let read = source(&mut vm, globals, "Model", &["Model"], "Current", Value::Nil);
|
||||||
|
vm.define_method(model, "check", read).unwrap();
|
||||||
|
let feature = capability(
|
||||||
|
&mut vm,
|
||||||
|
"Feature",
|
||||||
|
vec![T::Any],
|
||||||
|
T::Nominal(result_base),
|
||||||
|
CallEffect::External,
|
||||||
|
);
|
||||||
|
vm.implement_trait(model, feature).unwrap();
|
||||||
|
let value = vm.new_object().unwrap();
|
||||||
|
vm.attach_type(value, model).unwrap();
|
||||||
|
let actor = vm
|
||||||
|
.new_type("Actor", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let contract = capability(
|
||||||
|
&mut vm,
|
||||||
|
"Producer",
|
||||||
|
vec![T::Any],
|
||||||
|
T::Capability(feature),
|
||||||
|
CallEffect::External,
|
||||||
|
);
|
||||||
|
let function = vm
|
||||||
|
.allocate(ContextNativeFunction::new(
|
||||||
|
"produce",
|
||||||
|
RebindThenReturn {
|
||||||
|
signature: CallSignature::exact(
|
||||||
|
vec![T::Nominal(actor)],
|
||||||
|
T::Nominal(model),
|
||||||
|
CallEffect::Mutating,
|
||||||
|
),
|
||||||
|
globals,
|
||||||
|
other,
|
||||||
|
result: value,
|
||||||
|
},
|
||||||
|
vec![globals, other, value],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
vm.define_method(actor, "check", function).unwrap();
|
||||||
|
vm.implement_trait(actor, contract).unwrap();
|
||||||
|
let receiver = vm.new_object().unwrap();
|
||||||
|
vm.attach_type(receiver, actor).unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
vm.call(function, &[Value::Object(receiver)]),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
// Repeated failures must not leave a recursive-validation guard marked active.
|
||||||
|
for _ in 0..2 {
|
||||||
|
assert!(vm.implements_trait(model, feature).is_err());
|
||||||
|
}
|
||||||
|
vm.dict_set(
|
||||||
|
globals,
|
||||||
|
Value::Text("Current".into()),
|
||||||
|
Value::Object(result_child),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(vm.implements_trait(model, feature).unwrap());
|
||||||
|
for object in [
|
||||||
|
receiver,
|
||||||
|
function,
|
||||||
|
actor,
|
||||||
|
contract,
|
||||||
|
value,
|
||||||
|
read,
|
||||||
|
model,
|
||||||
|
feature,
|
||||||
|
globals,
|
||||||
|
other,
|
||||||
|
result_child,
|
||||||
|
result_base,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
] {
|
||||||
|
vm.release(object).unwrap();
|
||||||
|
}
|
||||||
|
vm.collect_cycles();
|
||||||
|
assert_eq!(vm.object_count(), 0);
|
||||||
|
}
|
||||||
@@ -21,12 +21,16 @@ module Endor {
|
|||||||
StmtKind = Binding(Binding binding)
|
StmtKind = Binding(Binding binding)
|
||||||
| Assignment(Assignment assignment)
|
| Assignment(Assignment assignment)
|
||||||
| MemberAssignment(MemberAssignment assignment)
|
| MemberAssignment(MemberAssignment assignment)
|
||||||
|
| If(IfStatement statement)
|
||||||
|
| While(WhileStatement statement)
|
||||||
| Return(ReturnStatement statement)
|
| Return(ReturnStatement statement)
|
||||||
| Expression(Expr expression)
|
| Expression(Expr expression)
|
||||||
| Declaration(Decl declaration)
|
| Declaration(Decl declaration)
|
||||||
Binding = (Name name, Expr value, span span)
|
Binding = (Name name, Expr value, span span)
|
||||||
Assignment = (Name name, Expr value, span span)
|
Assignment = (Name name, Expr value, span span)
|
||||||
MemberAssignment = (Expr object, Name name, Expr value, span span)
|
MemberAssignment = (Expr object, Name name, Expr value, span span)
|
||||||
|
IfStatement = (Expr condition, Block body, Block? otherwise, span span)
|
||||||
|
WhileStatement = (Expr condition, Block body, span span)
|
||||||
ReturnStatement = (Expr? value, span span)
|
ReturnStatement = (Expr? value, span span)
|
||||||
InteractiveInput = (Stmt* statements, span span)
|
InteractiveInput = (Stmt* statements, span span)
|
||||||
|
|
||||||
@@ -35,10 +39,11 @@ module Endor {
|
|||||||
| Number(string spelling)
|
| Number(string spelling)
|
||||||
| String(string value)
|
| String(string value)
|
||||||
| Symbol(Name name)
|
| Symbol(Name name)
|
||||||
|
| Await(Expr operand)
|
||||||
| Unary(UnaryOp operation, Expr operand)
|
| Unary(UnaryOp operation, Expr operand)
|
||||||
| Binary(BinaryOp operation, Expr left, Expr right)
|
| Binary(BinaryOp operation, Expr left, Expr right)
|
||||||
| Member(Expr object, Name name)
|
| Member(Expr object, Name name)
|
||||||
| Call(Expr callee, Expr* arguments)
|
| Call(Expr callee, Expr* arguments)
|
||||||
UnaryOp = Negate
|
UnaryOp = Negate
|
||||||
BinaryOp = Add | Subtract | Multiply | Divide
|
BinaryOp = Add | Subtract | Multiply | Divide | Equal | NotEqual | Less | LessEqual | Greater | GreaterEqual
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||||
"$id": "urn:endor:ast:sha256:ad2ac52943dd26e08395c0d2698861da98bb89c360241c4712c769de86709fd0",
|
"$id": "urn:endor:ast:sha256:21b0349ac295d9bc8cedcad8f07d8e2d45cf220e19e7c2fad10269555fcfd71a",
|
||||||
"$ref": "#/$defs/AstDocument",
|
"$ref": "#/$defs/AstDocument",
|
||||||
"$defs": {
|
"$defs": {
|
||||||
"Span": {
|
"Span": {
|
||||||
@@ -27,7 +27,7 @@
|
|||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"schema": {
|
"schema": {
|
||||||
"const": "urn:endor:ast:sha256:ad2ac52943dd26e08395c0d2698861da98bb89c360241c4712c769de86709fd0"
|
"const": "urn:endor:ast:sha256:21b0349ac295d9bc8cedcad8f07d8e2d45cf220e19e7c2fad10269555fcfd71a"
|
||||||
},
|
},
|
||||||
"root": {
|
"root": {
|
||||||
"$ref": "#/$defs/AstRoot"
|
"$ref": "#/$defs/AstRoot"
|
||||||
@@ -354,6 +354,38 @@
|
|||||||
],
|
],
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"kind": {
|
||||||
|
"const": "If"
|
||||||
|
},
|
||||||
|
"data": {
|
||||||
|
"$ref": "#/$defs/IfStatement"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"kind",
|
||||||
|
"data"
|
||||||
|
],
|
||||||
|
"additionalProperties": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"kind": {
|
||||||
|
"const": "While"
|
||||||
|
},
|
||||||
|
"data": {
|
||||||
|
"$ref": "#/$defs/WhileStatement"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"kind",
|
||||||
|
"data"
|
||||||
|
],
|
||||||
|
"additionalProperties": false
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -468,6 +500,56 @@
|
|||||||
],
|
],
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
"IfStatement": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"condition": {
|
||||||
|
"$ref": "#/$defs/Expr"
|
||||||
|
},
|
||||||
|
"body": {
|
||||||
|
"$ref": "#/$defs/Block"
|
||||||
|
},
|
||||||
|
"otherwise": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/$defs/Block"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"span": {
|
||||||
|
"$ref": "#/$defs/Span"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"condition",
|
||||||
|
"body",
|
||||||
|
"span"
|
||||||
|
],
|
||||||
|
"additionalProperties": false
|
||||||
|
},
|
||||||
|
"WhileStatement": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"condition": {
|
||||||
|
"$ref": "#/$defs/Expr"
|
||||||
|
},
|
||||||
|
"body": {
|
||||||
|
"$ref": "#/$defs/Block"
|
||||||
|
},
|
||||||
|
"span": {
|
||||||
|
"$ref": "#/$defs/Span"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"condition",
|
||||||
|
"body",
|
||||||
|
"span"
|
||||||
|
],
|
||||||
|
"additionalProperties": false
|
||||||
|
},
|
||||||
"ReturnStatement": {
|
"ReturnStatement": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -591,6 +673,22 @@
|
|||||||
],
|
],
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"kind": {
|
||||||
|
"const": "Await"
|
||||||
|
},
|
||||||
|
"data": {
|
||||||
|
"$ref": "#/$defs/Expr"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"kind",
|
||||||
|
"data"
|
||||||
|
],
|
||||||
|
"additionalProperties": false
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -726,7 +824,13 @@
|
|||||||
"Add",
|
"Add",
|
||||||
"Subtract",
|
"Subtract",
|
||||||
"Multiply",
|
"Multiply",
|
||||||
"Divide"
|
"Divide",
|
||||||
|
"Equal",
|
||||||
|
"NotEqual",
|
||||||
|
"Less",
|
||||||
|
"LessEqual",
|
||||||
|
"Greater",
|
||||||
|
"GreaterEqual"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Generated by scripts/generate_ast.py from Endor.asdl. Do not edit.
|
// Generated by scripts/generate_ast.py from Endor.asdl. Do not edit.
|
||||||
|
|
||||||
export const AST_SCHEMA_ID = "urn:endor:ast:sha256:ad2ac52943dd26e08395c0d2698861da98bb89c360241c4712c769de86709fd0" as const;
|
export const AST_SCHEMA_ID = "urn:endor:ast:sha256:21b0349ac295d9bc8cedcad8f07d8e2d45cf220e19e7c2fad10269555fcfd71a" as const;
|
||||||
export interface Span { start: number; end: number; }
|
export interface Span { start: number; end: number; }
|
||||||
export type AstDocument = { schema: typeof AST_SCHEMA_ID; root: AstRoot; };
|
export type AstDocument = { schema: typeof AST_SCHEMA_ID; root: AstRoot; };
|
||||||
export type AstRoot = { kind: "Module"; data: Program; } | { kind: "Expression"; data: Expr; } | { kind: "Interactive"; data: InteractiveInput; };
|
export type AstRoot = { kind: "Module"; data: Program; } | { kind: "Expression"; data: Expr; } | { kind: "Interactive"; data: InteractiveInput; };
|
||||||
@@ -13,16 +13,18 @@ export type Function = { name: Name; parameters: Array<Parameter>; return_type:
|
|||||||
export type Parameter = { name: Name; type_name: Name; span: Span; };
|
export type Parameter = { name: Name; type_name: Name; span: Span; };
|
||||||
export type Block = { statements: Array<Stmt>; span: Span; };
|
export type Block = { statements: Array<Stmt>; span: Span; };
|
||||||
export type Stmt = { kind: StmtKind; span: Span; };
|
export type Stmt = { kind: StmtKind; span: Span; };
|
||||||
export type StmtKind = { kind: "Binding"; data: Binding; } | { kind: "Assignment"; data: Assignment; } | { kind: "MemberAssignment"; data: MemberAssignment; } | { kind: "Return"; data: ReturnStatement; } | { kind: "Expression"; data: Expr; } | { kind: "Declaration"; data: Decl; };
|
export type StmtKind = { kind: "Binding"; data: Binding; } | { kind: "Assignment"; data: Assignment; } | { kind: "MemberAssignment"; data: MemberAssignment; } | { kind: "If"; data: IfStatement; } | { kind: "While"; data: WhileStatement; } | { kind: "Return"; data: ReturnStatement; } | { kind: "Expression"; data: Expr; } | { kind: "Declaration"; data: Decl; };
|
||||||
export type Binding = { name: Name; value: Expr; span: Span; };
|
export type Binding = { name: Name; value: Expr; span: Span; };
|
||||||
export type Assignment = { name: Name; value: Expr; span: Span; };
|
export type Assignment = { name: Name; value: Expr; span: Span; };
|
||||||
export type MemberAssignment = { object: Expr; name: Name; value: Expr; span: Span; };
|
export type MemberAssignment = { object: Expr; name: Name; value: Expr; span: Span; };
|
||||||
|
export type IfStatement = { condition: Expr; body: Block; otherwise?: Block | null; span: Span; };
|
||||||
|
export type WhileStatement = { condition: Expr; body: Block; span: Span; };
|
||||||
export type ReturnStatement = { value?: Expr | null; span: Span; };
|
export type ReturnStatement = { value?: Expr | null; span: Span; };
|
||||||
export type InteractiveInput = { statements: Array<Stmt>; span: Span; };
|
export type InteractiveInput = { statements: Array<Stmt>; span: Span; };
|
||||||
export type Expr = { kind: ExprKind; span: Span; };
|
export type Expr = { kind: ExprKind; span: Span; };
|
||||||
export type ExprKind = { kind: "Name"; data: string; } | { kind: "Number"; data: string; } | { kind: "String"; data: string; } | { kind: "Symbol"; data: Name; } | { kind: "Unary"; data: { operation: UnaryOp; operand: Expr; }; } | { kind: "Binary"; data: { operation: BinaryOp; left: Expr; right: Expr; }; } | { kind: "Member"; data: { object: Expr; name: Name; }; } | { kind: "Call"; data: { callee: Expr; arguments: Array<Expr>; }; };
|
export type ExprKind = { kind: "Name"; data: string; } | { kind: "Number"; data: string; } | { kind: "String"; data: string; } | { kind: "Symbol"; data: Name; } | { kind: "Await"; data: Expr; } | { kind: "Unary"; data: { operation: UnaryOp; operand: Expr; }; } | { kind: "Binary"; data: { operation: BinaryOp; left: Expr; right: Expr; }; } | { kind: "Member"; data: { object: Expr; name: Name; }; } | { kind: "Call"; data: { callee: Expr; arguments: Array<Expr>; }; };
|
||||||
export type UnaryOp = "Negate";
|
export type UnaryOp = "Negate";
|
||||||
export type BinaryOp = "Add" | "Subtract" | "Multiply" | "Divide";
|
export type BinaryOp = "Add" | "Subtract" | "Multiply" | "Divide" | "Equal" | "NotEqual" | "Less" | "LessEqual" | "Greater" | "GreaterEqual";
|
||||||
export interface Visitor {
|
export interface Visitor {
|
||||||
visitSpan?(node: Span): boolean | void;
|
visitSpan?(node: Span): boolean | void;
|
||||||
visitAstDocument?(node: AstDocument): boolean | void;
|
visitAstDocument?(node: AstDocument): boolean | void;
|
||||||
@@ -40,6 +42,8 @@ visitStmtKind?(node: StmtKind): boolean | void;
|
|||||||
visitBinding?(node: Binding): boolean | void;
|
visitBinding?(node: Binding): boolean | void;
|
||||||
visitAssignment?(node: Assignment): boolean | void;
|
visitAssignment?(node: Assignment): boolean | void;
|
||||||
visitMemberAssignment?(node: MemberAssignment): boolean | void;
|
visitMemberAssignment?(node: MemberAssignment): boolean | void;
|
||||||
|
visitIfStatement?(node: IfStatement): boolean | void;
|
||||||
|
visitWhileStatement?(node: WhileStatement): boolean | void;
|
||||||
visitReturnStatement?(node: ReturnStatement): boolean | void;
|
visitReturnStatement?(node: ReturnStatement): boolean | void;
|
||||||
visitInteractiveInput?(node: InteractiveInput): boolean | void;
|
visitInteractiveInput?(node: InteractiveInput): boolean | void;
|
||||||
visitExpr?(node: Expr): boolean | void;
|
visitExpr?(node: Expr): boolean | void;
|
||||||
@@ -135,6 +139,12 @@ break;
|
|||||||
case "MemberAssignment":
|
case "MemberAssignment":
|
||||||
walkMemberAssignment(node.data, visitor);
|
walkMemberAssignment(node.data, visitor);
|
||||||
break;
|
break;
|
||||||
|
case "If":
|
||||||
|
walkIfStatement(node.data, visitor);
|
||||||
|
break;
|
||||||
|
case "While":
|
||||||
|
walkWhileStatement(node.data, visitor);
|
||||||
|
break;
|
||||||
case "Return":
|
case "Return":
|
||||||
walkReturnStatement(node.data, visitor);
|
walkReturnStatement(node.data, visitor);
|
||||||
break;
|
break;
|
||||||
@@ -165,6 +175,19 @@ walkName(node.name, visitor);
|
|||||||
walkExpr(node.value, visitor);
|
walkExpr(node.value, visitor);
|
||||||
walkSpan(node.span, visitor);
|
walkSpan(node.span, visitor);
|
||||||
}
|
}
|
||||||
|
export function walkIfStatement(node: IfStatement, visitor: Visitor): void {
|
||||||
|
if (visitor.visitIfStatement?.(node) === false) return;
|
||||||
|
walkExpr(node.condition, visitor);
|
||||||
|
walkBlock(node.body, visitor);
|
||||||
|
if (node.otherwise != null) walkBlock(node.otherwise, visitor);
|
||||||
|
walkSpan(node.span, visitor);
|
||||||
|
}
|
||||||
|
export function walkWhileStatement(node: WhileStatement, visitor: Visitor): void {
|
||||||
|
if (visitor.visitWhileStatement?.(node) === false) return;
|
||||||
|
walkExpr(node.condition, visitor);
|
||||||
|
walkBlock(node.body, visitor);
|
||||||
|
walkSpan(node.span, visitor);
|
||||||
|
}
|
||||||
export function walkReturnStatement(node: ReturnStatement, visitor: Visitor): void {
|
export function walkReturnStatement(node: ReturnStatement, visitor: Visitor): void {
|
||||||
if (visitor.visitReturnStatement?.(node) === false) return;
|
if (visitor.visitReturnStatement?.(node) === false) return;
|
||||||
if (node.value != null) walkExpr(node.value, visitor);
|
if (node.value != null) walkExpr(node.value, visitor);
|
||||||
@@ -192,6 +215,9 @@ break;
|
|||||||
case "Symbol":
|
case "Symbol":
|
||||||
walkName(node.data, visitor);
|
walkName(node.data, visitor);
|
||||||
break;
|
break;
|
||||||
|
case "Await":
|
||||||
|
walkExpr(node.data, visitor);
|
||||||
|
break;
|
||||||
case "Unary":
|
case "Unary":
|
||||||
walkUnaryOp(node.data.operation, visitor);
|
walkUnaryOp(node.data.operation, visitor);
|
||||||
walkExpr(node.data.operand, visitor);
|
walkExpr(node.data.operand, visitor);
|
||||||
|
|||||||
@@ -17,3 +17,5 @@
|
|||||||
函数体允许独立方法调用等表达式语句;末尾 return 可省略,已有编译器隐式返回 Nil。
|
函数体允许独立方法调用等表达式语句;末尾 return 可省略,已有编译器隐式返回 Nil。
|
||||||
|
|
||||||
改节点先编辑 ../Endor.asdl,再用 scripts/generate_ast.py 更新 Rust/编辑器产物,契约见 [AST 模型](../../../docs/runtime/ast.md)。改输入适配时同步 [阅读器契约](../../../docs/runtime/reader.md) 与读取错误检查;改词法从 lexer.peg 进入,并同步 token、诊断和错误检查;改语法从 grammar.peg 进入,并同步 AST、[测试](../tests/README.md)、样例与 [前端契约](../../../docs/runtime/frontend.md)。词法与语法两份 PEG 动作均不做 VM 分配、I/O 或修改外部状态,避免回溯造成副作用。返回 [frontend](../README.md)。
|
改节点先编辑 ../Endor.asdl,再用 scripts/generate_ast.py 更新 Rust/编辑器产物,契约见 [AST 模型](../../../docs/runtime/ast.md)。改输入适配时同步 [阅读器契约](../../../docs/runtime/reader.md) 与读取错误检查;改词法从 lexer.peg 进入,并同步 token、诊断和错误检查;改语法从 grammar.peg 进入,并同步 AST、[测试](../tests/README.md)、样例与 [前端契约](../../../docs/runtime/frontend.md)。词法与语法两份 PEG 动作均不做 VM 分配、I/O 或修改外部状态,避免回溯造成副作用。返回 [frontend](../README.md)。
|
||||||
|
|
||||||
|
grammar.peg支持await前缀表达式;AST仍从Endor.asdl生成,不能手改generated.rs/visitor.rs/editor产物。
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
use super::Spanned;
|
use super::Spanned;
|
||||||
use crate::Span;
|
use crate::Span;
|
||||||
pub const AST_SCHEMA_ID: &str =
|
pub const AST_SCHEMA_ID: &str =
|
||||||
"urn:endor:ast:sha256:ad2ac52943dd26e08395c0d2698861da98bb89c360241c4712c769de86709fd0";
|
"urn:endor:ast:sha256:21b0349ac295d9bc8cedcad8f07d8e2d45cf220e19e7c2fad10269555fcfd71a";
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct AstDocument {
|
pub struct AstDocument {
|
||||||
@@ -141,6 +141,8 @@ pub enum StmtKind {
|
|||||||
Binding(Binding),
|
Binding(Binding),
|
||||||
Assignment(Assignment),
|
Assignment(Assignment),
|
||||||
MemberAssignment(MemberAssignment),
|
MemberAssignment(MemberAssignment),
|
||||||
|
If(IfStatement),
|
||||||
|
While(WhileStatement),
|
||||||
Return(ReturnStatement),
|
Return(ReturnStatement),
|
||||||
Expression(Expr),
|
Expression(Expr),
|
||||||
Declaration(Decl),
|
Declaration(Decl),
|
||||||
@@ -151,6 +153,8 @@ impl Spanned for StmtKind {
|
|||||||
Self::Binding(node) => node.span(),
|
Self::Binding(node) => node.span(),
|
||||||
Self::Assignment(node) => node.span(),
|
Self::Assignment(node) => node.span(),
|
||||||
Self::MemberAssignment(node) => node.span(),
|
Self::MemberAssignment(node) => node.span(),
|
||||||
|
Self::If(node) => node.span(),
|
||||||
|
Self::While(node) => node.span(),
|
||||||
Self::Return(node) => node.span(),
|
Self::Return(node) => node.span(),
|
||||||
Self::Expression(node) => node.span(),
|
Self::Expression(node) => node.span(),
|
||||||
Self::Declaration(node) => node.span(),
|
Self::Declaration(node) => node.span(),
|
||||||
@@ -196,6 +200,31 @@ impl Spanned for MemberAssignment {
|
|||||||
}
|
}
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
|
pub struct IfStatement {
|
||||||
|
pub condition: Expr,
|
||||||
|
pub body: Block,
|
||||||
|
pub otherwise: Option<Block>,
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
impl Spanned for IfStatement {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.span
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
pub struct WhileStatement {
|
||||||
|
pub condition: Expr,
|
||||||
|
pub body: Block,
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
impl Spanned for WhileStatement {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.span
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct ReturnStatement {
|
pub struct ReturnStatement {
|
||||||
pub value: Option<Expr>,
|
pub value: Option<Expr>,
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
@@ -234,6 +263,7 @@ pub enum ExprKind {
|
|||||||
Number(String),
|
Number(String),
|
||||||
String(String),
|
String(String),
|
||||||
Symbol(Name),
|
Symbol(Name),
|
||||||
|
Await(Box<Expr>),
|
||||||
Unary {
|
Unary {
|
||||||
operation: UnaryOp,
|
operation: UnaryOp,
|
||||||
operand: Box<Expr>,
|
operand: Box<Expr>,
|
||||||
@@ -262,4 +292,10 @@ pub enum BinaryOp {
|
|||||||
Subtract,
|
Subtract,
|
||||||
Multiply,
|
Multiply,
|
||||||
Divide,
|
Divide,
|
||||||
|
Equal,
|
||||||
|
NotEqual,
|
||||||
|
Less,
|
||||||
|
LessEqual,
|
||||||
|
Greater,
|
||||||
|
GreaterEqual,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
Assignment, AstDocument, AstRoot, BinaryOp, Binding, Block, Class, Decl, DeclKind, Expr,
|
Assignment, AstDocument, AstRoot, BinaryOp, Binding, Block, Class, Decl, DeclKind, Expr,
|
||||||
ExprKind, Function, InteractiveInput, MemberAssignment, Name, Parameter, Program,
|
ExprKind, Function, IfStatement, InteractiveInput, MemberAssignment, Name, Parameter, Program,
|
||||||
ReturnStatement, Spanned, Stmt, StmtKind, UnaryOp,
|
ReturnStatement, Spanned, Stmt, StmtKind, UnaryOp, WhileStatement,
|
||||||
};
|
};
|
||||||
use crate::Span;
|
use crate::Span;
|
||||||
pub trait Visitor {
|
pub trait Visitor {
|
||||||
@@ -102,6 +102,18 @@ pub trait Visitor {
|
|||||||
self.leave_node();
|
self.leave_node();
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
fn visit_if_statement(&mut self, node: &IfStatement) -> Result<(), Self::Error> {
|
||||||
|
self.enter_node(node.span())?;
|
||||||
|
let result = walk_if_statement(self, node);
|
||||||
|
self.leave_node();
|
||||||
|
result
|
||||||
|
}
|
||||||
|
fn visit_while_statement(&mut self, node: &WhileStatement) -> Result<(), Self::Error> {
|
||||||
|
self.enter_node(node.span())?;
|
||||||
|
let result = walk_while_statement(self, node);
|
||||||
|
self.leave_node();
|
||||||
|
result
|
||||||
|
}
|
||||||
fn visit_return_statement(&mut self, node: &ReturnStatement) -> Result<(), Self::Error> {
|
fn visit_return_statement(&mut self, node: &ReturnStatement) -> Result<(), Self::Error> {
|
||||||
self.enter_node(node.span())?;
|
self.enter_node(node.span())?;
|
||||||
let result = walk_return_statement(self, node);
|
let result = walk_return_statement(self, node);
|
||||||
@@ -243,6 +255,12 @@ pub fn walk_stmt_kind<V: Visitor + ?Sized>(
|
|||||||
StmtKind::MemberAssignment(value) => {
|
StmtKind::MemberAssignment(value) => {
|
||||||
visitor.visit_member_assignment(value)?;
|
visitor.visit_member_assignment(value)?;
|
||||||
}
|
}
|
||||||
|
StmtKind::If(value) => {
|
||||||
|
visitor.visit_if_statement(value)?;
|
||||||
|
}
|
||||||
|
StmtKind::While(value) => {
|
||||||
|
visitor.visit_while_statement(value)?;
|
||||||
|
}
|
||||||
StmtKind::Return(value) => {
|
StmtKind::Return(value) => {
|
||||||
visitor.visit_return_statement(value)?;
|
visitor.visit_return_statement(value)?;
|
||||||
}
|
}
|
||||||
@@ -280,6 +298,27 @@ pub fn walk_member_assignment<V: Visitor + ?Sized>(
|
|||||||
visitor.visit_span(&node.span)?;
|
visitor.visit_span(&node.span)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
pub fn walk_if_statement<V: Visitor + ?Sized>(
|
||||||
|
visitor: &mut V,
|
||||||
|
node: &IfStatement,
|
||||||
|
) -> Result<(), V::Error> {
|
||||||
|
visitor.visit_expr(&node.condition)?;
|
||||||
|
visitor.visit_block(&node.body)?;
|
||||||
|
if let Some(value) = &node.otherwise {
|
||||||
|
visitor.visit_block(value)?;
|
||||||
|
}
|
||||||
|
visitor.visit_span(&node.span)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn walk_while_statement<V: Visitor + ?Sized>(
|
||||||
|
visitor: &mut V,
|
||||||
|
node: &WhileStatement,
|
||||||
|
) -> Result<(), V::Error> {
|
||||||
|
visitor.visit_expr(&node.condition)?;
|
||||||
|
visitor.visit_block(&node.body)?;
|
||||||
|
visitor.visit_span(&node.span)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
pub fn walk_return_statement<V: Visitor + ?Sized>(
|
pub fn walk_return_statement<V: Visitor + ?Sized>(
|
||||||
visitor: &mut V,
|
visitor: &mut V,
|
||||||
node: &ReturnStatement,
|
node: &ReturnStatement,
|
||||||
@@ -316,6 +355,9 @@ pub fn walk_expr_kind<V: Visitor + ?Sized>(
|
|||||||
ExprKind::Symbol(value) => {
|
ExprKind::Symbol(value) => {
|
||||||
visitor.visit_name(value)?;
|
visitor.visit_name(value)?;
|
||||||
}
|
}
|
||||||
|
ExprKind::Await(value) => {
|
||||||
|
visitor.visit_expr(value)?;
|
||||||
|
}
|
||||||
ExprKind::Unary {
|
ExprKind::Unary {
|
||||||
operation, operand, ..
|
operation, operand, ..
|
||||||
} => {
|
} => {
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ peg::parser! {
|
|||||||
= declaration:declaration() {
|
= declaration:declaration() {
|
||||||
Stmt { span: declaration.span, kind: StmtKind::Declaration(declaration) }
|
Stmt { span: declaration.span, kind: StmtKind::Declaration(declaration) }
|
||||||
}
|
}
|
||||||
|
/ control:control_statement() { control }
|
||||||
/ binding:binding() { binding_statement(binding) }
|
/ binding:binding() { binding_statement(binding) }
|
||||||
/ assignment:member_assignment() { assignment }
|
/ assignment:member_assignment() { assignment }
|
||||||
/ assignment:assignment() { assignment }
|
/ assignment:assignment() { assignment }
|
||||||
@@ -104,13 +105,25 @@ peg::parser! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rule local_statement() -> Stmt
|
rule local_statement() -> Stmt
|
||||||
= binding:binding() { binding_statement(binding) }
|
= control:control_statement() { control }
|
||||||
|
/ binding:binding() { binding_statement(binding) }
|
||||||
/ assignment:member_assignment() { assignment }
|
/ assignment:member_assignment() { assignment }
|
||||||
/ assignment:assignment() { assignment }
|
/ assignment:assignment() { assignment }
|
||||||
/ !return_keyword() expression:expression() {
|
/ !return_keyword() expression:expression() {
|
||||||
Stmt { span: expression.span, kind: StmtKind::Expression(expression) }
|
Stmt { span: expression.span, kind: StmtKind::Expression(expression) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rule control_statement() -> Stmt
|
||||||
|
= start:keyword("if") condition:expression() body:block()
|
||||||
|
otherwise:(keyword("else") body:block() { body })? {
|
||||||
|
let span=start.through(otherwise.as_ref().map_or(body.span,|b|b.span));
|
||||||
|
Stmt {span,kind:StmtKind::If(IfStatement {condition,body,otherwise,span})}
|
||||||
|
}
|
||||||
|
/ start:keyword("while") condition:expression() body:block() {
|
||||||
|
let span=start.through(body.span);
|
||||||
|
Stmt {span,kind:StmtKind::While(WhileStatement {condition,body,span})}
|
||||||
|
}
|
||||||
|
|
||||||
rule assignment() -> Stmt
|
rule assignment() -> Stmt
|
||||||
= name:name() equal() value:expression() {
|
= name:name() equal() value:expression() {
|
||||||
let span = name.span.through(value.span);
|
let span = name.span.through(value.span);
|
||||||
@@ -140,12 +153,22 @@ peg::parser! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub rule expression() -> Expr = precedence! {
|
pub rule expression() -> Expr = precedence! {
|
||||||
|
left:(@) [Token {kind:TokenKind::EqualEqual,..}] right:@ {binary(left,BinaryOp::Equal,right)}
|
||||||
|
left:(@) [Token {kind:TokenKind::NotEqual,..}] right:@ {binary(left,BinaryOp::NotEqual,right)}
|
||||||
|
left:(@) [Token {kind:TokenKind::Less,..}] right:@ {binary(left,BinaryOp::Less,right)}
|
||||||
|
left:(@) [Token {kind:TokenKind::LessEqual,..}] right:@ {binary(left,BinaryOp::LessEqual,right)}
|
||||||
|
left:(@) [Token {kind:TokenKind::Greater,..}] right:@ {binary(left,BinaryOp::Greater,right)}
|
||||||
|
left:(@) [Token {kind:TokenKind::GreaterEqual,..}] right:@ {binary(left,BinaryOp::GreaterEqual,right)}
|
||||||
|
--
|
||||||
left:(@) plus() right:@ { binary(left, BinaryOp::Add, right) }
|
left:(@) plus() right:@ { binary(left, BinaryOp::Add, right) }
|
||||||
left:(@) minus() right:@ { binary(left, BinaryOp::Subtract, right) }
|
left:(@) minus() right:@ { binary(left, BinaryOp::Subtract, right) }
|
||||||
--
|
--
|
||||||
left:(@) star() right:@ { binary(left, BinaryOp::Multiply, right) }
|
left:(@) star() right:@ { binary(left, BinaryOp::Multiply, right) }
|
||||||
left:(@) slash() right:@ { binary(left, BinaryOp::Divide, right) }
|
left:(@) slash() right:@ { binary(left, BinaryOp::Divide, right) }
|
||||||
--
|
--
|
||||||
|
start:keyword("await") operand:@ {
|
||||||
|
Expr { span: start.through(operand.span), kind: ExprKind::Await(Box::new(operand)) }
|
||||||
|
}
|
||||||
start:minus() operand:@ {
|
start:minus() operand:@ {
|
||||||
Expr { span: start.through(operand.span), kind: ExprKind::Unary { operation: UnaryOp::Negate, operand: Box::new(operand) } }
|
Expr { span: start.through(operand.span), kind: ExprKind::Unary { operation: UnaryOp::Negate, operand: Box::new(operand) } }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,12 @@ peg::parser! {
|
|||||||
/ ":" { TokenKind::Colon }
|
/ ":" { TokenKind::Colon }
|
||||||
/ "," { TokenKind::Comma }
|
/ "," { TokenKind::Comma }
|
||||||
/ "." { TokenKind::Dot }
|
/ "." { TokenKind::Dot }
|
||||||
|
/ "==" { TokenKind::EqualEqual }
|
||||||
|
/ "!=" { TokenKind::NotEqual }
|
||||||
|
/ "<=" { TokenKind::LessEqual }
|
||||||
|
/ ">=" { TokenKind::GreaterEqual }
|
||||||
|
/ "<" { TokenKind::Less }
|
||||||
|
/ ">" { TokenKind::Greater }
|
||||||
/ "=" { TokenKind::Equal }
|
/ "=" { TokenKind::Equal }
|
||||||
/ "+" { TokenKind::Plus }
|
/ "+" { TokenKind::Plus }
|
||||||
/ "-" { TokenKind::Minus }
|
/ "-" { TokenKind::Minus }
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::ast::{
|
use crate::ast::{
|
||||||
Assignment, AstRoot, BinaryOp, Binding, Block, Class, Decl, DeclKind, Expr, ExprKind, Function,
|
Assignment, AstRoot, BinaryOp, Binding, Block, Class, Decl, DeclKind, Expr, ExprKind, Function,
|
||||||
InteractiveInput, MemberAssignment, Name, Parameter, Program, ReturnStatement, Stmt, StmtKind,
|
IfStatement, InteractiveInput, MemberAssignment, Name, Parameter, Program, ReturnStatement,
|
||||||
UnaryOp,
|
Stmt, StmtKind, UnaryOp, WhileStatement,
|
||||||
};
|
};
|
||||||
use crate::lexer::decode_string;
|
use crate::lexer::decode_string;
|
||||||
use crate::{Diagnostic, DiagnosticKind, Span, Token, TokenKind, lex};
|
use crate::{Diagnostic, DiagnosticKind, Span, Token, TokenKind, lex};
|
||||||
|
|||||||
@@ -16,6 +16,12 @@ pub enum TokenKind {
|
|||||||
Comma,
|
Comma,
|
||||||
Dot,
|
Dot,
|
||||||
Equal,
|
Equal,
|
||||||
|
EqualEqual,
|
||||||
|
NotEqual,
|
||||||
|
Less,
|
||||||
|
LessEqual,
|
||||||
|
Greater,
|
||||||
|
GreaterEqual,
|
||||||
Plus,
|
Plus,
|
||||||
Minus,
|
Minus,
|
||||||
Star,
|
Star,
|
||||||
|
|||||||
@@ -272,7 +272,7 @@ fn parser_requires_complete_input_and_rejects_unimplemented_or_malformed_syntax(
|
|||||||
}
|
}
|
||||||
for source in [
|
for source in [
|
||||||
"", "x y", "1 +", "+1", ".5", "1.", "f(1,)", "f(,1)", "f(1 2)", "(1", "obj.", "[1]",
|
"", "x y", "1 +", "+1", ".5", "1.", "f(1,)", "f(,1)", "f(1 2)", "(1", "obj.", "[1]",
|
||||||
"a == b", "1.2.3",
|
"1.2.3",
|
||||||
] {
|
] {
|
||||||
assert!(parse_expression(source).is_err(), "{source}");
|
assert!(parse_expression(source).is_err(), "{source}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,3 +7,8 @@ license.workspace = true
|
|||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
num-bigint = "0.4"
|
||||||
|
num-rational = "0.4"
|
||||||
|
num-traits = "0.2"
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
# objects
|
# objects
|
||||||
|
|
||||||
Endor 核心对象与对象存储包,零外部依赖,不依赖 core。包含 EndorObject、ObjectPayload、Value、ObjectRef、Number、字符串/字典/Cell/函数/异常载荷,以及代码对象与 wordcode 数据定义、编解码和静态验证。提供显式引导的 Object/Type 名义类型,已接入普通源码 class 和继承方法分派,已支持显式 NativeTyped 分配继承和可转移字段存储;trait 与完整元类行为仍待实现。
|
Endor 核心对象与对象存储包,零外部依赖,不依赖 core。包含 EndorObject、ObjectPayload、Value、ObjectRef、Number、字符串/字典/Cell/函数/异常载荷,以及代码对象与 wordcode 数据定义、编解码和静态验证。提供显式引导的 Object/Type 名义类型,已接入普通源码 class 和继承方法分派,已支持显式 NativeTyped 分配继承和可转移字段存储;显式/类型化Trait和保守效果查询已接入,正式trait语法与完整元类行为仍待实现。
|
||||||
|
|
||||||
| 内容 | 职责 |
|
| 内容 | 职责 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| Cargo.toml | objects 包元数据和工作区 lint;无依赖 |
|
| Cargo.toml | objects 包元数据和工作区 lint;无依赖 |
|
||||||
| [src](src/README.md) | 对象定义、私有堆、ObjectStore、对象工厂、字节码数据与共享错误 |
|
| [src](src/README.md) | 对象定义、私有堆、ObjectStore、对象工厂、字节码数据、保守只读调用图分析与共享错误 |
|
||||||
| [tests](tests/README.md) | 不依赖 VM 的对象存储、事务失败、回收和共享代码检查 |
|
| [tests](tests/README.md) | 不依赖 VM 的对象存储、事务失败、回收和共享代码检查 |
|
||||||
|
|
||||||
ObjectStore 独立拥有空堆,负责对象分配、代际校验、引用计数、循环回收、属性与内置容器操作;底层 Heap 与句柄字段保持私有。replace_payload 通过公开的受校验事务替换载荷及所有托管边;NativeContext 提供受限原生分配与变更能力。代码对象只保存及验证执行数据,求值循环与调用帧属于 [core](../core/README.md)。共享 VmError 定义在本包,保留现有名称以避免对象操作接口依赖 VM。
|
ObjectStore 独立拥有空堆,负责对象分配、代际校验、引用计数、循环回收、属性与内置容器操作;底层 Heap 与句柄字段保持私有。replace_payload 通过公开的受校验事务替换载荷及所有托管边;NativeContext 提供受限原生分配与变更能力。代码对象只保存及验证执行数据,求值循环与调用帧属于 [core](../core/README.md)。共享 VmError 定义在本包,保留现有名称以避免对象操作接口依赖 VM。
|
||||||
|
|||||||
@@ -4,20 +4,26 @@
|
|||||||
|
|
||||||
| 文件 | 职责 |
|
| 文件 | 职责 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| [lib.rs](lib.rs) | 包边界、对象类型/ObjectStore 公开导出(含Trait/MethodRequirement/SourceSignature/AnnotationType/CallArity/CallSignature/CallType/CallEffect/ReadAccess)与独立存储 rustdoc 示例 |
|
| [lib.rs](lib.rs) | 包边界、对象类型/ObjectStore 公开导出(含Trait/MethodRequirement/TypedMethodRequirement/SourceSignature/AnnotationType/CallArity/CallSignature/CallType/CallEffect/ReadAccess)与独立存储 rustdoc 示例 |
|
||||||
| [object.rs](object.rs) | Value、CompareOp、ObjectPayload(含显式函数标记)、ObjectHeader、EndorObject、Record、可发布数量元数据的 NativeFunction |
|
| [object.rs](object.rs) | Value、CompareOp、ObjectPayload(含显式函数标记)、ObjectHeader、EndorObject、Record、可发布数量元数据的 NativeFunction |
|
||||||
| [types.rs](types.rs) | 托管名义类型、显式 Object/Type 引导、继承查询、受管方法表/接收者绑定、类字段、显式Trait定义/声明/方法参数约束、原生构造委托、统一源码/原生/绑定方法参数区间查询与能力验证、声明环境托管边、字段构造/写入检查与标记为校验读取的注解解析/匹配 |
|
| [types.rs](types.rs) | 托管名义类型、显式 Object/Type 引导、继承查询、受管方法表/接收者绑定、类字段、显式/类型化Trait定义、强类型签名边与声明约束、实例方法遮蔽检查和宿主缓存依赖名、原生构造委托、统一源码/原生/绑定方法参数区间和已知效果查询与能力验证、声明环境托管边、字段构造/写入检查与标记为校验读取的注解解析/匹配 |
|
||||||
| [native.rs](native.rs) | NativeContext、CallArity/CallSignature 类型与效果元数据、调用前检查、签名类型强边、上下文原生函数、作用域语言函数工厂、显式能力声明入口、函数标记、根与非递归委托调用 |
|
| [contracts.rs](contracts.rs) | 方法参数逆变/返回协变、名义/能力/联合包含关系、祖先方法更新的后代检查、动态注解调用前复查、类型化能力替换/返回约束与临时递归验证集合 |
|
||||||
|
| [native.rs](native.rs) | NativeContext、CallArity/CallSignature 类型、Delegated/固定效果元数据、调用前检查、签名类型强边、上下文原生函数与具体实现借用查询、作用域语言函数工厂、显式能力声明入口、函数标记、根与非递归委托调用 |
|
||||||
|
| [effects.rs](effects.rs) | 非递归只读调用图证明、当前全局/闭包/绑定接收者解析、控制流栈事实合并,无执行或隐藏强根 |
|
||||||
| [changes.rs](changes.rs) | 合并变更批次、变更键与有界通知额度 |
|
| [changes.rs](changes.rs) | 合并变更批次、变更键与有界通知额度 |
|
||||||
| [number.rs](number.rs) | Integer/Decimal/Rational/BinaryFloat/Complex 的无损表示和装箱载荷 |
|
| [number.rs](number.rs) | Integer/Decimal/Rational/BinaryFloat/Complex 的无损表示和装箱载荷 |
|
||||||
| [builtins.rs](builtins.rs) | String、Dict、Cell、Function、BaseException 载荷与 trace 边、语言函数标记 |
|
| [builtins.rs](builtins.rs) | String、Dict、Cell、Function、BaseException 载荷与 trace 边、语言函数标记 |
|
||||||
| [heap.rs](heap.rs) | 私有 Heap、不可伪造 ObjectRef、稳定 Box、引用事务、迭代释放、循环回收、后代契约更新的私有借用枚举、同类型附加幂等、只读变更屏障/全调用及嵌套子调用读取跟踪(区分注解校验与内容读取)及私有边界测试 |
|
| [heap.rs](heap.rs) | 私有 Heap、不可伪造 ObjectRef、稳定 Box、引用事务、迭代释放、循环回收、后代契约更新的私有借用枚举、同类型附加幂等、只读变更屏障/全调用及嵌套子调用读取跟踪(区分注解校验与内容读取)及私有边界测试 |
|
||||||
| [store.rs](store.rs) | ObjectStore、空堆创建、字段检查配置/属性写入屏障、详细读取区域结果、对象操作、retain/release 和回收入口、弱 Function 类型配置与分配分类 |
|
| [store.rs](store.rs) | ObjectStore、空堆创建、字段检查配置/属性写入屏障、详细读取区域结果、逐线程临时能力检查集合、对象操作、retain/release 和回收入口、弱 Function 类型配置与分配分类 |
|
||||||
| [store_builtins.rs](store_builtins.rs) | 载荷校验、代码/函数工厂、Dict/Cell 操作及键语义 |
|
| [store_builtins.rs](store_builtins.rs) | 载荷校验、代码/函数工厂、Dict/Cell 操作及键语义 |
|
||||||
| [instruction.rs](instruction.rs) | Opcode、Instruction、wordcode 编解码、MakeFunction/比较参数与跳转读取接口 |
|
| [instruction.rs](instruction.rs) | Opcode、Instruction、wordcode 编解码、MakeFunction/比较参数与跳转读取接口 |
|
||||||
| [code.rs](code.rs) | CodeLayout、EndorCode/EndorCodeObject、可选源码签名、共享不可变代码和操作数/控制流/栈验证 |
|
| [code.rs](code.rs) | CodeLayout、EndorCode/EndorCodeObject、可选源码签名、保守只读字节码证明、共享不可变代码和操作数/控制流/栈验证 |
|
||||||
| [error.rs](error.rs) | 对象、代码与执行共用的 VmError(含TraitViolation、UnknownAnnotation、TypeMismatch),避免反向依赖 core |
|
| [error.rs](error.rs) | 对象、代码与执行共用的 VmError(含TraitViolation、OverrideViolation、UnknownAnnotation、TypeMismatch),避免反向依赖 core |
|
||||||
|
|
||||||
对象操作变更同步 [对象模型](../../../docs/runtime/objects.md),内置载荷同步 [基本对象](../../../docs/runtime/builtins.md)。新增操作码需要同时修改本包 instruction.rs/code.rs 和 core 的 eval.rs 分派,见 [字节码 VM](../../../docs/runtime/bytecode.md)。
|
对象操作变更同步 [对象模型](../../../docs/runtime/objects.md),内置载荷同步 [基本对象](../../../docs/runtime/builtins.md)。新增操作码需要同时修改本包 instruction.rs/code.rs 和 core 的 eval.rs 分派,见 [字节码 VM](../../../docs/runtime/bytecode.md)。
|
||||||
|
|
||||||
Heap、ObjectRef 字段、对象头计数和可变容器字段不向包外开放;公开代码读取接口不允许改写已登记的常量和托管边。检查入口见 [tests](../tests/README.md)。返回 [objects](../README.md)。
|
Heap、ObjectRef 字段、对象头计数和可变容器字段不向包外开放;公开代码读取接口不允许改写已登记的常量和托管边。检查入口见 [tests](../tests/README.md)。返回 [objects](../README.md)。
|
||||||
|
|
||||||
|
object.rs提供AwaitPoll与ObjectPayload::poll_await;instruction.rs/code.rs验证Await操作数/栈效果,effects.rs将其视为不能证明只读,error.rs承载等待失败/取消。契约见docs/runtime/bytecode.md。
|
||||||
|
|
||||||
|
heap.rs的ReadDependency/ReadSet/TrackedReads记录借用观察,store.rs暴露精确结束/验证,store_builtins.rs按名读取与字典种类验证保留保守API;types.rs/effects.rs使用按名注解/全局解析。
|
||||||
|
|||||||
@@ -106,6 +106,16 @@ impl EndorCode {
|
|||||||
self.signature.as_ref()
|
self.signature.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Conservative proof over validated instructions. Local assignment is not
|
||||||
|
/// automatically pure: shared-reference assignment may update its old value.
|
||||||
|
/// Unknown calls and writes remain unknown, even when they could be safe for
|
||||||
|
/// a particular invocation. New opcodes require an explicit audit here.
|
||||||
|
pub fn is_statically_read_only(&self) -> bool {
|
||||||
|
self.instructions
|
||||||
|
.iter()
|
||||||
|
.all(|decoded| read_only_opcode(decoded.instruction.opcode))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn with_location(
|
pub fn with_location(
|
||||||
mut self,
|
mut self,
|
||||||
name: impl Into<String>,
|
name: impl Into<String>,
|
||||||
@@ -228,6 +238,37 @@ impl ObjectPayload for EndorCodeObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shared by the local proof and call-graph analysis. Unknown opcodes fail closed.
|
||||||
|
pub(crate) fn read_only_opcode(opcode: Opcode) -> bool {
|
||||||
|
matches!(
|
||||||
|
opcode,
|
||||||
|
Opcode::Nop
|
||||||
|
| Opcode::LoadConst
|
||||||
|
| Opcode::LoadFast
|
||||||
|
| Opcode::DeleteFast
|
||||||
|
| Opcode::LoadGlobal
|
||||||
|
| Opcode::PopTop
|
||||||
|
| Opcode::Copy
|
||||||
|
| Opcode::Swap
|
||||||
|
| Opcode::PushNull
|
||||||
|
| Opcode::ReturnValue
|
||||||
|
| Opcode::JumpForward
|
||||||
|
| Opcode::JumpBackward
|
||||||
|
| Opcode::PopJumpIfFalse
|
||||||
|
| Opcode::PopJumpIfTrue
|
||||||
|
| Opcode::ToBool
|
||||||
|
| Opcode::UnaryNot
|
||||||
|
| Opcode::BinaryArithmetic
|
||||||
|
| Opcode::UnaryNegate
|
||||||
|
| Opcode::CompareOp
|
||||||
|
| Opcode::LoadDeref
|
||||||
|
| Opcode::LoadClosure
|
||||||
|
| Opcode::LoadAttr
|
||||||
|
| Opcode::BuildMap
|
||||||
|
| Opcode::MakeFunction
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fn validate_layout(layout: &CodeLayout) -> Result<(), VmError> {
|
fn validate_layout(layout: &CodeLayout) -> Result<(), VmError> {
|
||||||
for names in [
|
for names in [
|
||||||
&layout.parameters,
|
&layout.parameters,
|
||||||
@@ -315,6 +356,7 @@ fn verify(
|
|||||||
| Opcode::LoadAttr
|
| Opcode::LoadAttr
|
||||||
| Opcode::StoreAttr => index < layout.names.len(),
|
| Opcode::StoreAttr => index < layout.names.len(),
|
||||||
Opcode::CompareOp => compare_operation(argument).is_some(),
|
Opcode::CompareOp => compare_operation(argument).is_some(),
|
||||||
|
Opcode::BinaryArithmetic => argument <= 3,
|
||||||
Opcode::Copy => argument >= 1,
|
Opcode::Copy => argument >= 1,
|
||||||
Opcode::Swap => argument >= 2,
|
Opcode::Swap => argument >= 2,
|
||||||
Opcode::JumpForward
|
Opcode::JumpForward
|
||||||
@@ -355,8 +397,12 @@ fn verify(
|
|||||||
| Opcode::StoreDeref
|
| Opcode::StoreDeref
|
||||||
| Opcode::PopJumpIfFalse
|
| Opcode::PopJumpIfFalse
|
||||||
| Opcode::PopJumpIfTrue => (1, 1, 0),
|
| Opcode::PopJumpIfTrue => (1, 1, 0),
|
||||||
Opcode::ToBool | Opcode::UnaryNot | Opcode::LoadAttr => (1, 1, 1),
|
Opcode::ToBool
|
||||||
Opcode::CompareOp => (2, 2, 1),
|
| Opcode::UnaryNot
|
||||||
|
| Opcode::LoadAttr
|
||||||
|
| Opcode::Await
|
||||||
|
| Opcode::UnaryNegate => (1, 1, 1),
|
||||||
|
Opcode::CompareOp | Opcode::BinaryArithmetic => (2, 2, 1),
|
||||||
Opcode::StoreAttr => (2, 2, 0),
|
Opcode::StoreAttr => (2, 2, 0),
|
||||||
Opcode::Copy => (argument, 0, 1),
|
Opcode::Copy => (argument, 0, 1),
|
||||||
Opcode::Swap => (argument, 0, 0),
|
Opcode::Swap => (argument, 0, 0),
|
||||||
|
|||||||
@@ -0,0 +1,462 @@
|
|||||||
|
//! Method substitution rules. This is a child of types so validation can inspect
|
||||||
|
//! method tables without exporting mutable definition internals.
|
||||||
|
use super::{AnnotationType, EndorTypeObject};
|
||||||
|
use crate::{
|
||||||
|
CallArity, CallEffect, CallSignature, CallType, EndorCodeObject, EndorFunctionObject,
|
||||||
|
ObjectRef, ObjectStore, VmError,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Contract {
|
||||||
|
arity: CallArity,
|
||||||
|
parameters: Vec<CallType>,
|
||||||
|
variadic: Option<CallType>,
|
||||||
|
returns: CallType,
|
||||||
|
effect: Option<CallEffect>,
|
||||||
|
}
|
||||||
|
impl Contract {
|
||||||
|
fn parameter(&self, index: usize) -> Option<&CallType> {
|
||||||
|
self.parameters.get(index).or(self.variadic.as_ref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<CallSignature> for Contract {
|
||||||
|
fn from(value: CallSignature) -> Self {
|
||||||
|
Self {
|
||||||
|
arity: value.arity,
|
||||||
|
parameters: value.parameters,
|
||||||
|
variadic: value.variadic,
|
||||||
|
returns: value.returns,
|
||||||
|
effect: if value.effect == CallEffect::Delegated {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(value.effect)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
struct Patch<'a> {
|
||||||
|
owner: ObjectRef,
|
||||||
|
name: &'a str,
|
||||||
|
function: ObjectRef,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TraitCheckGuard<'a> {
|
||||||
|
store: &'a ObjectStore,
|
||||||
|
thread: std::thread::ThreadId,
|
||||||
|
key: (ObjectRef, ObjectRef),
|
||||||
|
}
|
||||||
|
impl Drop for TraitCheckGuard<'_> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let mut checks = self
|
||||||
|
.store
|
||||||
|
.active_trait_checks
|
||||||
|
.lock()
|
||||||
|
.expect("trait checker poisoned");
|
||||||
|
let active = checks.get_mut(&self.thread).expect("active trait check");
|
||||||
|
active.remove(&self.key);
|
||||||
|
if active.is_empty() {
|
||||||
|
checks.remove(&self.thread);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CallType {
|
||||||
|
/// Whether every value admitted by provided is admitted by self.
|
||||||
|
/// Nominal inheritance and explicit capabilities participate; no structural
|
||||||
|
/// or callable-signature inference is implied by this relation.
|
||||||
|
pub fn accepts_type(&self, store: &ObjectStore, provided: &Self) -> Result<bool, VmError> {
|
||||||
|
self.validate(store)?;
|
||||||
|
provided.validate(store)?;
|
||||||
|
for actual in provided.alternatives() {
|
||||||
|
let mut accepted = false;
|
||||||
|
for expected in self.alternatives() {
|
||||||
|
accepted = match (*expected, *actual) {
|
||||||
|
(AnnotationType::Any, _) => true,
|
||||||
|
(AnnotationType::Nominal(base), AnnotationType::Nominal(child)) => {
|
||||||
|
store.is_subtype(child, base)?
|
||||||
|
}
|
||||||
|
(
|
||||||
|
AnnotationType::Capability(capability),
|
||||||
|
AnnotationType::Nominal(definition),
|
||||||
|
) => store.implements_trait(definition, capability)?,
|
||||||
|
(expected, actual) => expected == actual,
|
||||||
|
};
|
||||||
|
if accepted {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !accepted {
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectStore {
|
||||||
|
pub(super) fn with_trait_check(
|
||||||
|
&self,
|
||||||
|
definition: ObjectRef,
|
||||||
|
capability: ObjectRef,
|
||||||
|
check: impl FnOnce() -> Result<(), VmError>,
|
||||||
|
) -> Result<(), VmError> {
|
||||||
|
let thread = std::thread::current().id();
|
||||||
|
let key = (definition, capability);
|
||||||
|
if !self
|
||||||
|
.active_trait_checks
|
||||||
|
.lock()
|
||||||
|
.expect("trait checker poisoned")
|
||||||
|
.entry(thread)
|
||||||
|
.or_default()
|
||||||
|
.insert(key)
|
||||||
|
{
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let _guard = TraitCheckGuard {
|
||||||
|
store: self,
|
||||||
|
thread,
|
||||||
|
key,
|
||||||
|
};
|
||||||
|
check()
|
||||||
|
}
|
||||||
|
fn method_contract(
|
||||||
|
&self,
|
||||||
|
function: ObjectRef,
|
||||||
|
owner: ObjectRef,
|
||||||
|
) -> Result<Option<Contract>, VmError> {
|
||||||
|
if let Some(native) = self.native_signature(function)? {
|
||||||
|
return Ok(Some(native.into()));
|
||||||
|
}
|
||||||
|
let Some(data) = self
|
||||||
|
.object(function)?
|
||||||
|
.payload()
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<EndorFunctionObject>()
|
||||||
|
else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
let code = self.payload::<EndorCodeObject>(data.code(), "code")?.code();
|
||||||
|
let Some(signature) = code.source_signature() else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
let resolve = |name: &str| {
|
||||||
|
if signature.method_owner.as_deref() == Some(name) {
|
||||||
|
let mut current = Some(owner);
|
||||||
|
while let Some(definition) = current {
|
||||||
|
let data = self.payload::<EndorTypeObject>(definition, "Type")?;
|
||||||
|
if data.name == name
|
||||||
|
&& (definition == owner
|
||||||
|
|| data
|
||||||
|
.methods
|
||||||
|
.values()
|
||||||
|
.any(|candidate| *candidate == function))
|
||||||
|
{
|
||||||
|
return Ok(AnnotationType::Nominal(definition));
|
||||||
|
}
|
||||||
|
current = data.base;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.annotation_type(function, name, None)
|
||||||
|
};
|
||||||
|
let parameters = signature
|
||||||
|
.parameters
|
||||||
|
.iter()
|
||||||
|
.map(|name| resolve(name).map(CallType::from))
|
||||||
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
Ok(Some(Contract {
|
||||||
|
arity: self
|
||||||
|
.call_arity(function)?
|
||||||
|
.ok_or(VmError::InvalidArgument("source method lacks arity"))?,
|
||||||
|
parameters,
|
||||||
|
variadic: None,
|
||||||
|
returns: resolve(&signature.return_type)?.into(),
|
||||||
|
effect: self.call_effect(function)?,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
fn own_method(
|
||||||
|
&self,
|
||||||
|
owner: ObjectRef,
|
||||||
|
name: &str,
|
||||||
|
patch: Option<Patch<'_>>,
|
||||||
|
) -> Result<Option<ObjectRef>, VmError> {
|
||||||
|
if let Some(patch) = patch
|
||||||
|
&& patch.owner == owner
|
||||||
|
&& patch.name == name
|
||||||
|
{
|
||||||
|
return Ok(Some(patch.function));
|
||||||
|
}
|
||||||
|
Ok(self
|
||||||
|
.payload::<EndorTypeObject>(owner, "Type")?
|
||||||
|
.methods
|
||||||
|
.get(name)
|
||||||
|
.copied())
|
||||||
|
}
|
||||||
|
fn inherited_method(
|
||||||
|
&self,
|
||||||
|
mut owner: Option<ObjectRef>,
|
||||||
|
name: &str,
|
||||||
|
patch: Option<Patch<'_>>,
|
||||||
|
) -> Result<Option<(ObjectRef, ObjectRef)>, VmError> {
|
||||||
|
while let Some(definition) = owner {
|
||||||
|
if let Some(function) = self.own_method(definition, name, patch)? {
|
||||||
|
return Ok(Some((definition, function)));
|
||||||
|
}
|
||||||
|
owner = self.payload::<EndorTypeObject>(definition, "Type")?.base;
|
||||||
|
}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
fn check_override(
|
||||||
|
&self,
|
||||||
|
owner: ObjectRef,
|
||||||
|
name: &str,
|
||||||
|
function: ObjectRef,
|
||||||
|
patch: Option<Patch<'_>>,
|
||||||
|
defer_unknown: bool,
|
||||||
|
) -> Result<(), VmError> {
|
||||||
|
let definition = self.payload::<EndorTypeObject>(owner, "Type")?;
|
||||||
|
let Some((base_owner, base_function)) =
|
||||||
|
self.inherited_method(definition.base, name, patch)?
|
||||||
|
else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let checked = (|| {
|
||||||
|
let Some(base) = self.method_contract(base_function, base_owner)? else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let invalid = |reason| VmError::OverrideViolation {
|
||||||
|
definition: definition.name.clone(),
|
||||||
|
method: name.into(),
|
||||||
|
reason,
|
||||||
|
};
|
||||||
|
let derived = self
|
||||||
|
.method_contract(function, owner)?
|
||||||
|
.ok_or_else(|| invalid("override lacks type metadata"))?;
|
||||||
|
self.check_substitution(owner, &derived, &base, invalid)?;
|
||||||
|
Ok(())
|
||||||
|
})();
|
||||||
|
match checked {
|
||||||
|
Err(VmError::UnknownAnnotation(_)) if defer_unknown => Ok(()),
|
||||||
|
result => result,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn check_substitution(
|
||||||
|
&self,
|
||||||
|
owner: ObjectRef,
|
||||||
|
derived: &Contract,
|
||||||
|
base: &Contract,
|
||||||
|
invalid: impl Fn(&'static str) -> VmError,
|
||||||
|
) -> Result<(), VmError> {
|
||||||
|
if derived.arity.required > base.arity.required
|
||||||
|
|| derived.arity.maximum < base.arity.maximum
|
||||||
|
{
|
||||||
|
return Err(invalid("override narrows accepted argument counts"));
|
||||||
|
}
|
||||||
|
let receiver = CallType::from(AnnotationType::Nominal(owner));
|
||||||
|
if !derived
|
||||||
|
.parameter(0)
|
||||||
|
.ok_or_else(|| invalid("override lacks receiver"))?
|
||||||
|
.accepts_type(self, &receiver)?
|
||||||
|
{
|
||||||
|
return Err(invalid("override receiver rejects its declaring type"));
|
||||||
|
}
|
||||||
|
if !base
|
||||||
|
.parameter(0)
|
||||||
|
.ok_or_else(|| invalid("required signature lacks receiver"))?
|
||||||
|
.accepts_type(self, &receiver)?
|
||||||
|
{
|
||||||
|
return Err(invalid("required receiver rejects implementing type"));
|
||||||
|
}
|
||||||
|
let fixed = base
|
||||||
|
.parameters
|
||||||
|
.len()
|
||||||
|
.max(derived.parameters.len())
|
||||||
|
.min(base.arity.maximum);
|
||||||
|
for index in 1..fixed {
|
||||||
|
let expected = base
|
||||||
|
.parameter(index)
|
||||||
|
.ok_or_else(|| invalid("incomplete base parameter metadata"))?;
|
||||||
|
let actual = derived
|
||||||
|
.parameter(index)
|
||||||
|
.ok_or_else(|| invalid("incomplete override parameter metadata"))?;
|
||||||
|
if !actual.accepts_type(self, expected)? {
|
||||||
|
return Err(invalid("override narrows a parameter type"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if base.arity.maximum > fixed {
|
||||||
|
let expected = base
|
||||||
|
.parameter(fixed)
|
||||||
|
.ok_or_else(|| invalid("incomplete base variadic metadata"))?;
|
||||||
|
let actual = derived
|
||||||
|
.parameter(fixed)
|
||||||
|
.ok_or_else(|| invalid("incomplete override variadic metadata"))?;
|
||||||
|
if !actual.accepts_type(self, expected)? {
|
||||||
|
return Err(invalid("override narrows a variadic parameter type"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !base.returns.accepts_type(self, &derived.returns)? {
|
||||||
|
return Err(invalid("override widens the return type"));
|
||||||
|
}
|
||||||
|
if let (Some(expected), Some(actual)) = (base.effect, derived.effect) {
|
||||||
|
let compatible = match expected {
|
||||||
|
CallEffect::ReadOnly => actual == CallEffect::ReadOnly,
|
||||||
|
CallEffect::Mutating => actual != CallEffect::External,
|
||||||
|
CallEffect::External | CallEffect::Delegated => true,
|
||||||
|
};
|
||||||
|
if !compatible {
|
||||||
|
return Err(invalid("override expands known effects"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub(super) fn validate_typed_requirement(
|
||||||
|
&self,
|
||||||
|
definition: ObjectRef,
|
||||||
|
capability: ObjectRef,
|
||||||
|
name: &str,
|
||||||
|
function: ObjectRef,
|
||||||
|
expected: &CallSignature,
|
||||||
|
defer_unknown: bool,
|
||||||
|
) -> Result<(), VmError> {
|
||||||
|
let invalid = |reason| VmError::TraitViolation {
|
||||||
|
capability: self
|
||||||
|
.payload::<super::EndorTraitObject>(capability, "Trait")
|
||||||
|
.expect("checked trait")
|
||||||
|
.name
|
||||||
|
.clone(),
|
||||||
|
method: name.into(),
|
||||||
|
reason,
|
||||||
|
};
|
||||||
|
let checked = (|| {
|
||||||
|
let actual = self
|
||||||
|
.method_contract(function, definition)?
|
||||||
|
.ok_or_else(|| invalid("required member lacks type metadata"))?;
|
||||||
|
self.check_substitution(definition, &actual, &expected.clone().into(), invalid)
|
||||||
|
})();
|
||||||
|
match checked {
|
||||||
|
Err(VmError::UnknownAnnotation(_)) if defer_unknown => Ok(()),
|
||||||
|
result => result,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub(super) fn validate_class_overrides(&self, definition: ObjectRef) -> Result<(), VmError> {
|
||||||
|
if self.field_type_checks {
|
||||||
|
for (name, function) in &self.payload::<EndorTypeObject>(definition, "Type")?.methods {
|
||||||
|
self.check_override(definition, name, *function, None, true)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub(super) fn validate_override_update(
|
||||||
|
&self,
|
||||||
|
owner: ObjectRef,
|
||||||
|
name: &str,
|
||||||
|
function: ObjectRef,
|
||||||
|
) -> Result<(), VmError> {
|
||||||
|
if self.field_type_checks {
|
||||||
|
let patch = Some(Patch {
|
||||||
|
owner,
|
||||||
|
name,
|
||||||
|
function,
|
||||||
|
});
|
||||||
|
for descendant in self.descendant_types(owner)? {
|
||||||
|
if let Some(function) = self.own_method(descendant, name, patch)? {
|
||||||
|
self.check_override(descendant, name, function, patch, true)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
/// Explicit override validation, including any typed capabilities.
|
||||||
|
pub fn check_method_override_call(
|
||||||
|
&self,
|
||||||
|
function: ObjectRef,
|
||||||
|
receiver: ObjectRef,
|
||||||
|
) -> Result<(), VmError> {
|
||||||
|
self.check_method_contract_call(function, receiver, true)
|
||||||
|
.map(|_| ())
|
||||||
|
}
|
||||||
|
/// Typed capabilities are explicit opt-ins and always checked. Ordinary
|
||||||
|
/// override checking remains configurable by the VM's source-check flag.
|
||||||
|
pub fn check_method_contract_call(
|
||||||
|
&self,
|
||||||
|
function: ObjectRef,
|
||||||
|
receiver: ObjectRef,
|
||||||
|
overrides: bool,
|
||||||
|
) -> Result<bool, VmError> {
|
||||||
|
let mut typed = false;
|
||||||
|
let actual = self.type_of(receiver)?;
|
||||||
|
let mut owner = actual;
|
||||||
|
while let Some(definition) = owner {
|
||||||
|
let data = self.payload::<EndorTypeObject>(definition, "Type")?;
|
||||||
|
for (name, candidate) in &data.methods {
|
||||||
|
if *candidate != function {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
self.check_typed_instance_storage(receiver)?;
|
||||||
|
if overrides {
|
||||||
|
self.check_override(definition, name, function, None, false)?;
|
||||||
|
}
|
||||||
|
let effective = if self
|
||||||
|
.lookup_method(actual.expect("method receiver has a type"), name)?
|
||||||
|
== Some(function)
|
||||||
|
{
|
||||||
|
actual.unwrap()
|
||||||
|
} else {
|
||||||
|
definition
|
||||||
|
};
|
||||||
|
for capability in self.inherited_traits(effective)? {
|
||||||
|
if let Some(expected) = self
|
||||||
|
.payload::<super::EndorTraitObject>(capability, "Trait")?
|
||||||
|
.signatures
|
||||||
|
.get(name)
|
||||||
|
{
|
||||||
|
typed = true;
|
||||||
|
self.validate_typed_requirement(
|
||||||
|
effective, capability, name, function, expected, false,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
owner = data.base;
|
||||||
|
}
|
||||||
|
Ok(typed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Borrowed expected results for all typed capabilities of this effective
|
||||||
|
/// method. The evaluator retains definitions before entering the body.
|
||||||
|
pub fn method_trait_returns(
|
||||||
|
&self,
|
||||||
|
function: ObjectRef,
|
||||||
|
receiver: ObjectRef,
|
||||||
|
) -> Result<Vec<(String, CallType)>, VmError> {
|
||||||
|
let actual = self.type_of(receiver)?;
|
||||||
|
let mut owner = actual;
|
||||||
|
let mut returns = Vec::new();
|
||||||
|
while let Some(definition) = owner {
|
||||||
|
let data = self.payload::<EndorTypeObject>(definition, "Type")?;
|
||||||
|
for (name, candidate) in &data.methods {
|
||||||
|
if *candidate != function {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let effective = if self
|
||||||
|
.lookup_method(actual.expect("method receiver has a type"), name)?
|
||||||
|
== Some(function)
|
||||||
|
{
|
||||||
|
actual.unwrap()
|
||||||
|
} else {
|
||||||
|
definition
|
||||||
|
};
|
||||||
|
for capability in self.inherited_traits(effective)? {
|
||||||
|
let contract = self.payload::<super::EndorTraitObject>(capability, "Trait")?;
|
||||||
|
if let Some(expected) = contract.signatures.get(name) {
|
||||||
|
returns.push((
|
||||||
|
format!("{}.{}", contract.name, name),
|
||||||
|
expected.returns.clone(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
owner = data.base;
|
||||||
|
}
|
||||||
|
Ok(returns)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,275 @@
|
|||||||
|
//! Snapshot proof of read-only call graphs. All identities are borrowed and all
|
||||||
|
//! traversals iterative; analysis neither executes code nor allocates VM objects.
|
||||||
|
use super::{AttributeLookup, BoundMethod};
|
||||||
|
use crate::{
|
||||||
|
CallEffect, EndorCodeObject, EndorFunctionObject, ObjectRef, ObjectStore, Opcode, Value,
|
||||||
|
VmError,
|
||||||
|
};
|
||||||
|
use std::collections::{BTreeSet, VecDeque};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
struct Target {
|
||||||
|
function: ObjectRef,
|
||||||
|
receiver: Option<ObjectRef>,
|
||||||
|
}
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum Fact {
|
||||||
|
Unknown,
|
||||||
|
Null,
|
||||||
|
Object(ObjectRef),
|
||||||
|
Method(Target),
|
||||||
|
}
|
||||||
|
impl Fact {
|
||||||
|
fn value(value: Value) -> Self {
|
||||||
|
match value {
|
||||||
|
Value::Object(object) => Self::Object(object),
|
||||||
|
_ => Self::Unknown,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn object(self) -> Option<ObjectRef> {
|
||||||
|
match self {
|
||||||
|
Self::Object(object) => Some(object),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectStore {
|
||||||
|
fn effect_target(&self, mut target: Target) -> Result<Target, VmError> {
|
||||||
|
loop {
|
||||||
|
let payload = self.object(target.function)?.payload();
|
||||||
|
let Some(bound) = payload.as_any().downcast_ref::<BoundMethod>() else {
|
||||||
|
return Ok(target);
|
||||||
|
};
|
||||||
|
target = Target {
|
||||||
|
function: bound.function,
|
||||||
|
receiver: Some(bound.receiver),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn infer_call_effect(
|
||||||
|
&self,
|
||||||
|
function: ObjectRef,
|
||||||
|
) -> Result<Option<CallEffect>, VmError> {
|
||||||
|
let root = self.effect_target(Target {
|
||||||
|
function,
|
||||||
|
receiver: None,
|
||||||
|
})?;
|
||||||
|
if !self
|
||||||
|
.object(root.function)?
|
||||||
|
.payload()
|
||||||
|
.as_any()
|
||||||
|
.is::<EndorFunctionObject>()
|
||||||
|
{
|
||||||
|
return Ok(self.native_signature(root.function)?.and_then(|signature| {
|
||||||
|
(signature.effect != CallEffect::Delegated).then_some(signature.effect)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
let mut pending = VecDeque::from([root]);
|
||||||
|
let mut visited = BTreeSet::new();
|
||||||
|
while let Some(target) = pending.pop_front() {
|
||||||
|
let target = self.effect_target(target)?;
|
||||||
|
if !visited.insert(target) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if let Some(source) = self
|
||||||
|
.object(target.function)?
|
||||||
|
.payload()
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<EndorFunctionObject>()
|
||||||
|
{
|
||||||
|
let Some(callees) = self.read_only_callees(source, target.receiver)? else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
pending.extend(callees);
|
||||||
|
} else if self
|
||||||
|
.native_signature(target.function)?
|
||||||
|
.is_none_or(|signature| signature.effect != CallEffect::ReadOnly)
|
||||||
|
{
|
||||||
|
// An impure target could rebind later globals/Cells/methods, so
|
||||||
|
// snapshot edges cannot bound this source invocation's effects.
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Some(CallEffect::ReadOnly))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_only_callees(
|
||||||
|
&self,
|
||||||
|
function: &EndorFunctionObject,
|
||||||
|
receiver: Option<ObjectRef>,
|
||||||
|
) -> Result<Option<BTreeSet<Target>>, VmError> {
|
||||||
|
let code = self
|
||||||
|
.payload::<EndorCodeObject>(function.code(), "code")?
|
||||||
|
.code();
|
||||||
|
if code.is_statically_read_only() {
|
||||||
|
return Ok(Some(BTreeSet::new()));
|
||||||
|
}
|
||||||
|
let instructions = code.instructions();
|
||||||
|
if instructions.iter().any(|decoded| {
|
||||||
|
decoded.instruction.opcode != Opcode::Call
|
||||||
|
&& !crate::code::read_only_opcode(decoded.instruction.opcode)
|
||||||
|
}) {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
let mut states: Vec<Option<Vec<Fact>>> = vec![None; instructions.len()];
|
||||||
|
states[0] = Some(vec![]);
|
||||||
|
let mut work = VecDeque::from([0]);
|
||||||
|
let mut callees = BTreeSet::new();
|
||||||
|
while let Some(index) = work.pop_front() {
|
||||||
|
let decoded = instructions[index];
|
||||||
|
let opcode = decoded.instruction.opcode;
|
||||||
|
let argument = decoded.instruction.argument as usize;
|
||||||
|
let mut stack = states[index].clone().expect("reachable effect state");
|
||||||
|
match opcode {
|
||||||
|
Opcode::Await => return Ok(None),
|
||||||
|
Opcode::LoadConst => stack.push(Fact::value(code.constants()[argument].clone())),
|
||||||
|
Opcode::LoadFast => stack.push(if argument == 0 {
|
||||||
|
receiver.map(Fact::Object).unwrap_or(Fact::Unknown)
|
||||||
|
} else {
|
||||||
|
Fact::Unknown
|
||||||
|
}),
|
||||||
|
Opcode::LoadGlobal => {
|
||||||
|
let name = &code.layout().names[argument];
|
||||||
|
let mut value = self.dict_get_name(function.globals(), name)?;
|
||||||
|
if value.is_none()
|
||||||
|
&& let Some(builtins) = function.builtins()
|
||||||
|
{
|
||||||
|
value = self.dict_get_name(builtins, name)?;
|
||||||
|
}
|
||||||
|
stack.push(value.map(Fact::value).unwrap_or(Fact::Unknown));
|
||||||
|
}
|
||||||
|
Opcode::LoadDeref | Opcode::LoadClosure => {
|
||||||
|
let fact = if argument >= code.layout().locals.len() {
|
||||||
|
let cell = *function
|
||||||
|
.closure()
|
||||||
|
.get(argument - code.layout().locals.len())
|
||||||
|
.ok_or(VmError::InvalidArgument(
|
||||||
|
"function closure does not match code",
|
||||||
|
))?;
|
||||||
|
if opcode == Opcode::LoadClosure {
|
||||||
|
Fact::Object(cell)
|
||||||
|
} else {
|
||||||
|
match self.cell_get(cell) {
|
||||||
|
Ok(value) => Fact::value(value),
|
||||||
|
Err(VmError::EmptyCell) => Fact::Unknown,
|
||||||
|
Err(error) => return Err(error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if opcode == Opcode::LoadDeref && argument == 0 {
|
||||||
|
receiver.map(Fact::Object).unwrap_or(Fact::Unknown)
|
||||||
|
} else {
|
||||||
|
Fact::Unknown
|
||||||
|
};
|
||||||
|
stack.push(fact);
|
||||||
|
}
|
||||||
|
Opcode::PushNull => stack.push(Fact::Null),
|
||||||
|
Opcode::Copy => stack.push(stack[stack.len() - argument]),
|
||||||
|
Opcode::Swap => {
|
||||||
|
let length = stack.len();
|
||||||
|
stack.swap(length - 1, length - argument);
|
||||||
|
}
|
||||||
|
Opcode::PopTop
|
||||||
|
| Opcode::PopJumpIfFalse
|
||||||
|
| Opcode::PopJumpIfTrue
|
||||||
|
| Opcode::ReturnValue => {
|
||||||
|
stack.pop();
|
||||||
|
}
|
||||||
|
Opcode::ToBool | Opcode::UnaryNot => {
|
||||||
|
*stack.last_mut().unwrap() = Fact::Unknown;
|
||||||
|
}
|
||||||
|
Opcode::CompareOp => {
|
||||||
|
stack.pop();
|
||||||
|
*stack.last_mut().unwrap() = Fact::Unknown;
|
||||||
|
}
|
||||||
|
Opcode::LoadAttr => {
|
||||||
|
let fact = stack.last_mut().unwrap();
|
||||||
|
*fact = if let Some(owner) = fact.object() {
|
||||||
|
match self.lookup_attr(owner, &code.layout().names[argument]) {
|
||||||
|
Ok(AttributeLookup::Value(value)) => Fact::value(value),
|
||||||
|
Ok(AttributeLookup::Method { function, receiver }) => {
|
||||||
|
Fact::Method(Target {
|
||||||
|
function,
|
||||||
|
receiver: Some(receiver),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Err(
|
||||||
|
VmError::AttributeNotFound(_)
|
||||||
|
| VmError::UnsupportedOperation("attributes"),
|
||||||
|
) => Fact::Unknown,
|
||||||
|
Err(error) => return Err(error),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Fact::Unknown
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Opcode::Call => {
|
||||||
|
let start = stack.len() - argument - 2;
|
||||||
|
let implicit = if stack[start + 1] == Fact::Null {
|
||||||
|
(argument > 0)
|
||||||
|
.then(|| stack[start + 2])
|
||||||
|
.and_then(Fact::object)
|
||||||
|
} else {
|
||||||
|
stack[start + 1].object()
|
||||||
|
};
|
||||||
|
let target = match stack[start] {
|
||||||
|
Fact::Object(function) => Target {
|
||||||
|
function,
|
||||||
|
receiver: implicit,
|
||||||
|
},
|
||||||
|
Fact::Method(target) => target,
|
||||||
|
_ => return Ok(None),
|
||||||
|
};
|
||||||
|
callees.insert(target);
|
||||||
|
stack.truncate(start);
|
||||||
|
stack.push(Fact::Unknown);
|
||||||
|
}
|
||||||
|
Opcode::BuildMap => {
|
||||||
|
stack.truncate(stack.len() - argument * 2);
|
||||||
|
stack.push(Fact::Unknown);
|
||||||
|
}
|
||||||
|
Opcode::MakeFunction => {
|
||||||
|
let (defaults, closure) =
|
||||||
|
crate::Instruction::function_counts(decoded.instruction.argument);
|
||||||
|
stack.truncate(stack.len() - defaults - closure - 1);
|
||||||
|
stack.push(Fact::Unknown);
|
||||||
|
}
|
||||||
|
Opcode::Nop | Opcode::DeleteFast | Opcode::JumpForward | Opcode::JumpBackward => {}
|
||||||
|
_ => unreachable!("unproved opcodes were rejected before analysis"),
|
||||||
|
}
|
||||||
|
let successors = match opcode {
|
||||||
|
Opcode::ReturnValue => vec![],
|
||||||
|
Opcode::JumpForward | Opcode::JumpBackward => {
|
||||||
|
vec![crate::code::jump_target(decoded)?]
|
||||||
|
}
|
||||||
|
Opcode::PopJumpIfFalse | Opcode::PopJumpIfTrue => {
|
||||||
|
vec![crate::code::jump_target(decoded)?, decoded.next_offset]
|
||||||
|
}
|
||||||
|
_ => vec![decoded.next_offset],
|
||||||
|
};
|
||||||
|
for offset in successors {
|
||||||
|
let next = instructions
|
||||||
|
.binary_search_by_key(&offset, |item| item.offset)
|
||||||
|
.expect("validated successor");
|
||||||
|
let changed = if let Some(existing) = &mut states[next] {
|
||||||
|
let mut changed = false;
|
||||||
|
for (old, new) in existing.iter_mut().zip(&stack) {
|
||||||
|
if *old != *new && *old != Fact::Unknown {
|
||||||
|
*old = Fact::Unknown;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
changed
|
||||||
|
} else {
|
||||||
|
states[next] = Some(stack.clone());
|
||||||
|
true
|
||||||
|
};
|
||||||
|
if changed {
|
||||||
|
work.push_back(next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Some(callees))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,8 @@ use std::fmt;
|
|||||||
/// retained for embedders; defining it here avoids a dependency on the VM.
|
/// retained for embedders; defining it here avoids a dependency on the VM.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum VmError {
|
pub enum VmError {
|
||||||
|
AwaitFailed(String),
|
||||||
|
AwaitCancelled,
|
||||||
ForeignHandle,
|
ForeignHandle,
|
||||||
InvalidHandle,
|
InvalidHandle,
|
||||||
IdentityExhausted,
|
IdentityExhausted,
|
||||||
@@ -18,6 +20,11 @@ pub enum VmError {
|
|||||||
method: String,
|
method: String,
|
||||||
reason: &'static str,
|
reason: &'static str,
|
||||||
},
|
},
|
||||||
|
OverrideViolation {
|
||||||
|
definition: String,
|
||||||
|
method: String,
|
||||||
|
reason: &'static str,
|
||||||
|
},
|
||||||
UnknownAnnotation(String),
|
UnknownAnnotation(String),
|
||||||
TypeMismatch {
|
TypeMismatch {
|
||||||
function: String,
|
function: String,
|
||||||
@@ -44,6 +51,8 @@ pub enum VmError {
|
|||||||
impl fmt::Display for VmError {
|
impl fmt::Display for VmError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
Self::AwaitFailed(message) => write!(f, "await failed: {message}"),
|
||||||
|
Self::AwaitCancelled => f.write_str("await cancelled"),
|
||||||
Self::ForeignHandle => f.write_str("handle belongs to another VM"),
|
Self::ForeignHandle => f.write_str("handle belongs to another VM"),
|
||||||
Self::InvalidHandle => f.write_str("handle does not identify a live object"),
|
Self::InvalidHandle => f.write_str("handle does not identify a live object"),
|
||||||
Self::IdentityExhausted => f.write_str("process-local VM identities exhausted"),
|
Self::IdentityExhausted => f.write_str("process-local VM identities exhausted"),
|
||||||
@@ -63,6 +72,11 @@ impl fmt::Display for VmError {
|
|||||||
Self::UnknownAnnotation(name) => {
|
Self::UnknownAnnotation(name) => {
|
||||||
write!(f, "unknown or invalid type annotation: {name}")
|
write!(f, "unknown or invalid type annotation: {name}")
|
||||||
}
|
}
|
||||||
|
Self::OverrideViolation {
|
||||||
|
definition,
|
||||||
|
method,
|
||||||
|
reason,
|
||||||
|
} => write!(f, "override {definition}.{method}: {reason}"),
|
||||||
Self::TypeMismatch {
|
Self::TypeMismatch {
|
||||||
function,
|
function,
|
||||||
position,
|
position,
|
||||||
|
|||||||
+132
-9
@@ -41,6 +41,8 @@ struct ReadOnly {
|
|||||||
protected: BTreeSet<ObjectRef>,
|
protected: BTreeSet<ObjectRef>,
|
||||||
reads: Mutex<BTreeSet<ObjectRef>>,
|
reads: Mutex<BTreeSet<ObjectRef>>,
|
||||||
regions: Mutex<Vec<BTreeMap<ObjectRef, bool>>>,
|
regions: Mutex<Vec<BTreeMap<ObjectRef, bool>>>,
|
||||||
|
dependencies: Mutex<BTreeMap<ReadKey, ReadDependency>>,
|
||||||
|
dependency_regions: Mutex<Vec<BTreeMap<ReadKey, ReadDependency>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -63,6 +65,49 @@ pub struct ReadAccess {
|
|||||||
pub content_read: bool,
|
pub content_read: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Borrowed observations; Object handles never retain the observed graph.
|
||||||
|
/// Named values are immutable comparison snapshots, not managed owning edges.
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum ReadDependency {
|
||||||
|
Object {
|
||||||
|
object: ObjectRef,
|
||||||
|
version: u64,
|
||||||
|
},
|
||||||
|
DictionaryKind {
|
||||||
|
object: ObjectRef,
|
||||||
|
},
|
||||||
|
Name {
|
||||||
|
object: ObjectRef,
|
||||||
|
name: String,
|
||||||
|
value: Option<crate::Value>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
impl ReadDependency {
|
||||||
|
pub fn object(&self) -> ObjectRef {
|
||||||
|
match self {
|
||||||
|
Self::Object { object, .. }
|
||||||
|
| Self::DictionaryKind { object }
|
||||||
|
| Self::Name { object, .. } => *object,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
enum ReadKey {
|
||||||
|
Object(ObjectRef),
|
||||||
|
DictionaryKind(ObjectRef),
|
||||||
|
Name(ObjectRef, String),
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TrackedReads {
|
||||||
|
pub accesses: Vec<ReadAccess>,
|
||||||
|
pub dependencies: Vec<ReadDependency>,
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ReadSet {
|
||||||
|
pub objects: Vec<ObjectRef>,
|
||||||
|
pub dependencies: Vec<ReadDependency>,
|
||||||
|
}
|
||||||
|
|
||||||
struct ValidationScope<'a> {
|
struct ValidationScope<'a> {
|
||||||
heap: &'a Heap,
|
heap: &'a Heap,
|
||||||
thread: ThreadId,
|
thread: ThreadId,
|
||||||
@@ -128,23 +173,34 @@ impl Heap {
|
|||||||
protected,
|
protected,
|
||||||
reads: Mutex::new(BTreeSet::new()),
|
reads: Mutex::new(BTreeSet::new()),
|
||||||
regions: Mutex::new(Vec::new()),
|
regions: Mutex::new(Vec::new()),
|
||||||
|
dependencies: Mutex::new(BTreeMap::new()),
|
||||||
|
dependency_regions: Mutex::new(Vec::new()),
|
||||||
});
|
});
|
||||||
self.read_only.len()
|
self.read_only.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn end_read_only(&mut self, scope: usize) -> Result<Vec<ObjectRef>, VmError> {
|
pub(crate) fn end_read_only(&mut self, scope: usize) -> Result<Vec<ObjectRef>, VmError> {
|
||||||
|
Ok(self.end_read_only_precise(scope)?.objects)
|
||||||
|
}
|
||||||
|
pub(crate) fn end_read_only_precise(&mut self, scope: usize) -> Result<ReadSet, VmError> {
|
||||||
if scope == 0 || scope != self.read_only.len() {
|
if scope == 0 || scope != self.read_only.len() {
|
||||||
return Err(VmError::InvalidArgument("read-only scope nesting mismatch"));
|
return Err(VmError::InvalidArgument("read-only scope nesting mismatch"));
|
||||||
}
|
}
|
||||||
Ok(self
|
let scope = self.read_only.pop().unwrap();
|
||||||
.read_only
|
Ok(ReadSet {
|
||||||
.pop()
|
objects: scope
|
||||||
.unwrap()
|
|
||||||
.reads
|
.reads
|
||||||
.into_inner()
|
.into_inner()
|
||||||
.expect("read tracker poisoned")
|
.expect("read tracker poisoned")
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect())
|
.collect(),
|
||||||
|
dependencies: scope
|
||||||
|
.dependencies
|
||||||
|
.into_inner()
|
||||||
|
.expect("read tracker poisoned")
|
||||||
|
.into_values()
|
||||||
|
.collect(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn begin_read_tracking(&self) -> Result<usize, VmError> {
|
pub(crate) fn begin_read_tracking(&self) -> Result<usize, VmError> {
|
||||||
@@ -153,6 +209,11 @@ impl Heap {
|
|||||||
))?;
|
))?;
|
||||||
let mut regions = scope.regions.lock().expect("read tracker poisoned");
|
let mut regions = scope.regions.lock().expect("read tracker poisoned");
|
||||||
regions.push(BTreeMap::new());
|
regions.push(BTreeMap::new());
|
||||||
|
scope
|
||||||
|
.dependency_regions
|
||||||
|
.lock()
|
||||||
|
.expect("read tracker poisoned")
|
||||||
|
.push(BTreeMap::new());
|
||||||
Ok(regions.len())
|
Ok(regions.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,14 +229,22 @@ impl Heap {
|
|||||||
&self,
|
&self,
|
||||||
token: usize,
|
token: usize,
|
||||||
) -> Result<Vec<ReadAccess>, VmError> {
|
) -> Result<Vec<ReadAccess>, VmError> {
|
||||||
|
Ok(self.end_read_tracking_precise(token)?.accesses)
|
||||||
|
}
|
||||||
|
pub(crate) fn end_read_tracking_precise(&self, token: usize) -> Result<TrackedReads, VmError> {
|
||||||
let scope = self.read_only.last().ok_or(VmError::InvalidArgument(
|
let scope = self.read_only.last().ok_or(VmError::InvalidArgument(
|
||||||
"read tracking requires a read-only scope",
|
"read tracking requires a read-only scope",
|
||||||
))?;
|
))?;
|
||||||
let mut regions = scope.regions.lock().expect("read tracker poisoned");
|
let mut regions = scope.regions.lock().expect("read tracker poisoned");
|
||||||
if token == 0 || token != regions.len() {
|
let mut precise = scope
|
||||||
|
.dependency_regions
|
||||||
|
.lock()
|
||||||
|
.expect("read tracker poisoned");
|
||||||
|
if token == 0 || token != regions.len() || token != precise.len() {
|
||||||
return Err(VmError::InvalidArgument("read tracking nesting mismatch"));
|
return Err(VmError::InvalidArgument("read tracking nesting mismatch"));
|
||||||
}
|
}
|
||||||
Ok(regions
|
Ok(TrackedReads {
|
||||||
|
accesses: regions
|
||||||
.pop()
|
.pop()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -183,7 +252,9 @@ impl Heap {
|
|||||||
object,
|
object,
|
||||||
content_read,
|
content_read,
|
||||||
})
|
})
|
||||||
.collect())
|
.collect(),
|
||||||
|
dependencies: precise.pop().unwrap().into_values().collect(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn is_read_only(&self) -> bool {
|
pub(crate) fn is_read_only(&self) -> bool {
|
||||||
@@ -209,7 +280,45 @@ impl Heap {
|
|||||||
compute()
|
compute()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn record_read(&self, reference: ObjectRef) {
|
pub(crate) fn record_read(&self, reference: ObjectRef, version: u64) {
|
||||||
|
if self.read_only.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.record_observation(reference, ReadKey::Object(reference), || {
|
||||||
|
ReadDependency::Object {
|
||||||
|
object: reference,
|
||||||
|
version,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
pub(crate) fn record_dictionary_kind(&self, reference: ObjectRef) {
|
||||||
|
self.record_observation(reference, ReadKey::DictionaryKind(reference), || {
|
||||||
|
ReadDependency::DictionaryKind { object: reference }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
pub(crate) fn record_name(
|
||||||
|
&self,
|
||||||
|
reference: ObjectRef,
|
||||||
|
name: &str,
|
||||||
|
value: &Option<crate::Value>,
|
||||||
|
) {
|
||||||
|
if self.read_only.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.record_observation(reference, ReadKey::Name(reference, name.into()), || {
|
||||||
|
ReadDependency::Name {
|
||||||
|
object: reference,
|
||||||
|
name: name.into(),
|
||||||
|
value: value.clone(),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
fn record_observation(
|
||||||
|
&self,
|
||||||
|
reference: ObjectRef,
|
||||||
|
key: ReadKey,
|
||||||
|
build: impl Fn() -> ReadDependency,
|
||||||
|
) {
|
||||||
if self.read_only.is_empty() {
|
if self.read_only.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -220,6 +329,20 @@ impl Heap {
|
|||||||
.contains_key(&std::thread::current().id());
|
.contains_key(&std::thread::current().id());
|
||||||
for scope in &self.read_only {
|
for scope in &self.read_only {
|
||||||
if scope.protected.contains(&reference) {
|
if scope.protected.contains(&reference) {
|
||||||
|
scope
|
||||||
|
.dependencies
|
||||||
|
.lock()
|
||||||
|
.expect("read tracker poisoned")
|
||||||
|
.entry(key.clone())
|
||||||
|
.or_insert_with(&build);
|
||||||
|
for region in scope
|
||||||
|
.dependency_regions
|
||||||
|
.lock()
|
||||||
|
.expect("read tracker poisoned")
|
||||||
|
.iter_mut()
|
||||||
|
{
|
||||||
|
region.entry(key.clone()).or_insert_with(&build);
|
||||||
|
}
|
||||||
scope
|
scope
|
||||||
.reads
|
.reads
|
||||||
.lock()
|
.lock()
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ pub enum Opcode {
|
|||||||
BuildMap = 27,
|
BuildMap = 27,
|
||||||
MakeFunction = 28,
|
MakeFunction = 28,
|
||||||
BuildClass = 29,
|
BuildClass = 29,
|
||||||
|
Await = 30,
|
||||||
|
BinaryArithmetic = 31,
|
||||||
|
UnaryNegate = 32,
|
||||||
ExtendedArg = 255,
|
ExtendedArg = 255,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +74,9 @@ impl Opcode {
|
|||||||
27 => Self::BuildMap,
|
27 => Self::BuildMap,
|
||||||
28 => Self::MakeFunction,
|
28 => Self::MakeFunction,
|
||||||
29 => Self::BuildClass,
|
29 => Self::BuildClass,
|
||||||
|
30 => Self::Await,
|
||||||
|
31 => Self::BinaryArithmetic,
|
||||||
|
32 => Self::UnaryNegate,
|
||||||
255 => Self::ExtendedArg,
|
255 => Self::ExtendedArg,
|
||||||
_ => return None,
|
_ => return None,
|
||||||
})
|
})
|
||||||
@@ -85,6 +91,8 @@ impl Opcode {
|
|||||||
| Self::ReturnValue
|
| Self::ReturnValue
|
||||||
| Self::ToBool
|
| Self::ToBool
|
||||||
| Self::UnaryNot
|
| Self::UnaryNot
|
||||||
|
| Self::Await
|
||||||
|
| Self::UnaryNegate
|
||||||
| Self::BuildClass
|
| Self::BuildClass
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,21 +33,22 @@ pub use builtins::{
|
|||||||
pub use changes::{ChangeBatch, ChangeKey, ObjectChange};
|
pub use changes::{ChangeBatch, ChangeKey, ObjectChange};
|
||||||
pub use code::{CodeLayout, EndorCode, EndorCodeObject, SourceSignature};
|
pub use code::{CodeLayout, EndorCode, EndorCodeObject, SourceSignature};
|
||||||
pub use error::VmError;
|
pub use error::VmError;
|
||||||
pub use heap::{ObjectRef, ReadAccess, WeakObjectRef};
|
pub use heap::{ObjectRef, ReadAccess, ReadDependency, ReadSet, TrackedReads, WeakObjectRef};
|
||||||
pub use instruction::{DecodedInstruction, Instruction, Opcode};
|
pub use instruction::{DecodedInstruction, Instruction, Opcode};
|
||||||
pub use native::{
|
pub use native::{
|
||||||
CallArity, CallEffect, CallSignature, CallType, ContextNativeFunction, NativeCallable,
|
CallArity, CallEffect, CallSignature, CallType, ContextNativeFunction, NativeCallable,
|
||||||
NativeContext, NativeResult,
|
NativeContext, NativeResult,
|
||||||
};
|
};
|
||||||
pub use number::{
|
pub use number::{
|
||||||
EndorDecimal, EndorInteger, EndorNumber, EndorNumberObject, EndorRational, EndorReal,
|
ArithmeticOp, EndorDecimal, EndorInteger, EndorNumber, EndorNumberObject, EndorRational,
|
||||||
|
EndorReal,
|
||||||
};
|
};
|
||||||
pub use object::{
|
pub use object::{
|
||||||
CompareOp, EndorNativeCall, EndorNativeFunctionObject, EndorObject, NativeCall, NativeFunction,
|
AwaitPoll, CompareOp, EndorNativeCall, EndorNativeFunctionObject, EndorObject, NativeCall,
|
||||||
ObjectHeader, ObjectPayload, Record, Value,
|
NativeFunction, ObjectHeader, ObjectPayload, Record, Value,
|
||||||
};
|
};
|
||||||
pub use store::ObjectStore;
|
pub use store::ObjectStore;
|
||||||
pub use types::{
|
pub use types::{
|
||||||
AnnotationType, ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement, RootTypes,
|
AnnotationType, ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement, RootTypes,
|
||||||
TypeConstructor,
|
TypeConstructor, TypedMethodRequirement,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -55,6 +55,9 @@ impl CallArity {
|
|||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum CallEffect {
|
pub enum CallEffect {
|
||||||
ReadOnly,
|
ReadOnly,
|
||||||
|
/// The native body itself is read-only; delegated effects are dynamic and
|
||||||
|
/// checked by the VM in the same execution/heap scope.
|
||||||
|
Delegated,
|
||||||
Mutating,
|
Mutating,
|
||||||
External,
|
External,
|
||||||
}
|
}
|
||||||
@@ -75,7 +78,7 @@ impl CallType {
|
|||||||
pub fn nullable(value: crate::AnnotationType) -> Self {
|
pub fn nullable(value: crate::AnnotationType) -> Self {
|
||||||
Self::Union(vec![value, crate::AnnotationType::Unit])
|
Self::Union(vec![value, crate::AnnotationType::Unit])
|
||||||
}
|
}
|
||||||
fn alternatives(&self) -> &[crate::AnnotationType] {
|
pub(crate) fn alternatives(&self) -> &[crate::AnnotationType] {
|
||||||
match self {
|
match self {
|
||||||
Self::Single(value) => std::slice::from_ref(value),
|
Self::Single(value) => std::slice::from_ref(value),
|
||||||
Self::Union(values) => values,
|
Self::Union(values) => values,
|
||||||
@@ -192,7 +195,9 @@ impl CallSignature {
|
|||||||
) -> Result<(), VmError> {
|
) -> Result<(), VmError> {
|
||||||
self.validate(store)?;
|
self.validate(store)?;
|
||||||
self.arity.check(name.into(), arguments.len())?;
|
self.arity.check(name.into(), arguments.len())?;
|
||||||
if store.is_read_only() && self.effect != CallEffect::ReadOnly {
|
if store.is_read_only()
|
||||||
|
&& !matches!(self.effect, CallEffect::ReadOnly | CallEffect::Delegated)
|
||||||
|
{
|
||||||
return Err(VmError::InvalidArgument(
|
return Err(VmError::InvalidArgument(
|
||||||
"native effect is forbidden during read-only execution",
|
"native effect is forbidden during read-only execution",
|
||||||
));
|
));
|
||||||
@@ -217,7 +222,7 @@ impl CallSignature {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait NativeCallable: fmt::Debug + Send + Sync + 'static {
|
pub trait NativeCallable: fmt::Debug + Send + Sync + std::any::Any {
|
||||||
/// Immutable resolved types; referenced definitions must be traced by the
|
/// Immutable resolved types; referenced definitions must be traced by the
|
||||||
/// payload. ContextNativeFunction snapshots and traces them automatically.
|
/// payload. ContextNativeFunction snapshots and traces them automatically.
|
||||||
fn signature(&self) -> Option<CallSignature> {
|
fn signature(&self) -> Option<CallSignature> {
|
||||||
@@ -375,6 +380,12 @@ impl<'a> NativeContext<'a> {
|
|||||||
if let Some(signature) = &signature {
|
if let Some(signature) = &signature {
|
||||||
signature.check_arguments(store, &name, arguments)?;
|
signature.check_arguments(store, &name, arguments)?;
|
||||||
}
|
}
|
||||||
|
let trait_returns = if let Some(Value::Object(receiver)) = arguments.first() {
|
||||||
|
store.check_method_contract_call(function, *receiver, false)?;
|
||||||
|
store.method_trait_returns(function, *receiver)?
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
};
|
||||||
let mut context = Self {
|
let mut context = Self {
|
||||||
store,
|
store,
|
||||||
roots: Vec::new(),
|
roots: Vec::new(),
|
||||||
@@ -386,6 +397,11 @@ impl<'a> NativeContext<'a> {
|
|||||||
context.protect(&Value::Object(reference))?;
|
context.protect(&Value::Object(reference))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (_, expected) in &trait_returns {
|
||||||
|
for reference in expected.references() {
|
||||||
|
context.protect(&Value::Object(reference))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
for argument in arguments {
|
for argument in arguments {
|
||||||
context.protect(argument)?;
|
context.protect(argument)?;
|
||||||
}
|
}
|
||||||
@@ -393,6 +409,11 @@ impl<'a> NativeContext<'a> {
|
|||||||
if let (Some(signature), NativeResult::Return(value)) = (&signature, &result) {
|
if let (Some(signature), NativeResult::Return(value)) = (&signature, &result) {
|
||||||
signature.check_return(context.store(), &name, value)?;
|
signature.check_return(context.store(), &name, value)?;
|
||||||
}
|
}
|
||||||
|
if let NativeResult::Return(value) = &result {
|
||||||
|
for (name, expected) in &trait_returns {
|
||||||
|
expected.check(context.store(), name, "return".into(), value)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
let transferred_from = context.roots.len();
|
let transferred_from = context.roots.len();
|
||||||
match &result {
|
match &result {
|
||||||
NativeResult::Return(value) => context.protect(value)?,
|
NativeResult::Return(value) => context.protect(value)?,
|
||||||
@@ -432,6 +453,14 @@ pub struct ContextNativeFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ContextNativeFunction {
|
impl ContextNativeFunction {
|
||||||
|
/// Borrow the concrete host implementation for intrinsic dispatch checks.
|
||||||
|
/// Function identity or a mutable class-table slot alone is not proof that
|
||||||
|
/// its current payload still implements a host intrinsic.
|
||||||
|
pub fn implementation<T: NativeCallable>(&self) -> Option<&T> {
|
||||||
|
let implementation: &dyn std::any::Any = self.callable.as_ref();
|
||||||
|
implementation.downcast_ref()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
callable: impl NativeCallable,
|
callable: impl NativeCallable,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
//! Lossless number representations. Arithmetic and numeric equivalence are
|
//! Lossless number representations. Arithmetic and numeric equivalence are
|
||||||
//! deliberately separate from representation equality.
|
//! deliberately separate from representation equality; real operations preserve
|
||||||
|
//! exact values unless a binary-float operand explicitly selects float mode.
|
||||||
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
@@ -39,7 +40,16 @@ impl EndorInteger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_i64(&self) -> Option<i64> {
|
pub fn to_i64(&self) -> Option<i64> {
|
||||||
self.to_string().parse().ok()
|
let magnitude = self.digits.parse::<u64>().ok()?;
|
||||||
|
if self.negative {
|
||||||
|
if magnitude == (i64::MAX as u64) + 1 {
|
||||||
|
Some(i64::MIN)
|
||||||
|
} else {
|
||||||
|
i64::try_from(magnitude).ok()?.checked_neg()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
i64::try_from(magnitude).ok()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,10 +309,13 @@ impl ObjectPayload for EndorNumberObject {
|
|||||||
Err(VmError::UnsupportedOperation("numeric hash"))
|
Err(VmError::UnsupportedOperation("numeric hash"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compare(&self, other: &dyn ObjectPayload, _op: CompareOp) -> Result<Option<bool>, VmError> {
|
fn compare(&self, other: &dyn ObjectPayload, op: CompareOp) -> Result<Option<bool>, VmError> {
|
||||||
if other.as_any().is::<Self>() {
|
if let Some(other) = other.as_any().downcast_ref::<Self>() {
|
||||||
|
if self.value.as_real().is_none() || other.value.as_real().is_none() {
|
||||||
return Err(VmError::UnsupportedOperation("numeric comparison"));
|
return Err(VmError::UnsupportedOperation("numeric comparison"));
|
||||||
}
|
}
|
||||||
|
return self.value.numeric_compare(&other.value, op).map(Some);
|
||||||
|
}
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -318,3 +331,222 @@ fn split_sign(text: &str) -> (bool, &str) {
|
|||||||
fn is_digits(text: &str) -> bool {
|
fn is_digits(text: &str) -> bool {
|
||||||
!text.is_empty() && text.bytes().all(|byte| byte.is_ascii_digit())
|
!text.is_empty() && text.bytes().all(|byte| byte.is_ascii_digit())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Source arithmetic is exact for integers/decimals/rationals. A binary-float
|
||||||
|
/// operand explicitly selects IEEE floating-point arithmetic, as used by host
|
||||||
|
/// geometry/math APIs. Complex arithmetic remains unsupported.
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum ArithmeticOp {
|
||||||
|
Add,
|
||||||
|
Subtract,
|
||||||
|
Multiply,
|
||||||
|
Divide,
|
||||||
|
}
|
||||||
|
impl ArithmeticOp {
|
||||||
|
pub fn from_argument(value: u32) -> Option<Self> {
|
||||||
|
match value {
|
||||||
|
0 => Some(Self::Add),
|
||||||
|
1 => Some(Self::Subtract),
|
||||||
|
2 => Some(Self::Multiply),
|
||||||
|
3 => Some(Self::Divide),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl EndorNumber {
|
||||||
|
pub fn negated(&self) -> Result<Self, VmError> {
|
||||||
|
if let Some(EndorReal::BinaryFloat(bits)) = self.as_real() {
|
||||||
|
return Ok(Self::Real(EndorReal::BinaryFloat(bits ^ (1_u64 << 63))));
|
||||||
|
}
|
||||||
|
Self::from(0_i64).arithmetic(self, ArithmeticOp::Subtract)
|
||||||
|
}
|
||||||
|
/// Explicit narrowing for graphics/FFI boundaries; exact values themselves
|
||||||
|
/// retain their representation. Non-real or non-finite values are rejected.
|
||||||
|
pub fn to_f64(&self) -> Result<f64, VmError> {
|
||||||
|
use num_traits::ToPrimitive;
|
||||||
|
let real = self
|
||||||
|
.as_real()
|
||||||
|
.ok_or(VmError::UnsupportedOperation("complex to float"))?;
|
||||||
|
let value = if let EndorReal::BinaryFloat(bits) = real {
|
||||||
|
f64::from_bits(*bits)
|
||||||
|
} else {
|
||||||
|
exact_real(real)?.to_f64().ok_or(VmError::InvalidArgument(
|
||||||
|
"number exceeds binary float range",
|
||||||
|
))?
|
||||||
|
};
|
||||||
|
if !value.is_finite() {
|
||||||
|
return Err(VmError::InvalidArgument("non-finite float conversion"));
|
||||||
|
}
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
pub fn arithmetic(&self, other: &Self, op: ArithmeticOp) -> Result<Self, VmError> {
|
||||||
|
if let (Some(a), Some(b)) = (self.as_i64(), other.as_i64()) {
|
||||||
|
let integer = match op {
|
||||||
|
ArithmeticOp::Add => a.checked_add(b),
|
||||||
|
ArithmeticOp::Subtract => a.checked_sub(b),
|
||||||
|
ArithmeticOp::Multiply => a.checked_mul(b),
|
||||||
|
ArithmeticOp::Divide => a
|
||||||
|
.checked_rem(b)
|
||||||
|
.filter(|&r| r == 0)
|
||||||
|
.and_then(|_| a.checked_div(b)),
|
||||||
|
};
|
||||||
|
if let Some(result) = integer {
|
||||||
|
return Ok(Self::from(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
use num_traits::{ToPrimitive, Zero};
|
||||||
|
let (a, b) = (
|
||||||
|
self.as_real()
|
||||||
|
.ok_or(VmError::UnsupportedOperation("complex arithmetic"))?,
|
||||||
|
other
|
||||||
|
.as_real()
|
||||||
|
.ok_or(VmError::UnsupportedOperation("complex arithmetic"))?,
|
||||||
|
);
|
||||||
|
if matches!(a, EndorReal::BinaryFloat(_)) || matches!(b, EndorReal::BinaryFloat(_)) {
|
||||||
|
let convert = |n: &EndorReal| -> Result<f64, VmError> {
|
||||||
|
if let EndorReal::BinaryFloat(bits) = n {
|
||||||
|
Ok(f64::from_bits(*bits))
|
||||||
|
} else {
|
||||||
|
exact_real(n)?.to_f64().ok_or(VmError::InvalidArgument(
|
||||||
|
"number exceeds binary float range",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let (a, b) = (convert(a)?, convert(b)?);
|
||||||
|
if matches!(op, ArithmeticOp::Divide) && b == 0. {
|
||||||
|
return Err(VmError::InvalidArgument("division by zero"));
|
||||||
|
}
|
||||||
|
let result = match op {
|
||||||
|
ArithmeticOp::Add => a + b,
|
||||||
|
ArithmeticOp::Subtract => a - b,
|
||||||
|
ArithmeticOp::Multiply => a * b,
|
||||||
|
ArithmeticOp::Divide => a / b,
|
||||||
|
};
|
||||||
|
if !result.is_finite() {
|
||||||
|
return Err(VmError::InvalidArgument("non-finite arithmetic result"));
|
||||||
|
}
|
||||||
|
return Ok(Self::binary_float(result));
|
||||||
|
}
|
||||||
|
let (a, b) = (exact_real(a)?, exact_real(b)?);
|
||||||
|
if matches!(op, ArithmeticOp::Divide) && b.is_zero() {
|
||||||
|
return Err(VmError::InvalidArgument("division by zero"));
|
||||||
|
}
|
||||||
|
let result = match op {
|
||||||
|
ArithmeticOp::Add => a + b,
|
||||||
|
ArithmeticOp::Subtract => a - b,
|
||||||
|
ArithmeticOp::Multiply => a * b,
|
||||||
|
ArithmeticOp::Divide => a / b,
|
||||||
|
};
|
||||||
|
if result.numer().bits() > 16_384 || result.denom().bits() > 16_384 {
|
||||||
|
return Err(VmError::InvalidArgument(
|
||||||
|
"arithmetic result exceeds operation budget",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if result.is_integer() {
|
||||||
|
Self::integer(&result.to_integer().to_string())
|
||||||
|
} else {
|
||||||
|
Self::rational(&result.numer().to_string(), &result.denom().to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn numeric_compare(&self, other: &Self, op: CompareOp) -> Result<bool, VmError> {
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
let (a, b) = (
|
||||||
|
self.as_real()
|
||||||
|
.ok_or(VmError::UnsupportedOperation("complex comparison"))?,
|
||||||
|
other
|
||||||
|
.as_real()
|
||||||
|
.ok_or(VmError::UnsupportedOperation("complex comparison"))?,
|
||||||
|
);
|
||||||
|
if let (Some(a), Some(b)) = (self.as_i64(), other.as_i64()) {
|
||||||
|
return Ok(match op {
|
||||||
|
CompareOp::Eq => a == b,
|
||||||
|
CompareOp::Ne => a != b,
|
||||||
|
CompareOp::Lt => a < b,
|
||||||
|
CompareOp::Le => a <= b,
|
||||||
|
CompareOp::Gt => a > b,
|
||||||
|
CompareOp::Ge => a >= b,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Finite binary floats compare as exact binary rationals, so a large
|
||||||
|
// integer never loses precision merely to answer an ordering question.
|
||||||
|
let order = if let (EndorReal::BinaryFloat(a), EndorReal::BinaryFloat(b)) = (a, b) {
|
||||||
|
f64::from_bits(*a).partial_cmp(&f64::from_bits(*b))
|
||||||
|
} else {
|
||||||
|
let nonfinite = |n: &EndorReal| match n {
|
||||||
|
EndorReal::BinaryFloat(bits) if !f64::from_bits(*bits).is_finite() => {
|
||||||
|
Some(f64::from_bits(*bits))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
match (nonfinite(a), nonfinite(b)) {
|
||||||
|
(Some(a), _) if a.is_nan() => None,
|
||||||
|
(_, Some(b)) if b.is_nan() => None,
|
||||||
|
(Some(a), _) => Some(if a.is_sign_positive() {
|
||||||
|
Ordering::Greater
|
||||||
|
} else {
|
||||||
|
Ordering::Less
|
||||||
|
}),
|
||||||
|
(_, Some(b)) => Some(if b.is_sign_positive() {
|
||||||
|
Ordering::Less
|
||||||
|
} else {
|
||||||
|
Ordering::Greater
|
||||||
|
}),
|
||||||
|
_ => Some(exact_real(a)?.cmp(&exact_real(b)?)),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(match op {
|
||||||
|
CompareOp::Eq => order == Some(Ordering::Equal),
|
||||||
|
CompareOp::Ne => order != Some(Ordering::Equal),
|
||||||
|
CompareOp::Lt => order == Some(Ordering::Less),
|
||||||
|
CompareOp::Le => matches!(order, Some(Ordering::Less | Ordering::Equal)),
|
||||||
|
CompareOp::Gt => order == Some(Ordering::Greater),
|
||||||
|
CompareOp::Ge => matches!(order, Some(Ordering::Greater | Ordering::Equal)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn exact_real(value: &EndorReal) -> Result<num_rational::BigRational, VmError> {
|
||||||
|
use num_bigint::BigInt;
|
||||||
|
use num_rational::BigRational;
|
||||||
|
let integer = |s: &str| -> Result<BigInt, VmError> {
|
||||||
|
if s.len() > 4096 {
|
||||||
|
return Err(VmError::InvalidArgument(
|
||||||
|
"numeric operand exceeds operation budget",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
s.parse()
|
||||||
|
.map_err(|_| VmError::InvalidArgument("invalid exact number"))
|
||||||
|
};
|
||||||
|
match value {
|
||||||
|
EndorReal::Integer(n) => Ok(BigRational::from_integer(integer(&n.to_string())?)),
|
||||||
|
EndorReal::Rational(n) => Ok(BigRational::new(
|
||||||
|
integer(&n.numerator.to_string())?,
|
||||||
|
integer(&n.denominator.to_string())?,
|
||||||
|
)),
|
||||||
|
EndorReal::BinaryFloat(bits) => BigRational::from_float(f64::from_bits(*bits))
|
||||||
|
.ok_or(VmError::InvalidArgument("non-finite exact conversion")),
|
||||||
|
EndorReal::Decimal(n) => {
|
||||||
|
let parts: Vec<_> = n.literal.split(['e', 'E']).collect();
|
||||||
|
let exponent = if parts.len() == 2 {
|
||||||
|
parts[1].parse::<i32>().map_err(|_| {
|
||||||
|
VmError::InvalidArgument("numeric exponent exceeds operation budget")
|
||||||
|
})?
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
let decimals = parts[0].split_once('.').map_or(0, |(_, s)| s.len()) as i64;
|
||||||
|
let power = i64::from(exponent) - decimals;
|
||||||
|
if power.abs() > 4096 {
|
||||||
|
return Err(VmError::InvalidArgument(
|
||||||
|
"numeric exponent exceeds operation budget",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let digits = integer(&parts[0].replace('.', ""))?;
|
||||||
|
let factor = BigInt::from(10).pow(power.unsigned_abs() as u32);
|
||||||
|
Ok(if power < 0 {
|
||||||
|
BigRational::new(digits, factor)
|
||||||
|
} else {
|
||||||
|
BigRational::from_integer(digits * factor)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -42,6 +42,14 @@ impl CompareOp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Non-blocking awaitable observation. Ready object values are borrowed from
|
||||||
|
/// the managed graph and must be retained by the consumer before dropping it.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum AwaitPoll {
|
||||||
|
Pending,
|
||||||
|
Ready(Value),
|
||||||
|
}
|
||||||
|
|
||||||
/// Rust-side operation interface, not a language type or metaclass.
|
/// Rust-side operation interface, not a language type or metaclass.
|
||||||
///
|
///
|
||||||
/// Implementations must report every payload-owned object reference in trace(),
|
/// Implementations must report every payload-owned object reference in trace(),
|
||||||
@@ -51,6 +59,10 @@ impl CompareOp {
|
|||||||
/// Rust Drop releases host resources when the payload is destroyed.
|
/// Rust Drop releases host resources when the payload is destroyed.
|
||||||
/// Equal payloads must provide compatible hashes, or reject hashing explicitly.
|
/// Equal payloads must provide compatible hashes, or reject hashing explicitly.
|
||||||
pub trait ObjectPayload: Any + fmt::Debug + Send + Sync {
|
pub trait ObjectPayload: Any + fmt::Debug + Send + Sync {
|
||||||
|
fn poll_await(&self) -> Result<AwaitPoll, VmError> {
|
||||||
|
Err(VmError::UnsupportedOperation("await"))
|
||||||
|
}
|
||||||
|
|
||||||
/// Explicit function category; callable Type objects are not functions.
|
/// Explicit function category; callable Type objects are not functions.
|
||||||
fn is_function(&self) -> bool {
|
fn is_function(&self) -> bool {
|
||||||
false
|
false
|
||||||
|
|||||||
@@ -12,10 +12,19 @@ pub struct ObjectStore {
|
|||||||
pub(crate) heap: Heap,
|
pub(crate) heap: Heap,
|
||||||
function_type: Option<WeakObjectRef>,
|
function_type: Option<WeakObjectRef>,
|
||||||
pub(crate) field_type_checks: bool,
|
pub(crate) field_type_checks: bool,
|
||||||
|
pub(crate) active_attribute_checks: std::sync::Mutex<
|
||||||
|
std::collections::HashMap<std::thread::ThreadId, std::collections::BTreeSet<ObjectRef>>,
|
||||||
|
>,
|
||||||
|
pub(crate) active_trait_checks: std::sync::Mutex<
|
||||||
|
std::collections::HashMap<
|
||||||
|
std::thread::ThreadId,
|
||||||
|
std::collections::BTreeSet<(ObjectRef, ObjectRef)>,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectStore {
|
impl ObjectStore {
|
||||||
/// Opt-in declared-field checks, shared by VM and NativeContext writes.
|
/// Opt-in declared-field and method substitution checks, shared with the VM.
|
||||||
pub fn set_field_type_checks(&mut self, enabled: bool) -> Result<(), VmError> {
|
pub fn set_field_type_checks(&mut self, enabled: bool) -> Result<(), VmError> {
|
||||||
if self.is_read_only() {
|
if self.is_read_only() {
|
||||||
return Err(VmError::UnsupportedOperation(
|
return Err(VmError::UnsupportedOperation(
|
||||||
@@ -34,6 +43,29 @@ impl ObjectStore {
|
|||||||
pub fn end_read_only(&mut self, scope: usize) -> Result<Vec<ObjectRef>, VmError> {
|
pub fn end_read_only(&mut self, scope: usize) -> Result<Vec<ObjectRef>, VmError> {
|
||||||
self.heap.end_read_only(scope)
|
self.heap.end_read_only(scope)
|
||||||
}
|
}
|
||||||
|
pub fn end_read_only_precise(&mut self, scope: usize) -> Result<crate::ReadSet, VmError> {
|
||||||
|
self.heap.end_read_only_precise(scope)
|
||||||
|
}
|
||||||
|
pub fn read_dependency_valid(&self, dependency: &crate::ReadDependency) -> bool {
|
||||||
|
match dependency {
|
||||||
|
crate::ReadDependency::Object { object, version } => {
|
||||||
|
self.change_version(*object).ok() == Some(*version)
|
||||||
|
}
|
||||||
|
crate::ReadDependency::DictionaryKind { object } => {
|
||||||
|
self.dictionary_kind(*object).is_ok()
|
||||||
|
}
|
||||||
|
crate::ReadDependency::Name {
|
||||||
|
object,
|
||||||
|
name,
|
||||||
|
value,
|
||||||
|
} => self
|
||||||
|
.dict_get_name(*object, name)
|
||||||
|
.is_ok_and(|current| current == *value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn end_read_tracking_precise(&self, token: usize) -> Result<crate::TrackedReads, VmError> {
|
||||||
|
self.heap.end_read_tracking_precise(token)
|
||||||
|
}
|
||||||
/// Track one subcall inside the active read-only scope. Regions nest in
|
/// Track one subcall inside the active read-only scope. Regions nest in
|
||||||
/// LIFO order and do not clear the enclosing call's read dependencies.
|
/// LIFO order and do not clear the enclosing call's read dependencies.
|
||||||
/// Handles returned by end_read_tracking are borrowed, never owning roots.
|
/// Handles returned by end_read_tracking are borrowed, never owning roots.
|
||||||
@@ -151,6 +183,8 @@ impl ObjectStore {
|
|||||||
heap: Heap::new()?,
|
heap: Heap::new()?,
|
||||||
function_type: None,
|
function_type: None,
|
||||||
field_type_checks: false,
|
field_type_checks: false,
|
||||||
|
active_attribute_checks: std::sync::Mutex::new(std::collections::HashMap::new()),
|
||||||
|
active_trait_checks: std::sync::Mutex::new(std::collections::HashMap::new()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,8 +237,10 @@ impl ObjectStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn object(&self, reference: ObjectRef) -> Result<&EndorObject, VmError> {
|
pub fn object(&self, reference: ObjectRef) -> Result<&EndorObject, VmError> {
|
||||||
self.heap.record_read(reference);
|
let object = self.heap.get(reference)?;
|
||||||
self.heap.get(reference)
|
self.heap
|
||||||
|
.record_read(reference, object.header().change_version());
|
||||||
|
Ok(object)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Borrowed storage path: depend on Contents for intermediate objects and
|
/// Borrowed storage path: depend on Contents for intermediate objects and
|
||||||
@@ -305,6 +341,9 @@ impl ObjectStore {
|
|||||||
if self.field_type_checks {
|
if self.field_type_checks {
|
||||||
self.check_field_write(reference, &name, &value)?;
|
self.check_field_write(reference, &name, &value)?;
|
||||||
}
|
}
|
||||||
|
for object in self.attribute_path(reference)? {
|
||||||
|
self.check_typed_method_write(object, &name)?;
|
||||||
|
}
|
||||||
let storage = *self
|
let storage = *self
|
||||||
.attribute_path(reference)?
|
.attribute_path(reference)?
|
||||||
.last()
|
.last()
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ impl ObjectStore {
|
|||||||
"function defaults or closure do not match code",
|
"function defaults or closure do not match code",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
self.payload::<EndorDictObject>(function.globals(), "dict")?;
|
self.dictionary_kind(function.globals())?;
|
||||||
if let Some(builtins) = function.builtins() {
|
if let Some(builtins) = function.builtins() {
|
||||||
self.payload::<EndorDictObject>(builtins, "dict")?;
|
self.dictionary_kind(builtins)?;
|
||||||
}
|
}
|
||||||
for cell in function.closure() {
|
for cell in function.closure() {
|
||||||
self.payload::<EndorCellObject>(*cell, "cell")?;
|
self.payload::<EndorCellObject>(*cell, "cell")?;
|
||||||
@@ -67,10 +67,10 @@ impl ObjectStore {
|
|||||||
defaults: Vec<Value>,
|
defaults: Vec<Value>,
|
||||||
closure: Vec<ObjectRef>,
|
closure: Vec<ObjectRef>,
|
||||||
) -> Result<ObjectRef, VmError> {
|
) -> Result<ObjectRef, VmError> {
|
||||||
self.payload::<EndorDictObject>(globals, "dict")?;
|
self.dictionary_kind(globals)?;
|
||||||
let builtins = match self.dict_get(globals, &Value::Text("__builtins__".into()))? {
|
let builtins = match self.dict_get_name(globals, "__builtins__")? {
|
||||||
Some(Value::Object(reference)) => {
|
Some(Value::Object(reference)) => {
|
||||||
self.payload::<EndorDictObject>(reference, "dict")?;
|
self.dictionary_kind(reference)?;
|
||||||
Some(reference)
|
Some(reference)
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
@@ -101,6 +101,35 @@ impl ObjectStore {
|
|||||||
.entries())
|
.entries())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn dictionary_kind(
|
||||||
|
&self,
|
||||||
|
dictionary: ObjectRef,
|
||||||
|
) -> Result<&EndorDictObject, VmError> {
|
||||||
|
let dict = self
|
||||||
|
.heap
|
||||||
|
.get(dictionary)?
|
||||||
|
.payload()
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<EndorDictObject>()
|
||||||
|
.ok_or(VmError::ObjectKindMismatch("dict"))?;
|
||||||
|
self.heap.record_dictionary_kind(dictionary);
|
||||||
|
Ok(dict)
|
||||||
|
}
|
||||||
|
/// Name lookup records both present and missing text keys. General dict_get
|
||||||
|
/// and iteration continue to observe the entire dictionary contents.
|
||||||
|
pub fn dict_get_name(
|
||||||
|
&self,
|
||||||
|
dictionary: ObjectRef,
|
||||||
|
name: &str,
|
||||||
|
) -> Result<Option<Value>, VmError> {
|
||||||
|
let dict = self.dictionary_kind(dictionary)?;
|
||||||
|
let value = self
|
||||||
|
.dict_position(dict, &Value::Text(name.into()))?
|
||||||
|
.map(|index| dict.entries[index].1.clone());
|
||||||
|
self.heap.record_name(dictionary, name, &value);
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn dict_get(&self, dictionary: ObjectRef, key: &Value) -> Result<Option<Value>, VmError> {
|
pub fn dict_get(&self, dictionary: ObjectRef, key: &Value) -> Result<Option<Value>, VmError> {
|
||||||
let dict = self.payload::<EndorDictObject>(dictionary, "dict")?;
|
let dict = self.payload::<EndorDictObject>(dictionary, "dict")?;
|
||||||
self.key_hash(key)?;
|
self.key_hash(key)?;
|
||||||
|
|||||||
+280
-12
@@ -1,4 +1,8 @@
|
|||||||
//! Nominal type identities in the same managed heap as their instances.
|
//! Nominal type identities in the same managed heap as their instances.
|
||||||
|
#[path = "contracts.rs"]
|
||||||
|
mod contracts;
|
||||||
|
#[path = "effects.rs"]
|
||||||
|
mod effects;
|
||||||
use std::{collections::BTreeMap, sync::Arc};
|
use std::{collections::BTreeMap, sync::Arc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -6,6 +10,30 @@ use crate::{
|
|||||||
VmError,
|
VmError,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// A host type_target may read its receiver's raw attributes. Validate the
|
||||||
|
// outer lookup after resolution without recursively re-entering its guard.
|
||||||
|
struct AttributeCheckGuard<'a> {
|
||||||
|
store: &'a ObjectStore,
|
||||||
|
thread: std::thread::ThreadId,
|
||||||
|
object: ObjectRef,
|
||||||
|
}
|
||||||
|
impl Drop for AttributeCheckGuard<'_> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let mut checks = self
|
||||||
|
.store
|
||||||
|
.active_attribute_checks
|
||||||
|
.lock()
|
||||||
|
.expect("attribute checker poisoned");
|
||||||
|
let active = checks
|
||||||
|
.get_mut(&self.thread)
|
||||||
|
.expect("active attribute check");
|
||||||
|
active.remove(&self.object);
|
||||||
|
if active.is_empty() {
|
||||||
|
checks.remove(&self.thread);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum TypeConstructor {
|
pub enum TypeConstructor {
|
||||||
/// Allocate a plain, attribute-bearing instance of this type.
|
/// Allocate a plain, attribute-bearing instance of this type.
|
||||||
@@ -225,6 +253,9 @@ impl ObjectStore {
|
|||||||
type_object: ObjectRef,
|
type_object: ObjectRef,
|
||||||
) -> Result<(), VmError> {
|
) -> Result<(), VmError> {
|
||||||
self.payload::<EndorTypeObject>(type_object, "Type")?;
|
self.payload::<EndorTypeObject>(type_object, "Type")?;
|
||||||
|
for capability in self.inherited_traits(type_object)? {
|
||||||
|
self.check_capability_storage(object, capability)?;
|
||||||
|
}
|
||||||
self.heap.attach_type(object, type_object)
|
self.heap.attach_type(object, type_object)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,6 +317,7 @@ impl ObjectStore {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
let mut methods = definition.methods.clone();
|
let mut methods = definition.methods.clone();
|
||||||
|
self.validate_override_update(type_object, &name, function)?;
|
||||||
self.validate_method_update(type_object, &name, function)?;
|
self.validate_method_update(type_object, &name, function)?;
|
||||||
methods.insert(name, function);
|
methods.insert(name, function);
|
||||||
let updated = EndorTypeObject {
|
let updated = EndorTypeObject {
|
||||||
@@ -374,6 +406,7 @@ impl ObjectStore {
|
|||||||
})?;
|
})?;
|
||||||
if let Err(error) = self
|
if let Err(error) = self
|
||||||
.validate_inherited_traits(class)
|
.validate_inherited_traits(class)
|
||||||
|
.and_then(|()| self.validate_class_overrides(class))
|
||||||
.and_then(|()| self.attach_type(class, metatype))
|
.and_then(|()| self.attach_type(class, metatype))
|
||||||
{
|
{
|
||||||
self.release(class)?;
|
self.release(class)?;
|
||||||
@@ -454,6 +487,14 @@ impl ObjectStore {
|
|||||||
Ok(signature)
|
Ok(signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Known whole-call effect, after unwrapping bound receivers. Delegated
|
||||||
|
/// native wrappers and source call graphs with unproved operations remain unknown.
|
||||||
|
/// This query follows current globals, closure Cells and known receivers without
|
||||||
|
/// executing code, freezing targets or retaining a cross-query cache.
|
||||||
|
pub fn call_effect(&self, function: ObjectRef) -> Result<Option<crate::CallEffect>, VmError> {
|
||||||
|
self.infer_call_effect(function)
|
||||||
|
}
|
||||||
|
|
||||||
/// Unified source/native positional interval, after any bound receivers.
|
/// Unified source/native positional interval, after any bound receivers.
|
||||||
/// None means no published native metadata (including generic Type construction).
|
/// None means no published native metadata (including generic Type construction).
|
||||||
pub fn call_arity(&self, function: ObjectRef) -> Result<Option<crate::CallArity>, VmError> {
|
pub fn call_arity(&self, function: ObjectRef) -> Result<Option<crate::CallArity>, VmError> {
|
||||||
@@ -537,6 +578,7 @@ impl ObjectStore {
|
|||||||
if name == "name"
|
if name == "name"
|
||||||
&& let Some(capability) = object.payload().as_any().downcast_ref::<EndorTraitObject>()
|
&& let Some(capability) = object.payload().as_any().downcast_ref::<EndorTraitObject>()
|
||||||
{
|
{
|
||||||
|
self.check_typed_method_write(receiver, name)?;
|
||||||
return Ok(AttributeLookup::Value(Value::Text(capability.name.clone())));
|
return Ok(AttributeLookup::Value(Value::Text(capability.name.clone())));
|
||||||
}
|
}
|
||||||
if let Some(definition) = object.payload().as_any().downcast_ref::<EndorTypeObject>() {
|
if let Some(definition) = object.payload().as_any().downcast_ref::<EndorTypeObject>() {
|
||||||
@@ -546,9 +588,11 @@ impl ObjectStore {
|
|||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
if let Some(value) = metadata {
|
if let Some(value) = metadata {
|
||||||
|
self.check_typed_method_write(receiver, name)?;
|
||||||
return Ok(AttributeLookup::Value(value));
|
return Ok(AttributeLookup::Value(value));
|
||||||
}
|
}
|
||||||
if let Some(function) = self.lookup_method(receiver, name)? {
|
if let Some(function) = self.lookup_method(receiver, name)? {
|
||||||
|
self.check_typed_method_write(receiver, name)?;
|
||||||
return Ok(AttributeLookup::Value(Value::Object(function)));
|
return Ok(AttributeLookup::Value(Value::Object(function)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -557,7 +601,10 @@ impl ObjectStore {
|
|||||||
.last()
|
.last()
|
||||||
.expect("nonempty attribute path");
|
.expect("nonempty attribute path");
|
||||||
match self.heap.get_attr(storage, name) {
|
match self.heap.get_attr(storage, name) {
|
||||||
Ok(value) => return Ok(AttributeLookup::Value(value)),
|
Ok(value) => {
|
||||||
|
self.check_typed_method_write(receiver, name)?;
|
||||||
|
return Ok(AttributeLookup::Value(value));
|
||||||
|
}
|
||||||
Err(VmError::AttributeNotFound(_) | VmError::UnsupportedOperation("attributes")) => {}
|
Err(VmError::AttributeNotFound(_) | VmError::UnsupportedOperation("attributes")) => {}
|
||||||
Err(error) => return Err(error),
|
Err(error) => return Err(error),
|
||||||
}
|
}
|
||||||
@@ -630,16 +677,23 @@ impl NativeCallable for CallBoundMethod {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Minimal explicit capability contract. Arity excludes the bound receiver.
|
/// Minimal explicit capability contract. Arity excludes the bound receiver.
|
||||||
/// Parameter/return types, default implementations and syntax are host concerns.
|
/// Use TypedMethodRequirement for parameter/return/effect contracts.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct MethodRequirement {
|
pub struct MethodRequirement {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub parameters: usize,
|
pub parameters: usize,
|
||||||
}
|
}
|
||||||
|
/// Typed capability requirement, including the explicit receiver slot.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct TypedMethodRequirement {
|
||||||
|
pub name: String,
|
||||||
|
pub signature: crate::CallSignature,
|
||||||
|
}
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct EndorTraitObject {
|
pub struct EndorTraitObject {
|
||||||
name: String,
|
name: String,
|
||||||
requirements: Vec<MethodRequirement>,
|
requirements: Vec<MethodRequirement>,
|
||||||
|
signatures: BTreeMap<String, crate::CallSignature>,
|
||||||
}
|
}
|
||||||
impl EndorTraitObject {
|
impl EndorTraitObject {
|
||||||
pub fn name(&self) -> &str {
|
pub fn name(&self) -> &str {
|
||||||
@@ -648,8 +702,18 @@ impl EndorTraitObject {
|
|||||||
pub fn requirements(&self) -> &[MethodRequirement] {
|
pub fn requirements(&self) -> &[MethodRequirement] {
|
||||||
&self.requirements
|
&self.requirements
|
||||||
}
|
}
|
||||||
|
pub fn signature(&self, name: &str) -> Option<&crate::CallSignature> {
|
||||||
|
self.signatures.get(name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl ObjectPayload for EndorTraitObject {
|
impl ObjectPayload for EndorTraitObject {
|
||||||
|
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||||
|
for signature in self.signatures.values() {
|
||||||
|
for reference in signature.references() {
|
||||||
|
visit(reference);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
fn reference_assignment_only(&self) -> bool {
|
fn reference_assignment_only(&self) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@@ -686,7 +750,49 @@ impl ObjectStore {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.allocate(EndorTraitObject { name, requirements })
|
self.allocate(EndorTraitObject {
|
||||||
|
name,
|
||||||
|
requirements,
|
||||||
|
signatures: BTreeMap::new(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_typed_trait(
|
||||||
|
&mut self,
|
||||||
|
name: impl Into<String>,
|
||||||
|
methods: Vec<TypedMethodRequirement>,
|
||||||
|
) -> Result<ObjectRef, VmError> {
|
||||||
|
let name = name.into();
|
||||||
|
if name.is_empty() {
|
||||||
|
return Err(VmError::InvalidArgument("trait name must not be empty"));
|
||||||
|
}
|
||||||
|
let mut signatures = BTreeMap::new();
|
||||||
|
let mut requirements = Vec::new();
|
||||||
|
for method in methods {
|
||||||
|
method.signature.validate(self)?;
|
||||||
|
let arity = method.signature.arity;
|
||||||
|
if method.name.is_empty()
|
||||||
|
|| method.name == "type"
|
||||||
|
|| arity.required == 0
|
||||||
|
|| arity.required != arity.maximum
|
||||||
|
|| arity.maximum == usize::MAX
|
||||||
|
|| signatures.contains_key(&method.name)
|
||||||
|
{
|
||||||
|
return Err(VmError::InvalidArgument(
|
||||||
|
"invalid typed trait canonical signature",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
requirements.push(MethodRequirement {
|
||||||
|
name: method.name.clone(),
|
||||||
|
parameters: arity.maximum - 1,
|
||||||
|
});
|
||||||
|
signatures.insert(method.name, method.signature);
|
||||||
|
}
|
||||||
|
self.allocate(EndorTraitObject {
|
||||||
|
name,
|
||||||
|
requirements,
|
||||||
|
signatures,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inherited_traits(&self, definition: ObjectRef) -> Result<Vec<ObjectRef>, VmError> {
|
fn inherited_traits(&self, definition: ObjectRef) -> Result<Vec<ObjectRef>, VmError> {
|
||||||
@@ -704,6 +810,8 @@ impl ObjectStore {
|
|||||||
capability: ObjectRef,
|
capability: ObjectRef,
|
||||||
requirement: &MethodRequirement,
|
requirement: &MethodRequirement,
|
||||||
function: Option<ObjectRef>,
|
function: Option<ObjectRef>,
|
||||||
|
definition: ObjectRef,
|
||||||
|
defer_unknown: bool,
|
||||||
) -> Result<(), VmError> {
|
) -> Result<(), VmError> {
|
||||||
let invalid = |reason| VmError::TraitViolation {
|
let invalid = |reason| VmError::TraitViolation {
|
||||||
capability: self
|
capability: self
|
||||||
@@ -723,22 +831,45 @@ impl ObjectStore {
|
|||||||
"positional parameter count does not match (including self)",
|
"positional parameter count does not match (including self)",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
if let Some(expected) = self
|
||||||
|
.payload::<EndorTraitObject>(capability, "Trait")?
|
||||||
|
.signatures
|
||||||
|
.get(&requirement.name)
|
||||||
|
{
|
||||||
|
self.validate_typed_requirement(
|
||||||
|
definition,
|
||||||
|
capability,
|
||||||
|
&requirement.name,
|
||||||
|
function,
|
||||||
|
expected,
|
||||||
|
defer_unknown,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn validate_trait(&self, definition: ObjectRef, capability: ObjectRef) -> Result<(), VmError> {
|
fn validate_trait(
|
||||||
|
&self,
|
||||||
|
definition: ObjectRef,
|
||||||
|
capability: ObjectRef,
|
||||||
|
defer_unknown: bool,
|
||||||
|
) -> Result<(), VmError> {
|
||||||
|
self.with_trait_check(definition, capability, || {
|
||||||
let contract = self.payload::<EndorTraitObject>(capability, "Trait")?;
|
let contract = self.payload::<EndorTraitObject>(capability, "Trait")?;
|
||||||
for requirement in &contract.requirements {
|
for requirement in &contract.requirements {
|
||||||
self.validate_requirement(
|
self.validate_requirement(
|
||||||
capability,
|
capability,
|
||||||
requirement,
|
requirement,
|
||||||
self.lookup_method(definition, &requirement.name)?,
|
self.lookup_method(definition, &requirement.name)?,
|
||||||
|
definition,
|
||||||
|
defer_unknown,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
fn validate_inherited_traits(&self, definition: ObjectRef) -> Result<(), VmError> {
|
fn validate_inherited_traits(&self, definition: ObjectRef) -> Result<(), VmError> {
|
||||||
for capability in self.inherited_traits(definition)? {
|
for capability in self.inherited_traits(definition)? {
|
||||||
self.validate_trait(definition, capability)?;
|
self.validate_trait(definition, capability, true)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -789,7 +920,13 @@ impl ObjectStore {
|
|||||||
.requirements
|
.requirements
|
||||||
{
|
{
|
||||||
if requirement.name == name {
|
if requirement.name == name {
|
||||||
self.validate_requirement(capability, requirement, Some(function))?;
|
self.validate_requirement(
|
||||||
|
capability,
|
||||||
|
requirement,
|
||||||
|
Some(function),
|
||||||
|
descendant,
|
||||||
|
true,
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -803,7 +940,22 @@ impl ObjectStore {
|
|||||||
) -> Result<(), VmError> {
|
) -> Result<(), VmError> {
|
||||||
self.payload::<EndorTraitObject>(capability, "Trait")?;
|
self.payload::<EndorTraitObject>(capability, "Trait")?;
|
||||||
for descendant in self.descendant_types(definition)? {
|
for descendant in self.descendant_types(definition)? {
|
||||||
self.validate_trait(descendant, capability)?;
|
self.validate_trait(descendant, capability, false)?;
|
||||||
|
}
|
||||||
|
// Existing objects must satisfy the same storage invariant as new writes.
|
||||||
|
// Borrowed heap enumeration adds no owning instance registry.
|
||||||
|
if !self
|
||||||
|
.payload::<EndorTraitObject>(capability, "Trait")?
|
||||||
|
.signatures
|
||||||
|
.is_empty()
|
||||||
|
{
|
||||||
|
for object in self.heap.live_references() {
|
||||||
|
if let Some(actual) = self.type_of(object)?
|
||||||
|
&& self.is_subtype(actual, definition)?
|
||||||
|
{
|
||||||
|
self.check_capability_storage(object, capability)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let mut updated = self.payload::<EndorTypeObject>(definition, "Type")?.clone();
|
let mut updated = self.payload::<EndorTypeObject>(definition, "Type")?.clone();
|
||||||
if !updated.traits.contains(&capability) {
|
if !updated.traits.contains(&capability) {
|
||||||
@@ -820,14 +972,130 @@ impl ObjectStore {
|
|||||||
self.payload::<EndorTraitObject>(capability, "Trait")?;
|
self.payload::<EndorTraitObject>(capability, "Trait")?;
|
||||||
let declared = self.inherited_traits(definition)?.contains(&capability);
|
let declared = self.inherited_traits(definition)?.contains(&capability);
|
||||||
if declared {
|
if declared {
|
||||||
self.validate_trait(definition, capability)?;
|
self.validate_trait(definition, capability, false)?;
|
||||||
}
|
}
|
||||||
Ok(declared)
|
Ok(declared)
|
||||||
}
|
}
|
||||||
|
/// Typed capability methods belong to the method table. Instance fields
|
||||||
|
/// cannot replace them independently of dispatch through that table.
|
||||||
|
pub(crate) fn check_typed_method_write(
|
||||||
|
&self,
|
||||||
|
object: ObjectRef,
|
||||||
|
name: &str,
|
||||||
|
) -> Result<(), VmError> {
|
||||||
|
let thread = std::thread::current().id();
|
||||||
|
if !self
|
||||||
|
.active_attribute_checks
|
||||||
|
.lock()
|
||||||
|
.expect("attribute checker poisoned")
|
||||||
|
.entry(thread)
|
||||||
|
.or_default()
|
||||||
|
.insert(object)
|
||||||
|
{
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let _guard = AttributeCheckGuard {
|
||||||
|
store: self,
|
||||||
|
thread,
|
||||||
|
object,
|
||||||
|
};
|
||||||
|
if let Some(definition) = self.type_of(object)? {
|
||||||
|
for capability in self.inherited_traits(definition)? {
|
||||||
|
let contract = self.payload::<EndorTraitObject>(capability, "Trait")?;
|
||||||
|
if contract.signatures.contains_key(name) {
|
||||||
|
return Err(VmError::TraitViolation {
|
||||||
|
capability: contract.name.clone(),
|
||||||
|
method: name.into(),
|
||||||
|
reason: "instance attribute shadows a typed capability method",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_capability_storage(
|
||||||
|
&self,
|
||||||
|
object: ObjectRef,
|
||||||
|
capability: ObjectRef,
|
||||||
|
) -> Result<(), VmError> {
|
||||||
|
let contract = self.payload::<EndorTraitObject>(capability, "Trait")?;
|
||||||
|
if contract.signatures.is_empty() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let storage = *self
|
||||||
|
.attribute_path(object)?
|
||||||
|
.last()
|
||||||
|
.expect("nonempty attribute path");
|
||||||
|
for name in contract.signatures.keys() {
|
||||||
|
let payload = self.object(object)?.payload();
|
||||||
|
let intrinsic = if payload.as_any().is::<EndorTypeObject>() {
|
||||||
|
name == "name" || name == "base" || self.lookup_method(object, name)?.is_some()
|
||||||
|
} else {
|
||||||
|
payload.as_any().is::<EndorTraitObject>() && name == "name"
|
||||||
|
};
|
||||||
|
if intrinsic {
|
||||||
|
return Err(VmError::TraitViolation {
|
||||||
|
capability: contract.name.clone(),
|
||||||
|
method: name.clone(),
|
||||||
|
reason: "intrinsic attribute shadows a typed capability method",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
match self.heap.get_attr(storage, name) {
|
||||||
|
Ok(_) => {
|
||||||
|
return Err(VmError::TraitViolation {
|
||||||
|
capability: contract.name.clone(),
|
||||||
|
method: name.clone(),
|
||||||
|
reason: "instance attribute shadows a typed capability method",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Err(
|
||||||
|
VmError::AttributeNotFound(_) | VmError::UnsupportedOperation("attributes"),
|
||||||
|
) => {}
|
||||||
|
Err(error) => return Err(error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Method names whose instance storage participates in typed capability
|
||||||
|
/// validity. Hosts can subscribe to these attribute keys when caching.
|
||||||
|
pub fn typed_method_names(&self, object: ObjectRef) -> Result<Vec<String>, VmError> {
|
||||||
|
let mut names = std::collections::BTreeSet::new();
|
||||||
|
if let Some(definition) = self.type_of(object)? {
|
||||||
|
for capability in self.inherited_traits(definition)? {
|
||||||
|
names.extend(
|
||||||
|
self.payload::<EndorTraitObject>(capability, "Trait")?
|
||||||
|
.signatures
|
||||||
|
.keys()
|
||||||
|
.cloned(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(names.into_iter().collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate instance storage before host dispatch that bypasses attribute
|
||||||
|
/// lookup. Raw storage access is trusted but cannot certify conformance.
|
||||||
|
pub fn check_typed_instance_storage(&self, object: ObjectRef) -> Result<(), VmError> {
|
||||||
|
if let Some(definition) = self.type_of(object)? {
|
||||||
|
for capability in self.inherited_traits(definition)? {
|
||||||
|
self.check_capability_storage(object, capability)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn supports(&self, object: ObjectRef, capability: ObjectRef) -> Result<bool, VmError> {
|
pub fn supports(&self, object: ObjectRef, capability: ObjectRef) -> Result<bool, VmError> {
|
||||||
self.payload::<EndorTraitObject>(capability, "Trait")?;
|
self.payload::<EndorTraitObject>(capability, "Trait")?;
|
||||||
match self.type_of(object)? {
|
match self.type_of(object)? {
|
||||||
Some(definition) => self.implements_trait(definition, capability),
|
Some(definition) => {
|
||||||
|
let declared = self.implements_trait(definition, capability)?;
|
||||||
|
if declared {
|
||||||
|
self.check_capability_storage(object, capability)?;
|
||||||
|
}
|
||||||
|
Ok(declared)
|
||||||
|
}
|
||||||
None => Ok(false),
|
None => Ok(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -835,7 +1103,7 @@ impl ObjectStore {
|
|||||||
|
|
||||||
/// Borrowed expected-type descriptor. Frames retain its managed reference
|
/// Borrowed expected-type descriptor. Frames retain its managed reference
|
||||||
/// while validating the return, so rebinding globals cannot release it mid-call.
|
/// while validating the return, so rebinding globals cannot release it mid-call.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum AnnotationType {
|
pub enum AnnotationType {
|
||||||
Any,
|
Any,
|
||||||
Unit,
|
Unit,
|
||||||
@@ -998,9 +1266,9 @@ impl ObjectStore {
|
|||||||
return Ok(primitive);
|
return Ok(primitive);
|
||||||
}
|
}
|
||||||
let value = self
|
let value = self
|
||||||
.dict_get(data.globals(), &Value::Text(name.into()))?
|
.dict_get_name(data.globals(), name)?
|
||||||
.or(match data.builtins() {
|
.or(match data.builtins() {
|
||||||
Some(builtins) => self.dict_get(builtins, &Value::Text(name.into()))?,
|
Some(builtins) => self.dict_get_name(builtins, name)?,
|
||||||
None => None,
|
None => None,
|
||||||
});
|
});
|
||||||
if let Some(Value::Object(definition)) = value {
|
if let Some(Value::Object(definition)) = value {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
| 文件 | 覆盖 |
|
| 文件 | 覆盖 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| [attribute_storage.rs](attribute_storage.rs) | 可转移字段存储、原类型身份、替换释放、属性写删和非法路径拒绝 |
|
| [attribute_storage.rs](attribute_storage.rs) | 可转移字段存储、原类型身份、替换释放、属性写删、非法路径拒绝及类型化能力方法遮蔽的登记/调用/修复 |
|
||||||
| [store.rs](store.rs) | 只读嵌套、容器与类型写入屏障、稳定地址、跨存储句柄拒绝与更新原子性、Dict/Cell 循环回收、函数校验失败无泄漏、共享代码与常量生命周期 |
|
| [store.rs](store.rs) | 只读嵌套、容器与类型写入屏障、稳定地址、跨存储句柄拒绝与更新原子性、Dict/Cell 循环回收、函数校验失败无泄漏、共享代码与常量生命周期 |
|
||||||
|
|
||||||
非法句柄索引、槽位代数与计数溢出仍在 src/heap.rs 的私有单元测试内,不开放句柄构造器。VM 委托 API 与求值行为继续由 [core/tests](../../core/tests/README.md) 检查。随工作区在指定远端运行,本地不测试;现有测试源码不代表已通过,见 [验证](../../../docs/operations/validation.md)。返回 [objects](../README.md)。
|
非法句柄索引、槽位代数与计数溢出仍在 src/heap.rs 的私有单元测试内,不开放句柄构造器。VM 委托 API 与求值行为继续由 [core/tests](../../core/tests/README.md) 检查。随工作区在指定远端运行,本地不测试;现有测试源码不代表已通过,见 [验证](../../../docs/operations/validation.md)。返回 [objects](../README.md)。
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
use objects::{ArithmeticOp, CompareOp, EndorNumber, EndorReal};
|
||||||
|
fn equal(a: &EndorNumber, b: &EndorNumber) {
|
||||||
|
assert!(a.numeric_compare(b, CompareOp::Eq).unwrap());
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn exact_real_arithmetic_keeps_large_values_and_cross_representation_equivalence() {
|
||||||
|
let tenth = EndorNumber::decimal("0.10").unwrap();
|
||||||
|
let fifth = EndorNumber::rational("2", "10").unwrap();
|
||||||
|
equal(
|
||||||
|
&tenth.arithmetic(&fifth, ArithmeticOp::Add).unwrap(),
|
||||||
|
&EndorNumber::decimal("0.3").unwrap(),
|
||||||
|
);
|
||||||
|
let large = EndorNumber::integer("9223372036854775807").unwrap();
|
||||||
|
equal(
|
||||||
|
&large.arithmetic(&1_i64.into(), ArithmeticOp::Add).unwrap(),
|
||||||
|
&EndorNumber::integer("9223372036854775808").unwrap(),
|
||||||
|
);
|
||||||
|
let min = EndorNumber::integer("-9223372036854775808").unwrap();
|
||||||
|
assert_eq!(min.as_i64(), Some(i64::MIN));
|
||||||
|
equal(
|
||||||
|
&min.negated().unwrap(),
|
||||||
|
&EndorNumber::integer("9223372036854775808").unwrap(),
|
||||||
|
);
|
||||||
|
equal(
|
||||||
|
&EndorNumber::rational("2", "6").unwrap(),
|
||||||
|
&EndorNumber::rational("1", "3").unwrap(),
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
EndorNumber::integer("9007199254740993")
|
||||||
|
.unwrap()
|
||||||
|
.numeric_compare(&EndorNumber::binary_float(9007199254740992.), CompareOp::Gt)
|
||||||
|
.unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn ieee_signed_zero_nan_and_operation_limits_are_explicit() {
|
||||||
|
let negative = EndorNumber::binary_float(0.).negated().unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
negative.as_real(),
|
||||||
|
Some(&EndorReal::BinaryFloat((-0.0_f64).to_bits()))
|
||||||
|
);
|
||||||
|
let nan = EndorNumber::binary_float(f64::NAN);
|
||||||
|
assert!(!nan.numeric_compare(&nan, CompareOp::Eq).unwrap());
|
||||||
|
assert!(nan.numeric_compare(&nan, CompareOp::Ne).unwrap());
|
||||||
|
assert!(
|
||||||
|
EndorNumber::from(1_i64)
|
||||||
|
.arithmetic(&0_i64.into(), ArithmeticOp::Divide)
|
||||||
|
.is_err()
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
EndorNumber::binary_float(f64::MAX)
|
||||||
|
.arithmetic(&2_i64.into(), ArithmeticOp::Multiply)
|
||||||
|
.is_err()
|
||||||
|
);
|
||||||
|
let huge = EndorNumber::decimal("1e99999999999999999").unwrap();
|
||||||
|
assert_eq!(huge.to_string(), "1e99999999999999999");
|
||||||
|
assert!(huge.arithmetic(&1_i64.into(), ArithmeticOp::Add).is_err());
|
||||||
|
}
|
||||||
@@ -76,3 +76,174 @@ fn cyclic_and_foreign_attribute_storage_fail_without_mutating_fields() {
|
|||||||
store.release(proxy).unwrap();
|
store.release(proxy).unwrap();
|
||||||
assert_eq!(store.collect_cycles(), 1);
|
assert_eq!(store.collect_cycles(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct ReflectiveType(ObjectRef);
|
||||||
|
impl ObjectPayload for ReflectiveType {
|
||||||
|
fn as_any(&self) -> &dyn std::any::Any {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
fn kind_name(&self) -> &'static str {
|
||||||
|
"reflective-type"
|
||||||
|
}
|
||||||
|
fn allows_attributes(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||||
|
visit(self.0);
|
||||||
|
}
|
||||||
|
fn type_target(&self, store: &ObjectStore) -> Result<Option<ObjectRef>, VmError> {
|
||||||
|
if let Value::Object(receiver) = store.cell_get(self.0)? {
|
||||||
|
store.get_attr(receiver, "selector")?;
|
||||||
|
}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct TypedCheck;
|
||||||
|
impl objects::NativeCallable for TypedCheck {
|
||||||
|
fn signature(&self) -> Option<objects::CallSignature> {
|
||||||
|
Some(objects::CallSignature::exact(
|
||||||
|
vec![objects::AnnotationType::Any],
|
||||||
|
objects::AnnotationType::Number,
|
||||||
|
objects::CallEffect::ReadOnly,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
fn invoke(
|
||||||
|
&self,
|
||||||
|
_: &mut objects::NativeContext<'_>,
|
||||||
|
_: &[Value],
|
||||||
|
) -> Result<objects::NativeResult, VmError> {
|
||||||
|
Ok(objects::NativeResult::Return(Value::Number(1.into())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn typed_method_storage_cannot_diverge_from_class_dispatch() {
|
||||||
|
let mut store = ObjectStore::new().unwrap();
|
||||||
|
let roots = store.bootstrap_types().unwrap();
|
||||||
|
let class = store
|
||||||
|
.new_type("Checked", roots.object, TypeConstructor::Record)
|
||||||
|
.unwrap();
|
||||||
|
let function = store
|
||||||
|
.allocate(objects::ContextNativeFunction::new(
|
||||||
|
"check",
|
||||||
|
TypedCheck,
|
||||||
|
vec![],
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
store.define_method(class, "check", function).unwrap();
|
||||||
|
let capability = store
|
||||||
|
.new_typed_trait(
|
||||||
|
"Checked",
|
||||||
|
vec![objects::TypedMethodRequirement {
|
||||||
|
name: "check".into(),
|
||||||
|
signature: objects::CallSignature::exact(
|
||||||
|
vec![objects::AnnotationType::Any],
|
||||||
|
objects::AnnotationType::Number,
|
||||||
|
objects::CallEffect::ReadOnly,
|
||||||
|
),
|
||||||
|
}],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let storage = store.new_object().unwrap();
|
||||||
|
let proxy = store.allocate(Proxy(storage)).unwrap();
|
||||||
|
store.attach_type(proxy, class).unwrap();
|
||||||
|
store.set_attr(storage, "check", Value::Nil).unwrap();
|
||||||
|
let version = store.change_version(class).unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
store.implement_trait(class, capability),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
assert_eq!(store.change_version(class).unwrap(), version);
|
||||||
|
assert!(!store.implements_trait(class, capability).unwrap());
|
||||||
|
store.del_attr(storage, "check").unwrap();
|
||||||
|
store.implement_trait(class, capability).unwrap();
|
||||||
|
let version = store.change_version(storage).unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
store.set_attr(proxy, "check", Value::Nil),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
assert_eq!(store.change_version(storage).unwrap(), version);
|
||||||
|
assert!(store.supports(proxy, capability).unwrap());
|
||||||
|
// A trusted host can access the untyped backing record directly. Every
|
||||||
|
// language/host dispatch gate must detect the resulting invalid instance.
|
||||||
|
store.set_attr(storage, "check", Value::Nil).unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
store.supports(proxy, capability),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
assert!(matches!(
|
||||||
|
store.get_attr(proxy, "check"),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
assert!(matches!(
|
||||||
|
store.bind_attr(proxy, "check"),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
assert!(matches!(
|
||||||
|
store.check_method_contract_call(function, proxy, false),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
assert!(matches!(
|
||||||
|
store.check_typed_instance_storage(proxy),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
let candidate = store.new_object().unwrap();
|
||||||
|
store.set_attr(candidate, "check", Value::Nil).unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
store.attach_type(candidate, class),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
assert_eq!(store.type_of(candidate).unwrap(), None);
|
||||||
|
store.del_attr(storage, "check").unwrap();
|
||||||
|
assert!(store.supports(proxy, capability).unwrap());
|
||||||
|
assert_eq!(
|
||||||
|
store.get_attr(proxy, "check").unwrap(),
|
||||||
|
Value::Object(function)
|
||||||
|
);
|
||||||
|
store
|
||||||
|
.check_method_contract_call(function, proxy, false)
|
||||||
|
.unwrap();
|
||||||
|
let self_cell = store.new_cell(Some(Value::Nil)).unwrap();
|
||||||
|
let reflective = store.allocate(ReflectiveType(self_cell)).unwrap();
|
||||||
|
store
|
||||||
|
.set_attr(reflective, "selector", Value::Bool(true))
|
||||||
|
.unwrap();
|
||||||
|
store
|
||||||
|
.cell_set(self_cell, Value::Object(reflective))
|
||||||
|
.unwrap();
|
||||||
|
store.attach_type(reflective, class).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
store.get_attr(reflective, "selector").unwrap(),
|
||||||
|
Value::Bool(true)
|
||||||
|
);
|
||||||
|
assert!(store.supports(reflective, capability).unwrap());
|
||||||
|
assert!(matches!(
|
||||||
|
store.set_attr(reflective, "check", Value::Nil),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
// A failed guarded check must not suppress the next one.
|
||||||
|
assert!(matches!(
|
||||||
|
store.set_attr(reflective, "check", Value::Nil),
|
||||||
|
Err(VmError::TraitViolation { .. })
|
||||||
|
));
|
||||||
|
store.release(reflective).unwrap();
|
||||||
|
store.release(self_cell).unwrap();
|
||||||
|
|
||||||
|
for object in [
|
||||||
|
candidate,
|
||||||
|
proxy,
|
||||||
|
storage,
|
||||||
|
function,
|
||||||
|
capability,
|
||||||
|
class,
|
||||||
|
roots.object,
|
||||||
|
roots.type_type,
|
||||||
|
] {
|
||||||
|
store.release(object).unwrap();
|
||||||
|
}
|
||||||
|
store.collect_cycles();
|
||||||
|
assert_eq!(store.object_count(), 0);
|
||||||
|
}
|
||||||
|
|||||||
+9
-3
@@ -5,14 +5,14 @@
|
|||||||
| 引擎与宿主边界 | [架构](architecture/README.md) | [crates](../crates/README.md) |
|
| 引擎与宿主边界 | [架构](architecture/README.md) | [crates](../crates/README.md) |
|
||||||
| 解释执行、资源修订与引擎升级 | [执行与版本](architecture/execution-and-versions.md) | frontend/compiler/core |
|
| 解释执行、资源修订与引擎升级 | [执行与版本](architecture/execution-and-versions.md) | frontend/compiler/core |
|
||||||
| EndorObject、句柄、默认操作 | [对象模型](runtime/objects.md) | objects/src |
|
| EndorObject、句柄、默认操作 | [对象模型](runtime/objects.md) | objects/src |
|
||||||
| 原生上下文、只读调用、子调用读取依赖及校验分类、统一参数数量/类型/联合/效果、委托返回与变更通知 | [原生调用](runtime/native-calls.md) | objects/src/native.rs、changes.rs、core/src/eval.rs |
|
| 原生上下文、只读调用、子调用读取依赖及校验分类、统一参数数量/类型/联合/固定与委托效果、委托返回与变更通知 | [原生调用](runtime/native-calls.md) | objects/src/native.rs、changes.rs、core/src/eval.rs |
|
||||||
| 托管类型、宿主类注册、原生分配继承、字段存储、显式能力、函数分类、声明字段与可选源码注解检查 | [名义类型](runtime/types.md) | objects/src/types.rs |
|
| 托管类型、宿主类注册、原生分配继承、字段存储、显式/类型化能力、函数分类/已知效果、保守源码/调用图只读证明、声明字段/源码注解与方法覆写检查 | [名义类型/方法覆写](runtime/types.md) | objects/src/types.rs、contracts.rs |
|
||||||
| Number、String、Dict、Cell、Function | [基本对象](runtime/builtins.md) | objects/src |
|
| Number、String、Dict、Cell、Function | [基本对象](runtime/builtins.md) | objects/src |
|
||||||
| 源码读取与来源 | [Reader](runtime/reader.md) | frontend/src/reader.rs |
|
| 源码读取与来源 | [Reader](runtime/reader.md) | frontend/src/reader.rs |
|
||||||
| 词法、语法、裸赋值和诊断 | [Frontend](runtime/frontend.md) | frontend/src/lexer.peg/grammar.peg |
|
| 词法、语法、裸赋值和诊断 | [Frontend](runtime/frontend.md) | frontend/src/lexer.peg/grammar.peg |
|
||||||
| ASDL、AST 与编辑器接口 | [AST](runtime/ast.md) | frontend/Endor.asdl |
|
| ASDL、AST 与编辑器接口 | [AST](runtime/ast.md) | frontend/Endor.asdl |
|
||||||
| 名称分析、编译和装载 | [Compiler](runtime/compiler.md) | compiler/src |
|
| 名称分析、编译和装载 | [Compiler](runtime/compiler.md) | compiler/src |
|
||||||
| 字节码、函数、共享引用赋值与限额 | [字节码 VM](runtime/bytecode.md) | objects/src、core/src |
|
| 字节码、函数、共享引用赋值、可恢复只读分片、独立托管调用帧与限额 | [字节码 VM](runtime/bytecode.md) | objects/src、core/src |
|
||||||
| 引用与 VM 生命周期 | [生命周期](runtime/lifecycle.md) | objects/src、core/src |
|
| 引用与 VM 生命周期 | [生命周期](runtime/lifecycle.md) | objects/src、core/src |
|
||||||
| UTF-8 与地区 | [文本编码](runtime/text-encoding.md) | frontend、objects |
|
| UTF-8 与地区 | [文本编码](runtime/text-encoding.md) | frontend、objects |
|
||||||
| 嵌入及宿主入口 | [使用引擎](operations/README.md) | 独立宿主仓库 |
|
| 嵌入及宿主入口 | [使用引擎](operations/README.md) | 独立宿主仓库 |
|
||||||
@@ -22,3 +22,9 @@
|
|||||||
| 后续开发交接 | [交接](contributing/handoff.md) | 实时源码和 Git 状态 |
|
| 后续开发交接 | [交接](contributing/handoff.md) | 实时源码和 Git 状态 |
|
||||||
|
|
||||||
目录入口:[architecture](architecture/README.md)、[runtime](runtime/README.md)、[operations](operations/README.md)、[contributing](contributing/README.md)。宿主部署、文件信息和数据库正文已移至 Hestia,命令行与实际输出报告已移至 Lyra;本仓库不保留重复正文。研究资料仍在忽略的 work/research,不提交。
|
目录入口:[architecture](architecture/README.md)、[runtime](runtime/README.md)、[operations](operations/README.md)、[contributing](contributing/README.md)。宿主部署、文件信息和数据库正文已移至 Hestia,命令行与实际输出报告已移至 Lyra;本仓库不保留重复正文。研究资料仍在忽略的 work/research,不提交。
|
||||||
|
|
||||||
|
Await入口:语法见runtime/frontend.md,名称与发码见runtime/compiler.md,等待/恢复与所有权见runtime/bytecode.md,宿主协议见runtime/objects.md。
|
||||||
|
|
||||||
|
按名全局依赖、缺失键遮蔽与兼容保守读取见runtime/native-calls.md的精确只读观察章节。
|
||||||
|
|
||||||
|
同步与可恢复只读调用的精确ReadSet及旧对象列表兼容语义统一见[runtime/native-calls.md](runtime/native-calls.md)。
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ Aster 是可嵌入引擎,只保留 objects、core、frontend、compiler。Obje
|
|||||||
|
|
||||||
## 当前缺口
|
## 当前缺口
|
||||||
|
|
||||||
平台资源版本/依赖锁、源码闭包、数值算术、完整异常传播、内存配额、可靠取消、不可信代码沙箱、自动 GC 和宿主 panic 后执行根恢复尚未完成。源码函数参数/返回注解已支持默认关闭的运行时检查,Tessera 显式启用;普通名称按调用时函数全局解析,声明字段构造/写入检查已接入同一可选开关;完整初始化、静态类型/效果与原生参数/返回类型签名仍未完成;统一位置参数数量已接入原生调用与显式能力检查,见[类型机制](../runtime/types.md#源码函数注解检查)。不引入 Python 元类。UI 接入适配由 Tessera 实现。通用 ObjectPayload::assign_from 允许宿主对象选择共享引用赋值;协议和根计数归 Aster,UI 挂载、布局、绘制仍归 Tessera,见字节码 VM 的共享引用章节。
|
平台资源版本/依赖锁、源码闭包、数值算术、完整异常传播、内存配额、可靠取消、不可信代码沙箱、自动 GC 和宿主 panic 后执行根恢复尚未完成。源码函数参数/返回注解已支持默认关闭的运行时检查,Tessera 显式启用;普通名称按调用时函数全局解析,声明字段构造/写入检查已接入同一可选开关;完整初始化、完整静态类型/传递效果仍未完成;原生参数/返回签名、类型化Trait、覆写方差、统一位置参数数量、实例方法遮蔽检查与保守源码只读证明已接入原生调用和显式能力检查,见[类型机制](../runtime/types.md#源码函数注解检查)。不引入 Python 元类。UI 接入适配由 Tessera 实现。通用 ObjectPayload::assign_from 允许宿主对象选择共享引用赋值;协议和根计数归 Aster,UI 挂载、布局、绘制仍归 Tessera,见字节码 VM 的共享引用章节。
|
||||||
|
|
||||||
## 工作约定
|
## 工作约定
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
| 文档 | 内容 |
|
| 文档 | 内容 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| [types.md](types.md) | 托管 Object/Type、名义继承、声明字段/函数注解检查、构造与类型边生命周期 |
|
| [types.md](types.md) | 托管 Object/Type、名义继承、方法覆写/类型化Trait/已知效果与源码/调用图只读证明/动态检查、声明字段/函数注解检查、构造与类型边生命周期 |
|
||||||
| [objects.md](objects.md) | 对象、句柄与默认操作 |
|
| [objects.md](objects.md) | 对象、句柄与默认操作 |
|
||||||
| [native-calls.md](native-calls.md) | 原生上下文、读取分类、统一参数数量/类型/联合/效果、非递归委托返回、弱引用、托管赋值与变更批次 |
|
| [native-calls.md](native-calls.md) | 原生上下文、读取分类、统一参数数量/类型/联合/固定与委托效果、非递归委托返回、弱引用、托管赋值与变更批次 |
|
||||||
| [builtins.md](builtins.md) | Number、String、Dict、Cell、Function、异常数据 |
|
| [builtins.md](builtins.md) | Number、String、Dict、Cell、Function、异常数据 |
|
||||||
| [reader.md](reader.md) | 输入、来源和修订标签 |
|
| [reader.md](reader.md) | 输入、来源和修订标签 |
|
||||||
| [frontend.md](frontend.md) | 词法、语法和诊断 |
|
| [frontend.md](frontend.md) | 词法、语法和诊断 |
|
||||||
|
|||||||
@@ -75,7 +75,9 @@ fn main() -> Result<(), endor_core::VmError> {
|
|||||||
|
|
||||||
`EndorInteger`、`EndorDecimal`、`EndorRational` 是内部表示结构,不是三种语言数字类。构造器验证输入;分数不能有零分母;十进制指数不转换为机器整数,所以不会因指数很长而溢出。构造任意大整数或精确小数时传文本;已经被宿主转换为 f64 而丢失的精度无法恢复。整数的 `as_i64` 是显式、可失败的窄化读取,不会静默截断。
|
`EndorInteger`、`EndorDecimal`、`EndorRational` 是内部表示结构,不是三种语言数字类。构造器验证输入;分数不能有零分母;十进制指数不转换为机器整数,所以不会因指数很长而溢出。构造任意大整数或精确小数时传文本;已经被宿主转换为 f64 而丢失的精度无法恢复。整数的 `as_i64` 是显式、可失败的窄化读取,不会静默截断。
|
||||||
|
|
||||||
首版不实现四则运算、排序、跨表示数值相等和数值哈希。Rust 的 PartialEq / Eq 只检查表示是否相同,不能用于解释语言的 `==`:`1.0` 与 `1.00` 保留不同小数位数;`2/6` 与 `1/3` 尚未求数值等价;NaN 位模式也被保留。VM 对两个数字对象的数值比较与哈希显式报不支持。因此数字现在可以作为字典的值,暂不能作为字典键,避免先定义错误的键相等规则。没有用“全部转换 f64”的方式补齐这些操作。
|
实数四则运算和六种比较已通过统一 `EndorNumber` 实现,立即值和装箱 Number 共用语义。整数、十进制与有理数走精确运算,结果为整数或约分有理数;不将它们统一转换为 f64。任一操作数为 BinaryFloat 时显式选择二进制浮点运算;混合比较则把有限浮点数看作精确二进制有理数,避免大整数在比较时丢精度。NaN 比较遵守无序语义;一元负号保留浮点负零。复数算术/比较与数值哈希仍不支持。Rust 的 PartialEq/Eq 继续比较表示,不替代语言数值相等;字典键语义未改变,数字仍不能当键。
|
||||||
|
|
||||||
|
运算有单次资源预算:精确输入最多 4096 字符、十进制指数绝对值最多 4096、结果分子与分母各不超过 16384 位。超限报错但不改变原数的无损表示。除零和非有限浮点算术结果报错。`to_f64()` 是供图形/FFI 边界显式窄化的接口,不更改原数。
|
||||||
|
|
||||||
这里的“各类数字”指上述五种可表示形式,不包括符号表达式、无限展开的精确 π、带单位数量、任意位宽二进制浮点等全部数学对象;这些可在未来扩展表示协议。
|
这里的“各类数字”指上述五种可表示形式,不包括符号表达式、无限展开的精确 π、带单位数量、任意位宽二进制浮点等全部数学对象;这些可在未来扩展表示协议。
|
||||||
|
|
||||||
|
|||||||
@@ -158,9 +158,13 @@ fn main() -> Result<(), endor_core::VmError> {
|
|||||||
|
|
||||||
`ExecutionLimits` 默认限制 1000 个活动帧,指令配额为 `usize::MAX`。`execute_with_limits`/`call_with_limits` 可显式指定限额,嵌套语言调用和原生分派共享计步器;每次原生分派额外计一步,扩展参数不单独计步。原生 Invoke 委托通过同一帧循环推进,不重置预算;详见 [原生调用](native-calls.md)。指令预算不会抢占正在执行的宿主回调。
|
`ExecutionLimits` 默认限制 1000 个活动帧,指令配额为 `usize::MAX`。`execute_with_limits`/`call_with_limits` 可显式指定限额,嵌套语言调用和原生分派共享计步器;每次原生分派额外计一步,扩展参数不单独计步。原生 Invoke 委托通过同一帧循环推进,不重置预算;详见 [原生调用](native-calls.md)。指令预算不会抢占正在执行的宿主回调。
|
||||||
|
|
||||||
|
`start_read_only_call(function, arguments, limits, slice)` 和 `resume_read_only_call(slice)` 提供可恢复的只读字节码调用。`ExecutionProgress::Pending` 保留同一 VM 中的活动帧、局部槽、栈、原生委托返回契约、读取追踪及所有执行根;继续时不重放已执行指令。`Complete` 返回拥有式 value、借用式 reads 和累计 instructions。slice 必须大于0,暂停只能发生在字节码指令边界;原生回调和原生委托链原子完成,软分片不保证墙钟延迟,整个调用仍共用原始硬指令/帧限额。
|
||||||
|
|
||||||
|
暂停期间只读保护一直有效;已有对象不可写,也不能用普通 call 或第二个 start 在同一 VM 中交错执行。宿主不得更改只读作用域栈或手动释放并非自己拥有的引用。`cancel_read_only_call`、完成或执行错误会清理帧根并退出只读作用域,错误保留 traceback。取消只终止只读执行,不是通用事务回滚。跨线程任务调度、GUI 帧发布及响应新输入由宿主负责;本接口不实现调度器。参见 execution.rs 的分片/取消/累计限额/GC 根回归(未本地执行)。
|
||||||
|
|
||||||
运行错误仍返回 `VmError`。`last_traceback()` 另提供从外到内的函数名、文件、代码首行和出错指令偏移;它保存独立元数据,不保活已经清理的帧。首行是代码声明的 first_line,不是由行号表推导出的实际错误源码行。成功的新一次调用会清空旧记录。尚无语言异常对象传播、raise/try/except 或完整源码行号表。
|
运行错误仍返回 `VmError`。`last_traceback()` 另提供从外到内的函数名、文件、代码首行和出错指令偏移;它保存独立元数据,不保活已经清理的帧。首行是代码声明的 first_line,不是由行号表推导出的实际错误源码行。成功的新一次调用会清空旧记录。尚无语言异常对象传播、raise/try/except 或完整源码行号表。
|
||||||
|
|
||||||
暂未实现:源码闭包编译、关键字/可变参数、生成器/协程、迭代器专用指令、数值算术、自动方法绑定、指令特化和 JIT。
|
暂未实现:源码闭包编译、关键字/可变参数、生成器/协程、迭代器专用指令、复数算术、自动方法绑定、指令特化和 JIT。
|
||||||
|
|
||||||
## 验证
|
## 验证
|
||||||
|
|
||||||
@@ -182,3 +186,28 @@ fn main() -> Result<(), endor_core::VmError> {
|
|||||||
BuildClass(编号 29,无参数)消费四个栈值 `[name, baseType, fieldsDict, methodsDict]`,产生一个受管类对象;验证器按 4 入 1 出检查栈。字段字典按声明顺序保存字段名与注解名,方法字典保存 MakeFunction 创建的函数。VM 先验证基类、字段与方法关系,再创建完整类型;输入根在成功前保持在执行栈上,失败由帧统一清理。
|
BuildClass(编号 29,无参数)消费四个栈值 `[name, baseType, fieldsDict, methodsDict]`,产生一个受管类对象;验证器按 4 入 1 出检查栈。字段字典按声明顺序保存字段名与注解名,方法字典保存 MakeFunction 创建的函数。VM 先验证基类、字段与方法关系,再创建完整类型;输入根在成功前保持在执行栈上,失败由帧统一清理。
|
||||||
|
|
||||||
LoadAttr 对实例自有属性返回原值;缺失时沿类型父链找方法并创建绑定方法。绑定方法持有 receiver 和选定的函数,调用时经已有非递归委托路径补入 self。类对象上的方法读取提供未绑定函数;type/name/base 元数据优先。宿主读取继承方法应使用返回拥有式结果的 bind_attr;get_attr 保持借用式读取,类型方法结果为未绑定函数。所有委托消耗同一指令预算。
|
LoadAttr 对实例自有属性返回原值;缺失时沿类型父链找方法并创建绑定方法。绑定方法持有 receiver 和选定的函数,调用时经已有非递归委托路径补入 self。类对象上的方法读取提供未绑定函数;type/name/base 元数据优先。宿主读取继承方法应使用返回拥有式结果的 bind_attr;get_attr 保持借用式读取,类型方法结果为未绑定函数。所有委托消耗同一指令预算。
|
||||||
|
|
||||||
|
|
||||||
|
## 独立托管调用帧
|
||||||
|
|
||||||
|
`Vm::new_coroutine(function, arguments, limits)` 返回拥有一份外部根的通用 coroutine 对象;`resume_coroutine(handle, slice)` 返回 `CoroutineProgress::Pending` 或带拥有式结果的 `Complete`。源码指令首次在 resume 执行,原生函数/委托可能在 new 的准备期间执行,仍受硬指令限额约束。该接口目前只提供分片执行基础,不等于 Endor async/await 语法或等待协议。
|
||||||
|
|
||||||
|
与只读续算不同,每个 coroutine 把自己的帧保存在同一 VM 的托管对象载荷中,多个调用可独立暂停;Pending 之间可执行普通调用和堆写入,没有跨量子的只读作用域。resume 不重放先前指令,局部槽、栈、返回类型及原生委托契约保持,所有量子共用原始硬指令/帧预算。原生调用仍是原子的,slice 不保证墙钟延迟。读写共享堆,无快照、事务或自动回滚。不能在只读执行/只读暂停期间创建、恢复或取消 coroutine。
|
||||||
|
|
||||||
|
暂停时,帧内原本的执行根转为载荷 trace 的托管边(包括重复边);恢复前先保活为执行根,再清空暂停载荷。函数边继续拥有 code/globals/builtins。运行中的 handle 临时保活,不能恢复或取消 Running/已结束帧;完成和错误均消耗帧,错误保留 traceback。`cancel_coroutine(handle)` 对尚未结束帧返回 true 并释放托管边,已结束返回 false;已发生的写入不回滚。调用方最终 release handle 和完成结果。丢弃未完成且不可达的 coroutine 不要求先 cancel,局部/全局指回 coroutine 的环可由正常循环回收处理,不把暂停帧永久登记为 VM 根。调度、任务归属、等待条件及取消策略由宿主实现,网络/GUI不进入内核。
|
||||||
|
|
||||||
|
|
||||||
|
## Await 等待边界
|
||||||
|
|
||||||
|
`Await`(opcode 30,无参数)把栈顶 awaitable 换成结果,验证栈效果为1入1出。`ObjectPayload::poll_await()` 是非阻塞、无副作用的宿主协议,默认不支持;返回 Pending 或 Ready(借用 Value),也可返回 AwaitFailed(String)/AwaitCancelled。引擎验证 Ready 句柄并先retain结果,再释放原操作数;不可信宿主仍需遵守借用与非阻塞契约。Await 不被保守效果分析证明为只读,运行时也拒绝只读阶段。
|
||||||
|
|
||||||
|
在独立coroutine中,Pending保持已求值的栈顶、当前帧和返回契约,返回CoroutineProgress::Waiting { awaitable, instructions };awaitable是由暂停帧保活的借用身份。宿主等待条件满足后再次resume,仅重查同一个Await,不重放操作数表达式、前缀写入或IO请求。每次实际执行Await计入同一硬预算;宿主不应对未就绪对象反复resume来轮询。完成结果与错误沿普通执行/清理路径传播。非coroutine调用允许立即Ready的await,Pending则明确报错,不阻塞线程。调度、取消传播和循环等待检测属于宿主;本引擎不依赖Tessera Task。
|
||||||
|
|
||||||
|
LoadGlobal按文本名字分别观察函数globals及缺失时的captured builtins,不因无关全局写入而失效。可恢复只读Complete新增dependencies,reads保留兼容保守列表;精确验证/所有权见native-calls.md。
|
||||||
|
|
||||||
|
|
||||||
|
## 实数运算指令
|
||||||
|
|
||||||
|
`BinaryArithmetic`(31,operand 0/1/2/3)消费两个 Number 并产生一个 Number,分别对应加减乘除;operand 0 也接受两个直接文本值进行有界连接(最多 1 MiB)。`UnaryNegate`(32,无 operand)保持 1 入 1 出。验证器检查算术 operand 和栈深度;两者是无外部写入的运算,已纳入保守只读证明。失败沿同一个 VM 栈清理及 traceback 路径传播。
|
||||||
|
|
||||||
|
源码 if/else 和 while 使用既有条件跳转、正向跳转和回跳,不引入宿主循环。Compiler 先记录逻辑指令标签,再迭代求解 EXTENDED_ARG 展开后的 code-unit 距离,避免大分支错跳到参数前缀。每次真实循环迭代仍消耗同一累计预算并支持已有协作暂停。
|
||||||
|
|||||||
@@ -26,7 +26,9 @@ CompileOptions.globals 显式声明宿主会提供的 globals/builtins 名称
|
|||||||
| 源结构 | 生成方式 |
|
| 源结构 | 生成方式 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| 字符串、整数、十进制常量 | 放入常量表并 LoadConst;数字经现有 EndorNumber 解析,不转换为 f64 |
|
| 字符串、整数、十进制常量 | 放入常量表并 LoadConst;数字经现有 EndorNumber 解析,不转换为 f64 |
|
||||||
| 数字字面量前的一个或多个负号 | 折叠符号为精确常量;不是任意表达式的一元算术 |
|
| 一元负号 | 字面量折叠保留拼写;其他表达式发出 UnaryNegate |
|
||||||
|
| 四则运算、比较 | BinaryArithmetic / CompareOp;实数精确语义见 builtins |
|
||||||
|
| if / else、while | 函数局部初始化按分支合流,循环可执行零次;相对跳转经过 EXTENDED_ARG 布局解析 |
|
||||||
| 局部读取/let/赋值 | LoadFast/StoreFast;参数和局部名称在 CodeLayout 中 |
|
| 局部读取/let/赋值 | LoadFast/StoreFast;参数和局部名称在 CodeLayout 中 |
|
||||||
| 全局读取/赋值 | LoadGlobal/StoreGlobal,使用该代码对象的 names 表 |
|
| 全局读取/赋值 | LoadGlobal/StoreGlobal,使用该代码对象的 names 表 |
|
||||||
| 函数声明 | 子代码模板 → LoadConst(code) → MakeFunction(0, 0) → StoreGlobal |
|
| 函数声明 | 子代码模板 → LoadConst(code) → MakeFunction(0, 0) → StoreGlobal |
|
||||||
@@ -38,7 +40,7 @@ CompileOptions.globals 显式声明宿主会提供的 globals/builtins 名称
|
|||||||
|
|
||||||
源码 parser 接受绑定/名称及属性赋值/表达式语句与可选末尾带值 return;空 return 仍仅由 AST 调用方构造。模块初始化发布函数/类并返回 nil,不自动调用 main。Lyra 宿主选择入口属于宿主行为。
|
源码 parser 接受绑定/名称及属性赋值/表达式语句与可选末尾带值 return;空 return 仍仅由 AST 调用方构造。模块初始化发布函数/类并返回 nil,不自动调用 main。Lyra 宿主选择入口属于宿主行为。
|
||||||
|
|
||||||
二元算术、非字面量的一元负号和符号值 .name 目前产生明确 Compilation 诊断,不伪造运行结果。导入、Agent、条件/循环、关键字参数等仍未实现。函数参数个数和对象是否可调用由 VM 在运行时检查。
|
符号值 .name 仍产生明确 Compilation 诊断。四则运算、一元负号、六种比较、if/else 与 while 已接入真实字节码。导入、Agent、关键字参数、布尔运算符及 break/continue 等仍未实现。函数参数个数和对象是否可调用由 VM 在运行时检查。
|
||||||
|
|
||||||
## 装载、执行与所有权
|
## 装载、执行与所有权
|
||||||
|
|
||||||
@@ -58,3 +60,8 @@ lyra run [--entry name] <file|-> 编译模块、执行初始化、调用无参
|
|||||||
|
|
||||||
|
|
||||||
类方法使用显式首参数 self;编译器检查本类重复字段/方法和缺少 self。方法代码仍是共享的普通代码对象,参数/返回注解和声明类名随代码保存,由宿主开关选择运行时检查。BuildClass 在 VM 中检查继承字段冲突和原生分配边界;构造失败不发布类型到目标全局名。默认基类 Object 须由宿主通过 CompileOptions 声明并在运行全局中提供,编译器不自行引导类型。普通类构造和继承分派见[类型机制](types.md)。
|
类方法使用显式首参数 self;编译器检查本类重复字段/方法和缺少 self。方法代码仍是共享的普通代码对象,参数/返回注解和声明类名随代码保存,由宿主开关选择运行时检查。BuildClass 在 VM 中检查继承字段冲突和原生分配边界;构造失败不发布类型到目标全局名。默认基类 Object 须由宿主通过 CompileOptions 声明并在运行全局中提供,编译器不自行引导类型。普通类构造和继承分派见[类型机制](types.md)。
|
||||||
|
|
||||||
|
|
||||||
|
Await表达式参与普通名称/初始化分析,先编译operand,再发出无参数Await opcode。该链路保存实际结果的栈位置,不通过回放函数实现等待;没有新增async声明或静态awaitable类型检查。语法与运行上下文见frontend及bytecode主题。
|
||||||
|
|
||||||
|
控制流中的局部变量仍为函数作用域。if 两边都能到达后续语句时,仅保留两边都已初始化的名字;一边 return 时采用仍可到达路径的初始化集合。while 中新初始化的名字不能被当作循环后一定存在。循环共享正常调用、只读调用或 coroutine 的累计指令预算,不建立第二套执行器。
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ parameters 和调用参数列表使用逗号分隔,不接受尾随逗号。函
|
|||||||
|
|
||||||
符号字面量、名称与成员分别保留为独立 AST 结构。true、false、Nil 等词目前只是普通名称,没有额外的源码字面量或内建值绑定。
|
符号字面量、名称与成员分别保留为独立 AST 结构。true、false、Nil 等词目前只是普通名称,没有额外的源码字面量或内建值绑定。
|
||||||
|
|
||||||
use、namespace、trait、agent、完整类型语法、条件/循环、比较/布尔运算和闭包声明等尚未加入首版。当前语法可修改;扩展时同步 AST、PEG 规则、样例和测试,不将旧草案视作不可修改的 ABI。
|
use、namespace、trait、agent、完整类型语法、布尔运算、break/continue和闭包声明等尚未加入首版。当前语法可修改;扩展时同步 AST、PEG 规则、样例和测试,不将旧草案视作不可修改的 ABI。
|
||||||
|
|
||||||
## 诊断和命令
|
## 诊断和命令
|
||||||
|
|
||||||
@@ -76,9 +76,29 @@ use、namespace、trait、agent、完整类型语法、条件/循环、比较
|
|||||||
|
|
||||||
`lyra parse <file>` 与 `lyra parse -` 成功时输出 Rust Debug 格式的 AST,失败时在 stderr 输出来源/行/列和诊断并返回非零退出码;`-` 表示从 stdin 读到 EOF,不是 REPL。阅读与来源包装规则见 [阅读器](reader.md)。可用 --mode 选择 module/expression/interactive,用 --json 输出带 schema 标识的 AstDocument;普通输出为 AstRoot Debug 文本,稳定交换格式见 [AST 模型](ast.md)。命令不执行源码、不初始化 VM、不连接数据库;具体运行与本地禁测约定见 [运行](../operations/README.md) 和 [验证](../operations/validation.md)。
|
`lyra parse <file>` 与 `lyra parse -` 成功时输出 Rust Debug 格式的 AST,失败时在 stderr 输出来源/行/列和诊断并返回非零退出码;`-` 表示从 stdin 读到 EOF,不是 REPL。阅读与来源包装规则见 [阅读器](reader.md)。可用 --mode 选择 module/expression/interactive,用 --json 输出带 schema 标识的 AstDocument;普通输出为 AstRoot Debug 文本,稳定交换格式见 [AST 模型](ast.md)。命令不执行源码、不初始化 VM、不连接数据库;具体运行与本地禁测约定见 [运行](../operations/README.md) 和 [验证](../operations/validation.md)。
|
||||||
|
|
||||||
当前 Compiler 将 AST 经模块/函数名称分析转换为现有字节码,再交给 VM 解释执行。这是解释器内部的编译/执行准备步骤,不要求使用者先构建程序,见 [执行与版本模型](../architecture/execution-and-versions.md)。MakeFunction 已接入源码函数编译,但解析接受的二元算术和符号值仍没有执行语义;编译器会给出明确诊断,见 [编译链路](compiler.md)。源码闭包尚未实现。解析器也不是不可信代码沙箱,当前没有通用输入大小或解析递归深度限额。
|
当前 Compiler 将 AST 经模块/函数名称分析转换为现有字节码,再交给 VM 解释执行。这是解释器内部的编译/执行准备步骤,不要求使用者先构建程序,见 [执行与版本模型](../architecture/execution-and-versions.md)。MakeFunction 已接入源码函数编译,四则运算、比较和条件/循环已可执行;符号值仍会给出明确编译诊断,见 [编译链路](compiler.md)。源码闭包尚未实现。解析器也不是不可信代码沙箱,当前没有通用输入大小或解析递归深度限额。
|
||||||
|
|
||||||
裸赋值 `name = expression` 创建或更新当前作用域中的名称;它不是表达式,不能链式赋值,属性目标使用独立 MemberAssignment 节点;索引目标尚不支持。`let` 仍表示声明,保留重复声明检查;名称与初始化规则见 [Compiler](compiler.md)。两种源码写法产生独立的 Binding/Assignment 节点,宿主不改写输入。
|
裸赋值 `name = expression` 创建或更新当前作用域中的名称;它不是表达式,不能链式赋值,属性目标使用独立 MemberAssignment 节点;索引目标尚不支持。`let` 仍表示声明,保留重复声明检查;名称与初始化规则见 [Compiler](compiler.md)。两种源码写法产生独立的 Binding/Assignment 节点,宿主不改写输入。
|
||||||
|
|
||||||
|
|
||||||
源码类实现子集采用 `class Name: Base { field: TypeName function method(self: Name): ResultType { ... } }`;基类可省略,执行环境须提供 Object。成员函数沿用既有 function 语法,接收者 self 为显式首参数。普通类字段按基类字段、再本类字段的顺序接收位置构造实参。这里记录实现范围,不确认语言文档中的全部候选构造语法;详细限制见[名义类型](types.md)。
|
源码类实现子集采用 `class Name: Base { field: TypeName function method(self: Name): ResultType { ... } }`;基类可省略,执行环境须提供 Object。成员函数沿用既有 function 语法,接收者 self 为显式首参数。普通类字段按基类字段、再本类字段的顺序接收位置构造实参。这里记录实现范围,不确认语言文档中的全部候选构造语法;详细限制见[名义类型](types.md)。
|
||||||
|
|
||||||
|
|
||||||
|
`await expression` 解析为 ExprKind::Await(operand),优先级与前缀负号同级,成员/调用绑定更紧;例如await task.result()等待该调用的返回对象,(await task).field先等待task。await按表达式前缀位置识别,不引入async函数声明。执行是否允许等待由调用上下文决定,见字节码VM的Await章节。
|
||||||
|
|
||||||
|
|
||||||
|
## 条件与循环
|
||||||
|
|
||||||
|
```endor
|
||||||
|
function sum(limit: Number): Number {
|
||||||
|
i = 0
|
||||||
|
total = 0
|
||||||
|
while i < limit {
|
||||||
|
if i < 10 { total = total + i } else { total = total - i }
|
||||||
|
i = i + 1
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
比较包括 `== != < <= > >=`,优先级低于四则运算;比较链暂按左结合解析,不提供 Python 式链式比较。条件使用运行时真值;字面量 true/false、and/or/not、for、break/continue 和 else-if 简写暂未加入。可使用比较结果、宿主 Bool,或 0/非零数值。if/while 可出现在函数体与单条 Interactive 输入中;Module 仍只收声明,Interactive 仍只接受一个完整语句。else 接一个花括号块,嵌套 if 写在块内。
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ take_changes 按对象与变更键合并同一批写入,返回最终版本。
|
|||||||
|
|
||||||
`ObjectStore::call_arity(function)`/`Vm::call_arity(function)` 统一查询源码与原生函数。源码从真实形参和默认值计算区间;绑定方法沿实际函数扣除每层接收者,最大值不足时报错,最小值饱和到零。查询返回无托管引用的复制数据,不增加 GC 根。旧 positional_parameter_count 保留“源码总形参数量减绑定接收者”的兼容语义,不用于新的原生回调分派。通用 Type 构造器的参数组合由宿主决定,返回 None;NativeTyped 内部委托和宿主实际构造仍各自检查。
|
`ObjectStore::call_arity(function)`/`Vm::call_arity(function)` 统一查询源码与原生函数。源码从真实形参和默认值计算区间;绑定方法沿实际函数扣除每层接收者,最大值不足时报错,最小值饱和到零。查询返回无托管引用的复制数据,不增加 GC 根。旧 positional_parameter_count 保留“源码总形参数量减绑定接收者”的兼容语义,不用于新的原生回调分派。通用 Type 构造器的参数组合由宿主决定,返回 None;NativeTyped 内部委托和宿主实际构造仍各自检查。
|
||||||
|
|
||||||
显式 Trait 声明、源码派生及方法表更新使用统一区间检查“规范参数数量加 self”是否被接受;缺少元数据的原生方法不能再登记为能力实现。可选参数允许实现接受规范调用数量之外的额外形式。更新仍先检查后代再修改,无隐藏类型根。类型注解兼容性、方差、参数名称及效果检查尚未接入此结构。
|
显式 Trait 声明、源码派生及方法表更新使用统一区间检查“规范参数数量加 self”是否被接受;缺少元数据的原生方法不能再登记为能力实现。可选参数允许实现接受规范调用数量之外的额外形式。更新仍先检查后代再修改,无隐藏类型根。旧 new_trait 保留数量规则;new_typed_trait 的参数/返回类型、方差及已知效果检查见类型主题,参数名称不构成调用契约。
|
||||||
|
|
||||||
Tessera 为原生方法、回调和内部视觉 continuation 发布数量元数据,事件监听要求接受零参数或一个 Event;两者都接受时优先传 Event。绑定计算和 Task 回调要求接受零参数,登记前拒绝无元数据/不兼容函数;普通旧原生调用的兼容性不等于允许其参加这些严格回调契约。
|
Tessera 为原生方法、回调和内部视觉 continuation 发布数量元数据,事件监听要求接受零参数或一个 Event;两者都接受时优先传 Event。绑定计算和 Task 回调要求接受零参数,登记前拒绝无元数据/不兼容函数;普通旧原生调用的兼容性不等于允许其参加这些严格回调契约。
|
||||||
|
|
||||||
@@ -100,10 +100,21 @@ NativeCallable::signature 默认 None,明确表示尚未发布类型/效果契
|
|||||||
|
|
||||||
NativeResult::Invoke 仍是尾委托,不递归进入 Rust/VM。VM 为每层已声明的返回契约保留检查项;委托原生立即返回或 Endor 帧返回时,按由内到外顺序验证全部契约。返回定义作为执行根保留,原生载荷替换、释放原委托者或源码全局重绑定不会使检查丢失。准备失败、帧限额、指令限额、错误返回与正常完成都释放这些执行根。委托不会为检查额外创建 VM 帧;仍共享每次原生分派和 Endor 指令预算。NativeContext::execute 单独返回 Invoke 时,调用者须使用具有等效最终返回检查的求值器;通常直接使用 Vm::call。
|
NativeResult::Invoke 仍是尾委托,不递归进入 Rust/VM。VM 为每层已声明的返回契约保留检查项;委托原生立即返回或 Endor 帧返回时,按由内到外顺序验证全部契约。返回定义作为执行根保留,原生载荷替换、释放原委托者或源码全局重绑定不会使检查丢失。准备失败、帧限额、指令限额、错误返回与正常完成都释放这些执行根。委托不会为检查额外创建 VM 帧;仍共享每次原生分派和 Endor 指令预算。NativeContext::execute 单独返回 Invoke 时,调用者须使用具有等效最终返回检查的求值器;通常直接使用 Vm::call。
|
||||||
|
|
||||||
CallEffect::ReadOnly 声明只读现存对象,可分配临时结果;Mutating 与 External 在 ObjectStore 只读作用域内、进入实现前拒绝。普通执行允许这些效果。ReadOnly 声明不绕过已有写屏障,也不证明可信 Rust 不会进行隐藏 I/O;未知旧原生函数仍沿原调用/宿主检查,不能把 None 理解为纯函数。这不是静态效果推断、完整 I/O 沙箱或对阻塞原生代码的抢占。
|
CallEffect::ReadOnly 声明只读现存对象,可分配临时结果;Delegated 声明原生包装自身只读,但后续委托的整体效果动态确定,VM 保持同一只读/预算作用域并检查实际目标;Mutating 与 External 在 ObjectStore 只读作用域内、进入实现前拒绝。普通执行允许这些效果。ReadOnly 声明不绕过已有写屏障,也不证明可信 Rust 不会进行隐藏 I/O;未知旧原生函数仍沿原调用/宿主检查,不能把 None 理解为纯函数。这不是完整 I/O 沙箱或对阻塞原生代码的抢占。统一已知效果查询和保守源码/调用图只读证明见[类型主题](types.md#已知调用效果与保守源码证明)。
|
||||||
|
|
||||||
Tessera 的 measure/arrange/paint/hitTest、Point 几何查询、Window、Block/Container/Page、Mount、Task、Event、Subscription 方法和 ObjectList.at 发布签名。Page.open 的 Window | Unit、Page.window 的 Window | Unit 返回,以及 Block.on 的 Bool | String 可选阶段有准确元数据;Mount 的父/子/同级/Page 弱查询为可空结果。Mount.bounds 现在返回带 Bounds 名义类型的现有几何记录,可直接组合到 Point.inside 或视觉方法。滚动位置/字典等通用记录仍声明 Object,不冒充新语言类型;任务 owner 的具体资源资格仍按原域检查。构造器、其他管理 API 和完整源码覆写方差仍未全部接入。能力注册目前继续检查参数数量,不声称已经比较签名类型或效果。
|
Tessera 的 measure/arrange/paint/hitTest、Point 几何查询、Window、Block/Container/Page、Mount、Task、Event、Subscription 方法和 ObjectList.at 发布签名。Page.open 的 Window | Unit、Page.window 的 Window | Unit 返回,以及 Block.on 的 Bool | String 可选阶段有准确元数据;Mount 的父/子/同级/Page 弱查询为可空结果。Mount.bounds 现在返回带 Bounds 名义类型的现有几何记录,可直接组合到 Point.inside 或视觉方法。滚动位置/字典等通用记录仍声明 Object,不冒充新语言类型;任务 owner 的具体资源资格仍按原域检查。构造器和其他管理 API 的签名覆盖仍未全部接入。已接入的方法覆写方差与动态名称规则见类型主题。旧能力注册保留数量模式;类型化能力还检查类型/已知效果,并在实际返回和尾委托结果处保留能力返回约束。
|
||||||
|
|
||||||
回归源码 native_context.rs 覆盖参数副作用前拒绝、错误结果清理、跨原生/源码委托检查、单帧限制、名义签名边与绑定方法循环回收、只读效果拒绝、传统直接函数及重复参数和元数据错误。只编译,不本地执行。
|
回归源码 native_context.rs 覆盖参数副作用前拒绝、错误结果清理、跨原生/源码委托检查、单帧限制、名义签名边与绑定方法循环回收、只读效果拒绝、传统直接函数及重复参数和元数据错误。只编译,不本地执行。
|
||||||
|
|
||||||
联合/可空回归另覆盖两个标量分支、nil、数值拒绝、Object 分支不掩盖非法名义类型、空联合拒绝,以及移除签名后联合返回定义仍存活。
|
联合/可空回归另覆盖两个标量分支、nil、数值拒绝、Object 分支不掩盖非法名义类型、空联合拒绝,以及移除签名后联合返回定义仍存活。
|
||||||
|
|
||||||
|
|
||||||
|
Delegated 在替换兼容中视为未知整体效果,与源码未知效果一样不冒充静态纯度证明。包装不得在委托前进行业务写入或外部I/O,直接返回路径也须只读;委托的原生 Mutating/External 目标仍在当前只读作用域内、进入目标实现前拒绝。源码委托继续受堆写屏障约束,旧无元数据宿主调用仍须遵守可信边界。native_context.rs 的回归覆盖只读包装进入后拒绝写目标、普通作用域允许同一链,以及替换成纯目标后只读调用成功,仅编译。
|
||||||
|
|
||||||
|
|
||||||
|
ContextNativeFunction::implementation<T: NativeCallable>() 只借用当前具体宿主实现,不执行或复制载荷,也不改变捕获/签名的托管边。NativeCallable 继承Any(原有static要求保持);宿主可据自己的实现类型/操作识别原生快速路径,不能只凭函数句柄或当前可变类型方法表认定原生身份。替换载荷后查询跟随新实现,旧类型查询返回None;宿主缓存仍须订阅函数对象版本变化。此接口不暴露可变借用,不替代原生类型/数量/效果检查。
|
||||||
|
|
||||||
|
|
||||||
|
精确只读观察:end_read_only_precise返回ReadSet,兼容objects列表并提供ReadDependency;end_read_tracking_precise返回区域的ReadAccess与相同观察。Object依赖验证对象版本;DictionaryKind只验证存活/字典载荷;Name验证文本键的存在与值身份/表示。缺失名字也被记录,因此后续全局遮蔽内建名会失效;相同值删除/重建后可恢复有效,这是值观察而非“发生过写入”的通知。dict_get_name用于全局/注解/__builtins__名字查询,普通dict_get、dict_items和其他内容读取仍保守依赖整个对象。read_dependency_valid在只读区域内检查时会重记相同观察,使缓存命中仍向外传播依赖。所有对象句柄(包括Name中的Value::Object)均借用,不增加GC根;立即字符串/数字保留比较快照,值不写回业务对象。现有end_read_only/end_read_tracking/details仍返回保守对象列表,不自动替调用方放宽缓存。类型验证字典通过DictionaryKind观察,不把只验证载荷种类误当读取所有条目。
|
||||||
|
|
||||||
|
同步宿主可调用Vm::call_read_only_precise_with_limits(function, arguments, limits),返回拥有式Value及借用式ReadSet(objects与dependencies)。它与可恢复只读调用提供同样的名字观察,不创建新的调度器;单次调用仍同步执行并受正常硬预算约束,不能等待Pending await。旧call_read_only_with_limits委托同一执行逻辑,只返回ReadSet.objects,兼容原有保守宿主。成功或执行错误均先关闭只读作用域;不提供panic捕获。
|
||||||
|
|||||||
@@ -105,3 +105,6 @@ compare 的反向规则是 Lt 对应 Gt、Le 对应 Ge,Eq/Ne 保持不变。
|
|||||||
尚未实现:Python type 兼容、trait 和描述符、任意 VM 重入调用上下文、自动/分代/增量 GC、语言终结器/复活、完整资源限额和 VM 快照;独立 frontend 已提供阅读器、lexer/parser 与 AST,基础源码执行由独立 [Compiler](compiler.md) 接入,见 [源码前端](frontend.md);执行器已有帧数和指令数限额。数据库仅为未来特殊类提供按需连接,普通对象或宿主基础信息不自动持久化,详见 [数据库特殊类](https://gitea.entroia.com/Zayden-Jung/endorphin-hestia-server/src/branch/main/docs/runtime/database-objects.md)。
|
尚未实现:Python type 兼容、trait 和描述符、任意 VM 重入调用上下文、自动/分代/增量 GC、语言终结器/复活、完整资源限额和 VM 快照;独立 frontend 已提供阅读器、lexer/parser 与 AST,基础源码执行由独立 [Compiler](compiler.md) 接入,见 [源码前端](frontend.md);执行器已有帧数和指令数限额。数据库仅为未来特殊类提供按需连接,普通对象或宿主基础信息不自动持久化,详见 [数据库特殊类](https://gitea.entroia.com/Zayden-Jung/endorphin-hestia-server/src/branch/main/docs/runtime/database-objects.md)。
|
||||||
|
|
||||||
验证范围与执行约定见 [验证](../operations/validation.md)。
|
验证范围与执行约定见 [验证](../operations/validation.md)。
|
||||||
|
|
||||||
|
|
||||||
|
ObjectPayload::poll_await是通用非阻塞等待观察协议,默认UnsupportedOperation。AwaitPoll::Ready的对象结果是借用,调用方须在放弃awaitable前retain;协议不允许内部阻塞/IO/修改对象。完整指令和恢复契约见bytecode.md。
|
||||||
|
|||||||
+55
-3
@@ -72,7 +72,7 @@ ObjectPayload 可选择实现 `attribute_target(store)`,把实例字段放在
|
|||||||
|
|
||||||
implement_trait(Type, Trait)明确建立能力关系并校验必需方法。只有同名成员不算实现。implements_trait(Type, Trait)检查声明和继承;supports(instance, Trait)先查实际类型;未分类对象返回false,非法句柄或非定义参数报错。一个类型可显式实现多个能力,派生类继承关系。声明由Type的受管强边持有,查询和临时后代扫描不保活定义。
|
implement_trait(Type, Trait)明确建立能力关系并校验必需方法。只有同名成员不算实现。implements_trait(Type, Trait)检查声明和继承;supports(instance, Trait)先查实际类型;未分类对象返回false,非法句柄或非定义参数报错。一个类型可显式实现多个能力,派生类继承关系。声明由Type的受管强边持有,查询和临时后代扫描不保活定义。
|
||||||
|
|
||||||
源码与原生方法通过统一 CallArity 验证是否接受规范数量加 self;无元数据的原生方法不能登记能力。数量区间包括源码默认参数与原生可选参数,详见[原生调用](native-calls.md#统一位置参数数量)。缺少方法、非可调用成员或参数数量错误以TraitViolation报告能力名、方法名和原因。本子集只验证数量接受性,不验证注解、关联类型、返回类型、效果或完整替换兼容性;没有默认实现和trait源码语法。
|
源码与原生方法通过统一 CallArity 验证是否接受规范数量加 self;无元数据的原生方法不能登记能力。数量区间包括源码默认参数与原生可选参数,详见[原生调用](native-calls.md#统一位置参数数量)。缺少方法、非可调用成员或参数数量错误以TraitViolation报告能力名、方法名和原因。旧 new_trait 只验证数量接受性;new_typed_trait 的类型契约见下文。关联类型、默认实现和 trait 源码语法尚未实现。
|
||||||
|
|
||||||
建立声明前检查已有后代;构造源码子类检查继承的所有能力。define_method更新前检查受影响后代的显式/继承契约,被后代自己的覆盖遮蔽的方法不受该祖先更新影响。失败保留原方法表和关系。内部扫描只枚举当前借用句柄,不引入全局强类型注册表。声明更新通过普通堆写屏障与trace进行,既有类型的只读作用域内变更会被拒绝。NativeContext也可使用同一implement_trait入口,仍受该屏障约束。
|
建立声明前检查已有后代;构造源码子类检查继承的所有能力。define_method更新前检查受影响后代的显式/继承契约,被后代自己的覆盖遮蔽的方法不受该祖先更新影响。失败保留原方法表和关系。内部扫描只枚举当前借用句柄,不引入全局强类型注册表。声明更新通过普通堆写屏障与trace进行,既有类型的只读作用域内变更会被拒绝。NativeContext也可使用同一implement_trait入口,仍受该屏障约束。
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ implement_trait(Type, Trait)明确建立能力关系并校验必需方法。只
|
|||||||
| Type/Trait | 真实 Type/Trait 定义载荷 |
|
| Type/Trait | 真实 Type/Trait 定义载荷 |
|
||||||
| 其他名称 | 从函数自己的 globals、其次创建时捕获的 builtins 找到实际 Type 或 Trait;前者检查名义实例/继承,后者检查显式能力 |
|
| 其他名称 | 从函数自己的 globals、其次创建时捕获的 builtins 找到实际 Type 或 Trait;前者检查名义实例/继承,后者检查显式能力 |
|
||||||
|
|
||||||
普通名称每次调用重新解析,不是静态推断或定义时冻结的类型绑定。同名新定义不等于旧定义;自由函数可观察后续全局类型名重绑定。方法注解中指向自身声明类的名称,优先从实际接收者祖先中查找仍持有该函数的方法定义,因此 REPL 重定义全局类名后,原实例保存的绑定方法仍可调用。其他类型名仍遵循上述全局解析规则。此机制不提供重写方差、泛型、效果类型或原生参数/返回类型与效果签名;位置参数数量元数据见[原生调用](native-calls.md#统一位置参数数量)。字段赋值检查见下文。
|
普通名称每次调用重新解析,不是静态推断或定义时冻结的类型绑定。同名新定义不等于旧定义;自由函数可观察后续全局类型名重绑定。方法注解中指向自身声明类的名称,优先从实际接收者祖先中查找仍持有该函数的方法定义,因此 REPL 重定义全局类名后,原实例保存的绑定方法仍可调用。其他类型名仍遵循上述全局解析规则。源码方法替换规则见下文;泛型和静态效果类型仍未实现,原生参数/返回类型与效果签名见原生调用主题;位置参数数量元数据见[原生调用](native-calls.md#统一位置参数数量)。字段赋值检查见下文。
|
||||||
|
|
||||||
源码与原生函数的统一名义分类由 `ObjectStore::set_function_type`/`Vm::set_function_type` 显式配置。配置验证 Type、拒绝只读修改,只保留弱引用且不可改成另一个定义;设置后新分配的显式 `ObjectPayload::is_function` 载荷自动附加该类型,已有对象不追溯改写。语言函数、两类原生函数及绑定方法提供此标记;仅有 call 能力不算函数。配置类型过期则函数分配报错并清理新对象,不引入隐藏 GC 根。宿主应在创建函数前配置,并通过正常对象边保活定义。
|
源码与原生函数的统一名义分类由 `ObjectStore::set_function_type`/`Vm::set_function_type` 显式配置。配置验证 Type、拒绝只读修改,只保留弱引用且不可改成另一个定义;设置后新分配的显式 `ObjectPayload::is_function` 载荷自动附加该类型,已有对象不追溯改写。语言函数、两类原生函数及绑定方法提供此标记;仅有 call 能力不算函数。配置类型过期则函数分配报错并清理新对象,不引入隐藏 GC 根。宿主应在创建函数前配置,并通过正常对象边保活定义。
|
||||||
|
|
||||||
@@ -111,6 +111,58 @@ Record 构造在分配前校验全部位置字段。NativeTyped 分配器通过
|
|||||||
|
|
||||||
继承字段总是使用声明它的祖先定义和环境。注解为本类名时匹配该实际定义,REPL 重定义同名类不会改变旧实例的自引用字段约束;其他名称在每次构造/写入时从声明环境动态解析真实 Type/Trait,前向名称可在构造前建立,未知名称写入报错。不会仅以字符串相同判断名义兼容。基本注解与函数检查共用匹配规则。字段环境尚不解析函数局部类型名称或捕获的类型变量。
|
继承字段总是使用声明它的祖先定义和环境。注解为本类名时匹配该实际定义,REPL 重定义同名类不会改变旧实例的自引用字段约束;其他名称在每次构造/写入时从声明环境动态解析真实 Type/Trait,前向名称可在构造前建立,未知名称写入报错。不会仅以字符串相同判断名义兼容。基本注解与函数检查共用匹配规则。字段环境尚不解析函数局部类型名称或捕获的类型变量。
|
||||||
|
|
||||||
原生共享引用对象以当前 type_target 决定声明,以 attribute_target 决定存储;共享替换后的别名因而按新内容的字段契约写入。宿主直接写裸存储对象、改载荷、附加类型或改变字段所引用对象的共享转发,不属于 `set_attr` 的完整初始化/持续不变量保证;这些低层入口仍由宿主维护。没有静态推断、字段默认值、非空递归图初始化语法、重写方差或效果系统。
|
原生共享引用对象以当前 type_target 决定声明,以 attribute_target 决定存储;共享替换后的别名因而按新内容的字段契约写入。宿主直接写裸存储对象、改载荷、附加类型或改变字段所引用对象的共享转发,不属于 `set_attr` 的完整初始化/持续不变量保证;这些低层入口仍由宿主维护。没有静态推断、字段默认值、非空递归图初始化语法或静态效果系统。
|
||||||
|
|
||||||
回归源码位于 compiler/tests/classes.rs,覆盖普通/原生构造、继承、错误写入的值/版本保持、未知名称、动态类型重绑定、自引用定义、开关、删除和 GC 清理,仅编译未本地执行。Tessera 在真实原生程序中验证共享字段更新、错误赋值与构造前副作用保护。
|
回归源码位于 compiler/tests/classes.rs,覆盖普通/原生构造、继承、错误写入的值/版本保持、未知名称、动态类型重绑定、自引用定义、开关、删除和 GC 清理,仅编译未本地执行。Tessera 在真实原生程序中验证共享字段更新、错误赋值与构造前副作用保护。
|
||||||
|
|
||||||
|
|
||||||
|
## 方法覆写兼容
|
||||||
|
|
||||||
|
声明字段检查开关同时控制 new_class 与 define_method 的覆写替换验证;Vm.set_source_type_checks(true) 同时启用它。默认关闭以保留旧宿主行为。它与 Trait 的规范数量检查不同:对具有类型签名的实际基类方法,子方法必须接受基类允许的全部位置数量,参数类型只能扩大(逆变),返回类型只能缩小(协变)。接收者不按父类实例逆变,而必须接受子方法自己的声明类;self: Child 因而可以覆写 self: Base。普通未覆写的新方法仍按自身调用注解检查。
|
||||||
|
|
||||||
|
CallType::accepts_type 判断包含关系,支持 Object、基础值、实际名义继承、显式 Trait 实现及平坦联合;联合提供方的每一分支都必须被接收方至少一个分支覆盖。不同名义定义不以名称相同视作相等。不推断泛型、结构类型、函数内部可调用签名或关联类型。源码签名从真实参数/默认值和 SourceSignature 解析;自己的声明类绑定到实际定义,包括尚未发布的源码类和复用的祖先方法,不依赖重定义后的同名全局变量。
|
||||||
|
|
||||||
|
new_class 在发布之前校验本类覆写,失败释放候选定义。define_method 用候选替换进行只读检查,覆盖本类及所有已有后代的同名覆写,成功后才改变方法表;不能因后代遮蔽祖先的旧方法而跳过兼容验证。失败保持原方法、版本和继承关系。检查只借用定义/函数/注解身份,不引入强后代索引或冻结名称。
|
||||||
|
|
||||||
|
未知前向注解在类建立和方法更新时暂缓,其他已知不兼容以 OverrideViolation 报类名、方法名和原因。启用 VM 类型检查时,源码和原生调用均在进入实现前检查实际接收者所属方法的当前替换契约;UnknownAnnotation 此时不能暂缓。普通函数的首参数恰好是对象,不会因此被视为方法。默认接收者也使用实际默认参数值参与复查。每次复查动态全局注解,所以 REPL 重绑定使旧覆写不兼容时,函数体执行前拒绝;恢复兼容绑定后可继续普通调用。不回滚先前已发生的其他输入副作用。
|
||||||
|
|
||||||
|
已知调用效果只允许缩小:ReadOnly 基类不能被已知 Mutating/External 覆写;Mutating 基类不允许 External 覆写;External 可接受三者。已证明只读的源码方法参与同一检查;其余源码和原生 Delegated 的整体效果均未知,不能声称已经证明其纯度;实际只读布局/绘制仍通过原有堆屏障与原生效果入口限制执行。未知旧基类类型签名仍是未证明的兼容边界,旧 new_trait 仍仅校验数量;new_typed_trait 提供独立类型与已知效果要求,见下文。
|
||||||
|
|
||||||
|
回归源码 core/tests/method_contracts.rs 覆盖合法参数扩大/返回缩小、错误参数/返回、祖先替换对已有覆写后代的原子拒绝、调用前动态重绑定拒绝和恢复、前向名称、原生联合与效果及根清理,仅编译,不在本地执行。
|
||||||
|
|
||||||
|
|
||||||
|
## 类型化 Trait
|
||||||
|
|
||||||
|
ObjectStore/Vm::new_typed_trait(name, Vec<TypedMethodRequirement>) 以每项 name 和 CallSignature 定义能力。保留旧 new_trait/MethodRequirement 的数量契约兼容性;两者都有名义身份和显式声明,不靠同名方法推断。类型化签名必须给出包含 self 的非零、有限、固定规范数量;实现仍可提供可选参数或更多调用形式。签名完整验证、重复/保留名字检查在分配前完成。EndorTraitObject::signature(name) 返回借用描述;所有名义/能力类型引用由 Trait 载荷 trace 追踪,定义不可变,没有隐藏全局强根。
|
||||||
|
|
||||||
|
显式 implement_trait 严格检查实际方法的参数逆变、返回协变、接收者和已知原生效果;失败不登记、不改变方法表/版本。所有已有后代也检查。继承的源码子类及方法更新使用同一规则,未知前向注解可暂缓,但显式登记/查询和实际调用不暂缓。旧无类型元数据的方法不能满足类型化要求。Trait 的 self 可声明 Object 或实际名义约束,实际接收者必须满足它;不会因为普通函数首参是对象而隐式声明能力。
|
||||||
|
|
||||||
|
类型化能力的方法槽不允许由实例属性遮蔽;正常 set_attr 在修改前检查接收者及属性转发路径,旧数量型 Trait 仍保留动态属性行为。attach_type 与 implement_trait 分别检查待分类对象和已有实例的存储,冲突时不建立类型/能力关系。宿主仍可直接修改独立存储对象,但 supports、属性读取和方法调用会拒绝这种已失效的实例;删除冲突属性可以恢复。宿主绕过普通属性调用(如原生渲染快路径)须先调用 check_typed_instance_storage。该检查只验证实例存储,不替代方法签名检查,也不引入实例强引用索引。typed_method_names 返回宿主缓存应订阅的属性名;固有 Type/Trait 属性也不能遮住类型化方法。宿主 type_target 读取自身原始属性时,作用域检查集合避免重复进入,外层解析完成后仍执行完整校验;错误/展开路径通过 RAII 清理。
|
||||||
|
|
||||||
|
对于实际接收者拥有类型化能力的方法,VM 在体前复查当前契约,并强制该源码方法自己的参数/返回注解,即使全局源码检查开关关闭。参数更宽的实现仍可以通过具体方法接受额外值;不会把实现收窄成 Trait 的规范参数集合,也不删除原生方便调用的省略参数形式。未携带相应能力的普通函数调用仍沿全局配置,不是对所有自由函数的静态类型系统。
|
||||||
|
|
||||||
|
除了实现自己的返回注解,所有匹配的类型化 Trait 返回约束也检查最终实际结果,覆盖源码/原生及尾委托链。执行帧/原生作用域保留这些定义的执行根,失败和预算清理均释放。方法体中修改其他类型的动态注解可能使返回对象的能力失效,不能仅依赖体前形式签名比较;最终 Capability 匹配会再次校验。错误返回不回滚已经发生的业务修改。
|
||||||
|
|
||||||
|
嵌套能力比较按线程/定义/能力保留临时验证集合,对已经在校验的同一名义能力关系按递归约束处理,避免无限递归;它不建立新声明关系。集合不持有 GC 根,RAII 在成功、错误和展开路径清理,不让失败验证成为后续成功缓存。没有长期签名缓存;动态名称每次仍从原环境解析。
|
||||||
|
|
||||||
|
部分源码可由已验证字节码证明只读(见下文);其余源码和 Delegated 包装的整体效果仍未知,类型化 ReadOnly 要求不等于已经证明这些函数纯度。已知效果会比较,真正只读布局/绘制继续执行堆屏障;普通输入中源码副作用仍按现有运行模式处理。没有泛型/关联类型/完整传递效果推断或 Trait 源码声明语法。Tessera 的四个宿主能力使用类型化要求,其中 Presentable 允许普通展示构造副作用,不额外宣称纯函数。
|
||||||
|
|
||||||
|
core/tests/typed_traits.rs 覆盖登记失败原子性、参数扩大仍可直接调用、全局检查关闭时的显式能力实际参数/返回检查、签名类型保活/回收,以及体内动态改变返回对象能力后的最终拒绝和验证集合恢复,仅编译未本地执行。
|
||||||
|
|
||||||
|
|
||||||
|
## 已知调用效果与保守源码证明
|
||||||
|
|
||||||
|
ObjectStore/Vm::call_effect(function) 返回已知的整体 CallEffect 或 None,解包绑定方法后检查真实目标;原生签名照常验证,Delegated 包装整体未知,旧函数和通用Type构造也不猜测。查询不执行函数,不固定动态全局名称,不新增托管强边。参数/返回类型签名查询仍由 native_signature 和源码注解承担。
|
||||||
|
|
||||||
|
EndorCode::is_statically_read_only 只在全部已验证指令都在只读白名单内时返回true。读取、控制流、局部删除、创建新Dict/Function允许;Call、所有Store、DeleteGlobal/DeleteDeref、BuildClass及未来未审阅指令均保守未知。StoreFast也不能直接视为纯度:已有局部值可能通过共享引用赋值更新原对象。未知不表示一定有写入;创建新结果后填字段或调用纯函数仍可能安全,但此局部证明不作别名/新鲜性或调用图推断。不可达指令也参与保守扫描,嵌套函数代码不被当作工厂已执行的函数体。
|
||||||
|
|
||||||
|
源码方法的此项只读证明用于方法替换/类型化能力的已知效果比较,不扩大全局源码检查开关的范围,也不拒绝目前未知的源码实现。只读执行的堆屏障和实际原生目标检查继续沿原VM帧、预算和尾委托作用域传递;它们不能被静态结果替代。可信ObjectPayload读取/比较仍属于宿主约定,不构成Rust隐藏I/O沙箱;只读不证明终止。查询另有保守调用图证明,规则见下段;别名/新对象写入、未知调用结果和完整效果推断仍未实现。
|
||||||
|
|
||||||
|
method_contracts.rs新增已证明源码基类的原生效果扩大拒绝/原子性、绑定查询,以及共享局部赋值、自由Cell写入、动态调用保持未知的回归源码。仅编译,不在本地执行。
|
||||||
|
|
||||||
|
|
||||||
|
call_effect 对含 Call 的源码执行只读调用图证明:控制流逐点传播栈上的已知对象/绑定方法事实,分支或循环汇合的不同事实退化为未知。解析当前 globals,缺失时查该函数实际捕获的 builtins;自由Cell从当前闭包读取。绑定接收者及可确定的第一实参用于解析方法/对象字段,按普通属性查找顺序,不绕过实例遮蔽规则。其他参数、调用结果、新建函数/Dict内容和无法解析的调用目标保持未知;不凭注解猜测具体对象。调用图与控制流均采用显式队列,无Rust递归或长期缓存/保活索引,循环关系只证明无写入,不证明终止。
|
||||||
|
|
||||||
|
只有所有可能执行的调用目标都已证明/声明ReadOnly,且源码自身没有未证明操作时,整个源码查询返回ReadOnly。有Mutating/External、Delegated、旧未知原生目标或无法确定的调用就返回None;尤其不将含已知写目标的源码简单总结成Mutating,因为该目标可能重绑定后续全局、Cell或方法,让快照边失效。直接查询原生函数仍返回其签名的已知效果。当前只读证明依赖可信宿主读取/比较和原生ReadOnly契约,不是外部I/O沙箱。
|
||||||
|
|
||||||
|
全局/闭包/方法更新之后每次重新解析,不把某次查询作为永久保证;实际只读调用继续执行原VM作用域和目标检查。method_contracts.rs增加当前builtins与全局覆盖/恢复、自由Cell变化、递归图、控制流事实汇合和实际绑定方法/实例字段更新回归;Tessera增加真实Endor转调、self.helper与重绑定后重新查询且不执行业务方法的回归。仅编译,不本地运行。
|
||||||
|
|||||||
Reference in New Issue
Block a user