Implement validated wordcode execution with shared code objects and frames
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m27s
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m27s
This commit is contained in:
+21
-67
@@ -150,89 +150,42 @@ impl ObjectPayload for EndorCellObject {
|
||||
}
|
||||
}
|
||||
|
||||
/// Code data only: bytecode is opaque until an instruction format/executor
|
||||
/// is defined. Constants may own managed objects through the enclosing function.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EndorCode {
|
||||
bytecode: Vec<u8>,
|
||||
constants: Vec<Value>,
|
||||
parameters: Vec<String>,
|
||||
free_variables: Vec<String>,
|
||||
}
|
||||
|
||||
impl EndorCode {
|
||||
pub fn new(
|
||||
bytecode: Vec<u8>,
|
||||
constants: Vec<Value>,
|
||||
parameters: Vec<String>,
|
||||
free_variables: Vec<String>,
|
||||
) -> Result<Self, VmError> {
|
||||
for names in [¶meters, &free_variables] {
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
for name in names {
|
||||
if name.is_empty() || !seen.insert(name) {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"empty or duplicate code variable name",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
bytecode,
|
||||
constants,
|
||||
parameters,
|
||||
free_variables,
|
||||
})
|
||||
}
|
||||
pub fn bytecode(&self) -> &[u8] {
|
||||
&self.bytecode
|
||||
}
|
||||
pub fn constants(&self) -> &[Value] {
|
||||
&self.constants
|
||||
}
|
||||
pub fn parameters(&self) -> &[String] {
|
||||
&self.parameters
|
||||
}
|
||||
pub fn free_variables(&self) -> &[String] {
|
||||
&self.free_variables
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EndorFunctionObject {
|
||||
name: String,
|
||||
code: EndorCode,
|
||||
code: ObjectRef,
|
||||
builtins: Option<ObjectRef>,
|
||||
globals: ObjectRef,
|
||||
defaults: Vec<Value>,
|
||||
closure: Vec<ObjectRef>,
|
||||
}
|
||||
|
||||
impl EndorFunctionObject {
|
||||
pub fn new(
|
||||
name: impl Into<String>,
|
||||
code: EndorCode,
|
||||
pub(crate) fn new(
|
||||
name: String,
|
||||
code: ObjectRef,
|
||||
globals: ObjectRef,
|
||||
builtins: Option<ObjectRef>,
|
||||
defaults: Vec<Value>,
|
||||
closure: Vec<ObjectRef>,
|
||||
) -> Result<Self, VmError> {
|
||||
if defaults.len() > code.parameters.len() || closure.len() != code.free_variables.len() {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"function defaults or closure do not match code",
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
name: name.into(),
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
code,
|
||||
globals,
|
||||
builtins,
|
||||
defaults,
|
||||
closure,
|
||||
})
|
||||
}
|
||||
}
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
pub fn code(&self) -> &EndorCode {
|
||||
&self.code
|
||||
pub fn code(&self) -> ObjectRef {
|
||||
self.code
|
||||
}
|
||||
pub fn builtins(&self) -> Option<ObjectRef> {
|
||||
self.builtins
|
||||
}
|
||||
pub fn globals(&self) -> ObjectRef {
|
||||
self.globals
|
||||
@@ -256,20 +209,21 @@ impl ObjectPayload for EndorFunctionObject {
|
||||
true
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
visit(self.code);
|
||||
visit(self.globals);
|
||||
if let Some(builtins) = self.builtins {
|
||||
visit(builtins);
|
||||
}
|
||||
for reference in &self.closure {
|
||||
visit(*reference);
|
||||
}
|
||||
for value in self.defaults.iter().chain(&self.code.constants) {
|
||||
for value in &self.defaults {
|
||||
trace_value(value, visit);
|
||||
}
|
||||
}
|
||||
fn repr(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(format!("<function {}>", self.name))
|
||||
}
|
||||
fn call(&self, _arguments: &[Value]) -> Result<Value, VmError> {
|
||||
Err(VmError::UnsupportedOperation("language function execution"))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
||||
@@ -0,0 +1,375 @@
|
||||
use std::any::Any;
|
||||
use std::collections::{BTreeSet, VecDeque};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::builtins::trace_value;
|
||||
use crate::instruction::{compare_operation, decode, invalid};
|
||||
use crate::{DecodedInstruction, Instruction, ObjectPayload, ObjectRef, Opcode, Value, VmError};
|
||||
|
||||
/// Local slots start with positional parameters. Captured locals occupy their
|
||||
/// original slots as cells; free-variable cells follow all local slots.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct CodeLayout {
|
||||
pub parameters: Vec<String>,
|
||||
pub locals: Vec<String>,
|
||||
pub cell_variables: Vec<String>,
|
||||
pub free_variables: Vec<String>,
|
||||
pub names: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EndorCode {
|
||||
bytecode: Vec<u8>,
|
||||
instructions: Vec<DecodedInstruction>,
|
||||
constants: Vec<Value>,
|
||||
layout: CodeLayout,
|
||||
stack_size: usize,
|
||||
name: String,
|
||||
filename: String,
|
||||
first_line: usize,
|
||||
}
|
||||
|
||||
impl EndorCode {
|
||||
pub fn new(
|
||||
bytecode: Vec<u8>,
|
||||
constants: Vec<Value>,
|
||||
parameters: Vec<String>,
|
||||
free_variables: Vec<String>,
|
||||
) -> Result<Self, VmError> {
|
||||
Self::with_layout(
|
||||
bytecode,
|
||||
constants,
|
||||
CodeLayout {
|
||||
locals: parameters.clone(),
|
||||
parameters,
|
||||
free_variables,
|
||||
..CodeLayout::default()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn from_instructions(
|
||||
instructions: &[Instruction],
|
||||
constants: Vec<Value>,
|
||||
layout: CodeLayout,
|
||||
) -> Result<Self, VmError> {
|
||||
Self::with_layout(Instruction::encode_all(instructions)?, constants, layout)
|
||||
}
|
||||
|
||||
pub fn with_layout(
|
||||
bytecode: Vec<u8>,
|
||||
constants: Vec<Value>,
|
||||
layout: CodeLayout,
|
||||
) -> Result<Self, VmError> {
|
||||
validate_layout(&layout)?;
|
||||
let instructions = decode(&bytecode)?;
|
||||
let stack_size = verify(&instructions, constants.len(), &layout)?;
|
||||
Ok(Self {
|
||||
bytecode,
|
||||
instructions,
|
||||
constants,
|
||||
layout,
|
||||
stack_size,
|
||||
name: "<code>".into(),
|
||||
filename: "<endor>".into(),
|
||||
first_line: 1,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn with_location(
|
||||
mut self,
|
||||
name: impl Into<String>,
|
||||
filename: impl Into<String>,
|
||||
first_line: usize,
|
||||
) -> Self {
|
||||
self.name = name.into();
|
||||
self.filename = filename.into();
|
||||
self.first_line = first_line;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn bytecode(&self) -> &[u8] {
|
||||
&self.bytecode
|
||||
}
|
||||
pub fn instructions(&self) -> &[DecodedInstruction] {
|
||||
&self.instructions
|
||||
}
|
||||
pub fn constants(&self) -> &[Value] {
|
||||
&self.constants
|
||||
}
|
||||
pub fn parameters(&self) -> &[String] {
|
||||
&self.layout.parameters
|
||||
}
|
||||
pub fn free_variables(&self) -> &[String] {
|
||||
&self.layout.free_variables
|
||||
}
|
||||
pub fn layout(&self) -> &CodeLayout {
|
||||
&self.layout
|
||||
}
|
||||
pub fn stack_size(&self) -> usize {
|
||||
self.stack_size
|
||||
}
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
pub fn filename(&self) -> &str {
|
||||
&self.filename
|
||||
}
|
||||
pub fn first_line(&self) -> usize {
|
||||
self.first_line
|
||||
}
|
||||
pub fn slot_count(&self) -> usize {
|
||||
self.layout.locals.len() + self.layout.free_variables.len()
|
||||
}
|
||||
pub(crate) fn is_cell_slot(&self, slot: usize) -> bool {
|
||||
is_cell_slot(&self.layout, slot)
|
||||
}
|
||||
pub(crate) fn slot_name(&self, slot: usize) -> &str {
|
||||
if slot < self.layout.locals.len() {
|
||||
&self.layout.locals[slot]
|
||||
} else {
|
||||
&self.layout.free_variables[slot - self.layout.locals.len()]
|
||||
}
|
||||
}
|
||||
pub(crate) fn instruction_at(&self, offset: usize) -> Result<DecodedInstruction, VmError> {
|
||||
instruction_index(&self.instructions, offset)
|
||||
.map(|index| self.instructions[index])
|
||||
.ok_or_else(|| {
|
||||
invalid(
|
||||
offset,
|
||||
"instruction pointer is not on an instruction boundary",
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Heap-allocated, immutable and shareable executable. Function objects hold a
|
||||
/// managed reference to this object rather than embedding a copy of its code.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EndorCodeObject {
|
||||
code: Arc<EndorCode>,
|
||||
}
|
||||
|
||||
impl EndorCodeObject {
|
||||
pub fn new(code: EndorCode) -> Self {
|
||||
Self {
|
||||
code: Arc::new(code),
|
||||
}
|
||||
}
|
||||
pub fn code(&self) -> &EndorCode {
|
||||
&self.code
|
||||
}
|
||||
pub(crate) fn shared_code(&self) -> Arc<EndorCode> {
|
||||
Arc::clone(&self.code)
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectPayload for EndorCodeObject {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"code"
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
for value in self.code.constants() {
|
||||
trace_value(value, visit);
|
||||
}
|
||||
}
|
||||
fn repr(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(format!(
|
||||
"<code {} at {}:{}>",
|
||||
self.code.name(),
|
||||
self.code.filename(),
|
||||
self.code.first_line()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_layout(layout: &CodeLayout) -> Result<(), VmError> {
|
||||
for names in [
|
||||
&layout.parameters,
|
||||
&layout.locals,
|
||||
&layout.cell_variables,
|
||||
&layout.free_variables,
|
||||
&layout.names,
|
||||
] {
|
||||
let mut seen = BTreeSet::new();
|
||||
for name in names {
|
||||
if name.is_empty() || !seen.insert(name) {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"empty or duplicate code variable name",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
if !layout.locals.starts_with(&layout.parameters) {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"parameter names must lead local slots",
|
||||
));
|
||||
}
|
||||
if layout
|
||||
.cell_variables
|
||||
.iter()
|
||||
.any(|name| !layout.locals.contains(name))
|
||||
|| layout
|
||||
.free_variables
|
||||
.iter()
|
||||
.any(|name| layout.locals.contains(name))
|
||||
{
|
||||
return Err(VmError::InvalidArgument(
|
||||
"invalid local/cell/free-variable layout",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_cell_slot(layout: &CodeLayout, slot: usize) -> bool {
|
||||
if slot >= layout.locals.len() {
|
||||
slot < layout.locals.len() + layout.free_variables.len()
|
||||
} else {
|
||||
layout.cell_variables.contains(&layout.locals[slot])
|
||||
}
|
||||
}
|
||||
|
||||
fn instruction_index(instructions: &[DecodedInstruction], offset: usize) -> Option<usize> {
|
||||
instructions
|
||||
.binary_search_by_key(&offset, |instruction| instruction.offset)
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub(crate) fn jump_target(instruction: DecodedInstruction) -> Result<usize, VmError> {
|
||||
let argument = instruction.instruction.argument as usize;
|
||||
let target = if instruction.instruction.opcode == Opcode::JumpBackward {
|
||||
instruction.next_offset.checked_sub(argument)
|
||||
} else {
|
||||
instruction.next_offset.checked_add(argument)
|
||||
};
|
||||
target.ok_or_else(|| invalid(instruction.offset, "jump offset overflow"))
|
||||
}
|
||||
|
||||
/// Validate every operand, then propagate stack heights through reachable
|
||||
/// control flow. Bad jumps, underflow, mismatched joins and fallthrough fail
|
||||
/// before execution; loops must return to the same stack height.
|
||||
fn verify(
|
||||
instructions: &[DecodedInstruction],
|
||||
constants: usize,
|
||||
layout: &CodeLayout,
|
||||
) -> Result<usize, VmError> {
|
||||
for instruction in instructions {
|
||||
let Instruction { opcode, argument } = instruction.instruction;
|
||||
let index = argument as usize;
|
||||
let valid = match opcode {
|
||||
Opcode::LoadConst => index < constants,
|
||||
Opcode::LoadFast | Opcode::StoreFast | Opcode::DeleteFast => {
|
||||
index < layout.locals.len() && !is_cell_slot(layout, index)
|
||||
}
|
||||
Opcode::LoadDeref | Opcode::StoreDeref | Opcode::DeleteDeref | Opcode::LoadClosure => {
|
||||
is_cell_slot(layout, index)
|
||||
}
|
||||
Opcode::LoadGlobal
|
||||
| Opcode::StoreGlobal
|
||||
| Opcode::DeleteGlobal
|
||||
| Opcode::LoadAttr
|
||||
| Opcode::StoreAttr => index < layout.names.len(),
|
||||
Opcode::CompareOp => compare_operation(argument).is_some(),
|
||||
Opcode::Copy => argument >= 1,
|
||||
Opcode::Swap => argument >= 2,
|
||||
Opcode::JumpForward
|
||||
| Opcode::JumpBackward
|
||||
| Opcode::PopJumpIfFalse
|
||||
| Opcode::PopJumpIfTrue => {
|
||||
instruction_index(instructions, jump_target(*instruction)?).is_some()
|
||||
}
|
||||
Opcode::ExtendedArg => false,
|
||||
_ => true,
|
||||
};
|
||||
if !valid {
|
||||
return Err(invalid(
|
||||
instruction.offset,
|
||||
"invalid operand or jump target",
|
||||
));
|
||||
}
|
||||
}
|
||||
let mut heights = vec![None; instructions.len()];
|
||||
heights[0] = Some(0_usize);
|
||||
let mut pending = VecDeque::from([0]);
|
||||
let mut maximum = 0;
|
||||
while let Some(index) = pending.pop_front() {
|
||||
let instruction = instructions[index];
|
||||
let height = heights[index].unwrap();
|
||||
let Instruction { opcode, argument } = instruction.instruction;
|
||||
let argument = argument as usize;
|
||||
let (required, popped, pushed) = match opcode {
|
||||
Opcode::LoadConst
|
||||
| Opcode::LoadFast
|
||||
| Opcode::LoadGlobal
|
||||
| Opcode::LoadDeref
|
||||
| Opcode::LoadClosure
|
||||
| Opcode::PushNull => (0, 0, 1),
|
||||
Opcode::StoreFast
|
||||
| Opcode::StoreGlobal
|
||||
| Opcode::PopTop
|
||||
| Opcode::StoreDeref
|
||||
| Opcode::PopJumpIfFalse
|
||||
| Opcode::PopJumpIfTrue => (1, 1, 0),
|
||||
Opcode::ToBool | Opcode::UnaryNot | Opcode::LoadAttr => (1, 1, 1),
|
||||
Opcode::CompareOp => (2, 2, 1),
|
||||
Opcode::StoreAttr => (2, 2, 0),
|
||||
Opcode::Copy => (argument, 0, 1),
|
||||
Opcode::Swap => (argument, 0, 0),
|
||||
Opcode::Call => {
|
||||
let consumed = argument
|
||||
.checked_add(2)
|
||||
.ok_or_else(|| invalid(instruction.offset, "call operand overflow"))?;
|
||||
(consumed, consumed, 1)
|
||||
}
|
||||
Opcode::BuildMap => {
|
||||
let consumed = argument
|
||||
.checked_mul(2)
|
||||
.ok_or_else(|| invalid(instruction.offset, "map operand overflow"))?;
|
||||
(consumed, consumed, 1)
|
||||
}
|
||||
Opcode::ReturnValue => (1, 1, 0),
|
||||
_ => (0, 0, 0),
|
||||
};
|
||||
if height < required {
|
||||
return Err(invalid(instruction.offset, "evaluation stack underflow"));
|
||||
}
|
||||
if opcode == Opcode::ReturnValue && height != 1 {
|
||||
return Err(invalid(
|
||||
instruction.offset,
|
||||
"RETURN_VALUE must leave an empty frame stack",
|
||||
));
|
||||
}
|
||||
let next_height = height - popped + pushed;
|
||||
maximum = maximum.max(height).max(next_height);
|
||||
let successors: Vec<usize> = match opcode {
|
||||
Opcode::ReturnValue => Vec::new(),
|
||||
Opcode::JumpForward | Opcode::JumpBackward => vec![jump_target(instruction)?],
|
||||
Opcode::PopJumpIfFalse | Opcode::PopJumpIfTrue => {
|
||||
vec![jump_target(instruction)?, instruction.next_offset]
|
||||
}
|
||||
_ => vec![instruction.next_offset],
|
||||
};
|
||||
for successor in successors {
|
||||
let target = instruction_index(instructions, successor).ok_or_else(|| {
|
||||
invalid(instruction.offset, "execution falls off the code object")
|
||||
})?;
|
||||
match heights[target] {
|
||||
Some(existing) if existing != next_height => {
|
||||
return Err(invalid(
|
||||
successor,
|
||||
"control-flow paths have different stack heights",
|
||||
));
|
||||
}
|
||||
Some(_) => {}
|
||||
None => {
|
||||
heights[target] = Some(next_height);
|
||||
pending.push_back(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(maximum)
|
||||
}
|
||||
@@ -0,0 +1,535 @@
|
||||
use crate::code::jump_target;
|
||||
use crate::frame::StackEntry;
|
||||
use crate::instruction::{compare_operation, invalid};
|
||||
use crate::{
|
||||
CompareOp, EndorCellObject, EndorCodeObject, EndorFrame, EndorFunctionObject,
|
||||
EndorStringObject, ExecutionLimits, ExecutionLocation, ObjectRef, Opcode, Value, Vm, VmError,
|
||||
};
|
||||
|
||||
impl Vm {
|
||||
/// Execute a code object in a globals dictionary. Like CPython's EvalCode
|
||||
/// entry path, this constructs a temporary function and enters its frame.
|
||||
/// Object results own one external reference: release_value() when done.
|
||||
pub fn execute(&mut self, code: ObjectRef, globals: ObjectRef) -> Result<Value, VmError> {
|
||||
self.execute_with_limits(code, globals, ExecutionLimits::default())
|
||||
}
|
||||
|
||||
pub fn execute_with_limits(
|
||||
&mut self,
|
||||
code: ObjectRef,
|
||||
globals: ObjectRef,
|
||||
limits: ExecutionLimits,
|
||||
) -> Result<Value, VmError> {
|
||||
self.last_traceback.clear();
|
||||
let name = self
|
||||
.payload::<EndorCodeObject>(code, "code")?
|
||||
.code()
|
||||
.name()
|
||||
.to_owned();
|
||||
let function = self.new_function(name, code, globals, Vec::new(), Vec::new())?;
|
||||
let result = self.call_with_limits(function, &[], limits);
|
||||
self.release(function)?;
|
||||
result
|
||||
}
|
||||
|
||||
/// Dispatch both language and host functions. Arguments are borrowed;
|
||||
/// every Object result owns one external reference, including native
|
||||
/// callbacks that return one of their borrowed arguments.
|
||||
pub fn call(&mut self, function: ObjectRef, arguments: &[Value]) -> Result<Value, VmError> {
|
||||
self.call_with_limits(function, arguments, ExecutionLimits::default())
|
||||
}
|
||||
|
||||
pub fn call_with_limits(
|
||||
&mut self,
|
||||
function: ObjectRef,
|
||||
arguments: &[Value],
|
||||
limits: ExecutionLimits,
|
||||
) -> Result<Value, VmError> {
|
||||
self.last_traceback.clear();
|
||||
if !self
|
||||
.object(function)?
|
||||
.payload()
|
||||
.as_any()
|
||||
.is::<EndorFunctionObject>()
|
||||
{
|
||||
return self.invoke_native(function, arguments);
|
||||
}
|
||||
if limits.max_frames == 0 {
|
||||
return Err(VmError::ExecutionLimit("frames"));
|
||||
}
|
||||
let frame = self.prepare_frame(function, arguments)?;
|
||||
let mut frames = vec![frame];
|
||||
let result = self.eval_frames(&mut frames, limits);
|
||||
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);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// 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] {
|
||||
&self.last_traceback
|
||||
}
|
||||
|
||||
pub fn release_value(&mut self, value: Value) -> Result<(), VmError> {
|
||||
if let Value::Object(reference) = value {
|
||||
self.release(reference)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn retain_value(&mut self, value: &Value) -> Result<(), VmError> {
|
||||
self.validate_value(value)?;
|
||||
if let Value::Object(reference) = value {
|
||||
self.retain(*reference)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn drop_root(&mut self, value: Value) {
|
||||
self.release_value(value)
|
||||
.expect("execution owns every object root it releases");
|
||||
}
|
||||
|
||||
fn clear_frame(&mut self, frame: EndorFrame) {
|
||||
for entry in frame.stack {
|
||||
if let StackEntry::Value(value) = entry {
|
||||
self.drop_root(value);
|
||||
}
|
||||
}
|
||||
for value in frame.localsplus.into_iter().flatten() {
|
||||
self.drop_root(value);
|
||||
}
|
||||
self.release(frame.function)
|
||||
.expect("frame owns its function root");
|
||||
}
|
||||
|
||||
fn prepare_frame(
|
||||
&mut self,
|
||||
function: ObjectRef,
|
||||
arguments: &[Value],
|
||||
) -> Result<EndorFrame, VmError> {
|
||||
let data = self
|
||||
.payload::<EndorFunctionObject>(function, "function")?
|
||||
.clone();
|
||||
let executable = self
|
||||
.payload::<EndorCodeObject>(data.code(), "code")?
|
||||
.shared_code();
|
||||
let maximum = executable.parameters().len();
|
||||
let required = maximum - data.defaults().len();
|
||||
if arguments.len() < required || arguments.len() > maximum {
|
||||
return Err(VmError::ArgumentCount {
|
||||
name: data.name().into(),
|
||||
required,
|
||||
maximum,
|
||||
provided: arguments.len(),
|
||||
});
|
||||
}
|
||||
for value in arguments {
|
||||
self.validate_value(value)?;
|
||||
}
|
||||
self.retain(function)?;
|
||||
let mut frame = EndorFrame {
|
||||
function,
|
||||
previous: None,
|
||||
code: data.code(),
|
||||
globals: data.globals(),
|
||||
builtins: data.builtins(),
|
||||
name: data.name().into(),
|
||||
instruction_offset: 0,
|
||||
current_offset: 0,
|
||||
localsplus: vec![None; executable.slot_count()],
|
||||
stack: Vec::with_capacity(executable.stack_size()),
|
||||
executable,
|
||||
};
|
||||
let initialized = (|| -> Result<(), VmError> {
|
||||
let defaults = data.defaults().iter().skip(arguments.len() - required);
|
||||
for (slot, value) in frame
|
||||
.localsplus
|
||||
.iter_mut()
|
||||
.zip(arguments.iter().chain(defaults))
|
||||
{
|
||||
self.retain_value(value)?;
|
||||
*slot = Some(value.clone());
|
||||
}
|
||||
// Captured local cells occupy the original local slots, as in
|
||||
// CPython's localsplus layout. Free cells follow the local slots.
|
||||
let local_count = frame.executable.layout().locals.len();
|
||||
for (index, slot) in frame.localsplus[..local_count].iter_mut().enumerate() {
|
||||
if frame.executable.is_cell_slot(index) {
|
||||
let cell = self.new_cell(slot.clone())?;
|
||||
if let Some(old) = slot.replace(Value::Object(cell)) {
|
||||
self.drop_root(old);
|
||||
}
|
||||
}
|
||||
}
|
||||
let first_free = frame.executable.layout().locals.len();
|
||||
for (index, cell) in data.closure().iter().enumerate() {
|
||||
self.retain(*cell)?;
|
||||
frame.localsplus[first_free + index] = Some(Value::Object(*cell));
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
if let Err(error) = initialized {
|
||||
self.clear_frame(frame);
|
||||
return Err(error);
|
||||
}
|
||||
Ok(frame)
|
||||
}
|
||||
|
||||
fn invoke_native(
|
||||
&mut self,
|
||||
function: ObjectRef,
|
||||
arguments: &[Value],
|
||||
) -> Result<Value, VmError> {
|
||||
self.object(function)?;
|
||||
for argument in arguments {
|
||||
self.validate_value(argument)?;
|
||||
}
|
||||
let result = self.object(function)?.payload().call(arguments)?;
|
||||
self.retain_value(&result)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn stack_value(frame: &EndorFrame, index: usize) -> Result<Value, VmError> {
|
||||
match frame.stack.get(index) {
|
||||
Some(StackEntry::Value(value)) => Ok(value.clone()),
|
||||
_ => Err(invalid(
|
||||
frame.current_offset,
|
||||
"NULL call marker used as a value",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn top_value(frame: &EndorFrame) -> Result<Value, VmError> {
|
||||
Self::stack_value(frame, frame.stack.len() - 1)
|
||||
}
|
||||
|
||||
fn push_borrowed(&mut self, frame: &mut EndorFrame, value: Value) -> Result<(), VmError> {
|
||||
self.retain_value(&value)?;
|
||||
frame.stack.push(StackEntry::Value(value));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn discard(&mut self, frame: &mut EndorFrame, count: usize) {
|
||||
for _ in 0..count {
|
||||
if let StackEntry::Value(value) = frame.stack.pop().expect("verified stack height") {
|
||||
self.drop_root(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn cell_slot(&self, frame: &EndorFrame, index: usize) -> Result<ObjectRef, VmError> {
|
||||
match &frame.localsplus[index] {
|
||||
Some(Value::Object(reference)) => {
|
||||
self.payload::<EndorCellObject>(*reference, "cell")?;
|
||||
Ok(*reference)
|
||||
}
|
||||
_ => Err(invalid(
|
||||
frame.current_offset,
|
||||
"cell slot is not initialized",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn value_is_true(&self, value: &Value) -> Result<bool, VmError> {
|
||||
match value {
|
||||
Value::Nil => Ok(false),
|
||||
Value::Bool(value) => Ok(*value),
|
||||
Value::Text(value) => Ok(!value.is_empty()),
|
||||
Value::Number(value) => Ok(!value.is_zero()),
|
||||
Value::Object(reference) => self.is_true(*reference),
|
||||
}
|
||||
}
|
||||
|
||||
fn text_value(&self, value: &Value) -> Result<Option<String>, VmError> {
|
||||
Ok(match value {
|
||||
Value::Text(text) => Some(text.clone()),
|
||||
Value::Object(reference) => self
|
||||
.object(*reference)?
|
||||
.payload()
|
||||
.as_any()
|
||||
.downcast_ref::<EndorStringObject>()
|
||||
.map(|text| text.as_str().to_owned()),
|
||||
_ => 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"));
|
||||
}
|
||||
if let (Some(left), Some(right)) = (self.text_value(left)?, self.text_value(right)?) {
|
||||
return Ok(match operation {
|
||||
CompareOp::Eq => left == right,
|
||||
CompareOp::Ne => left != right,
|
||||
CompareOp::Lt => left < right,
|
||||
CompareOp::Le => left <= right,
|
||||
CompareOp::Gt => left > right,
|
||||
CompareOp::Ge => left >= right,
|
||||
});
|
||||
}
|
||||
if let (Value::Object(left), Value::Object(right)) = (left, right) {
|
||||
return self.compare(*left, *right, operation);
|
||||
}
|
||||
match operation {
|
||||
CompareOp::Eq => Ok(left == right),
|
||||
CompareOp::Ne => Ok(left != right),
|
||||
_ => Err(VmError::UnsupportedOperation("ordering")),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_map(&mut self, frame: &EndorFrame, count: usize) -> Result<ObjectRef, VmError> {
|
||||
let dictionary = self.new_dict()?;
|
||||
let result = (|| -> Result<(), VmError> {
|
||||
let start = frame.stack.len() - count * 2;
|
||||
for index in 0..count {
|
||||
let key = Self::stack_value(frame, start + index * 2)?;
|
||||
let value = Self::stack_value(frame, start + index * 2 + 1)?;
|
||||
self.dict_set(dictionary, key, value)?;
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
if let Err(error) = result {
|
||||
self.release(dictionary)?;
|
||||
return Err(error);
|
||||
}
|
||||
Ok(dictionary)
|
||||
}
|
||||
|
||||
fn eval_frames(
|
||||
&mut self,
|
||||
frames: &mut Vec<EndorFrame>,
|
||||
limits: ExecutionLimits,
|
||||
) -> Result<Value, VmError> {
|
||||
let mut executed = 0;
|
||||
loop {
|
||||
if executed >= limits.max_instructions {
|
||||
return Err(VmError::ExecutionLimit("instructions"));
|
||||
}
|
||||
executed += 1;
|
||||
let frame = frames.last_mut().expect("an evaluation frame is active");
|
||||
let decoded = frame.executable.instruction_at(frame.instruction_offset)?;
|
||||
frame.current_offset = decoded.offset;
|
||||
frame.instruction_offset = decoded.next_offset;
|
||||
let opcode = decoded.instruction.opcode;
|
||||
let argument = decoded.instruction.argument as usize;
|
||||
match opcode {
|
||||
Opcode::Nop => {}
|
||||
Opcode::LoadConst => {
|
||||
let value = frame.executable.constants()[argument].clone();
|
||||
self.push_borrowed(frame, value)?;
|
||||
}
|
||||
Opcode::LoadFast => {
|
||||
let value = frame.localsplus[argument].clone().ok_or_else(|| {
|
||||
VmError::UnboundLocal(frame.executable.slot_name(argument).into())
|
||||
})?;
|
||||
self.push_borrowed(frame, value)?;
|
||||
}
|
||||
Opcode::StoreFast => {
|
||||
let value = Self::top_value(frame)?;
|
||||
// The stack root is moved into the local slot.
|
||||
frame.stack.pop();
|
||||
if let Some(old) = frame.localsplus[argument].replace(value) {
|
||||
self.drop_root(old);
|
||||
}
|
||||
}
|
||||
Opcode::DeleteFast => {
|
||||
let value = frame.localsplus[argument].take().ok_or_else(|| {
|
||||
VmError::UnboundLocal(frame.executable.slot_name(argument).into())
|
||||
})?;
|
||||
self.drop_root(value);
|
||||
}
|
||||
Opcode::LoadGlobal => {
|
||||
let name = &frame.executable.layout().names[argument];
|
||||
let key = Value::Text(name.clone());
|
||||
let mut value = self.dict_get(frame.globals, &key)?;
|
||||
if value.is_none()
|
||||
&& let Some(builtins) = frame.builtins
|
||||
{
|
||||
value = self.dict_get(builtins, &key)?;
|
||||
}
|
||||
let value = value.ok_or_else(|| VmError::NameNotFound(name.clone()))?;
|
||||
self.push_borrowed(frame, value)?;
|
||||
}
|
||||
Opcode::StoreGlobal => {
|
||||
let value = Self::top_value(frame)?;
|
||||
let key = Value::Text(frame.executable.layout().names[argument].clone());
|
||||
self.dict_set(frame.globals, key, value)?;
|
||||
self.discard(frame, 1);
|
||||
}
|
||||
Opcode::DeleteGlobal => {
|
||||
let name = &frame.executable.layout().names[argument];
|
||||
if !self.dict_delete(frame.globals, &Value::Text(name.clone()))? {
|
||||
return Err(VmError::NameNotFound(name.clone()));
|
||||
}
|
||||
}
|
||||
Opcode::LoadDeref | Opcode::LoadClosure => {
|
||||
let cell = self.cell_slot(frame, argument)?;
|
||||
let value = if opcode == Opcode::LoadClosure {
|
||||
Value::Object(cell)
|
||||
} else {
|
||||
self.cell_get(cell).map_err(|error| {
|
||||
if error == VmError::EmptyCell {
|
||||
VmError::UnboundLocal(frame.executable.slot_name(argument).into())
|
||||
} else {
|
||||
error
|
||||
}
|
||||
})?
|
||||
};
|
||||
self.push_borrowed(frame, value)?;
|
||||
}
|
||||
Opcode::StoreDeref => {
|
||||
let cell = self.cell_slot(frame, argument)?;
|
||||
self.cell_set(cell, Self::top_value(frame)?)?;
|
||||
self.discard(frame, 1);
|
||||
}
|
||||
Opcode::DeleteDeref => {
|
||||
let cell = self.cell_slot(frame, argument)?;
|
||||
if self.cell_get(cell).is_err() {
|
||||
return Err(VmError::UnboundLocal(
|
||||
frame.executable.slot_name(argument).into(),
|
||||
));
|
||||
}
|
||||
self.cell_clear(cell)?;
|
||||
}
|
||||
Opcode::PopTop => self.discard(frame, 1),
|
||||
Opcode::Copy => {
|
||||
let entry = frame.stack[frame.stack.len() - argument].clone();
|
||||
if let StackEntry::Value(value) = &entry {
|
||||
self.retain_value(value)?;
|
||||
}
|
||||
frame.stack.push(entry);
|
||||
}
|
||||
Opcode::Swap => {
|
||||
let length = frame.stack.len();
|
||||
frame.stack.swap(length - 1, length - argument);
|
||||
}
|
||||
Opcode::PushNull => frame.stack.push(StackEntry::Null),
|
||||
Opcode::Call => {
|
||||
let start = frame.stack.len() - argument - 2;
|
||||
let Value::Object(function) = Self::stack_value(frame, start)? else {
|
||||
return Err(VmError::UnsupportedOperation("call"));
|
||||
};
|
||||
let mut arguments = Vec::with_capacity(argument + 1);
|
||||
if let StackEntry::Value(value) = &frame.stack[start + 1] {
|
||||
arguments.push(value.clone());
|
||||
}
|
||||
for index in start + 2..frame.stack.len() {
|
||||
arguments.push(Self::stack_value(frame, index)?);
|
||||
}
|
||||
let language_function = self
|
||||
.object(function)?
|
||||
.payload()
|
||||
.as_any()
|
||||
.is::<EndorFunctionObject>();
|
||||
if language_function {
|
||||
if frames.len() >= limits.max_frames {
|
||||
return Err(VmError::ExecutionLimit("frames"));
|
||||
}
|
||||
let mut callee = self.prepare_frame(function, &arguments)?;
|
||||
callee.previous = Some(frames.len() - 1);
|
||||
self.discard(frames.last_mut().unwrap(), argument + 2);
|
||||
frames.push(callee);
|
||||
} else {
|
||||
let value = self.invoke_native(function, &arguments)?;
|
||||
self.discard(frames.last_mut().unwrap(), argument + 2);
|
||||
frames
|
||||
.last_mut()
|
||||
.unwrap()
|
||||
.stack
|
||||
.push(StackEntry::Value(value));
|
||||
}
|
||||
}
|
||||
Opcode::ReturnValue => {
|
||||
let result = Self::top_value(frame)?;
|
||||
frame.stack.pop();
|
||||
self.clear_frame(frames.pop().unwrap());
|
||||
if let Some(caller) = frames.last_mut() {
|
||||
caller.stack.push(StackEntry::Value(result));
|
||||
} else {
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
Opcode::JumpForward | Opcode::JumpBackward => {
|
||||
frame.instruction_offset = jump_target(decoded)?
|
||||
}
|
||||
Opcode::PopJumpIfFalse | Opcode::PopJumpIfTrue => {
|
||||
let truth = self.value_is_true(&Self::top_value(frame)?)?;
|
||||
self.discard(frame, 1);
|
||||
if truth == (opcode == Opcode::PopJumpIfTrue) {
|
||||
frame.instruction_offset = jump_target(decoded)?;
|
||||
}
|
||||
}
|
||||
Opcode::ToBool | Opcode::UnaryNot => {
|
||||
let truth = self.value_is_true(&Self::top_value(frame)?)?;
|
||||
self.discard(frame, 1);
|
||||
frame.stack.push(StackEntry::Value(Value::Bool(
|
||||
if opcode == Opcode::UnaryNot {
|
||||
!truth
|
||||
} else {
|
||||
truth
|
||||
},
|
||||
)));
|
||||
}
|
||||
Opcode::CompareOp => {
|
||||
let left = Self::stack_value(frame, frame.stack.len() - 2)?;
|
||||
let right = Self::top_value(frame)?;
|
||||
let result = self.compare_values(
|
||||
&left,
|
||||
&right,
|
||||
compare_operation(decoded.instruction.argument).unwrap(),
|
||||
)?;
|
||||
self.discard(frame, 2);
|
||||
frame.stack.push(StackEntry::Value(Value::Bool(result)));
|
||||
}
|
||||
Opcode::LoadAttr => {
|
||||
let Value::Object(owner) = Self::top_value(frame)? else {
|
||||
return Err(VmError::ObjectKindMismatch("object"));
|
||||
};
|
||||
let value = self.get_attr(owner, &frame.executable.layout().names[argument])?;
|
||||
self.retain_value(&value)?;
|
||||
self.discard(frame, 1);
|
||||
frame.stack.push(StackEntry::Value(value));
|
||||
}
|
||||
Opcode::StoreAttr => {
|
||||
let Value::Object(owner) = Self::top_value(frame)? else {
|
||||
return Err(VmError::ObjectKindMismatch("object"));
|
||||
};
|
||||
let value = Self::stack_value(frame, frame.stack.len() - 2)?;
|
||||
self.set_attr(
|
||||
owner,
|
||||
frame.executable.layout().names[argument].clone(),
|
||||
value,
|
||||
)?;
|
||||
self.discard(frame, 2);
|
||||
}
|
||||
Opcode::BuildMap => {
|
||||
let dictionary = self.build_map(frame, argument)?;
|
||||
self.discard(frame, argument * 2);
|
||||
frame
|
||||
.stack
|
||||
.push(StackEntry::Value(Value::Object(dictionary)));
|
||||
}
|
||||
Opcode::ExtendedArg => unreachable!("EXTENDED_ARG was decoded before evaluation"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{EndorCode, ObjectRef, Value};
|
||||
|
||||
/// NULL is a call-protocol marker and can never escape as a language value.
|
||||
/// Each Value::Object in a live frame owns one execution root.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) enum StackEntry {
|
||||
Null,
|
||||
Value(Value),
|
||||
}
|
||||
|
||||
/// Internal activation record, analogous to _PyInterpreterFrame. It has no
|
||||
/// EndorObject header; introspection objects can be added independently later.
|
||||
#[derive(Debug)]
|
||||
pub struct EndorFrame {
|
||||
pub(crate) function: ObjectRef,
|
||||
/// Index of the caller in the execution's frame vector, never a pointer.
|
||||
pub(crate) previous: Option<usize>,
|
||||
pub(crate) code: ObjectRef,
|
||||
pub(crate) executable: Arc<EndorCode>,
|
||||
pub(crate) globals: ObjectRef,
|
||||
pub(crate) builtins: Option<ObjectRef>,
|
||||
pub(crate) name: String,
|
||||
pub(crate) instruction_offset: usize,
|
||||
pub(crate) current_offset: usize,
|
||||
pub(crate) localsplus: Vec<Option<Value>>,
|
||||
pub(crate) stack: Vec<StackEntry>,
|
||||
}
|
||||
|
||||
impl EndorFrame {
|
||||
pub fn previous(&self) -> Option<usize> {
|
||||
self.previous
|
||||
}
|
||||
pub fn function(&self) -> ObjectRef {
|
||||
self.function
|
||||
}
|
||||
pub fn code(&self) -> ObjectRef {
|
||||
self.code
|
||||
}
|
||||
pub fn globals(&self) -> ObjectRef {
|
||||
self.globals
|
||||
}
|
||||
pub fn builtins(&self) -> Option<ObjectRef> {
|
||||
self.builtins
|
||||
}
|
||||
pub fn instruction_offset(&self) -> usize {
|
||||
self.instruction_offset
|
||||
}
|
||||
pub fn localsplus(&self) -> &[Option<Value>] {
|
||||
&self.localsplus
|
||||
}
|
||||
pub fn stack_depth(&self) -> usize {
|
||||
self.stack.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ExecutionLocation {
|
||||
pub function: String,
|
||||
pub filename: String,
|
||||
pub first_line: usize,
|
||||
pub instruction_offset: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ExecutionLimits {
|
||||
pub max_frames: usize,
|
||||
pub max_instructions: usize,
|
||||
}
|
||||
|
||||
impl Default for ExecutionLimits {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_frames: 1000,
|
||||
max_instructions: usize::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
use crate::{CompareOp, VmError};
|
||||
|
||||
/// Endor wordcode, following CPython's opcode/oparg code-unit design.
|
||||
/// Numeric opcode IDs are Endor's own, not a CPython bytecode ABI.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum Opcode {
|
||||
Nop = 0,
|
||||
LoadConst = 1,
|
||||
LoadFast = 2,
|
||||
StoreFast = 3,
|
||||
DeleteFast = 4,
|
||||
LoadGlobal = 5,
|
||||
StoreGlobal = 6,
|
||||
DeleteGlobal = 7,
|
||||
PopTop = 8,
|
||||
Copy = 9,
|
||||
Swap = 10,
|
||||
PushNull = 11,
|
||||
Call = 12,
|
||||
ReturnValue = 13,
|
||||
JumpForward = 14,
|
||||
JumpBackward = 15,
|
||||
PopJumpIfFalse = 16,
|
||||
PopJumpIfTrue = 17,
|
||||
ToBool = 18,
|
||||
UnaryNot = 19,
|
||||
CompareOp = 20,
|
||||
LoadDeref = 21,
|
||||
StoreDeref = 22,
|
||||
DeleteDeref = 23,
|
||||
LoadClosure = 24,
|
||||
LoadAttr = 25,
|
||||
StoreAttr = 26,
|
||||
BuildMap = 27,
|
||||
ExtendedArg = 255,
|
||||
}
|
||||
|
||||
impl Opcode {
|
||||
fn decode(byte: u8) -> Option<Self> {
|
||||
Some(match byte {
|
||||
0 => Self::Nop,
|
||||
1 => Self::LoadConst,
|
||||
2 => Self::LoadFast,
|
||||
3 => Self::StoreFast,
|
||||
4 => Self::DeleteFast,
|
||||
5 => Self::LoadGlobal,
|
||||
6 => Self::StoreGlobal,
|
||||
7 => Self::DeleteGlobal,
|
||||
8 => Self::PopTop,
|
||||
9 => Self::Copy,
|
||||
10 => Self::Swap,
|
||||
11 => Self::PushNull,
|
||||
12 => Self::Call,
|
||||
13 => Self::ReturnValue,
|
||||
14 => Self::JumpForward,
|
||||
15 => Self::JumpBackward,
|
||||
16 => Self::PopJumpIfFalse,
|
||||
17 => Self::PopJumpIfTrue,
|
||||
18 => Self::ToBool,
|
||||
19 => Self::UnaryNot,
|
||||
20 => Self::CompareOp,
|
||||
21 => Self::LoadDeref,
|
||||
22 => Self::StoreDeref,
|
||||
23 => Self::DeleteDeref,
|
||||
24 => Self::LoadClosure,
|
||||
25 => Self::LoadAttr,
|
||||
26 => Self::StoreAttr,
|
||||
27 => Self::BuildMap,
|
||||
255 => Self::ExtendedArg,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn takes_argument(self) -> bool {
|
||||
!matches!(
|
||||
self,
|
||||
Self::Nop
|
||||
| Self::PopTop
|
||||
| Self::PushNull
|
||||
| Self::ReturnValue
|
||||
| Self::ToBool
|
||||
| Self::UnaryNot
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A decoded logical instruction; EXTENDED_ARG is handled by the codec.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Instruction {
|
||||
pub opcode: Opcode,
|
||||
pub argument: u32,
|
||||
}
|
||||
|
||||
impl Instruction {
|
||||
pub const fn new(opcode: Opcode, argument: u32) -> Self {
|
||||
Self { opcode, argument }
|
||||
}
|
||||
pub const fn simple(opcode: Opcode) -> Self {
|
||||
Self::new(opcode, 0)
|
||||
}
|
||||
|
||||
pub const fn compare(operation: CompareOp) -> Self {
|
||||
Self::new(
|
||||
Opcode::CompareOp,
|
||||
match operation {
|
||||
CompareOp::Lt => 0,
|
||||
CompareOp::Le => 1,
|
||||
CompareOp::Eq => 2,
|
||||
CompareOp::Ne => 3,
|
||||
CompareOp::Gt => 4,
|
||||
CompareOp::Ge => 5,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Encodes 8-bit operands directly and prefixes up to three EXTENDED_ARG
|
||||
/// code units for larger operands. Each code unit is [opcode, operand].
|
||||
pub fn encode_all(instructions: &[Self]) -> Result<Vec<u8>, VmError> {
|
||||
let mut bytes = Vec::new();
|
||||
for instruction in instructions {
|
||||
if instruction.opcode == Opcode::ExtendedArg {
|
||||
return Err(invalid(0, "EXTENDED_ARG is emitted by the encoder"));
|
||||
}
|
||||
if !instruction.opcode.takes_argument() && instruction.argument != 0 {
|
||||
return Err(invalid(bytes.len() / 2, "unexpected instruction operand"));
|
||||
}
|
||||
let parts = instruction.argument.to_be_bytes();
|
||||
let first = parts.iter().position(|part| *part != 0).unwrap_or(3);
|
||||
for part in &parts[first..3] {
|
||||
bytes.extend([Opcode::ExtendedArg as u8, *part]);
|
||||
}
|
||||
bytes.extend([instruction.opcode as u8, parts[3]]);
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct DecodedInstruction {
|
||||
/// Offsets count two-byte code units, including EXTENDED_ARG prefixes.
|
||||
pub offset: usize,
|
||||
pub next_offset: usize,
|
||||
pub instruction: Instruction,
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8]) -> Result<Vec<DecodedInstruction>, VmError> {
|
||||
if bytes.is_empty() || !bytes.len().is_multiple_of(2) {
|
||||
return Err(invalid(
|
||||
0,
|
||||
"wordcode must contain complete two-byte code units",
|
||||
));
|
||||
}
|
||||
let mut instructions = Vec::new();
|
||||
let mut cursor = 0;
|
||||
while cursor < bytes.len() {
|
||||
let start = cursor / 2;
|
||||
let mut argument = 0_u32;
|
||||
let mut prefixes = 0;
|
||||
loop {
|
||||
if cursor >= bytes.len() {
|
||||
return Err(invalid(start, "dangling EXTENDED_ARG"));
|
||||
}
|
||||
let opcode = Opcode::decode(bytes[cursor])
|
||||
.ok_or_else(|| invalid(cursor / 2, "unknown opcode"))?;
|
||||
argument = (argument << 8) | u32::from(bytes[cursor + 1]);
|
||||
cursor += 2;
|
||||
if opcode == Opcode::ExtendedArg {
|
||||
prefixes += 1;
|
||||
if prefixes > 3 {
|
||||
return Err(invalid(start, "too many EXTENDED_ARG prefixes"));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if !opcode.takes_argument() && (argument != 0 || prefixes != 0) {
|
||||
return Err(invalid(start, "unexpected instruction operand"));
|
||||
}
|
||||
instructions.push(DecodedInstruction {
|
||||
offset: start,
|
||||
next_offset: cursor / 2,
|
||||
instruction: Instruction::new(opcode, argument),
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(instructions)
|
||||
}
|
||||
|
||||
pub(crate) fn compare_operation(argument: u32) -> Option<CompareOp> {
|
||||
Some(match argument {
|
||||
0 => CompareOp::Lt,
|
||||
1 => CompareOp::Le,
|
||||
2 => CompareOp::Eq,
|
||||
3 => CompareOp::Ne,
|
||||
4 => CompareOp::Gt,
|
||||
5 => CompareOp::Ge,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn invalid(offset: usize, reason: &'static str) -> VmError {
|
||||
VmError::InvalidBytecode { offset, reason }
|
||||
}
|
||||
+33
-2
@@ -1,4 +1,5 @@
|
||||
//! Embeddable object runtime. No language type system, parser or executor.
|
||||
//! Embeddable object runtime and a small wordcode interpreter. No parser or
|
||||
//! Python language type system; wordcode is not CPython bytecode compatible.
|
||||
//!
|
||||
//! ```
|
||||
//! use aster_core::{Value, Vm};
|
||||
@@ -13,19 +14,49 @@
|
||||
//! assert_eq!(vm.object_count(), 0);
|
||||
//! # Ok::<(), aster_core::VmError>(())
|
||||
//! ```
|
||||
//!
|
||||
//! Execute validated wordcode without a source parser:
|
||||
//! ```
|
||||
//! use aster_core::{CodeLayout, EndorCode, Instruction, Opcode, Value, Vm};
|
||||
//! let mut vm = Vm::new()?;
|
||||
//! let globals = vm.new_dict()?;
|
||||
//! let code = EndorCode::from_instructions(
|
||||
//! &[
|
||||
//! Instruction::new(Opcode::LoadConst, 0),
|
||||
//! Instruction::simple(Opcode::ReturnValue),
|
||||
//! ],
|
||||
//! vec![Value::Text("Hello Endor".into())],
|
||||
//! CodeLayout::default(),
|
||||
//! )?;
|
||||
//! let code = vm.new_code(code)?;
|
||||
//! let result = vm.execute(code, globals)?;
|
||||
//! assert_eq!(result, Value::Text("Hello Endor".into()));
|
||||
//! vm.release_value(result)?; // Object results own one external reference.
|
||||
//! vm.release(code)?;
|
||||
//! vm.release(globals)?;
|
||||
//! assert_eq!(vm.object_count(), 0);
|
||||
//! # Ok::<(), aster_core::VmError>(())
|
||||
//! ```
|
||||
|
||||
mod builtins;
|
||||
mod code;
|
||||
mod eval;
|
||||
mod frame;
|
||||
mod heap;
|
||||
mod instruction;
|
||||
mod number;
|
||||
mod object;
|
||||
mod vm;
|
||||
mod vm_builtins;
|
||||
|
||||
pub use builtins::{
|
||||
EndorBaseExceptionObject, EndorCellObject, EndorCode, EndorDictObject, EndorFunctionObject,
|
||||
EndorBaseExceptionObject, EndorCellObject, EndorDictObject, EndorFunctionObject,
|
||||
EndorStringObject, EndorTracebackEntry,
|
||||
};
|
||||
pub use code::{CodeLayout, EndorCode, EndorCodeObject};
|
||||
pub use frame::{EndorFrame, ExecutionLimits, ExecutionLocation};
|
||||
pub use heap::ObjectRef;
|
||||
pub use instruction::{DecodedInstruction, Instruction, Opcode};
|
||||
pub use number::{
|
||||
EndorDecimal, EndorInteger, EndorNumber, EndorNumberObject, EndorRational, EndorReal,
|
||||
};
|
||||
|
||||
@@ -6,8 +6,9 @@ use std::hash::{Hash, Hasher};
|
||||
|
||||
use crate::{EndorNumber, ObjectRef, VmError};
|
||||
|
||||
/// Immediate values stay outside the object heap. Object values are borrowed
|
||||
/// handles: copying or cloning one does not retain an external strong reference.
|
||||
/// Immediate values stay outside the object heap. Copying an ObjectRef or
|
||||
/// cloning a Value never retains a reference; ownership follows the API that
|
||||
/// produced it. In particular, call/execute results own a root until released.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
Nil,
|
||||
@@ -89,8 +90,9 @@ pub trait ObjectPayload: Any + fmt::Debug + Send + Sync {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Object arguments and object results are borrowed handles. Native calls
|
||||
/// compute values without mutating the VM; this is not a language executor.
|
||||
/// Host-call entry: arguments and raw results are borrowed handles. The VM
|
||||
/// validates/retains the result before exposing it as an owned call result.
|
||||
/// Language functions are dispatched by Vm's frame evaluator instead.
|
||||
fn call(&self, _arguments: &[Value]) -> Result<Value, VmError> {
|
||||
Err(VmError::UnsupportedOperation("call"))
|
||||
}
|
||||
@@ -173,6 +175,7 @@ impl ObjectHeader {
|
||||
self.strong_refs
|
||||
}
|
||||
|
||||
/// Host-owned references and live execution roots, excluding heap edges.
|
||||
pub fn external_ref_count(&self) -> usize {
|
||||
self.external_refs
|
||||
}
|
||||
|
||||
+36
-15
@@ -1,7 +1,7 @@
|
||||
use std::fmt;
|
||||
|
||||
use crate::heap::Heap;
|
||||
use crate::{CompareOp, EndorObject, ObjectPayload, ObjectRef, Record, Value};
|
||||
use crate::{CompareOp, EndorObject, ExecutionLocation, ObjectPayload, ObjectRef, Record, Value};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum VmError {
|
||||
@@ -15,6 +15,19 @@ pub enum VmError {
|
||||
InvalidArgument(&'static str),
|
||||
ObjectKindMismatch(&'static str),
|
||||
EmptyCell,
|
||||
InvalidBytecode {
|
||||
offset: usize,
|
||||
reason: &'static str,
|
||||
},
|
||||
UnboundLocal(String),
|
||||
NameNotFound(String),
|
||||
ArgumentCount {
|
||||
name: String,
|
||||
required: usize,
|
||||
maximum: usize,
|
||||
provided: usize,
|
||||
},
|
||||
ExecutionLimit(&'static str),
|
||||
}
|
||||
|
||||
impl fmt::Display for VmError {
|
||||
@@ -32,6 +45,21 @@ impl fmt::Display for VmError {
|
||||
Self::InvalidArgument(message) => f.write_str(message),
|
||||
Self::ObjectKindMismatch(expected) => write!(f, "expected {expected} object"),
|
||||
Self::EmptyCell => f.write_str("cell is empty"),
|
||||
Self::InvalidBytecode { offset, reason } => {
|
||||
write!(f, "invalid bytecode at code unit {offset}: {reason}")
|
||||
}
|
||||
Self::UnboundLocal(name) => write!(f, "local variable is unbound: {name}"),
|
||||
Self::NameNotFound(name) => write!(f, "name not found: {name}"),
|
||||
Self::ArgumentCount {
|
||||
name,
|
||||
required,
|
||||
maximum,
|
||||
provided,
|
||||
} => write!(
|
||||
f,
|
||||
"{name} expects {required}..={maximum} positional arguments, received {provided}"
|
||||
),
|
||||
Self::ExecutionLimit(limit) => write!(f, "execution limit reached: {limit}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,11 +71,15 @@ impl std::error::Error for VmError {}
|
||||
#[derive(Debug)]
|
||||
pub struct Vm {
|
||||
pub(crate) heap: Heap,
|
||||
pub(crate) last_traceback: Vec<ExecutionLocation>,
|
||||
}
|
||||
|
||||
impl Vm {
|
||||
pub fn new() -> Result<Self, VmError> {
|
||||
Ok(Self { heap: Heap::new()? })
|
||||
Ok(Self {
|
||||
heap: Heap::new()?,
|
||||
last_traceback: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn object_count(&self) -> usize {
|
||||
@@ -145,17 +177,6 @@ impl Vm {
|
||||
self.heap.del_attr(reference, name)
|
||||
}
|
||||
|
||||
/// Results are borrowed values; native operations cannot mutate this VM.
|
||||
pub fn call(&self, reference: ObjectRef, arguments: &[Value]) -> Result<Value, VmError> {
|
||||
let payload = self.heap.get(reference)?.payload();
|
||||
for argument in arguments {
|
||||
self.validate_value(argument)?;
|
||||
}
|
||||
let result = payload.call(arguments)?;
|
||||
self.validate_value(&result)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(crate) fn validate_value(&self, value: &Value) -> Result<(), VmError> {
|
||||
if let Value::Object(reference) = value {
|
||||
self.heap.get(*reference)?;
|
||||
@@ -163,8 +184,8 @@ impl Vm {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Explicit full collection of unreachable cycles, using external strong
|
||||
/// references as roots. A tracing execution stack can be added later.
|
||||
/// Explicit full collection. Execution slots use owned roots, so values
|
||||
/// on live execution stacks participate in the same root accounting.
|
||||
pub fn collect_cycles(&mut self) -> usize {
|
||||
self.heap.collect_cycles()
|
||||
}
|
||||
|
||||
@@ -2,15 +2,28 @@ use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use crate::{
|
||||
CompareOp, EndorBaseExceptionObject, EndorCellObject, EndorDictObject, EndorFunctionObject,
|
||||
EndorNumber, EndorNumberObject, EndorStringObject, ObjectPayload, ObjectRef, Value, Vm,
|
||||
VmError,
|
||||
CompareOp, EndorBaseExceptionObject, EndorCellObject, EndorCode, EndorCodeObject,
|
||||
EndorDictObject, EndorFunctionObject, EndorNumber, EndorNumberObject, EndorStringObject,
|
||||
ObjectPayload, ObjectRef, Value, Vm, VmError,
|
||||
};
|
||||
|
||||
impl Vm {
|
||||
pub(crate) fn validate_builtin(&self, payload: &dyn ObjectPayload) -> Result<(), VmError> {
|
||||
if let Some(function) = payload.as_any().downcast_ref::<EndorFunctionObject>() {
|
||||
let code = self
|
||||
.payload::<EndorCodeObject>(function.code(), "code")?
|
||||
.code();
|
||||
if function.defaults().len() > code.parameters().len()
|
||||
|| function.closure().len() != code.free_variables().len()
|
||||
{
|
||||
return Err(VmError::InvalidArgument(
|
||||
"function defaults or closure do not match code",
|
||||
));
|
||||
}
|
||||
self.payload::<EndorDictObject>(function.globals(), "dict")?;
|
||||
if let Some(builtins) = function.builtins() {
|
||||
self.payload::<EndorDictObject>(builtins, "dict")?;
|
||||
}
|
||||
for cell in function.closure() {
|
||||
self.payload::<EndorCellObject>(*cell, "cell")?;
|
||||
}
|
||||
@@ -42,6 +55,37 @@ impl Vm {
|
||||
self.allocate(EndorStringObject::new(text))
|
||||
}
|
||||
|
||||
pub fn new_code(&mut self, code: EndorCode) -> Result<ObjectRef, VmError> {
|
||||
self.allocate(EndorCodeObject::new(code))
|
||||
}
|
||||
|
||||
pub fn new_function(
|
||||
&mut self,
|
||||
name: impl Into<String>,
|
||||
code: ObjectRef,
|
||||
globals: ObjectRef,
|
||||
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()))? {
|
||||
Some(Value::Object(reference)) => {
|
||||
self.payload::<EndorDictObject>(reference, "dict")?;
|
||||
Some(reference)
|
||||
}
|
||||
None => None,
|
||||
_ => return Err(VmError::ObjectKindMismatch("dict")),
|
||||
};
|
||||
self.allocate(EndorFunctionObject::new(
|
||||
name.into(),
|
||||
code,
|
||||
globals,
|
||||
builtins,
|
||||
defaults,
|
||||
closure,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn new_number(&mut self, number: EndorNumber) -> Result<ObjectRef, VmError> {
|
||||
self.allocate(EndorNumberObject::new(number))
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -0,0 +1,761 @@
|
||||
use std::any::Any;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use aster_core::{
|
||||
CodeLayout, CompareOp, EndorCode, EndorCodeObject, EndorNativeFunctionObject, ExecutionLimits,
|
||||
Instruction, ObjectPayload, ObjectRef, Opcode, Value, Vm, VmError,
|
||||
};
|
||||
|
||||
fn op(opcode: Opcode, argument: u32) -> Instruction {
|
||||
Instruction::new(opcode, argument)
|
||||
}
|
||||
fn simple(opcode: Opcode) -> Instruction {
|
||||
Instruction::simple(opcode)
|
||||
}
|
||||
fn text(value: &str) -> Value {
|
||||
Value::Text(value.into())
|
||||
}
|
||||
fn make_code(
|
||||
vm: &mut Vm,
|
||||
instructions: &[Instruction],
|
||||
constants: Vec<Value>,
|
||||
layout: CodeLayout,
|
||||
) -> ObjectRef {
|
||||
vm.new_code(EndorCode::from_instructions(instructions, constants, layout).unwrap())
|
||||
.unwrap()
|
||||
}
|
||||
fn return_constant(vm: &mut Vm, value: Value) -> ObjectRef {
|
||||
make_code(
|
||||
vm,
|
||||
&[op(Opcode::LoadConst, 0), simple(Opcode::ReturnValue)],
|
||||
vec![value],
|
||||
CodeLayout::default(),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wordcode_extended_operands_decode_and_execute_without_truncation() {
|
||||
let encoded = Instruction::encode_all(&[op(Opcode::LoadConst, 0x01020304)]).unwrap();
|
||||
assert_eq!(encoded, vec![255, 1, 255, 2, 255, 3, 1, 4]);
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let mut constants = vec![Value::Nil; 257];
|
||||
constants[256] = text("extended");
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[op(Opcode::LoadConst, 256), simple(Opcode::ReturnValue)],
|
||||
constants,
|
||||
CodeLayout::default(),
|
||||
);
|
||||
let executable = vm.payload::<EndorCodeObject>(code, "code").unwrap().code();
|
||||
assert_eq!(executable.instructions()[0].offset, 0);
|
||||
assert_eq!(executable.instructions()[0].next_offset, 2);
|
||||
assert_eq!(executable.stack_size(), 1);
|
||||
let globals = vm.new_dict().unwrap();
|
||||
assert_eq!(vm.execute(code, globals).unwrap(), text("extended"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_wordcode_and_unaligned_jumps_are_rejected_at_construction() {
|
||||
for bytes in [
|
||||
vec![],
|
||||
vec![0],
|
||||
vec![254, 0],
|
||||
vec![255, 1],
|
||||
vec![255, 1, 255, 1, 255, 1, 255, 1, 1, 0],
|
||||
vec![13, 1],
|
||||
] {
|
||||
assert!(matches!(
|
||||
EndorCode::new(bytes, vec![Value::Nil], vec![], vec![]),
|
||||
Err(VmError::InvalidBytecode { .. })
|
||||
));
|
||||
}
|
||||
// Jump target 2 is inside a prefixed LOAD_CONST starting at code unit 1.
|
||||
let bytes = vec![14, 1, 255, 1, 1, 0, 13, 0];
|
||||
assert!(EndorCode::new(bytes, vec![Value::Nil; 257], vec![], vec![]).is_err());
|
||||
assert!(
|
||||
EndorCode::from_instructions(
|
||||
&[op(Opcode::LoadConst, 1), simple(Opcode::ReturnValue)],
|
||||
vec![Value::Nil],
|
||||
CodeLayout::default()
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verifier_rejects_underflow_fallthrough_bad_cells_and_inconsistent_stack_joins() {
|
||||
let cases = [
|
||||
vec![simple(Opcode::PopTop), simple(Opcode::ReturnValue)],
|
||||
vec![op(Opcode::LoadConst, 0)],
|
||||
vec![op(Opcode::LoadConst, 0), op(Opcode::JumpBackward, 2)],
|
||||
vec![op(Opcode::LoadDeref, 0), simple(Opcode::ReturnValue)],
|
||||
vec![
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::PopJumpIfFalse, 2),
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::JumpForward, 1),
|
||||
simple(Opcode::Nop),
|
||||
op(Opcode::LoadConst, 0),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
];
|
||||
for instructions in cases {
|
||||
assert!(
|
||||
EndorCode::from_instructions(&instructions, vec![Value::Nil], CodeLayout::default())
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
let layout = CodeLayout {
|
||||
locals: vec!["captured".into()],
|
||||
cell_variables: vec!["captured".into()],
|
||||
..CodeLayout::default()
|
||||
};
|
||||
assert!(
|
||||
EndorCode::from_instructions(
|
||||
&[op(Opcode::LoadFast, 0), simple(Opcode::ReturnValue)],
|
||||
vec![],
|
||||
layout
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn execute_moves_values_between_constants_local_slots_and_globals() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::StoreFast, 0),
|
||||
op(Opcode::LoadFast, 0),
|
||||
op(Opcode::StoreGlobal, 0),
|
||||
op(Opcode::LoadGlobal, 0),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![text("hello")],
|
||||
CodeLayout {
|
||||
locals: vec!["local".into()],
|
||||
names: vec!["saved".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
assert_eq!(vm.execute(code, globals).unwrap(), text("hello"));
|
||||
assert_eq!(
|
||||
vm.dict_get(globals, &text("saved")).unwrap(),
|
||||
Some(text("hello"))
|
||||
);
|
||||
assert!(vm.last_traceback().is_empty());
|
||||
vm.release(code).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Tick(Arc<AtomicUsize>);
|
||||
impl ObjectPayload for Tick {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"tick"
|
||||
}
|
||||
fn call(&self, arguments: &[Value]) -> Result<Value, VmError> {
|
||||
assert!(arguments.is_empty());
|
||||
Ok(Value::Bool(self.0.fetch_add(1, Ordering::SeqCst) < 3))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relative_branches_and_back_edges_execute_a_loop() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let count = Arc::new(AtomicUsize::new(0));
|
||||
let tick = vm.allocate(Tick(count.clone())).unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
vm.dict_set(globals, text("tick"), Value::Object(tick))
|
||||
.unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadGlobal, 0),
|
||||
simple(Opcode::PushNull),
|
||||
op(Opcode::Call, 0),
|
||||
op(Opcode::PopJumpIfFalse, 1),
|
||||
op(Opcode::JumpBackward, 5),
|
||||
op(Opcode::LoadConst, 0),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![text("done")],
|
||||
CodeLayout {
|
||||
names: vec!["tick".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
assert_eq!(vm.execute(code, globals).unwrap(), text("done"));
|
||||
assert_eq!(count.load(Ordering::SeqCst), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_language_calls_bind_defaults_and_return_owned_argument_objects() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let identity = make_code(
|
||||
&mut vm,
|
||||
&[op(Opcode::LoadFast, 0), simple(Opcode::ReturnValue)],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
parameters: vec!["value".into()],
|
||||
locals: vec!["value".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let default = vm.new_string("default").unwrap();
|
||||
let function = vm
|
||||
.new_function(
|
||||
"identity",
|
||||
identity,
|
||||
globals,
|
||||
vec![Value::Object(default)],
|
||||
vec![],
|
||||
)
|
||||
.unwrap();
|
||||
let before = vm.ref_count(default).unwrap();
|
||||
let returned = vm.call(function, &[]).unwrap();
|
||||
assert_eq!(returned, Value::Object(default));
|
||||
assert_eq!(vm.ref_count(default).unwrap(), before + 1);
|
||||
vm.release_value(returned).unwrap();
|
||||
assert_eq!(vm.ref_count(default).unwrap(), before);
|
||||
vm.dict_set(globals, text("identity"), Value::Object(function))
|
||||
.unwrap();
|
||||
let argument = vm.new_string("argument").unwrap();
|
||||
let wrapper = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadGlobal, 0),
|
||||
simple(Opcode::PushNull),
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::Call, 1),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Object(argument)],
|
||||
CodeLayout {
|
||||
names: vec!["identity".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let before = vm.ref_count(argument).unwrap();
|
||||
let returned = vm.execute(wrapper, globals).unwrap();
|
||||
assert_eq!(returned, Value::Object(argument));
|
||||
assert_eq!(vm.ref_count(argument).unwrap(), before + 1);
|
||||
vm.release_value(returned).unwrap();
|
||||
assert_eq!(vm.ref_count(argument).unwrap(), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn argument_count_and_foreign_argument_failures_do_not_retain_inputs() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[op(Opcode::LoadFast, 0), simple(Opcode::ReturnValue)],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
parameters: vec!["value".into()],
|
||||
locals: vec!["value".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let function = vm
|
||||
.new_function("identity", code, globals, vec![], vec![])
|
||||
.unwrap();
|
||||
assert!(matches!(
|
||||
vm.call(function, &[]),
|
||||
Err(VmError::ArgumentCount {
|
||||
required: 1,
|
||||
provided: 0,
|
||||
..
|
||||
})
|
||||
));
|
||||
assert!(matches!(
|
||||
vm.call(function, &[Value::Nil, Value::Nil]),
|
||||
Err(VmError::ArgumentCount {
|
||||
maximum: 1,
|
||||
provided: 2,
|
||||
..
|
||||
})
|
||||
));
|
||||
let mut other = Vm::new().unwrap();
|
||||
let foreign = other.new_object().unwrap();
|
||||
let before = vm.ref_count(function).unwrap();
|
||||
assert_eq!(
|
||||
vm.call(function, &[Value::Object(foreign)]),
|
||||
Err(VmError::ForeignHandle)
|
||||
);
|
||||
assert_eq!(vm.ref_count(function).unwrap(), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn freshly_built_maps_and_their_children_survive_frame_cleanup() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let child = vm.new_string("owned by result").unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::LoadConst, 1),
|
||||
op(Opcode::BuildMap, 1),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![text("child"), Value::Object(child)],
|
||||
CodeLayout::default(),
|
||||
);
|
||||
vm.release(child).unwrap();
|
||||
let result = vm.execute(code, globals).unwrap();
|
||||
vm.release(code).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
let Value::Object(dictionary) = result else {
|
||||
panic!("expected a map");
|
||||
};
|
||||
assert_eq!(vm.ref_count(dictionary).unwrap(), 1);
|
||||
assert_eq!(
|
||||
vm.dict_get(dictionary, &text("child")).unwrap(),
|
||||
Some(Value::Object(child))
|
||||
);
|
||||
assert_eq!(vm.str(child).unwrap(), "owned by result");
|
||||
vm.release(dictionary).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
fn echo(arguments: &[Value]) -> Result<Value, VmError> {
|
||||
arguments
|
||||
.first()
|
||||
.cloned()
|
||||
.ok_or(VmError::InvalidArgument("expected an argument"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_return_of_a_temporary_argument_is_retained_before_inputs_are_popped() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let function = vm
|
||||
.allocate(EndorNativeFunctionObject::new("echo", echo))
|
||||
.unwrap();
|
||||
vm.dict_set(globals, text("echo"), Value::Object(function))
|
||||
.unwrap();
|
||||
vm.release(function).unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadGlobal, 0),
|
||||
simple(Opcode::PushNull),
|
||||
op(Opcode::BuildMap, 0),
|
||||
op(Opcode::Call, 1),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
names: vec!["echo".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let result = vm.execute(code, globals).unwrap();
|
||||
vm.release(code).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
assert_eq!(vm.object_count(), 1);
|
||||
vm.release_value(result).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
fn fail(_arguments: &[Value]) -> Result<Value, VmError> {
|
||||
Err(VmError::InvalidArgument("host failure"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_failure_unwinds_temporary_roots_and_keeps_execution_locations() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let function = vm
|
||||
.allocate(EndorNativeFunctionObject::new("fail", fail))
|
||||
.unwrap();
|
||||
vm.dict_set(globals, text("fail"), Value::Object(function))
|
||||
.unwrap();
|
||||
let code = EndorCode::from_instructions(
|
||||
&[
|
||||
op(Opcode::LoadGlobal, 0),
|
||||
simple(Opcode::PushNull),
|
||||
op(Opcode::BuildMap, 0),
|
||||
op(Opcode::Call, 1),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
names: vec!["fail".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
.with_location("outer", "demo.endor", 12);
|
||||
let code = vm.new_code(code).unwrap();
|
||||
let before = vm.object_count();
|
||||
assert_eq!(
|
||||
vm.execute(code, globals),
|
||||
Err(VmError::InvalidArgument("host failure"))
|
||||
);
|
||||
assert_eq!(vm.object_count(), before);
|
||||
assert_eq!(vm.last_traceback()[0].function, "outer");
|
||||
assert_eq!(vm.last_traceback()[0].instruction_offset, 3);
|
||||
assert_eq!(vm.last_traceback()[0].filename, "demo.endor");
|
||||
assert_eq!(vm.last_traceback()[0].first_line, 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recursion_uses_explicit_frames_and_unwinds_at_the_frame_limit() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadGlobal, 0),
|
||||
simple(Opcode::PushNull),
|
||||
op(Opcode::Call, 0),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
names: vec!["recurse".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let function = vm
|
||||
.new_function("recurse", code, globals, vec![], vec![])
|
||||
.unwrap();
|
||||
vm.dict_set(globals, text("recurse"), Value::Object(function))
|
||||
.unwrap();
|
||||
let roots = vm.ref_count(function).unwrap();
|
||||
assert_eq!(
|
||||
vm.call_with_limits(
|
||||
function,
|
||||
&[],
|
||||
ExecutionLimits {
|
||||
max_frames: 64,
|
||||
max_instructions: 1000
|
||||
}
|
||||
),
|
||||
Err(VmError::ExecutionLimit("frames"))
|
||||
);
|
||||
assert_eq!(vm.last_traceback().len(), 64);
|
||||
assert_eq!(vm.ref_count(function).unwrap(), roots);
|
||||
vm.release(function).unwrap();
|
||||
vm.release(code).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
assert_eq!(vm.collect_cycles(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instruction_limit_stops_a_valid_infinite_loop_without_leaking_frames() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[op(Opcode::JumpBackward, 1)],
|
||||
vec![],
|
||||
CodeLayout::default(),
|
||||
);
|
||||
let before = vm.object_count();
|
||||
assert_eq!(
|
||||
vm.execute_with_limits(
|
||||
code,
|
||||
globals,
|
||||
ExecutionLimits {
|
||||
max_frames: 8,
|
||||
max_instructions: 3
|
||||
}
|
||||
),
|
||||
Err(VmError::ExecutionLimit("instructions"))
|
||||
);
|
||||
assert_eq!(vm.object_count(), before);
|
||||
assert_eq!(vm.last_traceback().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_globals_shadow_builtins_and_capture_the_original_builtins_dictionary() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let builtins = vm.new_dict().unwrap();
|
||||
vm.dict_set(builtins, text("answer"), 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!["answer".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let function = vm
|
||||
.new_function("read", code, globals, vec![], vec![])
|
||||
.unwrap();
|
||||
let replacement = vm.new_dict().unwrap();
|
||||
vm.dict_set(globals, text("__builtins__"), Value::Object(replacement))
|
||||
.unwrap();
|
||||
vm.release(builtins).unwrap();
|
||||
assert_eq!(vm.call(function, &[]).unwrap(), text("builtin"));
|
||||
vm.dict_set(globals, text("answer"), text("global"))
|
||||
.unwrap();
|
||||
assert_eq!(vm.call(function, &[]).unwrap(), text("global"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_free_variable_cells_are_read_written_and_deleted_by_bytecode() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let cell = vm.new_cell(Some(text("first"))).unwrap();
|
||||
let read_code = make_code(
|
||||
&mut vm,
|
||||
&[op(Opcode::LoadDeref, 0), simple(Opcode::ReturnValue)],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
free_variables: vec!["x".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let write_code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadFast, 0),
|
||||
op(Opcode::StoreDeref, 1),
|
||||
op(Opcode::LoadConst, 0),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Nil],
|
||||
CodeLayout {
|
||||
parameters: vec!["value".into()],
|
||||
locals: vec!["value".into()],
|
||||
free_variables: vec!["x".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let clear_code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::DeleteDeref, 0),
|
||||
op(Opcode::LoadConst, 0),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Nil],
|
||||
CodeLayout {
|
||||
free_variables: vec!["x".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let read = vm
|
||||
.new_function("read", read_code, globals, vec![], vec![cell])
|
||||
.unwrap();
|
||||
let write = vm
|
||||
.new_function("write", write_code, globals, vec![], vec![cell])
|
||||
.unwrap();
|
||||
let clear = vm
|
||||
.new_function("clear", clear_code, globals, vec![], vec![cell])
|
||||
.unwrap();
|
||||
assert_eq!(vm.call(read, &[]).unwrap(), text("first"));
|
||||
vm.call(write, &[text("updated")]).unwrap();
|
||||
assert_eq!(vm.call(read, &[]).unwrap(), text("updated"));
|
||||
vm.call(clear, &[]).unwrap();
|
||||
assert_eq!(vm.call(read, &[]), Err(VmError::UnboundLocal("x".into())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_argument_cells_keep_their_contents_after_the_frame_returns() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[op(Opcode::LoadClosure, 0), simple(Opcode::ReturnValue)],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
parameters: vec!["x".into()],
|
||||
locals: vec!["x".into()],
|
||||
cell_variables: vec!["x".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let function = vm
|
||||
.new_function("capture", code, globals, vec![], vec![])
|
||||
.unwrap();
|
||||
let value = vm.new_string("captured").unwrap();
|
||||
let returned = vm.call(function, &[Value::Object(value)]).unwrap();
|
||||
vm.release(value).unwrap();
|
||||
let Value::Object(cell) = returned else {
|
||||
panic!("expected cell");
|
||||
};
|
||||
assert_eq!(vm.cell_get(cell).unwrap(), Value::Object(value));
|
||||
assert_eq!(vm.str(value).unwrap(), "captured");
|
||||
vm.release(cell).unwrap();
|
||||
assert!(matches!(vm.object(value), Err(VmError::InvalidHandle)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attribute_operations_and_stack_copies_transfer_roots_without_leaks() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadFast, 1),
|
||||
op(Opcode::LoadFast, 0),
|
||||
op(Opcode::StoreAttr, 0),
|
||||
op(Opcode::LoadFast, 0),
|
||||
op(Opcode::LoadAttr, 0),
|
||||
op(Opcode::Copy, 1),
|
||||
op(Opcode::Swap, 2),
|
||||
simple(Opcode::PopTop),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
parameters: vec!["record".into(), "value".into()],
|
||||
locals: vec!["record".into(), "value".into()],
|
||||
names: vec!["x".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
let function = vm
|
||||
.new_function("assign", code, globals, vec![], vec![])
|
||||
.unwrap();
|
||||
let record = vm.new_object().unwrap();
|
||||
let value = vm.new_string("value").unwrap();
|
||||
let returned = vm
|
||||
.call(function, &[Value::Object(record), Value::Object(value)])
|
||||
.unwrap();
|
||||
assert_eq!(vm.ref_count(value).unwrap(), 3); // host, attribute, return value
|
||||
vm.release_value(returned).unwrap();
|
||||
vm.release(record).unwrap();
|
||||
assert_eq!(vm.ref_count(value).unwrap(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn comparison_truth_and_forward_conditionals_use_runtime_values() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::LoadConst, 1),
|
||||
Instruction::compare(CompareOp::Lt),
|
||||
simple(Opcode::ToBool),
|
||||
simple(Opcode::UnaryNot),
|
||||
op(Opcode::PopJumpIfTrue, 2),
|
||||
op(Opcode::LoadConst, 2),
|
||||
simple(Opcode::ReturnValue),
|
||||
op(Opcode::LoadConst, 3),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![text("a"), text("b"), text("less"), text("other")],
|
||||
CodeLayout::default(),
|
||||
);
|
||||
assert_eq!(vm.execute(code, globals).unwrap(), text("less"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbound_names_deleted_locals_and_invalid_null_values_fail_cleanly() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let missing = make_code(
|
||||
&mut vm,
|
||||
&[op(Opcode::LoadGlobal, 0), simple(Opcode::ReturnValue)],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
names: vec!["missing".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
vm.execute(missing, globals),
|
||||
Err(VmError::NameNotFound("missing".into()))
|
||||
);
|
||||
let deleted = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::StoreFast, 0),
|
||||
op(Opcode::DeleteFast, 0),
|
||||
op(Opcode::LoadFast, 0),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Nil],
|
||||
CodeLayout {
|
||||
locals: vec!["x".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
vm.execute(deleted, globals),
|
||||
Err(VmError::UnboundLocal("x".into()))
|
||||
);
|
||||
let null = make_code(
|
||||
&mut vm,
|
||||
&[simple(Opcode::PushNull), simple(Opcode::ReturnValue)],
|
||||
vec![],
|
||||
CodeLayout::default(),
|
||||
);
|
||||
assert!(matches!(
|
||||
vm.execute(null, globals),
|
||||
Err(VmError::InvalidBytecode { .. })
|
||||
));
|
||||
let success = return_constant(&mut vm, Value::Nil);
|
||||
assert_eq!(vm.execute(success, globals).unwrap(), Value::Nil);
|
||||
assert!(vm.last_traceback().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_map_construction_cleans_both_partial_map_and_temporary_input() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::BuildMap, 0),
|
||||
op(Opcode::BuildMap, 1),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Number(1_i64.into())],
|
||||
CodeLayout::default(),
|
||||
);
|
||||
let before = vm.object_count();
|
||||
assert_eq!(
|
||||
vm.execute(code, globals),
|
||||
Err(VmError::UnsupportedOperation("numeric hash"))
|
||||
);
|
||||
assert_eq!(vm.object_count(), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn implicit_receiver_slot_is_distinct_from_the_null_call_marker() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let function = vm
|
||||
.allocate(EndorNativeFunctionObject::new("echo", echo))
|
||||
.unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::LoadConst, 1),
|
||||
op(Opcode::Call, 0),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Object(function), Value::Nil],
|
||||
CodeLayout::default(),
|
||||
);
|
||||
// Nil is an actual implicit argument here, not the internal NULL marker.
|
||||
assert_eq!(vm.execute(code, globals).unwrap(), Value::Nil);
|
||||
}
|
||||
Reference in New Issue
Block a user