Struct runestick::Vm[][src]

pub struct Vm { /* fields omitted */ }
Expand description

A stack which references variables indirectly from a slab.

Implementations

Construct a new runestick virtual machine.

Construct a new runestick virtual machine.

Run the given vm to completion.

If any async instructions are encountered, this will error.

Run the given vm to completion with support for async functions.

Test if the virtual machine is the same context and unit as specified.

Set the current instruction pointer.

Get the stack.

Get the stack.

Get the stack mutably.

Access the context related to the virtual machine.

Access the underlying unit of the virtual machine.

Access the current instruction pointer.

Reset this virtual machine, freeing all memory used.

Modify the current instruction pointer.

Call the function identified by the given name.

Computing the function hash from the name can be a bit costly, so it’s worth noting that it can be precalculated:

use runestick::{Hash, Item};

let name = Hash::type_hash(&["main"]);

Examples

use runestick::{Context, Unit, FromValue, Source};
use std::sync::Arc;

fn main() -> runestick::Result<()> {
    let context = Context::with_default_modules()?;
    let context = Arc::new(context.runtime());
    let unit = Arc::new(Unit::default());
    // Normally the unit would be created by compiling some source,
    // and since this one is empty it won't do anything.

    let vm = runestick::Vm::new(context, unit);

    let output = vm.execute(&["main"], (33i64,))?.complete()?;
    let output = i64::from_value(output)?;

    println!("output: {}", output);
    Ok(())
}

You can use a Vec<Value> to provide a variadic collection of arguments.

use runestick::{Context, Unit, FromValue, Source, ToValue};
use std::sync::Arc;

fn main() -> runestick::Result<()> {
    let context = Context::with_default_modules()?;
    let context = Arc::new(context.runtime());
    let unit = Arc::new(Unit::default());
    // Normally the unit would be created by compiling some source,
    // and since this one is empty it won't do anything.

    let vm = runestick::Vm::new(context, unit);

    let mut args = Vec::new();
    args.push(1u32.to_value()?);
    args.push(String::from("Hello World").to_value()?);

    let output = vm.execute(&["main"], args)?.complete()?;
    let output = i64::from_value(output)?;

    println!("output: {}", output);
    Ok(())
}

An execute variant that returns an execution which implements Send, allowing it to be sent and executed on a different thread.

This is accomplished by preventing values escaping from being non-exclusively sent with the execution or escaping the execution. We only support encoding arguments which themselves are Send.

Call the given function immediately, returning the produced value.

This function permits for using references since it doesn’t defer its execution.

Panics

If any of the arguments passed in are references, and that references is captured somewhere in the call as Mut<T> or Ref<T> this call will panic as we are trying to free the metadata relatedc to the reference.

Call the given function immediately asynchronously, returning the produced value.

This function permits for using references since it doesn’t defer its execution.

Panics

If any of the arguments passed in are references, and that references is captured somewhere in the call as Mut<T> or Ref<T> this call will panic as we are trying to free the metadata relatedc to the reference.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.