Browse Source

[wasmtime-api] Multi value api support (#448)

pull/449/head
Yury Delendik 5 years ago
committed by GitHub
parent
commit
876d5e1075
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 89
      wasmtime-api/examples/multi.rs
  2. BIN
      wasmtime-api/examples/multi.wasm
  3. 7
      wasmtime-api/examples/multi.wat
  4. 64
      wasmtime-api/src/values.rs
  5. 9
      wasmtime-api/src/wasm.rs

89
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<Trap>> {
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(())
}

BIN
wasmtime-api/examples/multi.wasm

Binary file not shown.

7
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))
)
)

64
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<i32> for Val {
@ -88,41 +128,25 @@ impl From<f64> for Val {
impl Into<i32> 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<i64> 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<f32> 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<f64> 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()
}
}

9
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);
}

Loading…
Cancel
Save