stak_engine/
engine.rs

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
9/// A scripting engine.
10pub 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    /// Creates a scripting engine.
16    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    /// Runs a module synchronously.
23    ///
24    /// # Panics
25    ///
26    /// Panics if asynchronous operations occur during the run.
27    pub fn run<'c>(&mut self, module: &'c impl Module<'c>) -> Result<(), EngineError> {
28        block_on!(self.run_async(module))
29    }
30
31    /// Runs a module.
32    #[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        maybe_await!(self.vm.run_async(module.bytecode().iter().copied()))
36    }
37
38    /// Registers a type compatible between Scheme and Rust.
39    ///
40    /// We register all types that this crate implements [`SchemeValue`] for to
41    /// the engines by default.
42    ///
43    /// For more information, see
44    /// [`DynamicPrimitiveSet`][stak_dynamic::DynamicPrimitiveSet].
45    pub fn register_type<T: SchemeValue<H> + 'static>(&mut self) {
46        self.vm
47            .primitive_set_mut()
48            .dynamic_mut()
49            .register_type::<T>()
50    }
51}