mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-26 05:02:03 +00:00
drop interpreter
This commit is contained in:
@ -1,37 +0,0 @@
|
||||
// In this example we execute a contract funciton exported as "_call"
|
||||
#![allow(deprecated)]
|
||||
|
||||
extern crate parity_wasm;
|
||||
|
||||
use std::env::args;
|
||||
|
||||
use parity_wasm::ModuleInstanceInterface;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Intrepreter initialization.
|
||||
let program = parity_wasm::ProgramInstance::new();
|
||||
|
||||
// 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 and prints the result of execution
|
||||
println!("Result: {:?}", module.execute_export("_call", vec![parity_wasm::RuntimeValue::I32(argument)].into()));
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
#![allow(deprecated)]
|
||||
|
||||
extern crate parity_wasm;
|
||||
|
||||
use std::env::args;
|
||||
|
||||
use parity_wasm::{interpreter, ModuleInstanceInterface, RuntimeValue};
|
||||
use parity_wasm::elements::{Internal, External, Type, FunctionType, ValueType};
|
||||
|
||||
fn main() {
|
||||
let args: Vec<_> = args().collect();
|
||||
if args.len() < 3 {
|
||||
println!("Usage: {} <wasm file> <exported func> [<arg>...]", args[0]);
|
||||
return;
|
||||
}
|
||||
let func_name = &args[2];
|
||||
let (_, program_args) = args.split_at(3);
|
||||
|
||||
// Intrepreter initialization.
|
||||
let program = parity_wasm::ProgramInstance::new();
|
||||
|
||||
let module = parity_wasm::deserialize_file(&args[1]).expect("File to be deserialized");
|
||||
|
||||
// Extracts call arguments from command-line arguments
|
||||
let execution_params = {
|
||||
// Export section has an entry with a func_name with an index inside a module
|
||||
let export_section = module.export_section().expect("No export section found");
|
||||
// It's a section with function declarations (which are references to the type section entries)
|
||||
let function_section = module.function_section().expect("No function section found");
|
||||
// Type section stores function types which are referenced by function_section entries
|
||||
let type_section = module.type_section().expect("No type section found");
|
||||
|
||||
// Given function name used to find export section entry which contains
|
||||
// an `internal` field which points to the index in the function index space
|
||||
let found_entry = export_section.entries().iter()
|
||||
.find(|entry| func_name == entry.field()).expect(&format!("No export with name {} found", func_name));
|
||||
|
||||
// Function index in the function index space (internally-defined + imported)
|
||||
let function_index: usize = match found_entry.internal() {
|
||||
&Internal::Function(index) => index as usize,
|
||||
_ => panic!("Founded export is not a function"),
|
||||
};
|
||||
|
||||
// We need to count import section entries (functions only!) to subtract it from function_index
|
||||
// and obtain the index within the function section
|
||||
let import_section_len: usize = match module.import_section() {
|
||||
Some(import) =>
|
||||
import.entries().iter().filter(|entry| match entry.external() {
|
||||
&External::Function(_) => true,
|
||||
_ => false,
|
||||
}).count(),
|
||||
None => 0,
|
||||
};
|
||||
|
||||
// Calculates a function index within module's function section
|
||||
let function_index_in_section = function_index - import_section_len;
|
||||
|
||||
// Getting a type reference from a function section entry
|
||||
let func_type_ref: usize = function_section.entries()[function_index_in_section].type_ref() as usize;
|
||||
|
||||
// Use the reference to get an actual function type
|
||||
let function_type: &FunctionType = match &type_section.types()[func_type_ref] {
|
||||
&Type::Function(ref func_type) => func_type,
|
||||
};
|
||||
|
||||
// Parses arguments and constructs runtime values in correspondence of their types
|
||||
let args: Vec<RuntimeValue> = function_type.params().iter().enumerate().map(|(i, value)| match value {
|
||||
&ValueType::I32 => RuntimeValue::I32(program_args[i].parse::<i32>().expect(&format!("Can't parse arg #{} as i32", program_args[i]))),
|
||||
&ValueType::I64 => RuntimeValue::I64(program_args[i].parse::<i64>().expect(&format!("Can't parse arg #{} as i64", program_args[i]))),
|
||||
&ValueType::F32 => RuntimeValue::F32(program_args[i].parse::<f32>().expect(&format!("Can't parse arg #{} as f32", program_args[i]))),
|
||||
&ValueType::F64 => RuntimeValue::F64(program_args[i].parse::<f64>().expect(&format!("Can't parse arg #{} as f64", program_args[i]))),
|
||||
}).collect();
|
||||
|
||||
interpreter::ExecutionParams::from(args)
|
||||
};
|
||||
|
||||
// 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");
|
||||
|
||||
println!("Result: {:?}", module.execute_export(func_name, execution_params).expect(""));
|
||||
}
|
Reference in New Issue
Block a user