use super::Thunkable;
pub struct CodeEmitter {
thunks: Vec<Box<dyn Thunkable>>,
}
impl CodeEmitter {
pub fn new() -> Self {
CodeEmitter { thunks: Vec::new() }
}
pub fn emit(&self, base: *const ()) -> Vec<u8> {
let mut result = Vec::with_capacity(self.len());
let mut base = base as usize;
for thunk in &self.thunks {
let code = thunk.generate(base);
assert_eq!(code.len(), thunk.len());
base += thunk.len();
result.extend(code);
}
result
}
pub fn add_thunk(&mut self, thunk: Box<dyn Thunkable>) {
self.thunks.push(thunk);
}
pub fn len(&self) -> usize {
self.thunks.iter().fold(0, |sum, thunk| sum + thunk.len())
}
}