Skip to content

Commit

Permalink
how to get function body closes wildfluss#2
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakov Zaytsev committed Sep 25, 2019
1 parent 0213d7f commit b1d53fc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
9 changes: 3 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ edition = "2018"
[dependencies]
wabt = "0.9.0"
tempfile = "3.1.0"
wasmi = "0.5.1"
# TODO remove
wasmi = { path = "../wasmi" }
parity-wasm = "0.40"
# test
which = "3.0.0"
58 changes: 41 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate wabt;
extern crate tempfile;
extern crate wasmi;
extern crate parity_wasm;

pub fn wasm2ll(func: &str, _wasm: &[u8]) -> String {
String::from(format!(r#"
Expand Down Expand Up @@ -43,27 +44,50 @@ mod tests {
// wasm.write_all(&module_binary);
// wasm.flush();

use wasmi::{Module, ModuleInstance, ImportsBuilder, NopExternals, GlobalRef, ExternVal::Func, FuncInstance};
use std::cell::{Ref};
let module = Module::from_buffer(buf).unwrap();
let not_started = ModuleInstance::new(&module, &ImportsBuilder::default())
.expect("Failed to instantiate module")
.run_start(&mut NopExternals)
.expect("Failed to run start function in module");
// use wasmi::{Module, ModuleInstance, ImportsBuilder, NopExternals, GlobalRef, ExternVal::Func, FuncInstance, parity_wasm::elements::Instructions};
// use std::cell::{Ref};
// let module = Module::from_buffer(buf).unwrap();
// let not_started = ModuleInstance::new(&module, &ImportsBuilder::default())
// .expect("Failed to instantiate module")
// .run_start(&mut NopExternals)
// .expect("Failed to run start function in module");

// let tmp: Ref<Vec<GlobalRef>> = not_started.globals();
// for global in &*tmp {
// println!("{:?}", global);
// }
match not_started.export_by_name("add") {
None => (),
Some(Func(func_ref)) => {
// here
let fun: &FuncInstance = &*func_ref;
let body = fun.body(); // XXX method `body` is private
// FuncBody::code
},
_ => ()
};

// match not_started.export_by_name("add") {
// None => (),
// Some(Func(func_ref)) => {
// // here
// let fun: &FuncInstance = &*func_ref;
// let body = fun.body().unwrap(); // XXX method `body` is private
// // found struct `wasmi::isa::Instructions`
// let code: parity_wasm::elements::Instructions = body.code;
// for i in code.elements() {
// println!("ins");
// }
// },
// _ => ()
// };

use parity_wasm::elements::{Module, Internal::*};
let module: Module = parity_wasm::deserialize_buffer(&buf).unwrap();
for e in module.export_section().unwrap().entries() {
if e.field() == "add" {
if let Function(function_index) = e.internal() {
// println!("{:?}", index);
let code = &module.code_section().expect("Already checked, impossible").bodies()[*function_index as usize];
println!("code: ");
for instruction in code.code().elements() {
println!("{}", instruction);
}

break;
}
}
}

funcs.insert(String::from("add"), &[0, 97, 115, 109]); // TODO
}
Expand Down

0 comments on commit b1d53fc

Please sign in to comment.