mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Replace VsPointer with cached decoding
This commit is contained in:
44
valuescript_vm/src/bytecode.rs
Normal file
44
valuescript_vm/src/bytecode.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use std::{cell::RefCell, collections::HashMap, fmt, ops::Index, rc::Rc, slice::SliceIndex};
|
||||
|
||||
use crate::{bytecode_decoder::BytecodeDecoder, vs_value::Val};
|
||||
|
||||
pub struct Bytecode {
|
||||
pub code: Vec<u8>,
|
||||
pub cache: RefCell<HashMap<usize, Val>>,
|
||||
}
|
||||
|
||||
impl<I: SliceIndex<[u8]>> Index<I> for Bytecode {
|
||||
type Output = I::Output;
|
||||
|
||||
fn index(&self, index: I) -> &Self::Output {
|
||||
&self.code[index]
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Bytecode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Bytecode {{ code: {:?} }}", self.code)
|
||||
}
|
||||
}
|
||||
|
||||
impl Bytecode {
|
||||
pub fn new(code: Vec<u8>) -> Bytecode {
|
||||
Bytecode {
|
||||
code,
|
||||
cache: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait DecoderMaker {
|
||||
fn decoder(&self, pos: usize) -> BytecodeDecoder;
|
||||
}
|
||||
|
||||
impl DecoderMaker for Rc<Bytecode> {
|
||||
fn decoder(&self, pos: usize) -> BytecodeDecoder {
|
||||
BytecodeDecoder {
|
||||
bytecode: self.clone(),
|
||||
pos,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user