Function parity_wasm::interpreter::native_module [] [src]

pub fn native_module<'a, E: UserFunctionExecutor + 'a>(
    base: Arc<ModuleInstanceInterface>,
    user_elements: UserDefinedElements<E>
) -> Result<Arc<ModuleInstanceInterface + 'a>, Error>

Create wrapper for a module with given native user functions.

Examples

use parity_wasm::interpreter::{CallerContext, Error, RuntimeValue, UserFunctionExecutor};

struct MyExecutor;

impl UserFunctionExecutor for MyExecutor {
    fn execute(
        &mut self,
        name: &str,
        context: CallerContext,
    ) -> Result<Option<RuntimeValue>, Error> {
        match name {
            "add" => {
                // fn add(a: u32, b: u32) -> u32
                let b = context.value_stack.pop_as::<u32>()?;
                let a = context.value_stack.pop_as::<u32>()?;
                let sum = a + b;
                Ok(Some(RuntimeValue::I32(sum as i32)))
            }
            _ => Err(Error::Trap("not implemented".into()).into()),
        }
    }
}