[][src]Trait wasmer::WasmerEnv

pub trait WasmerEnv: Clone + Send + Sync {
    pub fn init_with_instance(
        &mut self,
        _instance: &Instance
    ) -> Result<(), HostEnvInitError> { ... } }

Trait for initializing the environments passed to host functions after instantiation but before execution.

This is useful for filling an environment with data that can only be accesed after instantiation. For example, exported items such as memories and functions which don't exist prior to instantiation can be accessed here so that host functions can use them.

Examples

This trait can be derived like so:

use wasmer::{WasmerEnv, LazyInit, Memory, NativeFunc};

#[derive(WasmerEnv, Clone)]
pub struct MyEnvWithNoInstanceData {
    non_instance_data: u8,
}

#[derive(WasmerEnv, Clone)]
pub struct MyEnvWithInstanceData {
    non_instance_data: u8,
    #[wasmer(export)]
    memory: LazyInit<Memory>,
    #[wasmer(export(name = "real_name"))]
    func: LazyInit<NativeFunc<(i32, i32), i32>>,
    #[wasmer(export(optional = true, alias = "memory2", alias = "_memory2"))]
    optional_memory: LazyInit<Memory>,
}

When deriving WasmerEnv, you must wrap your types to be initialized in LazyInit. The derive macro will also generate helper methods of the form <field_name>_ref and <field_name>_ref_unchecked for easy access to the data.

The valid arguments to export are:

  • name = "string": specify the name of this item in the Wasm module. If this is not specified, it will default to the name of the field.
  • optional = true: specify whether this export is optional. Defaults to false. Being optional means that if the export can't be found, the LazyInit will be left uninitialized.
  • alias = "string": specify additional names to look for in the Wasm module. alias may be specified multiple times to search for multiple aliases.

This trait may also be implemented manually:

#[derive(Clone)]
pub struct MyEnv {
   memory: LazyInit<Memory>,
}

impl WasmerEnv for MyEnv {
    fn init_with_instance(&mut self, instance: &Instance) -> Result<(), HostEnvInitError> {
        let memory = instance.exports.get_memory("memory").unwrap();
        self.memory.initialize(memory.clone());
        Ok(())
    }
}

Provided methods

pub fn init_with_instance(
    &mut self,
    _instance: &Instance
) -> Result<(), HostEnvInitError>
[src]

The function that Wasmer will call on your type to let it finish setting up the environment with data from the Instance.

This function is called after Instance is created but before it is returned to the user via Instance::new.

Loading content...

Implementations on Foreign Types

impl WasmerEnv for u8[src]

impl WasmerEnv for i8[src]

impl WasmerEnv for u16[src]

impl WasmerEnv for i16[src]

impl WasmerEnv for u32[src]

impl WasmerEnv for i32[src]

impl WasmerEnv for u64[src]

impl WasmerEnv for i64[src]

impl WasmerEnv for u128[src]

impl WasmerEnv for i128[src]

impl WasmerEnv for f32[src]

impl WasmerEnv for f64[src]

impl WasmerEnv for usize[src]

impl WasmerEnv for isize[src]

impl WasmerEnv for char[src]

impl WasmerEnv for bool[src]

impl WasmerEnv for String[src]

impl<'a> WasmerEnv for &'a AtomicBool[src]

impl<'a> WasmerEnv for &'a AtomicI8[src]

impl<'a> WasmerEnv for &'a AtomicU8[src]

impl<'a> WasmerEnv for &'a AtomicI16[src]

impl<'a> WasmerEnv for &'a AtomicU16[src]

impl<'a> WasmerEnv for &'a AtomicI32[src]

impl<'a> WasmerEnv for &'a AtomicU32[src]

impl<'a> WasmerEnv for &'a AtomicI64[src]

impl<'a> WasmerEnv for &'a AtomicUsize[src]

impl<'a> WasmerEnv for &'a AtomicIsize[src]

impl<T: WasmerEnv> WasmerEnv for Box<T>[src]

impl<T: WasmerEnv> WasmerEnv for Arc<Mutex<T>>[src]

Loading content...

Implementors

Loading content...