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

This commit is contained in:
2026-10-18 20:41:56 +08:00
parent 50e8fcb5e7
commit 6fa95b64b0
18 changed files with 2327 additions and 152 deletions
+21 -67
View File
@@ -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 [&parameters, &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)]
+375
View File
@@ -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)
}
+535
View File
@@ -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"),
}
}
}
}
+79
View File
@@ -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,
}
}
}
+203
View File
@@ -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
View File
@@ -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,
};
+7 -4
View File
@@ -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
View File
@@ -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()
}
+47 -3
View File
@@ -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))
}