pub trait Globals<'a>: Machine<'a> {
// Required methods
fn get<'b>(
&'b self,
name: &str
) -> Result<Self::Datum<'b, 'static>, MachineError>;
fn set<'b, 'c>(
&'b self,
name: &str,
datum: Self::Datum<'b, 'c>
) -> Result<(), MachineError>;
}
Expand description
Machine
s that support global variables
Example
use sandkiste::prelude::*;
fn call_func_with_globals<'a, 'b, M, F>(machine: &'b M, func: F, a: i32, b: i32) -> i32
where
M: Machine<'a> + Globals<'a>,
for<'c> <M as Machine<'a>>::Datum<'b, 'c>: MaybeInteger,
F: Function,
{
machine.set("a", a.into());
machine.set("b", b.into());
func.call([]).expect("runtime error");
machine.get("c").expect("runtime error").try_as_i32().expect("type error")
}