[][src]Function itsy::vm

Important traits for VM<T, U>
pub fn vm<T, U>(program: &str) -> VM<T, U> where
    T: VMFunc<T> + VMData<T, U>,
    U: Default

One stop shop to parse, resolve and compile given Itsy source code and create a VM for it. Program execution starts from the "main" function.

Call run on the returned VM struct to execute the program.

The following example defines a VM with an i32 context (for custom data) and makes the rust functions print and send available to it.

use itsy::*;

extern_rust!(MyFns, i32, {
    /// Prints given i32 value.
    fn print(vm: &mut VM, value: i32) {
        println!("print: {}", value);
    }
    /// Sets VM context to given value.
    fn send(vm: &mut VM, value: i32) {
        *vm.context() = value;
    }
});

fn main() {
    let mut vm = vm::<MyFns, i32>("
        fn main() {
            let x = 1;
            let y = 2;
            print(x+y);
            send(x+y);
        }
    ");

    vm.run();
    println!("vm sent: {}", vm.into_context());
}

Output:

print: 3
vm sent: 3