feat(vm): make VM no-std usable

This commit is contained in:
Alain Zscheile 2022-09-28 03:19:21 +02:00
parent d193a66f04
commit 9c770e4015
5 changed files with 35 additions and 19 deletions

1
Cargo.lock generated
View file

@ -111,6 +111,7 @@ name = "fogtix-vm-cli"
version = "0.1.0"
dependencies = [
"clap",
"fogtix-bytecode",
"fogtix-vm",
"readfilez",
"tracing-subscriber",

View file

@ -113,7 +113,7 @@ impl crate::Parse<'_> for Pointer {
type Err = ();
fn parse(inp: &[u8]) -> Result<(&[u8], Self), ()> {
if inp.len() < 16 {
return Err(());
Err(())
} else {
let (this, next) = inp.split_at(16);
Ok((next, Self(this.try_into().unwrap())))

View file

@ -10,3 +10,7 @@ clap = "3.2"
fogtix-vm.path = "../fogtix-vm"
readfilez = "0.3"
tracing-subscriber = "0.3"
[dependencies.fogtix-bytecode]
path = "../fogtix-bytecode"
features = ["std"]

View file

@ -11,7 +11,6 @@ tracing = "0.1"
[dependencies.fogtix-bytecode]
path = "../fogtix-bytecode"
features = ["std"]
[dev-dependencies]

View file

@ -1,5 +1,11 @@
#![no_std]
#![forbid(unsafe_code)]
extern crate alloc;
use alloc::{format, string::String, string::ToString, sync::Arc, vec::Vec};
use core::fmt;
use fogtix_bytecode::{consts, Atom, Instr, Parse, Pointer, Value as BcValue};
use std::sync::Arc;
pub type Module = Arc<dyn ModuleKind>;
@ -49,33 +55,39 @@ pub enum StackEntValue {
Pointer(Pointer),
}
#[derive(Debug, thiserror::Error)]
#[derive(Clone, Debug)]
pub enum Error {
#[error("`{0}` arrived at non-jump target `{1}` @ {2}")]
InvalidJumpTarget(&'static str, String, usize),
#[error("instruction pointer out-of-bounds")]
InstrpOutOfBounds,
#[error("reached unparsable instruction: {0:?}")]
UnparsableInstruction(Vec<u8>),
#[error("out of fuel")]
OutOfFuel,
#[error("not enough operands on stack")]
NotEnoughStacked,
#[error("operand from stack doesn't have correct data type: expected={expected}, got={got}")]
StackedInvalidType { expected: &'static str, got: String },
#[error("tried to divide by zero")]
DivisionByZero,
#[error("tried to call remote @ {:x?}", 0.0)]
RemoteCall(Pointer),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidJumpTarget(a, b, c) => {
write!(f, "`{}` arrived at non-jump target `{}` @ {}", a, b, c)
}
Self::InstrpOutOfBounds => write!(f, "instruction pointer out-of-bounds"),
Self::UnparsableInstruction(x) => write!(f, "reached unparsable instruction: {:x?}", x),
Self::OutOfFuel => write!(f, "out of fuel"),
Self::NotEnoughStacked => write!(f, "not enough operands on stack"),
Self::StackedInvalidType { expected, got } => write!(
f,
"operand from stack doesn't have correct data type: expected={}, got={}",
expected, got
),
Self::DivisionByZero => write!(f, "tried to divide by zero"),
Self::RemoteCall(ptr) => write!(f, "tried to call remote @ {:x?}", ptr.0),
}
}
}
pub struct Process {
pub stack: Vec<StackEntValue>,
pub callstack: Vec<InstrPtr>,