mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-23 13:41:32 +00:00
Add imports macro
This commit is contained in:
@ -8,22 +8,17 @@ fn main() -> Result<()> {
|
|||||||
let wasm_binary = wat2wasm(IMPORT_MODULE.as_bytes()).expect("WAST not valid or malformed");
|
let wasm_binary = wat2wasm(IMPORT_MODULE.as_bytes()).expect("WAST not valid or malformed");
|
||||||
let inner_module = runtime::compile(&wasm_binary, &CraneliftCompiler::new())?;
|
let inner_module = runtime::compile(&wasm_binary, &CraneliftCompiler::new())?;
|
||||||
|
|
||||||
let mut env_namespace = Namespace::new();
|
let import_object = imports! {
|
||||||
env_namespace.insert(
|
"env" => {
|
||||||
"print_i32",
|
"print_i32" => print_num<[i32] -> [i32]>,
|
||||||
export_func!(
|
},
|
||||||
print_num,
|
};
|
||||||
[i32] -> [i32]
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut imports = ImportObject::new();
|
let inner_instance = inner_module.instantiate(import_object)?;
|
||||||
imports.register("env", env_namespace);
|
|
||||||
|
|
||||||
let inner_instance = inner_module.instantiate(imports)?;
|
let outer_imports = imports! {
|
||||||
|
"env" => inner_instance,
|
||||||
let mut outer_imports = ImportObject::new();
|
};
|
||||||
outer_imports.register("env", inner_instance);
|
|
||||||
|
|
||||||
let outer_module = runtime::compile(EXAMPLE_WASM, &CraneliftCompiler::new())?;
|
let outer_module = runtime::compile(EXAMPLE_WASM, &CraneliftCompiler::new())?;
|
||||||
let mut outer_instance = outer_module.instantiate(outer_imports)?;
|
let mut outer_instance = outer_module.instantiate(outer_imports)?;
|
||||||
|
@ -32,7 +32,6 @@ pub use self::module::Module;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use crate::export_func;
|
|
||||||
pub use crate::import::{ImportObject, Namespace};
|
pub use crate::import::{ImportObject, Namespace};
|
||||||
pub use crate::types::{
|
pub use crate::types::{
|
||||||
FuncIndex, GlobalIndex, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex,
|
FuncIndex, GlobalIndex, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex,
|
||||||
@ -40,6 +39,7 @@ pub mod prelude {
|
|||||||
MemoryIndex, TableIndex, Type, Value,
|
MemoryIndex, TableIndex, Type, Value,
|
||||||
};
|
};
|
||||||
pub use crate::vm;
|
pub use crate::vm;
|
||||||
|
pub use crate::{export_func, imports};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compile a webassembly module using the provided compiler.
|
/// Compile a webassembly module using the provided compiler.
|
||||||
|
@ -50,3 +50,40 @@ macro_rules! __export_func_convert_type {
|
|||||||
compile_error!("Only `i32`, `u32`, `i64`, `u64`, `f32`, and `f64` are supported for argument and return types")
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user