Add bytecode function creation with defaults and shared closures
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 6m28s
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 6m28s
This commit is contained in:
@@ -11,10 +11,10 @@
|
||||
| [builtins.rs](builtins.rs) | String、Dict、Cell、Function、BaseException 载荷与 trace 边 |
|
||||
| [number.rs](number.rs) | EndorNumber、Integer/Decimal/Rational/BinaryFloat/Complex 表示及装箱载荷 |
|
||||
| [vm_builtins.rs](vm_builtins.rs) | 内置载荷校验、代码/函数工厂、Dict/Cell 操作、键语义和可变引用更新 |
|
||||
| [instruction.rs](instruction.rs) | Opcode、逻辑指令、EXTENDED_ARG 编解码、指令位置与比较参数 |
|
||||
| [instruction.rs](instruction.rs) | Opcode、逻辑指令、EXTENDED_ARG 编解码、MakeFunction 计数构造、指令位置与比较参数 |
|
||||
| [code.rs](code.rs) | CodeLayout、EndorCode、共享 EndorCodeObject、操作数/控制流/栈校验 |
|
||||
| [frame.rs](frame.rs) | EndorFrame、栈条目、执行位置元数据、ExecutionLimits |
|
||||
| [eval.rs](eval.rs) | execute/call、参数绑定、帧切换、取指分派、结果根转移与失败清理 |
|
||||
| [eval.rs](eval.rs) | execute/call、参数绑定、帧切换、取指分派、字节码创建函数、结果根转移与失败清理 |
|
||||
|
||||
## 常见修改入口
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::collections::{BTreeSet, VecDeque};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::builtins::trace_value;
|
||||
use crate::instruction::{compare_operation, decode, invalid};
|
||||
use crate::instruction::{compare_operation, decode, function_counts, invalid};
|
||||
use crate::{DecodedInstruction, Instruction, ObjectPayload, ObjectRef, Opcode, Value, VmError};
|
||||
|
||||
/// Local slots start with positional parameters. Captured locals occupy their
|
||||
@@ -330,6 +330,11 @@ fn verify(
|
||||
.ok_or_else(|| invalid(instruction.offset, "map operand overflow"))?;
|
||||
(consumed, consumed, 1)
|
||||
}
|
||||
Opcode::MakeFunction => {
|
||||
let (defaults, closure) = function_counts(instruction.instruction.argument);
|
||||
let consumed = defaults + closure + 1;
|
||||
(consumed, consumed, 1)
|
||||
}
|
||||
Opcode::ReturnValue => (1, 1, 0),
|
||||
_ => (0, 0, 0),
|
||||
};
|
||||
|
||||
+32
-1
@@ -1,6 +1,6 @@
|
||||
use crate::code::jump_target;
|
||||
use crate::frame::StackEntry;
|
||||
use crate::instruction::{compare_operation, invalid};
|
||||
use crate::instruction::{compare_operation, function_counts, invalid};
|
||||
use crate::{
|
||||
CompareOp, EndorCellObject, EndorCodeObject, EndorFrame, EndorFunctionObject,
|
||||
EndorStringObject, ExecutionLimits, ExecutionLocation, ObjectRef, Opcode, Value, Vm, VmError,
|
||||
@@ -314,6 +314,31 @@ impl Vm {
|
||||
Ok(dictionary)
|
||||
}
|
||||
|
||||
fn make_function(&mut self, frame: &EndorFrame, argument: u32) -> Result<ObjectRef, VmError> {
|
||||
let (default_count, closure_count) = function_counts(argument);
|
||||
let Value::Object(code) = Self::top_value(frame)? else {
|
||||
return Err(VmError::ObjectKindMismatch("code"));
|
||||
};
|
||||
let name = self
|
||||
.payload::<EndorCodeObject>(code, "code")?
|
||||
.code()
|
||||
.name()
|
||||
.to_owned();
|
||||
let start = frame.stack.len() - default_count - closure_count - 1;
|
||||
let defaults = (start..start + default_count)
|
||||
.map(|index| Self::stack_value(frame, index))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let closure = (start + default_count..frame.stack.len() - 1)
|
||||
.map(|index| match Self::stack_value(frame, index)? {
|
||||
Value::Object(cell) => Ok(cell),
|
||||
_ => Err(VmError::ObjectKindMismatch("cell")),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
// Keep every input root on the stack until allocation has validated
|
||||
// and retained all function edges. Failure leaves cleanup to the frame.
|
||||
self.new_function(name, code, frame.globals, defaults, closure)
|
||||
}
|
||||
|
||||
fn eval_frames(
|
||||
&mut self,
|
||||
frames: &mut Vec<EndorFrame>,
|
||||
@@ -528,6 +553,12 @@ impl Vm {
|
||||
.stack
|
||||
.push(StackEntry::Value(Value::Object(dictionary)));
|
||||
}
|
||||
Opcode::MakeFunction => {
|
||||
let function = self.make_function(frame, decoded.instruction.argument)?;
|
||||
let (defaults, closure) = function_counts(decoded.instruction.argument);
|
||||
self.discard(frame, defaults + closure + 1);
|
||||
frame.stack.push(StackEntry::Value(Value::Object(function)));
|
||||
}
|
||||
Opcode::ExtendedArg => unreachable!("EXTENDED_ARG was decoded before evaluation"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ pub enum Opcode {
|
||||
LoadAttr = 25,
|
||||
StoreAttr = 26,
|
||||
BuildMap = 27,
|
||||
MakeFunction = 28,
|
||||
ExtendedArg = 255,
|
||||
}
|
||||
|
||||
@@ -67,6 +68,7 @@ impl Opcode {
|
||||
25 => Self::LoadAttr,
|
||||
26 => Self::StoreAttr,
|
||||
27 => Self::BuildMap,
|
||||
28 => Self::MakeFunction,
|
||||
255 => Self::ExtendedArg,
|
||||
_ => return None,
|
||||
})
|
||||
@@ -100,6 +102,17 @@ impl Instruction {
|
||||
Self::new(opcode, 0)
|
||||
}
|
||||
|
||||
/// Build a function from [defaults..., closure cells..., code] on the stack.
|
||||
/// The low 16 operand bits count trailing positional defaults; the high
|
||||
/// 16 bits count cells in the code's free-variable order. No tuples or
|
||||
/// subsequent mutation of the function object are required.
|
||||
pub const fn make_function(defaults: u16, closure: u16) -> Self {
|
||||
Self::new(
|
||||
Opcode::MakeFunction,
|
||||
(defaults as u32) | ((closure as u32) << 16),
|
||||
)
|
||||
}
|
||||
|
||||
pub const fn compare(operation: CompareOp) -> Self {
|
||||
Self::new(
|
||||
Opcode::CompareOp,
|
||||
@@ -198,6 +211,10 @@ pub(crate) fn compare_operation(argument: u32) -> Option<CompareOp> {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn function_counts(argument: u32) -> (usize, usize) {
|
||||
((argument & 0xffff) as usize, (argument >> 16) as usize)
|
||||
}
|
||||
|
||||
pub(crate) fn invalid(offset: usize, reason: &'static str) -> VmError {
|
||||
VmError::InvalidBytecode { offset, reason }
|
||||
}
|
||||
|
||||
@@ -6,6 +6,6 @@
|
||||
| --- | --- |
|
||||
| [runtime.rs](runtime.rs) | 空堆启动、默认操作、引用/借用、属性原子性、稳定地址、长链/循环、原生回调、VM 销毁 |
|
||||
| [builtins.rs](builtins.rs) | 无损 Number、Unicode 字符串、Dict 顺序与键、Cell、函数环境/共享代码、异常数据 |
|
||||
| [execution.rs](execution.rs) | wordcode 与校验、控制流、参数/默认值、嵌套帧、闭包、返回值存活、失败清理、限额 |
|
||||
| [execution.rs](execution.rs) | wordcode 与校验、控制流、参数/默认值、嵌套帧、字节码函数创建/默认值/环境捕获、闭包、返回值存活、失败清理、限额 |
|
||||
|
||||
堆内部伪造句柄与计数溢出检查在 src/heap.rs,公开示例由 src/lib.rs rustdoc 提供。文件职责见 [src](../src/README.md),检查方式和结果边界见 [验证](../../../docs/operations/validation.md)。本地不执行测试;存在测试文件不代表某次提交已通过。返回 [core](../README.md)。
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user