[wasmtime-api] Multi value api support (#448)
This commit is contained in:
89
wasmtime-api/examples/multi.rs
Normal file
89
wasmtime-api/examples/multi.rs
Normal file
@@ -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
Normal file
BIN
wasmtime-api/examples/multi.wasm
Normal file
Binary file not shown.
7
wasmtime-api/examples/multi.wat
Normal file
7
wasmtime-api/examples/multi.wat
Normal file
@@ -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))
|
||||||
|
)
|
||||||
|
)
|
||||||
@@ -60,6 +60,46 @@ impl Val {
|
|||||||
pub fn from_f64_bits(v: u64) -> Val {
|
pub fn from_f64_bits(v: u64) -> Val {
|
||||||
Val::F64(v)
|
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 {
|
impl From<i32> for Val {
|
||||||
@@ -88,41 +128,25 @@ impl From<f64> for Val {
|
|||||||
|
|
||||||
impl Into<i32> for Val {
|
impl Into<i32> for Val {
|
||||||
fn into(self) -> i32 {
|
fn into(self) -> i32 {
|
||||||
if let Val::I32(i) = self {
|
self.i32()
|
||||||
i
|
|
||||||
} else {
|
|
||||||
panic!("Invalid conversion of {:?} to i32.", self);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<i64> for Val {
|
impl Into<i64> for Val {
|
||||||
fn into(self) -> i64 {
|
fn into(self) -> i64 {
|
||||||
if let Val::I64(i) = self {
|
self.i64()
|
||||||
i
|
|
||||||
} else {
|
|
||||||
panic!("Invalid conversion of {:?} to i64.", self);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<f32> for Val {
|
impl Into<f32> for Val {
|
||||||
fn into(self) -> f32 {
|
fn into(self) -> f32 {
|
||||||
if let Val::F32(i) = self {
|
self.f32()
|
||||||
RuntimeValue::F32(i).unwrap_f32()
|
|
||||||
} else {
|
|
||||||
panic!("Invalid conversion of {:?} to f32.", self);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<f64> for Val {
|
impl Into<f64> for Val {
|
||||||
fn into(self) -> f64 {
|
fn into(self) -> f64 {
|
||||||
if let Val::F64(i) = self {
|
self.f64()
|
||||||
RuntimeValue::F64(i).unwrap_f64()
|
|
||||||
} else {
|
|
||||||
panic!("Invalid conversion of {:?} to f64.", self);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1651,3 +1651,12 @@ pub unsafe extern "C" fn wasm_instance_set_host_info_with_finalizer(
|
|||||||
};
|
};
|
||||||
(*instance).instance.anyref().set_host_info(info);
|
(*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);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user