Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5ff1c68f5f | |||
| f3e1670c58 | |||
| 1aecc7e952 | |||
| ee68a0c54c | |||
| 42c27d083d | |||
| a775d5c827 | |||
| 89b817447e | |||
| ae75c54707 | |||
| 8a2135897e | |||
| 16816c38c7 | |||
| b33d322690 | |||
| e6b27ba7b6 |
Generated
+51
-1
@@ -2,6 +2,12 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||
|
||||
[[package]]
|
||||
name = "compiler"
|
||||
version = "0.1.0"
|
||||
@@ -38,9 +44,53 @@ version = "2.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "objects"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "peg"
|
||||
@@ -114,7 +164,7 @@ checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.9",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -7,3 +7,5 @@
|
||||
| [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)。
|
||||
|
||||
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) => {
|
||||
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(_) => {
|
||||
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());
|
||||
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 {
|
||||
StmtKind::Binding(binding) => {
|
||||
declare(&mut locals, &binding.name.text, binding.name.span)?;
|
||||
builder.scope.layout.locals.push(binding.name.text.clone());
|
||||
StmtKind::Binding(b) => {
|
||||
declare(locals, &b.name.text, b.name.span)?;
|
||||
layout.push(b.name.text.clone());
|
||||
}
|
||||
StmtKind::Assignment(assignment) => {
|
||||
if locals.insert(assignment.name.text.clone()) {
|
||||
builder
|
||||
.scope
|
||||
.layout
|
||||
.locals
|
||||
.push(assignment.name.text.clone());
|
||||
StmtKind::Assignment(a) => {
|
||||
if locals.insert(a.name.text.clone()) {
|
||||
layout.push(a.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(_) => {
|
||||
return Err(error(
|
||||
statement.span,
|
||||
@@ -231,39 +258,7 @@ fn function_scope(function: &Function, globals: &BTreeSet<String>) -> Result<Sco
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut returned = false;
|
||||
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)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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(
|
||||
&mut self,
|
||||
expression: &Expr,
|
||||
@@ -332,7 +409,9 @@ impl Builder {
|
||||
binding,
|
||||
});
|
||||
}
|
||||
ExprKind::Unary { operand, .. } => self.expression(operand, globals)?,
|
||||
ExprKind::Unary { operand, .. } | ExprKind::Await(operand) => {
|
||||
self.expression(operand, globals)?
|
||||
}
|
||||
ExprKind::Binary { left, right, .. } => {
|
||||
self.expression(left, globals)?;
|
||||
self.expression(right, globals)?;
|
||||
|
||||
+103
-16
@@ -2,7 +2,7 @@ use endor_core::{
|
||||
CodeLayout, DecodedInstruction, EndorCode, EndorNumber, Instruction, ObjectRef, Opcode, Value,
|
||||
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 crate::{NameBinding, Scope, ScopeKind, SymbolTable, diagnostic};
|
||||
@@ -115,6 +115,7 @@ struct Emitter<'a> {
|
||||
source: &'a SourceUnit,
|
||||
instructions: Vec<Instruction>,
|
||||
constants: Vec<Value>,
|
||||
jumps: Vec<(usize, usize)>,
|
||||
children: Vec<(usize, CompiledCode)>,
|
||||
}
|
||||
|
||||
@@ -125,6 +126,7 @@ impl<'a> Emitter<'a> {
|
||||
source,
|
||||
instructions: Vec::new(),
|
||||
constants: Vec::new(),
|
||||
jumps: Vec::new(),
|
||||
children: Vec::new(),
|
||||
}
|
||||
}
|
||||
@@ -249,6 +251,36 @@ impl<'a> Emitter<'a> {
|
||||
|
||||
fn statement(&mut self, statement: &Stmt, symbols: &SymbolTable) -> Result<(), Diagnostic> {
|
||||
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::Assignment(assignment) => {
|
||||
self.store_binding(&assignment.name.text, &assignment.value)?
|
||||
@@ -300,26 +332,46 @@ impl<'a> Emitter<'a> {
|
||||
}
|
||||
self.emit(Opcode::Call, arguments.len())?;
|
||||
}
|
||||
ExprKind::Await(operand) => {
|
||||
self.expression(operand)?;
|
||||
self.simple(Opcode::Await);
|
||||
}
|
||||
ExprKind::Member { object, name } => {
|
||||
self.expression(object)?;
|
||||
self.emit(Opcode::LoadAttr, self.name_index(&name.text))?;
|
||||
}
|
||||
ExprKind::Unary { .. } => {
|
||||
// Literal sign folding preserves arbitrarily large integers
|
||||
// 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",
|
||||
));
|
||||
};
|
||||
ExprKind::Unary { operand, .. } => {
|
||||
if let Some(spelling) = signed_literal(expression, false) {
|
||||
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(_) => {
|
||||
return Err(error(
|
||||
@@ -341,7 +393,42 @@ impl<'a> Emitter<'a> {
|
||||
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
|
||||
.scope
|
||||
.span
|
||||
|
||||
@@ -262,11 +262,22 @@ fn declared_field_checks_cover_construction_inheritance_writes_and_gc() {
|
||||
r#"
|
||||
class Base { count: Number }
|
||||
class Child: Base { label: String }
|
||||
value = Child(9, "ok")
|
||||
class Holder { child: Base }
|
||||
holder = Holder(value)
|
||||
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,
|
||||
);
|
||||
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 Seed: Link {}
|
||||
"#,
|
||||
ParseMode::Interactive,
|
||||
ParseMode::Module,
|
||||
);
|
||||
// A recursive nonnullable field can be initialized by the trusted host in two steps.
|
||||
let Value::Object(link) = vm
|
||||
|
||||
@@ -345,19 +345,11 @@ fn extended_operands_work_for_large_local_and_constant_tables() {
|
||||
|
||||
#[test]
|
||||
fn unsupported_execution_features_are_compile_errors_instead_of_fake_results() {
|
||||
for (text, mode) in [
|
||||
("1 + 2", ParseMode::Expression),
|
||||
(".title", ParseMode::Expression),
|
||||
(
|
||||
"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();
|
||||
let source = EndorReader::from_text("<unsupported>", ".title");
|
||||
let error =
|
||||
compile_source(&source, ParseMode::Expression, &CompileOptions::default()).unwrap_err();
|
||||
assert_eq!(error.diagnostic.kind, DiagnosticKind::Compilation);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -19,3 +19,9 @@
|
||||
| 新 VM 公开 API | vm.rs/eval.rs 与 lib.rs 导出 | 所属主题、示例、[tests 导航](../tests/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列表,预算、结果拥有式引用和失败关闭只读作用域的语义不变。
|
||||
|
||||
+369
-13
@@ -1,10 +1,11 @@
|
||||
use crate::ExecutionProgress;
|
||||
use crate::frame::{NativeReturn, StackEntry, SuspendedCall};
|
||||
use crate::frame::{Coroutine, CoroutineState, NativeReturn, StackEntry, SuspendedCall};
|
||||
use crate::{
|
||||
CompareOp, EndorCellObject, EndorCodeObject, EndorFrame, EndorFunctionObject,
|
||||
EndorStringObject, ExecutionLimits, ExecutionLocation, Instruction, ObjectRef, Opcode, Value,
|
||||
Vm, VmError,
|
||||
};
|
||||
use crate::{CoroutineProgress, ObjectPayload};
|
||||
use crate::{NativeContext, NativeResult};
|
||||
|
||||
enum PreparedCall {
|
||||
@@ -58,9 +59,23 @@ impl Vm {
|
||||
arguments: &[Value],
|
||||
limits: ExecutionLimits,
|
||||
) -> 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 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))
|
||||
}
|
||||
|
||||
@@ -87,7 +102,7 @@ impl Vm {
|
||||
}
|
||||
let mut frames = vec![frame];
|
||||
let result = self
|
||||
.eval_frames(&mut frames, limits, &mut executed, usize::MAX)
|
||||
.eval_frames(&mut frames, limits, &mut executed, usize::MAX, false)
|
||||
.map(|value| value.expect("unbounded slice completes or fails"));
|
||||
if result.is_err() {
|
||||
self.last_traceback = frames
|
||||
@@ -130,7 +145,7 @@ impl Vm {
|
||||
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(scope) {
|
||||
let reads = match self.objects.end_read_only_precise(scope) {
|
||||
Ok(reads) => reads,
|
||||
Err(error) => {
|
||||
self.drop_root(value);
|
||||
@@ -139,7 +154,8 @@ impl Vm {
|
||||
};
|
||||
Ok(ExecutionProgress::Complete {
|
||||
value,
|
||||
reads,
|
||||
dependencies: reads.dependencies,
|
||||
reads: reads.objects,
|
||||
instructions: executed,
|
||||
})
|
||||
}
|
||||
@@ -188,7 +204,13 @@ impl Vm {
|
||||
.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);
|
||||
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);
|
||||
@@ -209,11 +231,12 @@ impl Vm {
|
||||
while let Some(frame) = call.frames.pop() {
|
||||
self.clear_frame(frame);
|
||||
}
|
||||
let reads = self.objects.end_read_only(call.scope);
|
||||
let reads = self.objects.end_read_only_precise(call.scope);
|
||||
match (result, reads) {
|
||||
(Ok(Some(value)), Ok(reads)) => Ok(ExecutionProgress::Complete {
|
||||
value,
|
||||
reads,
|
||||
dependencies: reads.dependencies,
|
||||
reads: reads.objects,
|
||||
instructions: call.executed,
|
||||
}),
|
||||
(Ok(Some(value)), Err(error)) => {
|
||||
@@ -237,6 +260,245 @@ impl Vm {
|
||||
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.
|
||||
/// These are copied metadata, not heap references or language exceptions.
|
||||
pub fn last_traceback(&self) -> &[ExecutionLocation] {
|
||||
@@ -368,6 +630,7 @@ impl Vm {
|
||||
name: data.name().into(),
|
||||
instruction_offset: 0,
|
||||
current_offset: 0,
|
||||
waiting: false,
|
||||
localsplus: vec![None; executable.slot_count()],
|
||||
stack: Vec::with_capacity(executable.stack_size()),
|
||||
executable,
|
||||
@@ -638,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(
|
||||
&self,
|
||||
left: &Value,
|
||||
right: &Value,
|
||||
operation: CompareOp,
|
||||
) -> Result<bool, VmError> {
|
||||
if matches!(left, Value::Number(_)) || matches!(right, Value::Number(_)) {
|
||||
return Err(VmError::UnsupportedOperation("numeric comparison"));
|
||||
let (left_number, right_number) = (self.number_value(left)?, self.number_value(right)?);
|
||||
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)?) {
|
||||
return Ok(match operation {
|
||||
@@ -753,6 +1040,7 @@ impl Vm {
|
||||
limits: ExecutionLimits,
|
||||
executed: &mut usize,
|
||||
slice_end: usize,
|
||||
allow_wait: bool,
|
||||
) -> Result<Option<Value>, VmError> {
|
||||
loop {
|
||||
if *executed >= limits.max_instructions {
|
||||
@@ -769,6 +1057,36 @@ impl Vm {
|
||||
let opcode = decoded.instruction.opcode;
|
||||
let argument = decoded.instruction.argument as usize;
|
||||
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::LoadConst => {
|
||||
let value = frame.executable.constants()[argument].clone();
|
||||
@@ -799,12 +1117,11 @@ impl Vm {
|
||||
}
|
||||
Opcode::LoadGlobal => {
|
||||
let name = &frame.executable.layout().names[argument];
|
||||
let key = Value::Text(name.clone());
|
||||
let mut value = self.dict_get(frame.globals, &key)?;
|
||||
let mut value = self.objects.dict_get_name(frame.globals, name)?;
|
||||
if value.is_none()
|
||||
&& 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()))?;
|
||||
self.push_borrowed(frame, value)?;
|
||||
@@ -949,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 => {
|
||||
let left = Self::stack_value(frame, frame.stack.len() - 2)?;
|
||||
let right = Self::top_value(frame)?;
|
||||
|
||||
@@ -38,6 +38,7 @@ pub struct EndorFrame {
|
||||
pub(crate) name: String,
|
||||
pub(crate) instruction_offset: usize,
|
||||
pub(crate) current_offset: usize,
|
||||
pub(crate) waiting: bool,
|
||||
pub(crate) localsplus: Vec<Option<Value>>,
|
||||
pub(crate) stack: Vec<StackEntry>,
|
||||
}
|
||||
@@ -102,6 +103,7 @@ pub enum ExecutionProgress {
|
||||
Complete {
|
||||
value: Value,
|
||||
reads: Vec<ObjectRef>,
|
||||
dependencies: Vec<crate::ReadDependency>,
|
||||
instructions: usize,
|
||||
},
|
||||
}
|
||||
@@ -113,3 +115,83 @@ pub(crate) struct SuspendedCall {
|
||||
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 vm;
|
||||
|
||||
pub use frame::{EndorFrame, ExecutionLimits, ExecutionLocation, ExecutionProgress};
|
||||
pub use frame::{
|
||||
CoroutineProgress, EndorFrame, ExecutionLimits, ExecutionLocation, ExecutionProgress,
|
||||
};
|
||||
// Preserve existing embedding imports; objects owns these definitions.
|
||||
pub use objects::ObjectStore;
|
||||
pub use objects::{
|
||||
AnnotationType, ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement, RootTypes,
|
||||
TypeConstructor, TypedMethodRequirement,
|
||||
AnnotationType, AwaitPoll, ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement,
|
||||
RootTypes, TypeConstructor, TypedMethodRequirement,
|
||||
};
|
||||
pub use objects::{
|
||||
CallArity, CallEffect, CallSignature, CallType, ChangeBatch, ChangeKey, CodeLayout, CompareOp,
|
||||
ContextNativeFunction, DecodedInstruction, EndorBaseExceptionObject, EndorCellObject,
|
||||
EndorCode, EndorCodeObject, EndorDecimal, EndorDictObject, EndorFunctionObject, EndorInteger,
|
||||
EndorNativeCall, EndorNativeFunctionObject, EndorNumber, EndorNumberObject, EndorObject,
|
||||
EndorRational, EndorReal, EndorStringObject, EndorTracebackEntry, Instruction, NativeCall,
|
||||
NativeCallable, NativeContext, NativeFunction, NativeResult, ObjectChange, ObjectHeader,
|
||||
ObjectPayload, ObjectRef, Opcode, ReadAccess, Record, SourceSignature, Value, VmError,
|
||||
WeakObjectRef,
|
||||
ArithmeticOp, CallArity, CallEffect, CallSignature, CallType, ChangeBatch, ChangeKey,
|
||||
CodeLayout, CompareOp, ContextNativeFunction, DecodedInstruction, EndorBaseExceptionObject,
|
||||
EndorCellObject, EndorCode, EndorCodeObject, EndorDecimal, EndorDictObject,
|
||||
EndorFunctionObject, EndorInteger, EndorNativeCall, EndorNativeFunctionObject, EndorNumber,
|
||||
EndorNumberObject, EndorObject, EndorRational, EndorReal, EndorStringObject,
|
||||
EndorTracebackEntry, Instruction, NativeCall, NativeCallable, NativeContext, NativeFunction,
|
||||
NativeResult, ObjectChange, ObjectHeader, ObjectPayload, ObjectRef, Opcode, ReadAccess,
|
||||
ReadDependency, ReadSet, Record, SourceSignature, TrackedReads, Value, VmError, WeakObjectRef,
|
||||
};
|
||||
pub use vm::Vm;
|
||||
|
||||
@@ -15,3 +15,11 @@
|
||||
核心对象现由 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 的 managed_coroutine 用例覆盖交替恢复不重放写入、累计预算错误释放、取消幂等和暂停帧引用环回收;本地只经宿主Clippy编译,不运行测试。
|
||||
|
||||
execution.rs补AwaitGate,覆盖真实Waiting、前缀写入不重放、等待操作数保活及拥有式对象结果;本地仅编译不执行。
|
||||
|
||||
execution.rs的named_reads/precise_nested_regions覆盖无关键、缺失遮蔽、内建变化、区域传播、保守遍历及观察不保活;仅编译未本地运行。
|
||||
|
||||
named_reads同时比较同步精确入口与可恢复调用的观察,验证旧同步入口仍返回相同保守对象列表;本地仅编译。
|
||||
|
||||
@@ -1503,3 +1503,352 @@ fn suspended_call_cancellation_and_total_budget_restore_read_only_state() {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -21,12 +21,16 @@ module Endor {
|
||||
StmtKind = Binding(Binding binding)
|
||||
| Assignment(Assignment assignment)
|
||||
| MemberAssignment(MemberAssignment assignment)
|
||||
| If(IfStatement statement)
|
||||
| While(WhileStatement statement)
|
||||
| Return(ReturnStatement statement)
|
||||
| Expression(Expr expression)
|
||||
| Declaration(Decl declaration)
|
||||
Binding = (Name name, Expr value, span span)
|
||||
Assignment = (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)
|
||||
InteractiveInput = (Stmt* statements, span span)
|
||||
|
||||
@@ -35,10 +39,11 @@ module Endor {
|
||||
| Number(string spelling)
|
||||
| String(string value)
|
||||
| Symbol(Name name)
|
||||
| Await(Expr operand)
|
||||
| Unary(UnaryOp operation, Expr operand)
|
||||
| Binary(BinaryOp operation, Expr left, Expr right)
|
||||
| Member(Expr object, Name name)
|
||||
| Call(Expr callee, Expr* arguments)
|
||||
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",
|
||||
"$id": "urn:endor:ast:sha256:ad2ac52943dd26e08395c0d2698861da98bb89c360241c4712c769de86709fd0",
|
||||
"$id": "urn:endor:ast:sha256:21b0349ac295d9bc8cedcad8f07d8e2d45cf220e19e7c2fad10269555fcfd71a",
|
||||
"$ref": "#/$defs/AstDocument",
|
||||
"$defs": {
|
||||
"Span": {
|
||||
@@ -27,7 +27,7 @@
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"schema": {
|
||||
"const": "urn:endor:ast:sha256:ad2ac52943dd26e08395c0d2698861da98bb89c360241c4712c769de86709fd0"
|
||||
"const": "urn:endor:ast:sha256:21b0349ac295d9bc8cedcad8f07d8e2d45cf220e19e7c2fad10269555fcfd71a"
|
||||
},
|
||||
"root": {
|
||||
"$ref": "#/$defs/AstRoot"
|
||||
@@ -354,6 +354,38 @@
|
||||
],
|
||||
"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",
|
||||
"properties": {
|
||||
@@ -468,6 +500,56 @@
|
||||
],
|
||||
"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": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -591,6 +673,22 @@
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"kind": {
|
||||
"const": "Await"
|
||||
},
|
||||
"data": {
|
||||
"$ref": "#/$defs/Expr"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"kind",
|
||||
"data"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -726,7 +824,13 @@
|
||||
"Add",
|
||||
"Subtract",
|
||||
"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.
|
||||
|
||||
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 type AstDocument = { schema: typeof AST_SCHEMA_ID; root: AstRoot; };
|
||||
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 Block = { statements: Array<Stmt>; 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 Assignment = { 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 InteractiveInput = { statements: Array<Stmt>; 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 BinaryOp = "Add" | "Subtract" | "Multiply" | "Divide";
|
||||
export type BinaryOp = "Add" | "Subtract" | "Multiply" | "Divide" | "Equal" | "NotEqual" | "Less" | "LessEqual" | "Greater" | "GreaterEqual";
|
||||
export interface Visitor {
|
||||
visitSpan?(node: Span): boolean | void;
|
||||
visitAstDocument?(node: AstDocument): boolean | void;
|
||||
@@ -40,6 +42,8 @@ visitStmtKind?(node: StmtKind): boolean | void;
|
||||
visitBinding?(node: Binding): boolean | void;
|
||||
visitAssignment?(node: Assignment): boolean | void;
|
||||
visitMemberAssignment?(node: MemberAssignment): boolean | void;
|
||||
visitIfStatement?(node: IfStatement): boolean | void;
|
||||
visitWhileStatement?(node: WhileStatement): boolean | void;
|
||||
visitReturnStatement?(node: ReturnStatement): boolean | void;
|
||||
visitInteractiveInput?(node: InteractiveInput): boolean | void;
|
||||
visitExpr?(node: Expr): boolean | void;
|
||||
@@ -135,6 +139,12 @@ break;
|
||||
case "MemberAssignment":
|
||||
walkMemberAssignment(node.data, visitor);
|
||||
break;
|
||||
case "If":
|
||||
walkIfStatement(node.data, visitor);
|
||||
break;
|
||||
case "While":
|
||||
walkWhileStatement(node.data, visitor);
|
||||
break;
|
||||
case "Return":
|
||||
walkReturnStatement(node.data, visitor);
|
||||
break;
|
||||
@@ -165,6 +175,19 @@ walkName(node.name, visitor);
|
||||
walkExpr(node.value, 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 {
|
||||
if (visitor.visitReturnStatement?.(node) === false) return;
|
||||
if (node.value != null) walkExpr(node.value, visitor);
|
||||
@@ -192,6 +215,9 @@ break;
|
||||
case "Symbol":
|
||||
walkName(node.data, visitor);
|
||||
break;
|
||||
case "Await":
|
||||
walkExpr(node.data, visitor);
|
||||
break;
|
||||
case "Unary":
|
||||
walkUnaryOp(node.data.operation, visitor);
|
||||
walkExpr(node.data.operand, visitor);
|
||||
|
||||
@@ -17,3 +17,5 @@
|
||||
函数体允许独立方法调用等表达式语句;末尾 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)。
|
||||
|
||||
grammar.peg支持await前缀表达式;AST仍从Endor.asdl生成,不能手改generated.rs/visitor.rs/editor产物。
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use super::Spanned;
|
||||
use crate::Span;
|
||||
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)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct AstDocument {
|
||||
@@ -141,6 +141,8 @@ pub enum StmtKind {
|
||||
Binding(Binding),
|
||||
Assignment(Assignment),
|
||||
MemberAssignment(MemberAssignment),
|
||||
If(IfStatement),
|
||||
While(WhileStatement),
|
||||
Return(ReturnStatement),
|
||||
Expression(Expr),
|
||||
Declaration(Decl),
|
||||
@@ -151,6 +153,8 @@ impl Spanned for StmtKind {
|
||||
Self::Binding(node) => node.span(),
|
||||
Self::Assignment(node) => node.span(),
|
||||
Self::MemberAssignment(node) => node.span(),
|
||||
Self::If(node) => node.span(),
|
||||
Self::While(node) => node.span(),
|
||||
Self::Return(node) => node.span(),
|
||||
Self::Expression(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)]
|
||||
#[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 value: Option<Expr>,
|
||||
pub span: Span,
|
||||
@@ -234,6 +263,7 @@ pub enum ExprKind {
|
||||
Number(String),
|
||||
String(String),
|
||||
Symbol(Name),
|
||||
Await(Box<Expr>),
|
||||
Unary {
|
||||
operation: UnaryOp,
|
||||
operand: Box<Expr>,
|
||||
@@ -262,4 +292,10 @@ pub enum BinaryOp {
|
||||
Subtract,
|
||||
Multiply,
|
||||
Divide,
|
||||
Equal,
|
||||
NotEqual,
|
||||
Less,
|
||||
LessEqual,
|
||||
Greater,
|
||||
GreaterEqual,
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
use super::{
|
||||
Assignment, AstDocument, AstRoot, BinaryOp, Binding, Block, Class, Decl, DeclKind, Expr,
|
||||
ExprKind, Function, InteractiveInput, MemberAssignment, Name, Parameter, Program,
|
||||
ReturnStatement, Spanned, Stmt, StmtKind, UnaryOp,
|
||||
ExprKind, Function, IfStatement, InteractiveInput, MemberAssignment, Name, Parameter, Program,
|
||||
ReturnStatement, Spanned, Stmt, StmtKind, UnaryOp, WhileStatement,
|
||||
};
|
||||
use crate::Span;
|
||||
pub trait Visitor {
|
||||
@@ -102,6 +102,18 @@ pub trait Visitor {
|
||||
self.leave_node();
|
||||
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> {
|
||||
self.enter_node(node.span())?;
|
||||
let result = walk_return_statement(self, node);
|
||||
@@ -243,6 +255,12 @@ pub fn walk_stmt_kind<V: Visitor + ?Sized>(
|
||||
StmtKind::MemberAssignment(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) => {
|
||||
visitor.visit_return_statement(value)?;
|
||||
}
|
||||
@@ -280,6 +298,27 @@ pub fn walk_member_assignment<V: Visitor + ?Sized>(
|
||||
visitor.visit_span(&node.span)?;
|
||||
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>(
|
||||
visitor: &mut V,
|
||||
node: &ReturnStatement,
|
||||
@@ -316,6 +355,9 @@ pub fn walk_expr_kind<V: Visitor + ?Sized>(
|
||||
ExprKind::Symbol(value) => {
|
||||
visitor.visit_name(value)?;
|
||||
}
|
||||
ExprKind::Await(value) => {
|
||||
visitor.visit_expr(value)?;
|
||||
}
|
||||
ExprKind::Unary {
|
||||
operation, operand, ..
|
||||
} => {
|
||||
|
||||
@@ -78,6 +78,7 @@ peg::parser! {
|
||||
= declaration:declaration() {
|
||||
Stmt { span: declaration.span, kind: StmtKind::Declaration(declaration) }
|
||||
}
|
||||
/ control:control_statement() { control }
|
||||
/ binding:binding() { binding_statement(binding) }
|
||||
/ assignment:member_assignment() { assignment }
|
||||
/ assignment:assignment() { assignment }
|
||||
@@ -104,13 +105,25 @@ peg::parser! {
|
||||
}
|
||||
|
||||
rule local_statement() -> Stmt
|
||||
= binding:binding() { binding_statement(binding) }
|
||||
= control:control_statement() { control }
|
||||
/ binding:binding() { binding_statement(binding) }
|
||||
/ assignment:member_assignment() { assignment }
|
||||
/ assignment:assignment() { assignment }
|
||||
/ !return_keyword() 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
|
||||
= name:name() equal() value:expression() {
|
||||
let span = name.span.through(value.span);
|
||||
@@ -140,12 +153,22 @@ peg::parser! {
|
||||
}
|
||||
|
||||
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:(@) minus() right:@ { binary(left, BinaryOp::Subtract, right) }
|
||||
--
|
||||
left:(@) star() right:@ { binary(left, BinaryOp::Multiply, 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:@ {
|
||||
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::Comma }
|
||||
/ "." { TokenKind::Dot }
|
||||
/ "==" { TokenKind::EqualEqual }
|
||||
/ "!=" { TokenKind::NotEqual }
|
||||
/ "<=" { TokenKind::LessEqual }
|
||||
/ ">=" { TokenKind::GreaterEqual }
|
||||
/ "<" { TokenKind::Less }
|
||||
/ ">" { TokenKind::Greater }
|
||||
/ "=" { TokenKind::Equal }
|
||||
/ "+" { TokenKind::Plus }
|
||||
/ "-" { TokenKind::Minus }
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::ast::{
|
||||
Assignment, AstRoot, BinaryOp, Binding, Block, Class, Decl, DeclKind, Expr, ExprKind, Function,
|
||||
InteractiveInput, MemberAssignment, Name, Parameter, Program, ReturnStatement, Stmt, StmtKind,
|
||||
UnaryOp,
|
||||
IfStatement, InteractiveInput, MemberAssignment, Name, Parameter, Program, ReturnStatement,
|
||||
Stmt, StmtKind, UnaryOp, WhileStatement,
|
||||
};
|
||||
use crate::lexer::decode_string;
|
||||
use crate::{Diagnostic, DiagnosticKind, Span, Token, TokenKind, lex};
|
||||
|
||||
@@ -16,6 +16,12 @@ pub enum TokenKind {
|
||||
Comma,
|
||||
Dot,
|
||||
Equal,
|
||||
EqualEqual,
|
||||
NotEqual,
|
||||
Less,
|
||||
LessEqual,
|
||||
Greater,
|
||||
GreaterEqual,
|
||||
Plus,
|
||||
Minus,
|
||||
Star,
|
||||
|
||||
@@ -272,7 +272,7 @@ fn parser_requires_complete_input_and_rejects_unimplemented_or_malformed_syntax(
|
||||
}
|
||||
for source in [
|
||||
"", "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}");
|
||||
}
|
||||
|
||||
@@ -7,3 +7,8 @@ license.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
num-bigint = "0.4"
|
||||
num-rational = "0.4"
|
||||
num-traits = "0.2"
|
||||
|
||||
@@ -23,3 +23,7 @@
|
||||
对象操作变更同步 [对象模型](../../../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)。
|
||||
|
||||
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使用按名注解/全局解析。
|
||||
|
||||
@@ -258,6 +258,8 @@ pub(crate) fn read_only_opcode(opcode: Opcode) -> bool {
|
||||
| Opcode::PopJumpIfTrue
|
||||
| Opcode::ToBool
|
||||
| Opcode::UnaryNot
|
||||
| Opcode::BinaryArithmetic
|
||||
| Opcode::UnaryNegate
|
||||
| Opcode::CompareOp
|
||||
| Opcode::LoadDeref
|
||||
| Opcode::LoadClosure
|
||||
@@ -354,6 +356,7 @@ fn verify(
|
||||
| Opcode::LoadAttr
|
||||
| Opcode::StoreAttr => index < layout.names.len(),
|
||||
Opcode::CompareOp => compare_operation(argument).is_some(),
|
||||
Opcode::BinaryArithmetic => argument <= 3,
|
||||
Opcode::Copy => argument >= 1,
|
||||
Opcode::Swap => argument >= 2,
|
||||
Opcode::JumpForward
|
||||
@@ -394,8 +397,12 @@ fn verify(
|
||||
| Opcode::StoreDeref
|
||||
| Opcode::PopJumpIfFalse
|
||||
| Opcode::PopJumpIfTrue => (1, 1, 0),
|
||||
Opcode::ToBool | Opcode::UnaryNot | Opcode::LoadAttr => (1, 1, 1),
|
||||
Opcode::CompareOp => (2, 2, 1),
|
||||
Opcode::ToBool
|
||||
| Opcode::UnaryNot
|
||||
| Opcode::LoadAttr
|
||||
| Opcode::Await
|
||||
| Opcode::UnaryNegate => (1, 1, 1),
|
||||
Opcode::CompareOp | Opcode::BinaryArithmetic => (2, 2, 1),
|
||||
Opcode::StoreAttr => (2, 2, 0),
|
||||
Opcode::Copy => (argument, 0, 1),
|
||||
Opcode::Swap => (argument, 0, 0),
|
||||
|
||||
@@ -123,6 +123,7 @@ impl ObjectStore {
|
||||
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)
|
||||
@@ -130,12 +131,12 @@ impl ObjectStore {
|
||||
Fact::Unknown
|
||||
}),
|
||||
Opcode::LoadGlobal => {
|
||||
let key = Value::Text(code.layout().names[argument].clone());
|
||||
let mut value = self.dict_get(function.globals(), &key)?;
|
||||
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(builtins, &key)?;
|
||||
value = self.dict_get_name(builtins, name)?;
|
||||
}
|
||||
stack.push(value.map(Fact::value).unwrap_or(Fact::Unknown));
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ use std::fmt;
|
||||
/// retained for embedders; defining it here avoids a dependency on the VM.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum VmError {
|
||||
AwaitFailed(String),
|
||||
AwaitCancelled,
|
||||
ForeignHandle,
|
||||
InvalidHandle,
|
||||
IdentityExhausted,
|
||||
@@ -49,6 +51,8 @@ pub enum VmError {
|
||||
impl fmt::Display for VmError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
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::InvalidHandle => f.write_str("handle does not identify a live object"),
|
||||
Self::IdentityExhausted => f.write_str("process-local VM identities exhausted"),
|
||||
|
||||
+132
-9
@@ -41,6 +41,8 @@ struct ReadOnly {
|
||||
protected: BTreeSet<ObjectRef>,
|
||||
reads: Mutex<BTreeSet<ObjectRef>>,
|
||||
regions: Mutex<Vec<BTreeMap<ObjectRef, bool>>>,
|
||||
dependencies: Mutex<BTreeMap<ReadKey, ReadDependency>>,
|
||||
dependency_regions: Mutex<Vec<BTreeMap<ReadKey, ReadDependency>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -63,6 +65,49 @@ pub struct ReadAccess {
|
||||
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> {
|
||||
heap: &'a Heap,
|
||||
thread: ThreadId,
|
||||
@@ -128,23 +173,34 @@ impl Heap {
|
||||
protected,
|
||||
reads: Mutex::new(BTreeSet::new()),
|
||||
regions: Mutex::new(Vec::new()),
|
||||
dependencies: Mutex::new(BTreeMap::new()),
|
||||
dependency_regions: Mutex::new(Vec::new()),
|
||||
});
|
||||
self.read_only.len()
|
||||
}
|
||||
|
||||
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() {
|
||||
return Err(VmError::InvalidArgument("read-only scope nesting mismatch"));
|
||||
}
|
||||
Ok(self
|
||||
.read_only
|
||||
.pop()
|
||||
.unwrap()
|
||||
let scope = self.read_only.pop().unwrap();
|
||||
Ok(ReadSet {
|
||||
objects: scope
|
||||
.reads
|
||||
.into_inner()
|
||||
.expect("read tracker poisoned")
|
||||
.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> {
|
||||
@@ -153,6 +209,11 @@ impl Heap {
|
||||
))?;
|
||||
let mut regions = scope.regions.lock().expect("read tracker poisoned");
|
||||
regions.push(BTreeMap::new());
|
||||
scope
|
||||
.dependency_regions
|
||||
.lock()
|
||||
.expect("read tracker poisoned")
|
||||
.push(BTreeMap::new());
|
||||
Ok(regions.len())
|
||||
}
|
||||
|
||||
@@ -168,14 +229,22 @@ impl Heap {
|
||||
&self,
|
||||
token: usize,
|
||||
) -> 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(
|
||||
"read tracking requires a read-only scope",
|
||||
))?;
|
||||
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"));
|
||||
}
|
||||
Ok(regions
|
||||
Ok(TrackedReads {
|
||||
accesses: regions
|
||||
.pop()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
@@ -183,7 +252,9 @@ impl Heap {
|
||||
object,
|
||||
content_read,
|
||||
})
|
||||
.collect())
|
||||
.collect(),
|
||||
dependencies: precise.pop().unwrap().into_values().collect(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn is_read_only(&self) -> bool {
|
||||
@@ -209,7 +280,45 @@ impl Heap {
|
||||
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() {
|
||||
return;
|
||||
}
|
||||
@@ -220,6 +329,20 @@ impl Heap {
|
||||
.contains_key(&std::thread::current().id());
|
||||
for scope in &self.read_only {
|
||||
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
|
||||
.reads
|
||||
.lock()
|
||||
|
||||
@@ -35,6 +35,9 @@ pub enum Opcode {
|
||||
BuildMap = 27,
|
||||
MakeFunction = 28,
|
||||
BuildClass = 29,
|
||||
Await = 30,
|
||||
BinaryArithmetic = 31,
|
||||
UnaryNegate = 32,
|
||||
ExtendedArg = 255,
|
||||
}
|
||||
|
||||
@@ -71,6 +74,9 @@ impl Opcode {
|
||||
27 => Self::BuildMap,
|
||||
28 => Self::MakeFunction,
|
||||
29 => Self::BuildClass,
|
||||
30 => Self::Await,
|
||||
31 => Self::BinaryArithmetic,
|
||||
32 => Self::UnaryNegate,
|
||||
255 => Self::ExtendedArg,
|
||||
_ => return None,
|
||||
})
|
||||
@@ -85,6 +91,8 @@ impl Opcode {
|
||||
| Self::ReturnValue
|
||||
| Self::ToBool
|
||||
| Self::UnaryNot
|
||||
| Self::Await
|
||||
| Self::UnaryNegate
|
||||
| Self::BuildClass
|
||||
)
|
||||
}
|
||||
|
||||
@@ -33,18 +33,19 @@ pub use builtins::{
|
||||
pub use changes::{ChangeBatch, ChangeKey, ObjectChange};
|
||||
pub use code::{CodeLayout, EndorCode, EndorCodeObject, SourceSignature};
|
||||
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 native::{
|
||||
CallArity, CallEffect, CallSignature, CallType, ContextNativeFunction, NativeCallable,
|
||||
NativeContext, NativeResult,
|
||||
};
|
||||
pub use number::{
|
||||
EndorDecimal, EndorInteger, EndorNumber, EndorNumberObject, EndorRational, EndorReal,
|
||||
ArithmeticOp, EndorDecimal, EndorInteger, EndorNumber, EndorNumberObject, EndorRational,
|
||||
EndorReal,
|
||||
};
|
||||
pub use object::{
|
||||
CompareOp, EndorNativeCall, EndorNativeFunctionObject, EndorObject, NativeCall, NativeFunction,
|
||||
ObjectHeader, ObjectPayload, Record, Value,
|
||||
AwaitPoll, CompareOp, EndorNativeCall, EndorNativeFunctionObject, EndorObject, NativeCall,
|
||||
NativeFunction, ObjectHeader, ObjectPayload, Record, Value,
|
||||
};
|
||||
pub use store::ObjectStore;
|
||||
pub use types::{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! 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::fmt;
|
||||
@@ -39,7 +40,16 @@ impl EndorInteger {
|
||||
}
|
||||
|
||||
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"))
|
||||
}
|
||||
|
||||
fn compare(&self, other: &dyn ObjectPayload, _op: CompareOp) -> Result<Option<bool>, VmError> {
|
||||
if other.as_any().is::<Self>() {
|
||||
fn compare(&self, other: &dyn ObjectPayload, op: CompareOp) -> Result<Option<bool>, VmError> {
|
||||
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 self.value.numeric_compare(&other.value, op).map(Some);
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
@@ -318,3 +331,222 @@ fn split_sign(text: &str) -> (bool, &str) {
|
||||
fn is_digits(text: &str) -> bool {
|
||||
!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.
|
||||
///
|
||||
/// 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.
|
||||
/// Equal payloads must provide compatible hashes, or reject hashing explicitly.
|
||||
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.
|
||||
fn is_function(&self) -> bool {
|
||||
false
|
||||
|
||||
@@ -43,6 +43,29 @@ impl ObjectStore {
|
||||
pub fn end_read_only(&mut self, scope: usize) -> Result<Vec<ObjectRef>, VmError> {
|
||||
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
|
||||
/// LIFO order and do not clear the enclosing call's read dependencies.
|
||||
/// Handles returned by end_read_tracking are borrowed, never owning roots.
|
||||
@@ -214,8 +237,10 @@ impl ObjectStore {
|
||||
}
|
||||
|
||||
pub fn object(&self, reference: ObjectRef) -> Result<&EndorObject, VmError> {
|
||||
self.heap.record_read(reference);
|
||||
self.heap.get(reference)
|
||||
let object = 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
|
||||
|
||||
@@ -20,9 +20,9 @@ impl ObjectStore {
|
||||
"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() {
|
||||
self.payload::<EndorDictObject>(builtins, "dict")?;
|
||||
self.dictionary_kind(builtins)?;
|
||||
}
|
||||
for cell in function.closure() {
|
||||
self.payload::<EndorCellObject>(*cell, "cell")?;
|
||||
@@ -67,10 +67,10 @@ impl ObjectStore {
|
||||
defaults: Vec<Value>,
|
||||
closure: Vec<ObjectRef>,
|
||||
) -> Result<ObjectRef, VmError> {
|
||||
self.payload::<EndorDictObject>(globals, "dict")?;
|
||||
let builtins = match self.dict_get(globals, &Value::Text("__builtins__".into()))? {
|
||||
self.dictionary_kind(globals)?;
|
||||
let builtins = match self.dict_get_name(globals, "__builtins__")? {
|
||||
Some(Value::Object(reference)) => {
|
||||
self.payload::<EndorDictObject>(reference, "dict")?;
|
||||
self.dictionary_kind(reference)?;
|
||||
Some(reference)
|
||||
}
|
||||
None => None,
|
||||
@@ -101,6 +101,35 @@ impl ObjectStore {
|
||||
.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> {
|
||||
let dict = self.payload::<EndorDictObject>(dictionary, "dict")?;
|
||||
self.key_hash(key)?;
|
||||
|
||||
@@ -1266,9 +1266,9 @@ impl ObjectStore {
|
||||
return Ok(primitive);
|
||||
}
|
||||
let value = self
|
||||
.dict_get(data.globals(), &Value::Text(name.into()))?
|
||||
.dict_get_name(data.globals(), name)?
|
||||
.or(match data.builtins() {
|
||||
Some(builtins) => self.dict_get(builtins, &Value::Text(name.into()))?,
|
||||
Some(builtins) => self.dict_get_name(builtins, name)?,
|
||||
None => None,
|
||||
});
|
||||
if let Some(Value::Object(definition)) = value {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
+7
-1
@@ -12,7 +12,7 @@
|
||||
| 词法、语法、裸赋值和诊断 | [Frontend](runtime/frontend.md) | frontend/src/lexer.peg/grammar.peg |
|
||||
| ASDL、AST 与编辑器接口 | [AST](runtime/ast.md) | frontend/Endor.asdl |
|
||||
| 名称分析、编译和装载 | [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 |
|
||||
| UTF-8 与地区 | [文本编码](runtime/text-encoding.md) | frontend、objects |
|
||||
| 嵌入及宿主入口 | [使用引擎](operations/README.md) | 独立宿主仓库 |
|
||||
@@ -22,3 +22,9 @@
|
||||
| 后续开发交接 | [交接](contributing/handoff.md) | 实时源码和 Git 状态 |
|
||||
|
||||
目录入口:[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)。
|
||||
|
||||
@@ -75,7 +75,9 @@ fn main() -> Result<(), endor_core::VmError> {
|
||||
|
||||
`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 边界显式窄化的接口,不更改原数。
|
||||
|
||||
这里的“各类数字”指上述五种可表示形式,不包括符号表达式、无限展开的精确 π、带单位数量、任意位宽二进制浮点等全部数学对象;这些可在未来扩展表示协议。
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ fn main() -> Result<(), endor_core::VmError> {
|
||||
|
||||
运行错误仍返回 `VmError`。`last_traceback()` 另提供从外到内的函数名、文件、代码首行和出错指令偏移;它保存独立元数据,不保活已经清理的帧。首行是代码声明的 first_line,不是由行号表推导出的实际错误源码行。成功的新一次调用会清空旧记录。尚无语言异常对象传播、raise/try/except 或完整源码行号表。
|
||||
|
||||
暂未实现:源码闭包编译、关键字/可变参数、生成器/协程、迭代器专用指令、数值算术、自动方法绑定、指令特化和 JIT。
|
||||
暂未实现:源码闭包编译、关键字/可变参数、生成器/协程、迭代器专用指令、复数算术、自动方法绑定、指令特化和 JIT。
|
||||
|
||||
## 验证
|
||||
|
||||
@@ -186,3 +186,28 @@ fn main() -> Result<(), endor_core::VmError> {
|
||||
BuildClass(编号 29,无参数)消费四个栈值 `[name, baseType, fieldsDict, methodsDict]`,产生一个受管类对象;验证器按 4 入 1 出检查栈。字段字典按声明顺序保存字段名与注解名,方法字典保存 MakeFunction 创建的函数。VM 先验证基类、字段与方法关系,再创建完整类型;输入根在成功前保持在执行栈上,失败由帧统一清理。
|
||||
|
||||
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 |
|
||||
| 数字字面量前的一个或多个负号 | 折叠符号为精确常量;不是任意表达式的一元算术 |
|
||||
| 一元负号 | 字面量折叠保留拼写;其他表达式发出 UnaryNegate |
|
||||
| 四则运算、比较 | BinaryArithmetic / CompareOp;实数精确语义见 builtins |
|
||||
| if / else、while | 函数局部初始化按分支合流,循环可执行零次;相对跳转经过 EXTENDED_ARG 布局解析 |
|
||||
| 局部读取/let/赋值 | LoadFast/StoreFast;参数和局部名称在 CodeLayout 中 |
|
||||
| 全局读取/赋值 | LoadGlobal/StoreGlobal,使用该代码对象的 names 表 |
|
||||
| 函数声明 | 子代码模板 → LoadConst(code) → MakeFunction(0, 0) → StoreGlobal |
|
||||
@@ -38,7 +40,7 @@ CompileOptions.globals 显式声明宿主会提供的 globals/builtins 名称
|
||||
|
||||
源码 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)。
|
||||
|
||||
|
||||
Await表达式参与普通名称/初始化分析,先编译operand,再发出无参数Await opcode。该链路保存实际结果的栈位置,不通过回放函数实现等待;没有新增async声明或静态awaitable类型检查。语法与运行上下文见frontend及bytecode主题。
|
||||
|
||||
控制流中的局部变量仍为函数作用域。if 两边都能到达后续语句时,仅保留两边都已初始化的名字;一边 return 时采用仍可到达路径的初始化集合。while 中新初始化的名字不能被当作循环后一定存在。循环共享正常调用、只读调用或 coroutine 的累计指令预算,不建立第二套执行器。
|
||||
|
||||
@@ -68,7 +68,7 @@ parameters 和调用参数列表使用逗号分隔,不接受尾随逗号。函
|
||||
|
||||
符号字面量、名称与成员分别保留为独立 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)。
|
||||
|
||||
当前 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 节点,宿主不改写输入。
|
||||
|
||||
|
||||
源码类实现子集采用 `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 写在块内。
|
||||
|
||||
@@ -113,3 +113,8 @@ Delegated 在替换兼容中视为未知整体效果,与源码未知效果一
|
||||
|
||||
|
||||
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)。
|
||||
|
||||
验证范围与执行约定见 [验证](../operations/validation.md)。
|
||||
|
||||
|
||||
ObjectPayload::poll_await是通用非阻塞等待观察协议,默认UnsupportedOperation。AwaitPoll::Ready的对象结果是借用,调用方须在放弃awaitable前retain;协议不允许内部阻塞/IO/修改对象。完整指令和恢复契约见bytecode.md。
|
||||
|
||||
Reference in New Issue
Block a user