examples/exports.rs and examples/interpret.rs doc

This commit is contained in:
fro
2017-08-29 19:59:45 +03:00
parent c57797aaad
commit cfe80e364e
2 changed files with 20 additions and 3 deletions

View File

@ -65,8 +65,8 @@ fn main() {
// of the wasm module
let exports: Vec<String> = export_section.entries().iter()
.filter_map(|entry|
// This is match on export variant, which can either be function or global
// We are intereseted only in functions for an example
// This is match on export variant, which can either be function, global
// We are interested only in functions for an example
match *entry.internal() {
// Return function export name (return by field() function and it's index)
Internal::Function(index) => Some((entry.field(), index as usize)),

View File

@ -1,3 +1,5 @@
// In this example we execute a contract funciton exported as "_call"
extern crate parity_wasm;
use std::env::args;
@ -11,7 +13,10 @@ fn main() {
println!(" wasm file should contain exported `_call` function with single I32 argument");
return;
}
// Intrepreter initialization.
// parity_wasm::ProgramInstance can be parameterized with a custom User error to be returned from native modules
// parity_wasm::DefaultProgramInstance parametrize ProgramInstance with a pre-defined "DummyUserError"
// Initializes a default "env" module also.
let program = parity_wasm::DefaultProgramInstance::with_env_params(
interpreter::EnvParams {
total_stack: 128*1024,
@ -19,8 +24,20 @@ fn main() {
allow_memory_growth: false,
}
).expect("Failed to load program");
// Here we load module using dedicated for this purpose
// `deserialize_file` function (which works only with modules)
let module = parity_wasm::deserialize_file(&args[1]).expect("Failed to load module");
// Intialize deserialized module. It adds module into It expects 3 parameters:
// - 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
let module = program.add_module("main", module, None).expect("Failed to initialize 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. It prints a
println!("Result: {:?}", module.execute_export("_call", vec![parity_wasm::RuntimeValue::I32(argument)].into()));
}