* Rename the `wasmtime_api` library to match the containing `wasmtime` crate
Commit d9ca508f80 renamed the
`wasmtime-api` crate to `wasmtime`, but left the name of the library it
contains as `wasmtime_api`.
It's fairly unusual for a crate to contain a library with a different
name, and it results in rather confusing error messages for a user; if
you list `wasmtime = "0.7"` in `Cargo.toml`, you can't `use
wasmtime::*`, you have to `use wasmtime_api::*;`.
Rename the `wasmtime_api` library to `wasmtime`.
* Stop renaming wasmtime to api on imports
Various users renamed the crate formerly known as wasmtime_api to api,
and then used api:: prefixes everywhere; change those all to wasmtime::
and drop the renaming.
160 lines
4.6 KiB
Rust
160 lines
4.6 KiB
Rust
//! Support for a calling of a bounds (exported) function.
|
|
|
|
use crate::value::{pyobj_to_value, value_to_pyobj};
|
|
use pyo3::exceptions::Exception;
|
|
use pyo3::prelude::*;
|
|
use pyo3::types::{PyAny, PyDict, PyTuple};
|
|
use std::rc::Rc;
|
|
use wasmtime_interface_types::ModuleData;
|
|
|
|
// TODO support non-export functions
|
|
#[pyclass]
|
|
pub struct Function {
|
|
pub instance: wasmtime::HostRef<wasmtime::Instance>,
|
|
pub export_name: String,
|
|
pub args_types: Vec<wasmtime::ValType>,
|
|
pub data: Rc<ModuleData>,
|
|
}
|
|
|
|
impl Function {
|
|
pub fn func(&self) -> wasmtime::HostRef<wasmtime::Func> {
|
|
let e = self
|
|
.instance
|
|
.borrow()
|
|
.find_export_by_name(&self.export_name)
|
|
.expect("named export")
|
|
.clone();
|
|
e.func().expect("function export").clone()
|
|
}
|
|
}
|
|
|
|
#[pymethods]
|
|
impl Function {
|
|
#[__call__]
|
|
#[args(args = "*")]
|
|
fn call(&self, py: Python, args: &PyTuple) -> PyResult<PyObject> {
|
|
let mut runtime_args = Vec::new();
|
|
for item in args.iter() {
|
|
runtime_args.push(pyobj_to_value(py, item)?);
|
|
}
|
|
let results = self
|
|
.data
|
|
.invoke_export(&self.instance, self.export_name.as_str(), &runtime_args)
|
|
.map_err(crate::err2py)?;
|
|
let mut py_results = Vec::new();
|
|
for result in results {
|
|
py_results.push(value_to_pyobj(py, result)?);
|
|
}
|
|
if py_results.len() == 1 {
|
|
Ok(py_results[0].clone_ref(py))
|
|
} else {
|
|
Ok(PyTuple::new(py, py_results).to_object(py))
|
|
}
|
|
}
|
|
}
|
|
|
|
fn parse_annotation_type(s: &str) -> wasmtime::ValType {
|
|
match s {
|
|
"I32" | "i32" => wasmtime::ValType::I32,
|
|
"I64" | "i64" => wasmtime::ValType::I64,
|
|
"F32" | "f32" => wasmtime::ValType::F32,
|
|
"F64" | "f64" => wasmtime::ValType::F64,
|
|
_ => panic!("unknown type in annotations"),
|
|
}
|
|
}
|
|
|
|
struct WrappedFn {
|
|
func: PyObject,
|
|
returns_types: Vec<wasmtime::ValType>,
|
|
}
|
|
|
|
impl WrappedFn {
|
|
pub fn new(func: PyObject, returns_types: Vec<wasmtime::ValType>) -> Self {
|
|
WrappedFn {
|
|
func,
|
|
returns_types,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl wasmtime::Callable for WrappedFn {
|
|
fn call(
|
|
&self,
|
|
params: &[wasmtime::Val],
|
|
returns: &mut [wasmtime::Val],
|
|
) -> Result<(), wasmtime::HostRef<wasmtime::Trap>> {
|
|
let gil = Python::acquire_gil();
|
|
let py = gil.python();
|
|
|
|
let params = params
|
|
.iter()
|
|
.map(|p| match p {
|
|
wasmtime::Val::I32(i) => i.clone().into_py(py),
|
|
wasmtime::Val::I64(i) => i.clone().into_py(py),
|
|
_ => {
|
|
panic!();
|
|
}
|
|
})
|
|
.collect::<Vec<PyObject>>();
|
|
|
|
let result = self
|
|
.func
|
|
.call(py, PyTuple::new(py, params), None)
|
|
.expect("TODO: convert result to trap");
|
|
|
|
let result = if let Ok(t) = result.cast_as::<PyTuple>(py) {
|
|
t
|
|
} else {
|
|
if result.is_none() {
|
|
PyTuple::empty(py)
|
|
} else {
|
|
PyTuple::new(py, &[result])
|
|
}
|
|
};
|
|
for (i, ty) in self.returns_types.iter().enumerate() {
|
|
let result_item = result.get_item(i);
|
|
returns[i] = match ty {
|
|
wasmtime::ValType::I32 => wasmtime::Val::I32(result_item.extract::<i32>().unwrap()),
|
|
wasmtime::ValType::I64 => wasmtime::Val::I64(result_item.extract::<i64>().unwrap()),
|
|
_ => {
|
|
panic!();
|
|
}
|
|
};
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub fn wrap_into_pyfunction(
|
|
store: &wasmtime::HostRef<wasmtime::Store>,
|
|
callable: &PyAny,
|
|
) -> PyResult<wasmtime::HostRef<wasmtime::Func>> {
|
|
if !callable.hasattr("__annotations__")? {
|
|
// TODO support calls without annotations?
|
|
return Err(PyErr::new::<Exception, _>(
|
|
"import is not a function".to_string(),
|
|
));
|
|
}
|
|
|
|
let annot = callable.getattr("__annotations__")?.cast_as::<PyDict>()?;
|
|
let mut params = Vec::new();
|
|
let mut returns = Vec::new();
|
|
for (name, value) in annot.iter() {
|
|
let ty = parse_annotation_type(&value.to_string());
|
|
match name.to_string().as_str() {
|
|
"return" => returns.push(ty),
|
|
_ => params.push(ty),
|
|
}
|
|
}
|
|
|
|
let ft = wasmtime::FuncType::new(
|
|
params.into_boxed_slice(),
|
|
returns.clone().into_boxed_slice(),
|
|
);
|
|
|
|
let gil = Python::acquire_gil();
|
|
let wrapped = WrappedFn::new(callable.to_object(gil.python()), returns);
|
|
let f = wasmtime::Func::new(store, ft, Rc::new(wrapped));
|
|
Ok(wasmtime::HostRef::new(f))
|
|
}
|