Modify imports macro to work with all things that are exports

This commit is contained in:
Lachlan Sneff 2019-01-25 16:40:07 -08:00
parent db3a20a76b
commit a58f3318f0
6 changed files with 21 additions and 15 deletions

View File

@ -1,6 +1,6 @@
use wabt::wat2wasm; use wabt::wat2wasm;
use wasmer_clif_backend::CraneliftCompiler; use wasmer_clif_backend::CraneliftCompiler;
use wasmer_runtime_core::{error::Result, prelude::*}; use wasmer_runtime_core::{error::Result, prelude::*, memory::Memory, types::MemoryDesc};
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm"); static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
@ -8,9 +8,18 @@ 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 = wasmer_runtime_core::compile_with(&wasm_binary, &CraneliftCompiler::new())?; let inner_module = wasmer_runtime_core::compile_with(&wasm_binary, &CraneliftCompiler::new())?;
let mut memory = Memory::new(MemoryDesc {
min: 1,
max: Some(1),
shared: false,
}).unwrap();
memory.as_slice_mut()[0] = 42;
let import_object = imports! { let import_object = imports! {
"env" => { "env" => {
"print_i32" => print_num<[i32] -> [i32]>, "print_i32" => func!(print_num, [i32] -> [i32]),
"memory" => memory,
}, },
}; };
@ -36,9 +45,10 @@ extern "C" fn print_num(n: i32, _vmctx: &mut vm::Ctx) -> i32 {
static IMPORT_MODULE: &str = r#" static IMPORT_MODULE: &str = r#"
(module (module
(type $t0 (func (param i32) (result i32))) (type $t0 (func (param i32) (result i32)))
;; (import "env" "memory" (memory 0 1)) (import "env" "memory" (memory 1 1))
(import "env" "print_i32" (func $print_i32 (type $t0))) (import "env" "print_i32" (func $print_i32 (type $t0)))
(func $print_num (export "print_num") (type $t0) (param $p0 i32) (result i32) (func $print_num (export "print_num") (type $t0) (param $p0 i32) (result i32)
get_local $p0 i32.const 0
i32.load
call $print_i32)) call $print_i32))
"#; "#;

View File

@ -39,7 +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}; pub use crate::{func, imports};
} }
/// Compile a [`Module`] using the provided compiler from /// Compile a [`Module`] using the provided compiler from

View File

@ -6,7 +6,7 @@ macro_rules! debug {
} }
#[macro_export] #[macro_export]
macro_rules! export_func { macro_rules! func {
($func:ident, [ $( $params:ident ),* ] -> [ $( $returns:ident ),* ] ) => {{ ($func:ident, [ $( $params:ident ),* ] -> [ $( $returns:ident ),* ] ) => {{
use $crate::{ use $crate::{
export::{Context, Export, FuncPointer}, export::{Context, Export, FuncPointer},
@ -98,13 +98,10 @@ macro_rules! imports {
#[macro_export] #[macro_export]
#[doc(hidden)] #[doc(hidden)]
macro_rules! __imports_internal { macro_rules! __imports_internal {
( { $( $imp_name:expr => $func:ident < [ $( $params:ident ),* ] -> [ $( $returns:ident ),* ] >, )* } ) => {{ ( { $( $imp_name:expr => $import_item:expr, )* } ) => {{
let mut ns = Namespace::new(); let mut ns = Namespace::new();
$( $(
ns.insert($imp_name, $crate::export_func!( ns.insert($imp_name, $import_item);
$func,
[ $( $params ),* ] -> [ $( $returns )* ]
));
)* )*
ns ns
}}; }};

View File

@ -43,7 +43,6 @@ impl DynamicMemory {
local.base = storage.memory.as_ptr(); local.base = storage.memory.as_ptr();
local.bound = desc.min as usize * WASM_PAGE_SIZE; local.bound = desc.min as usize * WASM_PAGE_SIZE;
local.memory = storage_ptr as *mut (); local.memory = storage_ptr as *mut ();
println!("local: {:?}", local);
Some(storage) Some(storage)
} }

View File

@ -37,7 +37,7 @@ impl Memory {
MemoryType::Static => { MemoryType::Static => {
MemoryStorage::Static(StaticMemory::new(desc, &mut vm_local_memory)?) MemoryStorage::Static(StaticMemory::new(desc, &mut vm_local_memory)?)
} }
MemoryType::SharedStatic => unimplemented!(), MemoryType::SharedStatic => unimplemented!("shared memories are not yet implemented"),
}; };
Some(Memory { Some(Memory {

View File

@ -83,7 +83,7 @@ pub use wasmer_runtime_core::vm::Ctx;
pub use wasmer_runtime_core::{compile_with, validate}; pub use wasmer_runtime_core::{compile_with, validate};
pub use wasmer_runtime_core::error; pub use wasmer_runtime_core::error;
pub use wasmer_runtime_core::imports; pub use wasmer_runtime_core::{imports, func};
pub mod wasm { pub mod wasm {
pub use wasmer_runtime_core::instance::Function; pub use wasmer_runtime_core::instance::Function;