mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-13 13:58:38 +00:00
Added more examples
This commit is contained in:
65
examples/compiler_llvm.rs
Normal file
65
examples/compiler_llvm.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
//! A Wasm module can be compiled with multiple compilers.
|
||||
//!
|
||||
//! This example illustrates how to use the LLVM compiler.
|
||||
//!
|
||||
//! You can run the example directly by executing in Wasmer root:
|
||||
//!
|
||||
//! ```bash
|
||||
//! cargo run --example compiler-llvm --release --features "llvm"
|
||||
//! ```
|
||||
//!
|
||||
//! Ready?
|
||||
|
||||
use wasmer::{imports, wat2wasm, Instance, Module, Store, Value};
|
||||
use wasmer_compiler_llvm::LLVM;
|
||||
use wasmer_engine_jit::JIT;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Let's declare the Wasm module with the text representation.
|
||||
let wasm_bytes = wat2wasm(
|
||||
r#"
|
||||
(module
|
||||
(type $sum_t (func (param i32 i32) (result i32)))
|
||||
(func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)
|
||||
local.get $x
|
||||
local.get $y
|
||||
i32.add)
|
||||
(export "sum" (func $sum_f)))
|
||||
"#
|
||||
.as_bytes(),
|
||||
)?;
|
||||
|
||||
// Use LLVM compiler with the default settings
|
||||
let compiler = LLVM::default();
|
||||
|
||||
// Create the store
|
||||
let store = Store::new(&JIT::new(&compiler).engine());
|
||||
|
||||
println!("Compiling module...");
|
||||
// Let's compile the Wasm module.
|
||||
let module = Module::new(&store, wasm_bytes)?;
|
||||
|
||||
// Create an empty import object.
|
||||
let import_object = imports! {};
|
||||
|
||||
println!("Instantiating module...");
|
||||
// Let's instantiate the Wasm module.
|
||||
let instance = Instance::new(&module, &import_object)?;
|
||||
|
||||
let sum = instance.exports.get_function("sum")?;
|
||||
|
||||
println!("Calling `sum` function...");
|
||||
// Let's call the `sum` exported function. The parameters are a
|
||||
// slice of `Value`s. The results are a boxed slice of `Value`s.
|
||||
let results = sum.call(&[Value::I32(1), Value::I32(2)])?;
|
||||
|
||||
println!("Results: {:?}", results);
|
||||
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_exported_function() -> Result<(), Box<dyn std::error::Error>> {
|
||||
main()
|
||||
}
|
||||
Reference in New Issue
Block a user