use mlua::Lua; use std::error::Error; #[test] fn wasmer_examples_instance() -> Result<(), Box> { let lua = Lua::new(); mlua_wasmer::preload(&lua)?; let script = r#" -- Let's declare the Wasm module. local wasm_bytes = [[ (module (type $add_one_t (func (param i32) (result i32))) (func $add_one_f (type $add_one_t) (param $value i32) (result i32) local.get $value i32.const 1 i32.add) (export "add_one" (func $add_one_f))) ]] -- Create a Store. local wasmer = require('wasmer') local store = wasmer.Store:default() -- Let's compile the Wasm module. print('Compiling module...') local module = wasmer.Module:new(store, wasm_bytes) -- Create an empty import object. local import_object = wasmer.Imports:new() -- Let's instantiate the Wasm module. print('Instantiating module...') local instance = wasmer.Instance:new(store, module, import_object) -- We now have an instance ready to be used. -- -- From an `Instance` we can retrieve any exported entities. -- Each of these entities is covered in others examples. -- -- Here we are retrieving the exported function. We won't go into details here -- as the main focus of this example is to show how to create an instance out -- of a Wasm module and have basic interactions with it. local add_one = instance.exports:get_function('add_one') print('Calling add_one function...') local result = add_one:call(store, 1) print(string.format('Results of add_one: %d', result)) return result "#; let result: i32 = lua.load(script).eval()?; assert_eq!(result, 2); Ok(()) }