Run a WebAssembly function with WasmEdge low-level Rust APIs

    • wasmedge-sys v0.7.0
    • wasmedge-types v0.1.1

    This section demonstrates how to use the Rust APIs of the crate to run a host function.

    As you may know, several mainstream programming languages, such as C/C++, Rust, Go, and Python, support compiling their programs into WebAssembly binary. In this demo, we’ll introduce how to use the APIs defined in Vm of wasmedge-sys crate to call a WebAssembly function which could be coded in any programming language mentioned above.

    We use fibonacci.wasm in this demo, and the contents of the WebAssembly file are presented below. The statement, (export "fib" (func $fib)), declares an exported function named fib. This function computes a Fibonacci number with a given i32 number as input. We’ll use the function name later to achieve the goal of computing a Fibonacci number.

    In this step, we’ll create a WasmEdge AST Module instance from a WebAssembly file.

    • Then, load a specified WebAssebly file (“fibonacci.wasm”) via the from_file method of the Loader context. If the process is successful, then a WasmEdge AST Module instance is returned.

    In Step 1, we got a module that hosts the target fib function defined in the WebAssembly. Now, we can call the function via the run_wasm_from_module method of the Vm context by passing the exported function name, .

    This is the final result printing on the screen:

    The result of fib(5) is 8