mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-10 21:41:34 +00:00
various fixes, code cleanup
fix: exclude imports from function index
This commit is contained in:
@ -3,14 +3,7 @@ extern crate parity_wasm;
|
||||
use std::env::args;
|
||||
|
||||
use parity_wasm::{interpreter, ModuleInstanceInterface, RuntimeValue};
|
||||
use parity_wasm::elements::ExportEntry;
|
||||
use parity_wasm::elements::ExportSection;
|
||||
use parity_wasm::elements::FunctionSection;
|
||||
use parity_wasm::elements::TypeSection;
|
||||
use parity_wasm::elements::Internal;
|
||||
use parity_wasm::elements::Type;
|
||||
use parity_wasm::elements::FunctionType;
|
||||
use parity_wasm::elements::ValueType;
|
||||
use parity_wasm::elements::{Internal, Type, FunctionType, ValueType};
|
||||
|
||||
fn main() {
|
||||
let args: Vec<_> = args().collect();
|
||||
@ -27,36 +20,44 @@ fn main() {
|
||||
total_memory: 2*1024*1024,
|
||||
allow_memory_growth: false,
|
||||
}
|
||||
).expect("Failed to load program");
|
||||
).expect("Program instance to load");
|
||||
|
||||
let module_def = parity_wasm::deserialize_file(&args[1]).expect("Failed to load module");
|
||||
let module = parity_wasm::deserialize_file(&args[1]).expect("File to be deserialized");
|
||||
let execution_params = {
|
||||
let export_section: &ExportSection = module_def.export_section().expect("No export section found");
|
||||
let function_section: &FunctionSection = module_def.function_section().expect("No function section found");
|
||||
let type_section: &TypeSection = module_def.type_section().expect("No type section found");
|
||||
let found_entry: &ExportEntry = export_section.entries().iter()
|
||||
.find(|entry| func_name.eq(entry.field())).unwrap();
|
||||
let function_index = match found_entry.internal() {
|
||||
&Internal::Function(index) => index,
|
||||
let export_section = module.export_section().expect("No export section found");
|
||||
let function_section = module.function_section().expect("No function section found");
|
||||
let type_section = module.type_section().expect("No type section found");
|
||||
|
||||
let found_entry= export_section.entries().iter()
|
||||
.find(|entry| func_name == entry.field()).expect(&format!("No export with name {} found", func_name));
|
||||
|
||||
// Function index with imported functions
|
||||
let function_index: usize = match found_entry.internal() {
|
||||
&Internal::Function(index) => index as usize,
|
||||
_ => panic!("Founded export is not a function"),
|
||||
};
|
||||
let func_type_ref: u32 = function_section.entries()[function_index as usize].type_ref();
|
||||
let type_: &Type = &type_section.types()[func_type_ref as usize];
|
||||
let function_type: &FunctionType = match type_ {
|
||||
let import_section_len: usize = match module.import_section() {
|
||||
Some(import) => import.entries().len(),
|
||||
None => 0,
|
||||
};
|
||||
|
||||
let function_index_in_section = function_index - import_section_len;
|
||||
let func_type_ref: usize = function_section.entries()[function_index_in_section].type_ref() as usize;
|
||||
let function_type: &FunctionType = match &type_section.types()[func_type_ref] {
|
||||
&Type::Function(ref func_type) => func_type,
|
||||
};
|
||||
|
||||
let args: Vec<RuntimeValue> = function_type.params().iter().enumerate().map(|(i, value)| match value {
|
||||
&ValueType::I32 => RuntimeValue::I32(program_args[i].parse::<i32>().unwrap()),
|
||||
&ValueType::I64 => RuntimeValue::I64(program_args[i].parse::<i64>().unwrap()),
|
||||
&ValueType::F32 => RuntimeValue::F32(program_args[i].parse::<f32>().unwrap()),
|
||||
&ValueType::F64 => RuntimeValue::F64(program_args[i].parse::<f64>().unwrap()),
|
||||
&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)
|
||||
};
|
||||
|
||||
let module_def = program.add_module("main", module_def, None).expect("Failed to initialize module");
|
||||
let module = program.add_module("main", module, None).expect("Failed to initialize module");
|
||||
|
||||
println!("Result: {:?}", module_def.execute_export(func_name, execution_params));
|
||||
println!("Result: {:?}", module.execute_export(func_name, execution_params).expect(""));
|
||||
}
|
||||
|
Reference in New Issue
Block a user