diff --git a/wasmtime-api/examples/multi.rs b/wasmtime-api/examples/multi.rs new file mode 100644 index 0000000000..aac95deca6 --- /dev/null +++ b/wasmtime-api/examples/multi.rs @@ -0,0 +1,89 @@ +//! Translation of multi example + +extern crate alloc; + +use alloc::rc::Rc; +use core::cell::Ref; +use failure::{bail, format_err, Error}; +use std::fs::read; +use wasmtime_api::*; + +struct Callback; + +impl Callable for Callback { + fn call(&self, args: &[Val], results: &mut [Val]) -> Result<(), HostRef> { + println!("Calling back..."); + println!("> {} {}", args[0].i32(), args[1].i64()); + + results[0] = Val::I64(args[1].i64() + 1); + results[1] = Val::I32(args[0].i32() + 1); + Ok(()) + } +} + +fn main() -> Result<(), Error> { + // Initialize. + println!("Initializing..."); + let engine = HostRef::new(Engine::new(Config::default())); + let store = HostRef::new(Store::new(engine)); + + // Load binary. + println!("Loading binary..."); + let binary = read("examples/multi.wasm")?; + + // Compile. + println!("Compiling module..."); + let module = HostRef::new( + Module::new(store.clone(), &binary) + .map_err(|_| format_err!("> Error compiling module!"))?, + ); + + // Create external print functions. + println!("Creating callback..."); + let callback_type = FuncType::new( + Box::new([ValType::I32, ValType::I64]), + Box::new([ValType::I64, ValType::I32]), + ); + let callback_func = HostRef::new(Func::new(store.clone(), callback_type, Rc::new(Callback))); + + // Instantiate. + println!("Instantiating module..."); + let imports = vec![callback_func.into()]; + let instance = HostRef::new( + Instance::new(store.clone(), module, imports.as_slice()) + .map_err(|_| format_err!("> Error instantiating module!"))?, + ); + + // Extract export. + println!("Extracting export..."); + let exports = Ref::map(instance.borrow(), |instance| instance.exports()); + if exports.len() == 0 { + bail!("> Error accessing exports!"); + } + let run_func = exports[0] + .func() + .ok_or_else(|| format_err!("> Error accessing exports!"))?; + + // Call. + println!("Calling export..."); + let args = vec![Val::I32(1), Val::I64(3)]; + let results = run_func.borrow().call(&args); + if let Err(_) = results { + bail!("> Error calling function!"); + } + + let results = results.unwrap(); + println!("Printing result..."); + println!("> {} {}", results[0].i64(), results[1].i32()); + + debug_assert!(results[0].i64() == 4); + debug_assert!(results[1].i32() == 2); + + // Shut down. + println!("Shutting down..."); + drop(store); + + // All done. + println!("Done."); + Ok(()) +} diff --git a/wasmtime-api/examples/multi.wasm b/wasmtime-api/examples/multi.wasm new file mode 100644 index 0000000000..2b117daa86 Binary files /dev/null and b/wasmtime-api/examples/multi.wasm differ diff --git a/wasmtime-api/examples/multi.wat b/wasmtime-api/examples/multi.wat new file mode 100644 index 0000000000..f26a857e17 --- /dev/null +++ b/wasmtime-api/examples/multi.wat @@ -0,0 +1,7 @@ +(module + (func $f (import "" "f") (param i32 i64) (result i64 i32)) + + (func $g (export "g") (param i32 i64) (result i64 i32) + (call $f (local.get 0) (local.get 1)) + ) +) diff --git a/wasmtime-api/src/values.rs b/wasmtime-api/src/values.rs index 13b2959220..4dc762767a 100644 --- a/wasmtime-api/src/values.rs +++ b/wasmtime-api/src/values.rs @@ -60,6 +60,46 @@ impl Val { pub fn from_f64_bits(v: u64) -> Val { Val::F64(v) } + + pub fn i32(&self) -> i32 { + if let Val::I32(i) = self { + *i + } else { + panic!("Invalid conversion of {:?} to i32.", self); + } + } + + pub fn i64(&self) -> i64 { + if let Val::I64(i) = self { + *i + } else { + panic!("Invalid conversion of {:?} to i64.", self); + } + } + + pub fn f32(&self) -> f32 { + RuntimeValue::F32(self.f32_bits()).unwrap_f32() + } + + pub fn f64(&self) -> f64 { + RuntimeValue::F64(self.f64_bits()).unwrap_f64() + } + + pub fn f32_bits(&self) -> u32 { + if let Val::F32(i) = self { + *i + } else { + panic!("Invalid conversion of {:?} to f32.", self); + } + } + + pub fn f64_bits(&self) -> u64 { + if let Val::F64(i) = self { + *i + } else { + panic!("Invalid conversion of {:?} to f64.", self); + } + } } impl From for Val { @@ -88,41 +128,25 @@ impl From for Val { impl Into for Val { fn into(self) -> i32 { - if let Val::I32(i) = self { - i - } else { - panic!("Invalid conversion of {:?} to i32.", self); - } + self.i32() } } impl Into for Val { fn into(self) -> i64 { - if let Val::I64(i) = self { - i - } else { - panic!("Invalid conversion of {:?} to i64.", self); - } + self.i64() } } impl Into for Val { fn into(self) -> f32 { - if let Val::F32(i) = self { - RuntimeValue::F32(i).unwrap_f32() - } else { - panic!("Invalid conversion of {:?} to f32.", self); - } + self.f32() } } impl Into for Val { fn into(self) -> f64 { - if let Val::F64(i) = self { - RuntimeValue::F64(i).unwrap_f64() - } else { - panic!("Invalid conversion of {:?} to f64.", self); - } + self.f64() } } diff --git a/wasmtime-api/src/wasm.rs b/wasmtime-api/src/wasm.rs index 71aee96bda..e18125169f 100644 --- a/wasmtime-api/src/wasm.rs +++ b/wasmtime-api/src/wasm.rs @@ -1651,3 +1651,12 @@ pub unsafe extern "C" fn wasm_instance_set_host_info_with_finalizer( }; (*instance).instance.anyref().set_host_info(info); } + +#[no_mangle] +pub unsafe extern "C" fn wasm_valtype_vec_copy( + out: *mut wasm_valtype_vec_t, + src: *mut wasm_valtype_vec_t, +) { + let slice = slice::from_raw_parts((*src).data, (*src).size); + (*out).set_from_slice(slice); +}