wasmi/examples/interpret.rs

48 lines
1.8 KiB
Rust
Raw Permalink Normal View History

2018-01-22 15:39:54 +00:00
// In this example we execute a contract funciton exported as "_call"
extern crate wasmi;
use std::env::args;
use std::fs::File;
2018-12-11 11:54:06 +00:00
use wasmi::{ImportsBuilder, Module, ModuleInstance, NopExternals, RuntimeValue};
2018-01-22 15:39:54 +00:00
fn load_from_file(filename: &str) -> Module {
2018-12-11 11:54:06 +00:00
use std::io::prelude::*;
let mut file = File::open(filename).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
Module::from_buffer(buf).unwrap()
2018-01-22 15:39:54 +00:00
}
fn main() {
let args: Vec<_> = args().collect();
if args.len() != 3 {
println!("Usage: {} <wasm file> <arg>", args[0]);
println!(" wasm file should contain exported `_call` function with single I32 argument");
return;
}
// Here we load module using dedicated for this purpose
// `load_from_file` function (which works only with modules)
let module = load_from_file(&args[1]);
// Intialize deserialized module. It adds module into It expects 3 parameters:
2018-12-11 11:54:06 +00:00
// - a name for the module
// - a module declaration
// - "main" module doesn't import native module(s) this is why we don't need to provide external native modules here
// This test shows how to implement native module https://github.com/NikVolf/parity-wasm/blob/master/src/interpreter/tests/basics.rs#L197
2018-01-22 15:39:54 +00:00
let main = ModuleInstance::new(&module, &ImportsBuilder::default())
.expect("Failed to instantiate module")
.run_start(&mut NopExternals)
.expect("Failed to run start function in module");
// The argument should be parsable as a valid integer
let argument: i32 = args[2].parse().expect("Integer argument required");
// "_call" export of function to be executed with an i32 argument and prints the result of execution
2018-12-11 11:54:06 +00:00
println!(
"Result: {:?}",
main.invoke_export("_call", &[RuntimeValue::I32(argument)], &mut NopExternals)
);
2018-01-22 15:39:54 +00:00
}