# [−] [src] Trait wasmi::Externals

    pub trait Externals {
        fn invoke_index(
            &mut self,
            index: usize,
            args: RuntimeArgs
        ) -> Result<Option<RuntimeValue>, Trap>;
    }

Trait that allows to implement host functions.

# Examples

    use wasmi::{
        Externals, RuntimeValue, RuntimeArgs, Error, ModuleImportResolver,
        FuncRef, ValueType, Signature, FuncInstance, Trap,
    };

    struct HostExternals {
        counter: usize,
    }

    const ADD_FUNC_INDEX: usize = 0;

    impl Externals for HostExternals {
        fn invoke_index(
            &mut self,
            index: usize,
            args: RuntimeArgs,
        ) -> Result<Option<RuntimeValue>, Trap> {
            match index {
                ADD_FUNC_INDEX => {
                    let a: u32 = args.nth_checked(0)?;
                    let b: u32 = args.nth_checked(1)?;
                    let result = a + b;

                    Ok(Some(RuntimeValue::I32(result as i32)))
                }
                _ => panic!("Unimplemented function at {}", index),
            }
        }
    }

    impl HostExternals {
        fn check_signature(
            &self,
            index: usize,
            signature: &Signature
        ) -> bool {
            let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index {
                ADD_FUNC_INDEX => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
                _ => return false,
            };
            signature.params() == params && signature.return_type() == ret_ty
        }
    }

    impl ModuleImportResolver for HostExternals {
        fn resolve_func(
            &self,
            field_name: &str,
            signature: &Signature
        ) -> Result<FuncRef, Error> {
            let index = match field_name {
                "add" => ADD_FUNC_INDEX,
                _ => {
                    return Err(Error::Instantiation(
                        format!("Export {} not found", field_name),
                    ))
                }
            };

            if !self.check_signature(index, signature) {
                return Err(Error::Instantiation(
                    format!("Export {} has a bad signature", field_name)
                ));
            }

            Ok(FuncInstance::alloc_host(
                Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)),
                index,
            ))
        }
    }

## Required methods

### `fn invoke_index(
&mut self,
index: usize,
args: RuntimeArgs
) -> Result<Option<RuntimeValue>, Trap>`

Perform invoke of a host function by specified `index`.

Loading content...

## Implementors

### `impl Externals for NopExternals` [src]

#### `fn invoke_index(
&mut self,
_index: usize,
_args: RuntimeArgs
) -> Result<Option<RuntimeValue>, Trap>` [src]

Loading content...

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)