Implement validated wordcode execution with shared code objects and frames
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m27s

This commit is contained in:
2026-10-18 20:41:56 +08:00
parent 50e8fcb5e7
commit 6fa95b64b0
18 changed files with 2327 additions and 152 deletions
+56 -35
View File
@@ -1,7 +1,7 @@
use aster_core::{
CompareOp, EndorBaseExceptionObject, EndorCode, EndorDictObject, EndorFunctionObject,
EndorInteger, EndorNativeFunctionObject, EndorNumber, EndorNumberObject, EndorReal,
EndorStringObject, EndorTracebackEntry, Value, Vm, VmError,
CodeLayout, CompareOp, EndorBaseExceptionObject, EndorCode, EndorDictObject,
EndorFunctionObject, EndorInteger, EndorNativeFunctionObject, EndorNumber, EndorNumberObject,
EndorReal, EndorStringObject, EndorTracebackEntry, Instruction, Opcode, Value, Vm, VmError,
};
fn text(value: &str) -> Value {
@@ -208,31 +208,35 @@ fn functions_retain_globals_constants_defaults_and_shared_closure_cells() {
let globals = vm.new_dict().unwrap();
let constant = vm.new_string("constant").unwrap();
let cell = vm.new_cell(Some(text("first"))).unwrap();
let code = EndorCode::new(
vec![0],
let code = EndorCode::from_instructions(
&[
Instruction::new(Opcode::LoadDeref, 1),
Instruction::simple(Opcode::ReturnValue),
],
vec![Value::Object(constant)],
vec!["argument".into()],
vec!["captured".into()],
CodeLayout {
parameters: vec!["argument".into()],
locals: vec!["argument".into()],
free_variables: vec!["captured".into()],
..CodeLayout::default()
},
)
.unwrap();
let code = vm.new_code(code).unwrap();
let function = vm
.allocate(
EndorFunctionObject::new(
"read",
code.clone(),
globals,
vec![Value::Object(constant)],
vec![cell],
)
.unwrap(),
.new_function(
"read",
code,
globals,
vec![Value::Object(constant)],
vec![cell],
)
.unwrap();
let second = vm
.allocate(
EndorFunctionObject::new("read_again", code, globals, vec![], vec![cell]).unwrap(),
)
.new_function("read_again", code, globals, vec![], vec![cell])
.unwrap();
assert_eq!(vm.ref_count(constant).unwrap(), 4);
assert_eq!(vm.ref_count(constant).unwrap(), 3);
vm.release(code).unwrap();
vm.release(constant).unwrap();
vm.release(cell).unwrap();
vm.cell_set(cell, text("updated")).unwrap();
@@ -242,32 +246,49 @@ fn functions_retain_globals_constants_defaults_and_shared_closure_cells() {
.unwrap();
assert_eq!(vm.cell_get(f.closure()[0]).unwrap(), text("updated"));
}
assert_eq!(
vm.call(function, &[]),
Err(VmError::UnsupportedOperation("language function execution"))
);
assert_eq!(vm.call(function, &[]).unwrap(), text("updated"));
vm.dict_set(globals, text("read"), Value::Object(function))
.unwrap();
vm.release(globals).unwrap();
vm.release(function).unwrap();
vm.release(second).unwrap();
assert_eq!(vm.collect_cycles(), 4);
assert_eq!(vm.collect_cycles(), 5);
}
#[test]
fn malformed_function_environments_are_rejected_without_retaining_any_edges() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_object().unwrap();
let code = EndorCode::new(vec![], vec![], vec![], vec![]).unwrap();
let function = EndorFunctionObject::new("bad", code, globals, vec![], vec![]).unwrap();
let instructions = [
Instruction::new(Opcode::LoadConst, 0),
Instruction::simple(Opcode::ReturnValue),
];
let code = EndorCode::from_instructions(&instructions, vec![Value::Nil], CodeLayout::default())
.unwrap();
let code = vm.new_code(code).unwrap();
assert_eq!(
vm.allocate(function),
vm.new_function("bad", code, globals, vec![], vec![]),
Err(VmError::ObjectKindMismatch("dict"))
);
assert_eq!(vm.ref_count(globals).unwrap(), 1);
assert_eq!(vm.ref_count(code).unwrap(), 1);
assert!(EndorCode::new(vec![], vec![], vec!["x".into(), "x".into()], vec![]).is_err());
let code = EndorCode::new(vec![], vec![], vec![], vec!["x".into()]).unwrap();
assert!(EndorFunctionObject::new("bad", code, globals, vec![], vec![]).is_err());
let code = EndorCode::from_instructions(
&instructions,
vec![Value::Nil],
CodeLayout {
free_variables: vec!["x".into()],
..CodeLayout::default()
},
)
.unwrap();
let code = vm.new_code(code).unwrap();
let globals = vm.new_dict().unwrap();
assert!(
vm.new_function("bad", code, globals, vec![], vec![])
.is_err()
);
assert_eq!(vm.ref_count(code).unwrap(), 1);
}
#[test]
@@ -320,7 +341,7 @@ fn echo(arguments: &[Value]) -> Result<Value, VmError> {
}
#[test]
fn native_functions_pass_lossless_numbers_and_borrowed_objects() {
fn native_functions_pass_lossless_numbers_and_return_owned_objects() {
let mut vm = Vm::new().unwrap();
let function = vm
.allocate(EndorNativeFunctionObject::new("echo", echo))
@@ -331,10 +352,10 @@ fn native_functions_pass_lossless_numbers_and_borrowed_objects() {
number
);
let dict = vm.new_dict().unwrap();
assert_eq!(
vm.call(function, &[Value::Object(dict)]).unwrap(),
Value::Object(dict)
);
let returned = vm.call(function, &[Value::Object(dict)]).unwrap();
assert_eq!(returned, Value::Object(dict));
assert_eq!(vm.ref_count(dict).unwrap(), 2);
vm.release_value(returned).unwrap();
assert_eq!(vm.ref_count(dict).unwrap(), 1);
assert_eq!(
vm.payload::<EndorDictObject>(dict, "dict").unwrap().len(),