Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
advanced/WasmMem bindings
  • Loading branch information
yurydelendik committed Nov 12, 2019
commit 13e83c6dba4fc6059b376c883dc9ec24f38f1c7e
32 changes: 21 additions & 11 deletions crates/api/examples/hello4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ use wasmtime_api::*;
#[macro_use]
extern crate wasmtime_bindings_macro;

struct Callback;
pub struct Syscalls;

#[wasmtime_impl(module(callback_mod))]
impl Callback {
fn callback(&self) {
println!("Calling back...");
println!("> Hello World!");
use wasmtime_bindings_macro::wasmtime_impl;
#[wasmtime_impl(module(syscalls_mod), context(wasmtime_wasi::WasiMem))]
impl Syscalls {
fn __print_char(&self, c: u8) {
print!("{}", c as char);
}

fn __print_string(&self, buf: *mut u8, length: i32, capacity: i32) {
let s =
//unsafe { std::string::String::from_raw_parts(buf, length as usize, capacity as usize) };
String::from(
std::str::from_utf8(unsafe { std::slice::from_raw_parts(buf, length as usize) }).unwrap()
);

print!("{}", s);
}
}

Expand All @@ -30,7 +40,7 @@ fn main() -> Result<()> {

// Load binary.
println!("Loading binary...");
let binary = read("examples/hello.wasm")?;
let binary = read("examples/hello4.wasm")?;

// Compile.
println!("Compiling module...");
Expand All @@ -40,15 +50,15 @@ fn main() -> Result<()> {

// Create external print functions.
println!("Creating callback...");
let callback_mod = HostRef::new(
let syscalls_mod = HostRef::new(
wrap_wasmtime_module!(
&store, |_imports| Callback; module(callback_mod)
&store, |_imports| Syscalls; module(syscalls_mod)
)
.map_err(|_| format_err!("> Error compiling callback module!"))?,
);
let callback_instance = Instance::new(&store, &callback_mod, &[])
let callback_instance = Instance::new(&store, &syscalls_mod, &[])
.map_err(|_| format_err!("> Error instantiating callback module!"))?;
let hello_func = &callback_instance.exports()[0];
let hello_func = &callback_instance.exports()[1];

// Instantiate.
println!("Instantiating module...");
Expand Down
Binary file added crates/api/examples/hello4.wasm
Binary file not shown.
8 changes: 8 additions & 0 deletions crates/api/examples/hello4.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(module
(import "" "__print_string" (func $print_string (param i32 i32 i32)))
(memory (export "memory") 1 1)
(func (export "run")
(call $print_string (i32.const 0) (i32.const 9) (i32.const 9))
)
(data (i32.const 0) "Hello, 4!")
)
3 changes: 2 additions & 1 deletion crates/api/src/trampoline/create_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ pub(crate) fn create_handle(
signature_registry: Option<RefMut<Store>>,
finished_functions: PrimaryMap<DefinedFuncIndex, *const VMFunctionBody>,
state: Box<dyn Any>,
global_exports: Option<Rc<RefCell<HashMap<String, Option<wasmtime_runtime::Export>>>>>,
) -> Result<InstanceHandle> {
let global_exports: Rc<RefCell<HashMap<String, Option<wasmtime_runtime::Export>>>> =
Rc::new(RefCell::new(HashMap::new()));
global_exports.unwrap_or_else(|| Rc::new(RefCell::new(HashMap::new())));

let imports = Imports::new(
HashSet::new(),
Expand Down
11 changes: 10 additions & 1 deletion crates/api/src/trampoline/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ pub fn create_handle_with_function(
Some(store.borrow_mut()),
finished_functions,
Box::new(trampoline_state),
None,
)
}

Expand All @@ -264,6 +265,7 @@ pub fn create_handle_for_wrapped(
Some(store.borrow_mut()),
finished_functions,
Box::new(()),
None,
)
}

Expand Down Expand Up @@ -296,7 +298,14 @@ pub fn create_handle_for_trait(

let state = state_builder.build_state(externs);

create_handle(module, Some(store.borrow_mut()), finished_functions, state)
let exports = store.borrow().global_exports().clone();
create_handle(
module,
Some(store.borrow_mut()),
finished_functions,
state,
Some(exports),
)
}

/// We don't expect trampoline compilation to produce any relocations, so
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/trampoline/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn create_global(gt: &GlobalType, val: Val) -> Result<(wasmtime_runtime::Exp
initializer: cranelift_wasm::GlobalInit::Import, // TODO is it right?
};
let mut handle =
create_handle(Module::new(), None, PrimaryMap::new(), Box::new(())).expect("handle");
create_handle(Module::new(), None, PrimaryMap::new(), Box::new(()), None).expect("handle");
Ok((
wasmtime_runtime::Export::Global {
definition: definition.as_mut(),
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/trampoline/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ pub fn create_handle_with_memory(memory: &MemoryType) -> Result<InstanceHandle>
wasmtime_environ::Export::Memory(memory_id),
);

create_handle(module, None, PrimaryMap::new(), Box::new(()))
create_handle(module, None, PrimaryMap::new(), Box::new(()), None)
}
2 changes: 1 addition & 1 deletion crates/api/src/trampoline/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ pub fn create_handle_with_table(table: &TableType) -> Result<InstanceHandle> {
wasmtime_environ::Export::Table(table_id),
);

create_handle(module, None, PrimaryMap::new(), Box::new(()))
create_handle(module, None, PrimaryMap::new(), Box::new(()), None)
}
6 changes: 6 additions & 0 deletions crates/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ impl IntoIRType for i32 {
}
}

impl IntoIRType for u32 {
fn into_ir_type() -> codegen::ir::Type {
codegen::ir::types::I32
}
}

impl IntoIRType for i64 {
fn into_ir_type() -> codegen::ir::Type {
codegen::ir::types::I64
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ mod trait_impl;
#[macro_use]
extern crate wasmtime_bindings_macro;
pub use instantiate::{instantiate_wasi, instantiate_wasi_with_context};
pub use r#trait::{wasi_mod, Wasi};
pub use r#trait::{wasi_mod, Wasi, WasiMem};
pub use trait_impl::instantiate_wasi2;