1use crate::{EngineError, primitive_set::EnginePrimitiveSet};
2use any_fn::AnyFn;
3use stak_dynamic::SchemeValue;
4use stak_module::Module;
5use stak_util::block_on;
6use stak_vm::{Error, Heap, Vm};
7use winter_maybe_async::{maybe_async, maybe_await};
8
9pub struct Engine<'a, 'b, H: Heap> {
11 vm: Vm<'a, EnginePrimitiveSet<'a, 'b, H>, H>,
12}
13
14impl<'a, 'b, H: Heap> Engine<'a, 'b, H> {
15 pub fn new(heap: H, functions: &'a mut [(&'a str, AnyFn<'b>)]) -> Result<Self, Error> {
17 Ok(Self {
18 vm: Vm::new(heap, EnginePrimitiveSet::new(functions))?,
19 })
20 }
21
22 pub fn run<'c>(&mut self, module: &'c impl Module<'c>) -> Result<(), EngineError> {
28 block_on!(self.run_async(module))
29 }
30
31 #[cfg_attr(not(feature = "async"), doc(hidden))]
33 #[maybe_async]
34 pub fn run_async<'c>(&mut self, module: &'c impl Module<'c>) -> Result<(), EngineError> {
35 self.vm.initialize(module.bytecode().iter().copied())?;
36 maybe_await!(self.vm.run_async())
37 }
38
39 pub fn register_type<T: SchemeValue<H> + 'static>(&mut self) {
47 self.vm
48 .primitive_set_mut()
49 .dynamic_mut()
50 .register_type::<T>()
51 }
52}