Add imports macro

This commit is contained in:
Lachlan Sneff
2019-01-21 16:24:49 -08:00
parent c7520035fd
commit 39ddd8f258
3 changed files with 48 additions and 16 deletions

View File

@ -32,7 +32,6 @@ pub use self::module::Module;
use std::rc::Rc;
pub mod prelude {
pub use crate::export_func;
pub use crate::import::{ImportObject, Namespace};
pub use crate::types::{
FuncIndex, GlobalIndex, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex,
@ -40,6 +39,7 @@ pub mod prelude {
MemoryIndex, TableIndex, Type, Value,
};
pub use crate::vm;
pub use crate::{export_func, imports};
}
/// Compile a webassembly module using the provided compiler.

View File

@ -49,4 +49,41 @@ macro_rules! __export_func_convert_type {
($x:ty) => {
compile_error!("Only `i32`, `u32`, `i64`, `u64`, `f32`, and `f64` are supported for argument and return types")
};
}
}
#[macro_export]
macro_rules! imports {
( $( $ns_name:expr => $ns:tt, )* ) => {{
use wasmer_runtime::{
import::{ImportObject, Namespace},
};
let mut import_object = ImportObject::new();
$({
let ns = $crate::__imports_internal!($ns);
import_object.register($ns_name, ns);
})*
import_object
}};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __imports_internal {
( { $( $imp_name:expr => $func:ident < [ $( $params:ident ),* ] -> [ $( $returns:ident ),* ] >, )* } ) => {{
let mut ns = Namespace::new();
$(
ns.insert($imp_name, $crate::export_func!(
$func,
[ $( $params ),* ] -> [ $( $returns )* ]
));
)*
ns
}};
($ns:ident) => {
$ns
};
}