stak_engine/
engine.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::{primitive_set::EnginePrimitiveSet, EngineError};
use any_fn::AnyFn;
use stak_dynamic::SchemeValue;
use stak_module::Module;
use stak_vm::{Error, Value, Vm};

/// A scripting engine.
pub struct Engine<'a, 'b> {
    vm: Vm<'a, EnginePrimitiveSet<'a, 'b>>,
}

impl<'a, 'b> Engine<'a, 'b> {
    /// Creates a scripting engine.
    pub fn new(heap: &'a mut [Value], functions: &'a mut [AnyFn<'b>]) -> Result<Self, Error> {
        Ok(Self {
            vm: Vm::new(heap, EnginePrimitiveSet::new(functions))?,
        })
    }

    /// Runs a module.
    pub fn run(&mut self, module: &'static impl Module<'static>) -> Result<(), EngineError> {
        self.vm.initialize(module.bytecode().iter().copied())?;
        self.vm.run()
    }

    /// Registers a type compatible between Scheme and Rust.
    ///
    /// We register all types that this crate implements [`SchemeValue`] for to
    /// the engines by default.
    ///
    /// For more information, see
    /// [`DynamicPrimitiveSet`][stak_dynamic::DynamicPrimitiveSet].
    pub fn register_type<T: SchemeValue + 'static>(&mut self) {
        self.vm
            .primitive_set_mut()
            .dynamic_mut()
            .register_type::<T>()
    }
}