Moved macros to common mod

This commit is contained in:
Syrus Akbary 2018-10-16 17:01:47 +02:00
parent 2cc93ddc38
commit 606afda496
3 changed files with 40 additions and 21 deletions

21
src/macros.rs Normal file
View File

@ -0,0 +1,21 @@
/// This macro helps to get a function for an instance easily
/// let func: fn(i32) -> i32 = get_instance_function!(instance, func_index);
#[macro_export]
macro_rules! get_instance_function {
($instance:expr, $func_index:expr) => {
{
use std::mem;
let func_addr = $instance.get_function_pointer($func_index);
unsafe {
mem::transmute(func_addr)
}
}
};
}
// #[cfg(feature = "debug")]
#[macro_export]
macro_rules! debug {
($fmt:expr) => (println!(concat!("Wasmer::", $fmt)));
($fmt:expr, $($arg:tt)*) => (println!(concat!("Wasmer::", $fmt, "\n"), $($arg)*));
}

View File

@ -16,17 +16,6 @@ extern crate spin;
use std::time::{Duration, Instant};
// #[cfg(feature = "debug")]
macro_rules! debug {
($fmt:expr) => (println!(concat!("Wasmer::", $fmt)));
($fmt:expr, $($arg:tt)*) => (println!(concat!("Wasmer::", $fmt, "\n"), $($arg)*));
}
// #[cfg(not(feature = "debug"))]
// macro_rules! debug {
// ($($arg:tt)*) => {}
// }
// #[macro_use] extern crate log;
use std::error::Error;
@ -39,6 +28,8 @@ use std::process::exit;
use structopt::StructOpt;
use wabt::wat2wasm;
#[macro_use]
mod macros;
pub mod common;
pub mod spec;
pub mod webassembly;

View File

@ -167,6 +167,7 @@ mod tests {
use crate::test::Bencher;
use super::run_single_file;
use std::mem;
#[macro_use]
use crate::webassembly::{
compile, instantiate, Error, ErrorKind, Export, Instance, Module, ResultObject,
};
@ -197,6 +198,20 @@ mod tests {
}
}
macro_rules! instantiate_from_wast {
($x:expr) => {
{
pub const WAST_BYTES: &[u8] = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
$x
));
let wasm_bytes = wat2wasm(WAST_BYTES.to_vec()).expect("Can't convert wat to wasm");
let result_object = instantiate(wasm_bytes, None).expect("Not compiled properly");
result_object
}
};
}
wasm_tests!{
_type,
br_if,
@ -211,22 +226,14 @@ mod tests {
#[bench]
fn bench_identity(b: &mut Bencher) {
pub const BENCHMARK_BYTES: &[u8] = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/spec/tests/benchmark.wast"
));
let wasm_bytes = wat2wasm(BENCHMARK_BYTES.to_vec()).expect("Can't convert wat to wasm");
let result_object = instantiate(wasm_bytes, None).expect("Not compiled properly");
let result_object = instantiate_from_wast!("/src/spec/tests/benchmark.wast");
let instance = result_object.instance;
let module = result_object.module;
let func_index = match module.info.exports.get("identity") {
Some(&Export::Function(index)) => index,
_ => panic!("Function not found"),
};
let func_addr = instance.get_function_pointer(func_index);
let func = unsafe {
mem::transmute::<_, fn(i32) -> i32>(func_addr)
};
let func: fn(i32) -> i32 = get_instance_function!(instance, func_index);
assert_eq!(func(1), 1, "Identity function not working.");
b.iter(|| {
func(1);