Add bytecode function creation with defaults and shared closures
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 6m28s

This commit is contained in:
2026-10-19 02:29:39 +08:00
parent e52d2e1e4b
commit e24909a56e
14 changed files with 610 additions and 21 deletions
+505 -2
View File
@@ -3,8 +3,9 @@ use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use endor_core::{
CodeLayout, CompareOp, EndorCode, EndorCodeObject, EndorNativeFunctionObject, ExecutionLimits,
Instruction, ObjectPayload, ObjectRef, Opcode, Value, Vm, VmError,
CodeLayout, CompareOp, EndorCode, EndorCodeObject, EndorFunctionObject,
EndorNativeFunctionObject, ExecutionLimits, Instruction, ObjectPayload, ObjectRef, Opcode,
Value, Vm, VmError,
};
fn op(opcode: Opcode, argument: u32) -> Instruction {
@@ -759,3 +760,505 @@ fn implicit_receiver_slot_is_distinct_from_the_null_call_marker() {
// Nil is an actual implicit argument here, not the internal NULL marker.
assert_eq!(vm.execute(code, globals).unwrap(), Value::Nil);
}
#[test]
fn function_creation_operands_round_trip_and_require_all_inputs() {
assert_eq!(
Instruction::encode_all(&[Instruction::make_function(1, 1)]).unwrap(),
vec![255, 1, 255, 0, 28, 1]
);
assert_eq!(
Instruction::encode_all(&[Instruction::make_function(u16::MAX, u16::MAX)]).unwrap(),
vec![255, 255, 255, 255, 255, 255, 28, 255]
);
let code = EndorCode::from_instructions(
&[
op(Opcode::LoadConst, 0),
op(Opcode::LoadConst, 0),
op(Opcode::LoadConst, 0),
Instruction::make_function(1, 1),
simple(Opcode::ReturnValue),
],
vec![Value::Nil],
CodeLayout::default(),
)
.unwrap();
assert_eq!(
code.instructions()[3].instruction,
Instruction::make_function(1, 1)
);
assert_eq!(code.instructions()[3].next_offset, 6);
assert_eq!(code.stack_size(), 3);
for (defaults, closure) in [(0, 0), (1, 0), (0, 1), (1, 1), (u16::MAX, u16::MAX)] {
assert!(matches!(
EndorCode::from_instructions(
&[
Instruction::make_function(defaults, closure),
simple(Opcode::ReturnValue)
],
vec![],
CodeLayout::default(),
),
Err(VmError::InvalidBytecode { .. })
));
}
for creation in [
Instruction::make_function(1, 0),
Instruction::make_function(0, 1),
] {
assert!(
EndorCode::from_instructions(
&[
op(Opcode::LoadConst, 0),
creation,
simple(Opcode::ReturnValue)
],
vec![Value::Nil],
CodeLayout::default(),
)
.is_err()
);
}
}
#[test]
fn bytecode_defines_global_functions_and_calls_them_with_defaults() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let inner = EndorCode::from_instructions(
&[op(Opcode::LoadFast, 0), simple(Opcode::ReturnValue)],
vec![],
CodeLayout {
parameters: vec!["value".into()],
locals: vec!["value".into()],
..CodeLayout::default()
},
)
.unwrap()
.with_location("identity", "functions.endor", 2);
let inner = vm.new_code(inner).unwrap();
let module = make_code(
&mut vm,
&[
op(Opcode::LoadConst, 0),
op(Opcode::LoadConst, 1),
Instruction::make_function(1, 0),
op(Opcode::StoreGlobal, 0),
op(Opcode::LoadGlobal, 0),
simple(Opcode::PushNull),
op(Opcode::Call, 0),
simple(Opcode::ReturnValue),
],
vec![text("default"), Value::Object(inner)],
CodeLayout {
names: vec!["identity".into()],
..CodeLayout::default()
},
);
vm.release(inner).unwrap();
assert_eq!(vm.execute(module, globals).unwrap(), text("default"));
let Some(Value::Object(first)) = vm.dict_get(globals, &text("identity")).unwrap() else {
panic!("expected a function");
};
vm.retain(first).unwrap();
let data = vm
.payload::<EndorFunctionObject>(first, "function")
.unwrap();
assert_eq!(data.name(), "identity");
assert_eq!(data.code(), inner);
assert_eq!(data.globals(), globals);
assert_eq!(
vm.call(first, &[text("explicit")]).unwrap(),
text("explicit")
);
assert_eq!(vm.execute(module, globals).unwrap(), text("default"));
let Some(Value::Object(second)) = vm.dict_get(globals, &text("identity")).unwrap() else {
panic!("expected a second function");
};
assert_ne!(first, second);
assert_eq!(
vm.payload::<EndorFunctionObject>(second, "function")
.unwrap()
.code(),
inner
);
vm.release(first).unwrap();
vm.release(module).unwrap();
vm.release(globals).unwrap();
// The global function, its code and its globals form a collectible cycle.
assert_eq!(vm.collect_cycles(), 3);
assert_eq!(vm.object_count(), 0);
}
#[test]
fn bytecode_function_defaults_keep_temporary_objects_alive_after_factory_returns() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let inner = make_code(
&mut vm,
&[op(Opcode::LoadFast, 1), simple(Opcode::ReturnValue)],
vec![],
CodeLayout {
parameters: vec!["required".into(), "defaulted".into(), "tag".into()],
locals: vec!["required".into(), "defaulted".into(), "tag".into()],
..CodeLayout::default()
},
);
let factory = make_code(
&mut vm,
&[
op(Opcode::BuildMap, 0),
op(Opcode::LoadConst, 1),
op(Opcode::LoadConst, 0),
Instruction::make_function(2, 0),
simple(Opcode::ReturnValue),
],
vec![Value::Object(inner), text("tag")],
CodeLayout::default(),
);
vm.release(inner).unwrap();
let Value::Object(function) = vm.execute(factory, globals).unwrap() else {
panic!("expected a function");
};
vm.release(factory).unwrap();
vm.release(globals).unwrap();
assert!(matches!(
vm.call(function, &[]),
Err(VmError::ArgumentCount { required: 1, .. })
));
let first = vm.call(function, &[Value::Nil]).unwrap();
let second = vm.call(function, &[Value::Nil]).unwrap();
assert_eq!(first, second); // Defaults are evaluated once per creation.
let Value::Object(default) = first else {
panic!("expected a default map");
};
assert_eq!(vm.ref_count(default).unwrap(), 3); // Function and two results.
vm.release(function).unwrap();
vm.release_value(first).unwrap();
assert_eq!(vm.ref_count(default).unwrap(), 1);
vm.release_value(second).unwrap();
assert_eq!(vm.object_count(), 0);
}
#[test]
fn bytecode_nested_functions_share_cells_and_bind_defaults_after_outer_returns() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let read = make_code(
&mut vm,
&[op(Opcode::LoadDeref, 0), simple(Opcode::ReturnValue)],
vec![],
CodeLayout {
free_variables: vec!["x".into(), "sentinel".into()],
..CodeLayout::default()
},
);
let write = make_code(
&mut vm,
&[
op(Opcode::LoadFast, 0),
op(Opcode::StoreDeref, 1),
op(Opcode::LoadDeref, 1),
simple(Opcode::ReturnValue),
],
vec![],
CodeLayout {
parameters: vec!["value".into()],
locals: vec!["value".into()],
free_variables: vec!["x".into(), "sentinel".into()],
..CodeLayout::default()
},
);
let outer = make_code(
&mut vm,
&[
op(Opcode::LoadConst, 0),
op(Opcode::LoadClosure, 0),
op(Opcode::LoadClosure, 1),
op(Opcode::LoadConst, 1),
Instruction::make_function(0, 2),
op(Opcode::LoadConst, 2),
op(Opcode::LoadConst, 3),
op(Opcode::LoadClosure, 0),
op(Opcode::LoadClosure, 1),
op(Opcode::LoadConst, 4),
Instruction::make_function(1, 2),
op(Opcode::BuildMap, 2),
simple(Opcode::ReturnValue),
],
vec![
text("read"),
Value::Object(read),
text("write"),
text("default"),
Value::Object(write),
],
CodeLayout {
parameters: vec!["x".into(), "sentinel".into()],
locals: vec!["x".into(), "sentinel".into()],
cell_variables: vec!["x".into(), "sentinel".into()],
..CodeLayout::default()
},
);
let outer_function = vm
.new_function("outer", outer, globals, vec![], vec![])
.unwrap();
let Value::Object(pair) = vm
.call(outer_function, &[text("first"), text("sentinel")])
.unwrap()
else {
panic!("expected a function pair");
};
for reference in [read, write, outer, outer_function, globals] {
vm.release(reference).unwrap();
}
let Some(Value::Object(read)) = vm.dict_get(pair, &text("read")).unwrap() else {
panic!("expected read");
};
let Some(Value::Object(write)) = vm.dict_get(pair, &text("write")).unwrap() else {
panic!("expected write");
};
assert_eq!(
vm.payload::<EndorFunctionObject>(read, "function")
.unwrap()
.closure(),
vm.payload::<EndorFunctionObject>(write, "function")
.unwrap()
.closure(),
);
assert_eq!(vm.collect_cycles(), 0);
assert_eq!(vm.call(read, &[]).unwrap(), text("first"));
assert_eq!(vm.call(write, &[text("updated")]).unwrap(), text("updated"));
assert_eq!(vm.call(read, &[]).unwrap(), text("updated"));
assert_eq!(vm.call(write, &[]).unwrap(), text("default"));
assert_eq!(vm.call(read, &[]).unwrap(), text("default"));
vm.release(pair).unwrap();
assert_eq!(vm.object_count(), 0);
}
#[test]
fn bytecode_function_creation_rejects_bad_inputs_without_leaking_roots() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let plain = return_constant(&mut vm, Value::Nil);
let captured = make_code(
&mut vm,
&[op(Opcode::LoadDeref, 0), simple(Opcode::ReturnValue)],
vec![],
CodeLayout {
free_variables: vec!["x".into()],
..CodeLayout::default()
},
);
let cell = vm.new_cell(None).unwrap();
let mismatch = VmError::InvalidArgument("function defaults or closure do not match code");
let cases = [
(
vec![op(Opcode::LoadConst, 0), Instruction::make_function(0, 0)],
vec![Value::Nil],
VmError::ObjectKindMismatch("code"),
),
(
vec![op(Opcode::BuildMap, 0), Instruction::make_function(0, 0)],
vec![],
VmError::ObjectKindMismatch("code"),
),
(
vec![simple(Opcode::PushNull), Instruction::make_function(0, 0)],
vec![],
invalid_null(),
),
(
vec![
op(Opcode::BuildMap, 0),
op(Opcode::LoadConst, 0),
Instruction::make_function(1, 0),
],
vec![Value::Object(plain)],
mismatch.clone(),
),
(
vec![op(Opcode::LoadConst, 0), Instruction::make_function(0, 0)],
vec![Value::Object(captured)],
mismatch.clone(),
),
(
vec![
op(Opcode::LoadConst, 0),
op(Opcode::LoadConst, 1),
Instruction::make_function(0, 1),
],
vec![Value::Object(cell), Value::Object(plain)],
mismatch,
),
(
vec![
op(Opcode::LoadConst, 0),
op(Opcode::LoadConst, 1),
Instruction::make_function(0, 1),
],
vec![Value::Nil, Value::Object(captured)],
VmError::ObjectKindMismatch("cell"),
),
(
vec![
op(Opcode::BuildMap, 0),
op(Opcode::LoadConst, 0),
Instruction::make_function(0, 1),
],
vec![Value::Object(captured)],
VmError::ObjectKindMismatch("cell"),
),
(
vec![
simple(Opcode::PushNull),
op(Opcode::LoadConst, 0),
Instruction::make_function(0, 1),
],
vec![Value::Object(captured)],
invalid_null(),
),
(
vec![
simple(Opcode::PushNull),
op(Opcode::LoadConst, 0),
Instruction::make_function(1, 0),
],
vec![Value::Object(plain)],
invalid_null(),
),
];
for (mut instructions, constants, expected) in cases {
instructions.push(simple(Opcode::ReturnValue));
let code = make_code(&mut vm, &instructions, constants, CodeLayout::default());
let count = vm.object_count();
let roots =
[plain, captured, cell, globals].map(|reference| vm.ref_count(reference).unwrap());
let error = vm.execute(code, globals).unwrap_err();
if matches!(expected, VmError::InvalidBytecode { .. }) {
assert!(matches!(
error,
VmError::InvalidBytecode {
reason: "NULL call marker used as a value",
..
}
));
} else {
assert_eq!(error, expected);
}
assert_eq!(vm.object_count(), count);
assert_eq!(
[plain, captured, cell, globals].map(|reference| vm.ref_count(reference).unwrap()),
roots
);
vm.release(code).unwrap();
}
for reference in [plain, captured, cell, globals] {
vm.release(reference).unwrap();
}
assert_eq!(vm.object_count(), 0);
}
fn invalid_null() -> VmError {
VmError::InvalidBytecode {
offset: 0,
reason: "NULL call marker used as a value",
}
}
#[test]
fn bytecode_functions_capture_builtins_at_creation_and_share_live_globals() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let original = vm.new_dict().unwrap();
let replacement = vm.new_dict().unwrap();
vm.dict_set(original, text("answer"), text("original"))
.unwrap();
vm.dict_set(replacement, text("answer"), text("replacement"))
.unwrap();
vm.dict_set(globals, text("__builtins__"), Value::Object(original))
.unwrap();
let inner = make_code(
&mut vm,
&[op(Opcode::LoadGlobal, 0), simple(Opcode::ReturnValue)],
vec![],
CodeLayout {
names: vec!["answer".into()],
..CodeLayout::default()
},
);
let factory = make_code(
&mut vm,
&[
op(Opcode::LoadConst, 0),
Instruction::make_function(0, 0),
simple(Opcode::ReturnValue),
],
vec![Value::Object(inner)],
CodeLayout::default(),
);
let Value::Object(first) = vm.execute(factory, globals).unwrap() else {
panic!("expected first function");
};
vm.dict_set(globals, text("__builtins__"), Value::Object(replacement))
.unwrap();
let Value::Object(second) = vm.execute(factory, globals).unwrap() else {
panic!("expected second function");
};
for reference in [original, replacement, factory, inner, globals] {
vm.release(reference).unwrap();
}
assert_eq!(vm.call(first, &[]).unwrap(), text("original"));
assert_eq!(vm.call(second, &[]).unwrap(), text("replacement"));
vm.dict_set(globals, text("answer"), text("global"))
.unwrap();
assert_eq!(vm.call(first, &[]).unwrap(), text("global"));
assert_eq!(vm.call(second, &[]).unwrap(), text("global"));
vm.release(first).unwrap();
vm.release(second).unwrap();
assert_eq!(vm.object_count(), 0);
}
#[test]
fn later_execution_failure_releases_a_new_function_and_its_temporary_default() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let inner = make_code(
&mut vm,
&[op(Opcode::LoadFast, 0), simple(Opcode::ReturnValue)],
vec![],
CodeLayout {
parameters: vec!["value".into()],
locals: vec!["value".into()],
..CodeLayout::default()
},
);
let fail = vm
.allocate(EndorNativeFunctionObject::new("fail", fail))
.unwrap();
let factory = make_code(
&mut vm,
&[
op(Opcode::LoadConst, 0),
simple(Opcode::PushNull),
op(Opcode::BuildMap, 0),
op(Opcode::LoadConst, 1),
Instruction::make_function(1, 0),
op(Opcode::Call, 1),
simple(Opcode::ReturnValue),
],
vec![Value::Object(fail), Value::Object(inner)],
CodeLayout::default(),
);
vm.release(fail).unwrap();
vm.release(inner).unwrap();
let count = vm.object_count();
assert_eq!(
vm.execute(factory, globals),
Err(VmError::InvalidArgument("host failure"))
);
assert_eq!(vm.object_count(), count);
vm.release(factory).unwrap();
vm.release(globals).unwrap();
assert_eq!(vm.object_count(), 0);
}