refactor(vm): split NOOP_* stuff from rest

This commit is contained in:
Alain Zscheile 2022-09-23 19:57:02 +02:00
parent 17efdff6f5
commit d5148669b9
2 changed files with 39 additions and 32 deletions

View file

@ -1,49 +1,25 @@
use fogtix_bytecode::{Atom, Parse, Pointer, Value as BcValue};
use once_cell::sync::Lazy;
use std::marker::PhantomData;
use std::path::PathBuf;
use std::sync::Arc;
struct Module {
mod noop;
use noop::NOOP_ORIGIN;
pub struct Module {
h: readfilez::FileHandle,
}
type InstrPtr = (Arc<Module>, usize);
trait Origin: Send + Sync + core::fmt::Debug {
pub trait Origin: Send + Sync + core::fmt::Debug {
fn call(&self, p: &Pointer, a: &Atom, stack: &mut Vec<StackEntValue>) -> InstrPtr;
fn incr_refcount(&self, p: &Pointer);
fn decr_refcount(&self, p: &Pointer);
}
static NOOP_MODULE: Lazy<Arc<Module>> = Lazy::new(|| {
let mut v = Vec::new();
use fogtix_bytecode::Instr;
Instr::Label.write_to(&mut v).unwrap();
Instr::Return.write_to(&mut v).unwrap();
Arc::new(Module {
h: readfilez::FileHandle::Buffered(v),
})
});
#[derive(Debug)]
struct NoopOrigin;
impl Origin for NoopOrigin {
fn call(&self, p: &Pointer, a: &Atom, _stack: &mut Vec<StackEntValue>) -> InstrPtr {
eprintln!(
"WARN: tried to invoke pointer {:?}({:?}) without valid context",
p, a
);
(Arc::clone(&NOOP_MODULE), 0)
}
fn incr_refcount(&self, _p: &Pointer) {}
fn decr_refcount(&self, _p: &Pointer) {}
}
static NOOP_ORIGIN: Lazy<Arc<dyn Origin>> = Lazy::new(|| Arc::new(NoopOrigin));
#[derive(Debug)]
struct WrappedPointer {
pub struct WrappedPointer {
orig: Arc<dyn Origin>,
p: Pointer,
_h: PhantomData<*const StackEntValue>,
@ -70,7 +46,7 @@ impl Drop for WrappedPointer {
}
#[derive(Clone, Debug)]
enum StackEntValue {
pub enum StackEntValue {
Bytes(Vec<u8>),
Int(u64),
Atom(Atom),
@ -191,7 +167,7 @@ impl Process {
BcValue::Int(i) => StackEntValue::Int(i),
BcValue::Atom(a) => StackEntValue::Atom(a),
BcValue::Pointer(p) => StackEntValue::Pointer(WrappedPointer {
orig: Arc::clone(Lazy::force(&NOOP_ORIGIN)),
orig: Arc::clone(&*NOOP_ORIGIN),
p,
_h: PhantomData,
}),

View file

@ -0,0 +1,31 @@
use fogtix_bytecode::{Atom, Pointer};
use once_cell::sync::Lazy;
use std::sync::Arc;
use crate::{InstrPtr, Module, Origin, StackEntValue};
pub static NOOP_MODULE: Lazy<Arc<Module>> = Lazy::new(|| {
let mut v = Vec::new();
use fogtix_bytecode::Instr;
Instr::Label.write_to(&mut v).unwrap();
Instr::Return.write_to(&mut v).unwrap();
Arc::new(Module {
h: readfilez::FileHandle::Buffered(v),
})
});
#[derive(Debug)]
struct NoopOrigin;
impl Origin for NoopOrigin {
fn call(&self, p: &Pointer, a: &Atom, _stack: &mut Vec<StackEntValue>) -> InstrPtr {
eprintln!(
"WARN: tried to invoke pointer {:?}({:?}) without valid context",
p, a
);
(Arc::clone(&NOOP_MODULE), 0)
}
fn incr_refcount(&self, _p: &Pointer) {}
fn decr_refcount(&self, _p: &Pointer) {}
}
pub static NOOP_ORIGIN: Lazy<Arc<dyn Origin>> = Lazy::new(|| Arc::new(NoopOrigin));