From cfe80e364eb081f9716d600aa63c92ce79c242e0 Mon Sep 17 00:00:00 2001 From: fro Date: Tue, 29 Aug 2017 19:59:45 +0300 Subject: [PATCH] examples/exports.rs and examples/interpret.rs doc --- examples/exports.rs | 4 ++-- examples/interpret.rs | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/examples/exports.rs b/examples/exports.rs index a38af59..021fac9 100644 --- a/examples/exports.rs +++ b/examples/exports.rs @@ -65,8 +65,8 @@ fn main() { // of the wasm module let exports: Vec = 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)), diff --git a/examples/interpret.rs b/examples/interpret.rs index b90e4d3..d07639f 100644 --- a/examples/interpret.rs +++ b/examples/interpret.rs @@ -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())); }