use crate::{
compiler::{CompilationResult, CompilationStage},
jit::JITExecutor,
};
use std::{collections::HashMap, sync::Arc};
use typescript_ir::Program;
use typescript_types::{TsError, TsValue};
#[derive(Debug, Clone)]
pub struct Executor {
current_stage: CompilationStage,
jit_executor: Arc<JITExecutor>,
globals: HashMap<String, TsValue>,
}
impl Executor {
pub fn new() -> Self {
Self { current_stage: CompilationStage::Execution, jit_executor: Arc::new(JITExecutor::new()), globals: HashMap::new() }
}
pub fn execute(&mut self, ir: &Program) -> CompilationResult<TsValue> {
self.current_stage = CompilationStage::Execution;
let mut executor = (*self.jit_executor).clone();
match executor.execute(ir, &self.globals) {
Ok(value) => CompilationResult::Success(value),
Err(error) => CompilationResult::Error(error),
}
}
pub fn set_global(&mut self, name: String, value: TsValue) {
self.globals.insert(name, value);
}
pub fn get_global(&self, name: &str) -> Option<&TsValue> {
self.globals.get(name)
}
pub fn current_stage(&self) -> CompilationStage {
self.current_stage
}
pub fn reset(&mut self) {
self.current_stage = CompilationStage::Execution;
self.jit_executor = Arc::new(JITExecutor::new());
self.globals.clear();
}
}
impl Default for Executor {
fn default() -> Self {
Self::new()
}
}