mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-28 14:11:57 +00:00
examples/exports.rs and examples/interpret.rs doc
This commit is contained in:
@ -65,8 +65,8 @@ fn main() {
|
|||||||
// of the wasm module
|
// of the wasm module
|
||||||
let exports: Vec<String> = export_section.entries().iter()
|
let exports: Vec<String> = export_section.entries().iter()
|
||||||
.filter_map(|entry|
|
.filter_map(|entry|
|
||||||
// This is match on export variant, which can either be function or global
|
// This is match on export variant, which can either be function, global
|
||||||
// We are intereseted only in functions for an example
|
// We are interested only in functions for an example
|
||||||
match *entry.internal() {
|
match *entry.internal() {
|
||||||
// Return function export name (return by field() function and it's index)
|
// Return function export name (return by field() function and it's index)
|
||||||
Internal::Function(index) => Some((entry.field(), index as usize)),
|
Internal::Function(index) => Some((entry.field(), index as usize)),
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// In this example we execute a contract funciton exported as "_call"
|
||||||
|
|
||||||
extern crate parity_wasm;
|
extern crate parity_wasm;
|
||||||
|
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
@ -11,7 +13,10 @@ fn main() {
|
|||||||
println!(" wasm file should contain exported `_call` function with single I32 argument");
|
println!(" wasm file should contain exported `_call` function with single I32 argument");
|
||||||
return;
|
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(
|
let program = parity_wasm::DefaultProgramInstance::with_env_params(
|
||||||
interpreter::EnvParams {
|
interpreter::EnvParams {
|
||||||
total_stack: 128*1024,
|
total_stack: 128*1024,
|
||||||
@ -19,8 +24,20 @@ fn main() {
|
|||||||
allow_memory_growth: false,
|
allow_memory_growth: false,
|
||||||
}
|
}
|
||||||
).expect("Failed to load program");
|
).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");
|
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");
|
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");
|
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()));
|
println!("Result: {:?}", module.execute_export("_call", vec![parity_wasm::RuntimeValue::I32(argument)].into()));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user